From adf0c76865eafffdf54717d5fb044662d6703e1f Mon Sep 17 00:00:00 2001 From: glihm Date: Thu, 5 Sep 2024 18:16:21 -0600 Subject: [PATCH] fix: bump to 2.8.0 --- .tool-versions | 4 +- Scarb.lock | 102 +++- Scarb.toml | 6 +- bindings/src/bindings.rs | 484 +++++++++--------- .../onchain_data_fact_tree_encoder.cairo | 3 +- src/config/component.cairo | 3 +- src/config/tests/test_config.cairo | 8 +- src/lib.cairo | 4 +- src/messaging/component.cairo | 7 +- src/messaging/interface.cairo | 32 +- src/messaging/tests/test_messaging.cairo | 12 +- src/messaging/types.cairo | 4 +- src/state/component.cairo | 3 +- src/state/tests/test_state.cairo | 8 +- tests/test_appchain.cairo | 22 +- 15 files changed, 414 insertions(+), 288 deletions(-) diff --git a/.tool-versions b/.tool-versions index fbe9b81..49abc31 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -scarb 2.6.5 -starknet-foundry 0.27.0 +scarb 2.8.0 +starknet-foundry 0.30.0 diff --git a/Scarb.lock b/Scarb.lock index e2a0cd9..60500bc 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -3,8 +3,94 @@ version = 1 [[package]] name = "openzeppelin" -version = "0.14.0" -source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.14.0#f091c4f51ddeb10297db984acae965328c5a4e5b" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" +dependencies = [ + "openzeppelin_access", + "openzeppelin_account", + "openzeppelin_governance", + "openzeppelin_introspection", + "openzeppelin_merkle_tree", + "openzeppelin_presets", + "openzeppelin_security", + "openzeppelin_token", + "openzeppelin_upgrades", + "openzeppelin_utils", +] + +[[package]] +name = "openzeppelin_access" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" +dependencies = [ + "openzeppelin_introspection", + "openzeppelin_utils", +] + +[[package]] +name = "openzeppelin_account" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" +dependencies = [ + "openzeppelin_introspection", + "openzeppelin_utils", +] + +[[package]] +name = "openzeppelin_governance" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" +dependencies = [ + "openzeppelin_access", + "openzeppelin_introspection", +] + +[[package]] +name = "openzeppelin_introspection" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" + +[[package]] +name = "openzeppelin_merkle_tree" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" + +[[package]] +name = "openzeppelin_presets" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" +dependencies = [ + "openzeppelin_access", + "openzeppelin_account", + "openzeppelin_introspection", + "openzeppelin_token", + "openzeppelin_upgrades", +] + +[[package]] +name = "openzeppelin_security" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" + +[[package]] +name = "openzeppelin_token" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" +dependencies = [ + "openzeppelin_account", + "openzeppelin_governance", + "openzeppelin_introspection", +] + +[[package]] +name = "openzeppelin_upgrades" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" + +[[package]] +name = "openzeppelin_utils" +version = "0.16.0" +source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.16.0#ba00ce76a93dcf25c081ab2698da20690b5a1cfb" [[package]] name = "piltover" @@ -14,7 +100,15 @@ dependencies = [ "snforge_std", ] +[[package]] +name = "snforge_scarb_plugin" +version = "0.1.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.30.0#196f06b251926697c3d66800f2a93ae595e76496" + [[package]] name = "snforge_std" -version = "0.27.0" -source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.27.0#2d99b7c00678ef0363881ee0273550c44a9263de" +version = "0.30.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.30.0#196f06b251926697c3d66800f2a93ae595e76496" +dependencies = [ + "snforge_scarb_plugin", +] diff --git a/Scarb.toml b/Scarb.toml index f7459c2..3c98a57 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -4,11 +4,11 @@ version = "0.1.0" edition = "2023_10" [dependencies] -starknet = "2.5.3" -openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.14.0" } +starknet = "2.8.0" +openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.16.0" } [dev-dependencies] -snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.27.0" } +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.30.0" } [lib] sierra = true diff --git a/bindings/src/bindings.rs b/bindings/src/bindings.rs index 2eaff08..dff3c53 100644 --- a/bindings/src/bindings.rs +++ b/bindings/src/bindings.rs @@ -630,127 +630,7 @@ cairo_serde :: CairoSerde for Upgraded cairo_serialized_size(& class_hash); Ok(Upgraded { class_hash }) } } #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event -{ - OwnershipTransferred(OwnershipTransferred), - OwnershipTransferStarted(OwnershipTransferStarted) -} impl cainome :: cairo_serde :: CairoSerde for Event -{ - type RustType = Self; const SERIALIZED_SIZE : std :: option :: Option < - usize > = std :: option :: Option :: None; #[inline] fn - cairo_serialized_size(__rust : & Self :: RustType) -> usize - { - match __rust - { - Event :: OwnershipTransferred(val) => OwnershipTransferred :: - cairo_serialized_size(val) + 1, Event :: - OwnershipTransferStarted(val) => OwnershipTransferStarted :: - cairo_serialized_size(val) + 1, _ => 0 - } - } fn cairo_serialize(__rust : & Self :: RustType) -> Vec < starknet :: - core :: types :: Felt > - { - match __rust - { - Event :: OwnershipTransferred(val) => - { - let mut temp = vec! []; - temp.extend(usize :: cairo_serialize(& 0usize)); - temp.extend(OwnershipTransferred :: cairo_serialize(val)); - temp - }, Event :: OwnershipTransferStarted(val) => - { - let mut temp = vec! []; - temp.extend(usize :: cairo_serialize(& 1usize)); - temp.extend(OwnershipTransferStarted :: cairo_serialize(val)); - temp - }, _ => vec! [] - } - } fn - cairo_deserialize(__felts : & [starknet :: core :: types :: Felt], - __offset : usize) -> cainome :: cairo_serde :: Result < Self :: RustType > - { - let __f = __felts [__offset]; let __index = u128 :: - from_be_bytes(__f.to_bytes_be() [16 ..].try_into().unwrap()); match - __index as usize - { - 0usize => - Ok(Event :: - OwnershipTransferred(OwnershipTransferred :: - cairo_deserialize(__felts, __offset + 1) ?)), 1usize => - Ok(Event :: - OwnershipTransferStarted(OwnershipTransferStarted :: - cairo_deserialize(__felts, __offset + 1) ?)), _ => return - Err(cainome :: cairo_serde :: Error :: - Deserialize(format! ("Index not handle for enum {}", "Event"))) - } - } -} impl TryFrom < starknet :: core :: types :: EmittedEvent > for Event -{ - type Error = String; fn - try_from(event : starknet :: core :: types :: EmittedEvent) -> Result < - Self, Self :: Error > - { - use cainome :: cairo_serde :: CairoSerde; if event.keys.is_empty() - { return Err("Event has no key".to_string()); } let selector = - event.keys [0]; if selector == starknet :: core :: utils :: - get_selector_from_name("OwnershipTransferred").unwrap_or_else(| _ | - panic! ("Invalid selector for {}", "OwnershipTransferred")) - { - let mut key_offset = 0 + 1; let mut data_offset = 0; let - previous_owner = match cainome :: cairo_serde :: ContractAddress - :: cairo_deserialize(& event.keys, key_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", - "previous_owner", "OwnershipTransferred", e)), - }; key_offset += cainome :: cairo_serde :: ContractAddress :: - cairo_serialized_size(& previous_owner); let new_owner = match - cainome :: cairo_serde :: ContractAddress :: - cairo_deserialize(& event.keys, key_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", "new_owner", - "OwnershipTransferred", e)), - }; key_offset += cainome :: cairo_serde :: ContractAddress :: - cairo_serialized_size(& new_owner); return - Ok(Event :: - OwnershipTransferred(OwnershipTransferred - { previous_owner, new_owner })) - }; let selector = event.keys [0]; if selector == starknet :: core :: - utils :: - get_selector_from_name("OwnershipTransferStarted").unwrap_or_else(| _ - | panic! ("Invalid selector for {}", "OwnershipTransferStarted")) - { - let mut key_offset = 0 + 1; let mut data_offset = 0; let - previous_owner = match cainome :: cairo_serde :: ContractAddress - :: cairo_deserialize(& event.keys, key_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", - "previous_owner", "OwnershipTransferStarted", e)), - }; key_offset += cainome :: cairo_serde :: ContractAddress :: - cairo_serialized_size(& previous_owner); let new_owner = match - cainome :: cairo_serde :: ContractAddress :: - cairo_deserialize(& event.keys, key_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", "new_owner", - "OwnershipTransferStarted", e)), - }; key_offset += cainome :: cairo_serde :: ContractAddress :: - cairo_serialized_size(& new_owner); return - Ok(Event :: - OwnershipTransferStarted(OwnershipTransferStarted - { previous_owner, new_owner })) - }; - Err(format! ("Could not match any event from keys {:?}", event.keys)) - } -} #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event -{ ProgramInfoChanged(ProgramInfoChanged) } impl cainome :: cairo_serde :: -CairoSerde for Event +{ Upgraded(Upgraded) } impl cainome :: cairo_serde :: CairoSerde for Event { type RustType = Self; const SERIALIZED_SIZE : std :: option :: Option < usize > = std :: option :: Option :: None; #[inline] fn @@ -758,19 +638,19 @@ CairoSerde for Event { match __rust { - Event :: ProgramInfoChanged(val) => ProgramInfoChanged :: - cairo_serialized_size(val) + 1, _ => 0 + Event :: Upgraded(val) => Upgraded :: cairo_serialized_size(val) + + 1, _ => 0 } } fn cairo_serialize(__rust : & Self :: RustType) -> Vec < starknet :: core :: types :: Felt > { match __rust { - Event :: ProgramInfoChanged(val) => + Event :: Upgraded(val) => { let mut temp = vec! []; temp.extend(usize :: cairo_serialize(& 0usize)); - temp.extend(ProgramInfoChanged :: cairo_serialize(val)); temp + temp.extend(Upgraded :: cairo_serialize(val)); temp }, _ => vec! [] } } fn @@ -783,8 +663,8 @@ CairoSerde for Event { 0usize => Ok(Event :: - ProgramInfoChanged(ProgramInfoChanged :: - cairo_deserialize(__felts, __offset + 1) ?)), _ => return + Upgraded(Upgraded :: cairo_deserialize(__felts, __offset + 1) ?)), + _ => return Err(cainome :: cairo_serde :: Error :: Deserialize(format! ("Index not handle for enum {}", "Event"))) } @@ -798,61 +678,20 @@ CairoSerde for Event use cainome :: cairo_serde :: CairoSerde; if event.keys.is_empty() { return Err("Event has no key".to_string()); } let selector = event.keys [0]; if selector == starknet :: core :: utils :: - get_selector_from_name("ProgramInfoChanged").unwrap_or_else(| _ | - panic! ("Invalid selector for {}", "ProgramInfoChanged")) + get_selector_from_name("Upgraded").unwrap_or_else(| _ | panic! + ("Invalid selector for {}", "Upgraded")) { let mut key_offset = 0 + 1; let mut data_offset = 0; let - changed_by = match cainome :: cairo_serde :: ContractAddress :: - cairo_deserialize(& event.data, data_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", "changed_by", - "ProgramInfoChanged", e)), - }; data_offset += cainome :: cairo_serde :: ContractAddress :: - cairo_serialized_size(& changed_by); let old_program_hash = match - starknet :: core :: types :: Felt :: - cairo_deserialize(& event.data, data_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", - "old_program_hash", "ProgramInfoChanged", e)), - }; data_offset += starknet :: core :: types :: Felt :: - cairo_serialized_size(& old_program_hash); let new_program_hash = - match starknet :: core :: types :: Felt :: - cairo_deserialize(& event.data, data_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", - "new_program_hash", "ProgramInfoChanged", e)), - }; data_offset += starknet :: core :: types :: Felt :: - cairo_serialized_size(& new_program_hash); let old_config_hash = - match starknet :: core :: types :: Felt :: - cairo_deserialize(& event.data, data_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", - "old_config_hash", "ProgramInfoChanged", e)), - }; data_offset += starknet :: core :: types :: Felt :: - cairo_serialized_size(& old_config_hash); let new_config_hash = - match starknet :: core :: types :: Felt :: + class_hash = match cainome :: cairo_serde :: ClassHash :: cairo_deserialize(& event.data, data_offset) { Ok(v) => v, Err(e) => return Err(format! - ("Could not deserialize field {} for {}: {:?}", - "new_config_hash", "ProgramInfoChanged", e)), - }; data_offset += starknet :: core :: types :: Felt :: - cairo_serialized_size(& new_config_hash); return - Ok(Event :: - ProgramInfoChanged(ProgramInfoChanged - { - changed_by, old_program_hash, new_program_hash, - old_config_hash, new_config_hash - })) + ("Could not deserialize field {} for {}: {:?}", "class_hash", + "Upgraded", e)), + }; data_offset += cainome :: cairo_serde :: ClassHash :: + cairo_serialized_size(& class_hash); return + Ok(Event :: Upgraded(Upgraded { class_hash })) }; Err(format! ("Could not match any event from keys {:?}", event.keys)) } @@ -1507,6 +1346,233 @@ CairoSerde for Event Err(format! ("Could not match any event from keys {:?}", event.keys)) } } #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event +{ ProgramInfoChanged(ProgramInfoChanged) } impl cainome :: cairo_serde :: +CairoSerde for Event +{ + type RustType = Self; const SERIALIZED_SIZE : std :: option :: Option < + usize > = std :: option :: Option :: None; #[inline] fn + cairo_serialized_size(__rust : & Self :: RustType) -> usize + { + match __rust + { + Event :: ProgramInfoChanged(val) => ProgramInfoChanged :: + cairo_serialized_size(val) + 1, _ => 0 + } + } fn cairo_serialize(__rust : & Self :: RustType) -> Vec < starknet :: + core :: types :: Felt > + { + match __rust + { + Event :: ProgramInfoChanged(val) => + { + let mut temp = vec! []; + temp.extend(usize :: cairo_serialize(& 0usize)); + temp.extend(ProgramInfoChanged :: cairo_serialize(val)); temp + }, _ => vec! [] + } + } fn + cairo_deserialize(__felts : & [starknet :: core :: types :: Felt], + __offset : usize) -> cainome :: cairo_serde :: Result < Self :: RustType > + { + let __f = __felts [__offset]; let __index = u128 :: + from_be_bytes(__f.to_bytes_be() [16 ..].try_into().unwrap()); match + __index as usize + { + 0usize => + Ok(Event :: + ProgramInfoChanged(ProgramInfoChanged :: + cairo_deserialize(__felts, __offset + 1) ?)), _ => return + Err(cainome :: cairo_serde :: Error :: + Deserialize(format! ("Index not handle for enum {}", "Event"))) + } + } +} impl TryFrom < starknet :: core :: types :: EmittedEvent > for Event +{ + type Error = String; fn + try_from(event : starknet :: core :: types :: EmittedEvent) -> Result < + Self, Self :: Error > + { + use cainome :: cairo_serde :: CairoSerde; if event.keys.is_empty() + { return Err("Event has no key".to_string()); } let selector = + event.keys [0]; if selector == starknet :: core :: utils :: + get_selector_from_name("ProgramInfoChanged").unwrap_or_else(| _ | + panic! ("Invalid selector for {}", "ProgramInfoChanged")) + { + let mut key_offset = 0 + 1; let mut data_offset = 0; let + changed_by = match cainome :: cairo_serde :: ContractAddress :: + cairo_deserialize(& event.data, data_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", "changed_by", + "ProgramInfoChanged", e)), + }; data_offset += cainome :: cairo_serde :: ContractAddress :: + cairo_serialized_size(& changed_by); let old_program_hash = match + starknet :: core :: types :: Felt :: + cairo_deserialize(& event.data, data_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", + "old_program_hash", "ProgramInfoChanged", e)), + }; data_offset += starknet :: core :: types :: Felt :: + cairo_serialized_size(& old_program_hash); let new_program_hash = + match starknet :: core :: types :: Felt :: + cairo_deserialize(& event.data, data_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", + "new_program_hash", "ProgramInfoChanged", e)), + }; data_offset += starknet :: core :: types :: Felt :: + cairo_serialized_size(& new_program_hash); let old_config_hash = + match starknet :: core :: types :: Felt :: + cairo_deserialize(& event.data, data_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", + "old_config_hash", "ProgramInfoChanged", e)), + }; data_offset += starknet :: core :: types :: Felt :: + cairo_serialized_size(& old_config_hash); let new_config_hash = + match starknet :: core :: types :: Felt :: + cairo_deserialize(& event.data, data_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", + "new_config_hash", "ProgramInfoChanged", e)), + }; data_offset += starknet :: core :: types :: Felt :: + cairo_serialized_size(& new_config_hash); return + Ok(Event :: + ProgramInfoChanged(ProgramInfoChanged + { + changed_by, old_program_hash, new_program_hash, + old_config_hash, new_config_hash + })) + }; + Err(format! ("Could not match any event from keys {:?}", event.keys)) + } +} #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event +{ + OwnershipTransferred(OwnershipTransferred), + OwnershipTransferStarted(OwnershipTransferStarted) +} impl cainome :: cairo_serde :: CairoSerde for Event +{ + type RustType = Self; const SERIALIZED_SIZE : std :: option :: Option < + usize > = std :: option :: Option :: None; #[inline] fn + cairo_serialized_size(__rust : & Self :: RustType) -> usize + { + match __rust + { + Event :: OwnershipTransferred(val) => OwnershipTransferred :: + cairo_serialized_size(val) + 1, Event :: + OwnershipTransferStarted(val) => OwnershipTransferStarted :: + cairo_serialized_size(val) + 1, _ => 0 + } + } fn cairo_serialize(__rust : & Self :: RustType) -> Vec < starknet :: + core :: types :: Felt > + { + match __rust + { + Event :: OwnershipTransferred(val) => + { + let mut temp = vec! []; + temp.extend(usize :: cairo_serialize(& 0usize)); + temp.extend(OwnershipTransferred :: cairo_serialize(val)); + temp + }, Event :: OwnershipTransferStarted(val) => + { + let mut temp = vec! []; + temp.extend(usize :: cairo_serialize(& 1usize)); + temp.extend(OwnershipTransferStarted :: cairo_serialize(val)); + temp + }, _ => vec! [] + } + } fn + cairo_deserialize(__felts : & [starknet :: core :: types :: Felt], + __offset : usize) -> cainome :: cairo_serde :: Result < Self :: RustType > + { + let __f = __felts [__offset]; let __index = u128 :: + from_be_bytes(__f.to_bytes_be() [16 ..].try_into().unwrap()); match + __index as usize + { + 0usize => + Ok(Event :: + OwnershipTransferred(OwnershipTransferred :: + cairo_deserialize(__felts, __offset + 1) ?)), 1usize => + Ok(Event :: + OwnershipTransferStarted(OwnershipTransferStarted :: + cairo_deserialize(__felts, __offset + 1) ?)), _ => return + Err(cainome :: cairo_serde :: Error :: + Deserialize(format! ("Index not handle for enum {}", "Event"))) + } + } +} impl TryFrom < starknet :: core :: types :: EmittedEvent > for Event +{ + type Error = String; fn + try_from(event : starknet :: core :: types :: EmittedEvent) -> Result < + Self, Self :: Error > + { + use cainome :: cairo_serde :: CairoSerde; if event.keys.is_empty() + { return Err("Event has no key".to_string()); } let selector = + event.keys [0]; if selector == starknet :: core :: utils :: + get_selector_from_name("OwnershipTransferred").unwrap_or_else(| _ | + panic! ("Invalid selector for {}", "OwnershipTransferred")) + { + let mut key_offset = 0 + 1; let mut data_offset = 0; let + previous_owner = match cainome :: cairo_serde :: ContractAddress + :: cairo_deserialize(& event.keys, key_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", + "previous_owner", "OwnershipTransferred", e)), + }; key_offset += cainome :: cairo_serde :: ContractAddress :: + cairo_serialized_size(& previous_owner); let new_owner = match + cainome :: cairo_serde :: ContractAddress :: + cairo_deserialize(& event.keys, key_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", "new_owner", + "OwnershipTransferred", e)), + }; key_offset += cainome :: cairo_serde :: ContractAddress :: + cairo_serialized_size(& new_owner); return + Ok(Event :: + OwnershipTransferred(OwnershipTransferred + { previous_owner, new_owner })) + }; let selector = event.keys [0]; if selector == starknet :: core :: + utils :: + get_selector_from_name("OwnershipTransferStarted").unwrap_or_else(| _ + | panic! ("Invalid selector for {}", "OwnershipTransferStarted")) + { + let mut key_offset = 0 + 1; let mut data_offset = 0; let + previous_owner = match cainome :: cairo_serde :: ContractAddress + :: cairo_deserialize(& event.keys, key_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", + "previous_owner", "OwnershipTransferStarted", e)), + }; key_offset += cainome :: cairo_serde :: ContractAddress :: + cairo_serialized_size(& previous_owner); let new_owner = match + cainome :: cairo_serde :: ContractAddress :: + cairo_deserialize(& event.keys, key_offset) + { + Ok(v) => v, Err(e) => return + Err(format! + ("Could not deserialize field {} for {}: {:?}", "new_owner", + "OwnershipTransferStarted", e)), + }; key_offset += cainome :: cairo_serde :: ContractAddress :: + cairo_serialized_size(& new_owner); return + Ok(Event :: + OwnershipTransferStarted(OwnershipTransferStarted + { previous_owner, new_owner })) + }; + Err(format! ("Could not match any event from keys {:?}", event.keys)) + } +} #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event { MessageSent(MessageSent), MessageConsumed(MessageConsumed), MessageCancellationStarted(MessageCancellationStarted), @@ -1951,72 +2017,6 @@ CairoSerde for Event }; Err(format! ("Could not match any event from keys {:?}", event.keys)) } -} #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event -{ Upgraded(Upgraded) } impl cainome :: cairo_serde :: CairoSerde for Event -{ - type RustType = Self; const SERIALIZED_SIZE : std :: option :: Option < - usize > = std :: option :: Option :: None; #[inline] fn - cairo_serialized_size(__rust : & Self :: RustType) -> usize - { - match __rust - { - Event :: Upgraded(val) => Upgraded :: cairo_serialized_size(val) + - 1, _ => 0 - } - } fn cairo_serialize(__rust : & Self :: RustType) -> Vec < starknet :: - core :: types :: Felt > - { - match __rust - { - Event :: Upgraded(val) => - { - let mut temp = vec! []; - temp.extend(usize :: cairo_serialize(& 0usize)); - temp.extend(Upgraded :: cairo_serialize(val)); temp - }, _ => vec! [] - } - } fn - cairo_deserialize(__felts : & [starknet :: core :: types :: Felt], - __offset : usize) -> cainome :: cairo_serde :: Result < Self :: RustType > - { - let __f = __felts [__offset]; let __index = u128 :: - from_be_bytes(__f.to_bytes_be() [16 ..].try_into().unwrap()); match - __index as usize - { - 0usize => - Ok(Event :: - Upgraded(Upgraded :: cairo_deserialize(__felts, __offset + 1) ?)), - _ => return - Err(cainome :: cairo_serde :: Error :: - Deserialize(format! ("Index not handle for enum {}", "Event"))) - } - } -} impl TryFrom < starknet :: core :: types :: EmittedEvent > for Event -{ - type Error = String; fn - try_from(event : starknet :: core :: types :: EmittedEvent) -> Result < - Self, Self :: Error > - { - use cainome :: cairo_serde :: CairoSerde; if event.keys.is_empty() - { return Err("Event has no key".to_string()); } let selector = - event.keys [0]; if selector == starknet :: core :: utils :: - get_selector_from_name("Upgraded").unwrap_or_else(| _ | panic! - ("Invalid selector for {}", "Upgraded")) - { - let mut key_offset = 0 + 1; let mut data_offset = 0; let - class_hash = match cainome :: cairo_serde :: ClassHash :: - cairo_deserialize(& event.data, data_offset) - { - Ok(v) => v, Err(e) => return - Err(format! - ("Could not deserialize field {} for {}: {:?}", "class_hash", - "Upgraded", e)), - }; data_offset += cainome :: cairo_serde :: ClassHash :: - cairo_serialized_size(& class_hash); return - Ok(Event :: Upgraded(Upgraded { class_hash })) - }; - Err(format! ("Could not match any event from keys {:?}", event.keys)) - } } #[derive(Debug, PartialEq, Eq, Clone, Copy,)] pub enum Event {} impl cainome :: cairo_serde :: CairoSerde for Event { diff --git a/src/components/onchain_data_fact_tree_encoder.cairo b/src/components/onchain_data_fact_tree_encoder.cairo index c0e4ae6..3af2efe 100644 --- a/src/components/onchain_data_fact_tree_encoder.cairo +++ b/src/components/onchain_data_fact_tree_encoder.cairo @@ -5,7 +5,8 @@ struct DataAvailabilityFact { } /// Encodes a GPS fact Merkle tree where the root has two children. -/// The left child contains the data we care about and the right child contains on-chain data for the fact. +/// The left child contains the data we care about and the right child contains on-chain data for +/// the fact. /// /// # Arguments /// diff --git a/src/config/component.cairo b/src/config/component.cairo index 5f04a95..8b8ad17 100644 --- a/src/config/component.cairo +++ b/src/config/component.cairo @@ -21,12 +21,13 @@ mod config_cpt { }; use piltover::config::interface::IConfig; use starknet::ContractAddress; + use starknet::storage::Map; use super::errors; #[storage] struct Storage { /// Appchain operators that are allowed to update the state. - operators: LegacyMap, + operators: Map, /// Program info (StarknetOS), with program hash and config hash. program_info: (felt252, felt252), /// Facts registry contract address. diff --git a/src/config/tests/test_config.cairo b/src/config/tests/test_config.cairo index 81b59b3..3818aae 100644 --- a/src/config/tests/test_config.cairo +++ b/src/config/tests/test_config.cairo @@ -4,11 +4,15 @@ use piltover::config::{ }; use piltover::messaging::tests::constants as c; use snforge_std as snf; -use snforge_std::ContractClassTrait; +use snforge_std::{ContractClassTrait, DeclareResult}; use starknet::ContractAddress; fn deploy_mock() -> IConfigDispatcher { - let contract = snf::declare("config_mock").unwrap(); + let contract = match snf::declare("config_mock").unwrap() { + DeclareResult::Success(contract) => contract, + DeclareResult::AlreadyDeclared(contract) => contract, + }; + let calldata = array![c::OWNER().into()]; let (contract_address, _) = contract.deploy(@calldata).unwrap(); IConfigDispatcher { contract_address } diff --git a/src/lib.cairo b/src/lib.cairo index 817096a..ca976db 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -31,7 +31,7 @@ mod messaging { use interface::{IMessaging, IMessagingDispatcher, IMessagingDispatcherTrait}; use mock::messaging_mock; - #[cfg(test)] + #[cfg(target: "test")] mod tests { mod constants; mod test_messaging; @@ -47,7 +47,7 @@ mod state { use interface::{IState, IStateDispatcher, IStateDispatcherTrait}; use mock::state_mock; - #[cfg(test)] + #[cfg(target: "test")] mod tests { mod test_state; } diff --git a/src/messaging/component.cairo b/src/messaging/component.cairo index f689021..92758c0 100644 --- a/src/messaging/component.cairo +++ b/src/messaging/component.cairo @@ -51,6 +51,7 @@ mod messaging_cpt { types::{MessageToAppchainStatus, MessageToStarknetStatus, MessageHash, Nonce} }; use starknet::ContractAddress; + use starknet::storage::Map; use super::errors; #[storage] @@ -58,14 +59,14 @@ mod messaging_cpt { /// Cancellation delay in seconds for message from Starknet to Appchain. cancellation_delay_secs: u64, /// Ledger of messages from Starknet to Appchain that are being cancelled. - sn_to_appc_cancellations: LegacyMap::, + sn_to_appc_cancellations: Map::, /// The nonce for messages sent to the Appchain from Starknet. sn_to_appc_nonce: Nonce, /// Ledger of messages hashes sent from Starknet to the appchain. - sn_to_appc_messages: LegacyMap::, + sn_to_appc_messages: Map::, /// Ledger of messages hashes registered from the Appchain and a refcount /// associated to it to control messages consumption. - appc_to_sn_messages: LegacyMap::, + appc_to_sn_messages: Map::, } #[event] diff --git a/src/messaging/interface.cairo b/src/messaging/interface.cairo index 0be1460..ff8192e 100644 --- a/src/messaging/interface.cairo +++ b/src/messaging/interface.cairo @@ -15,7 +15,8 @@ trait IMessaging { /// # Arguments /// /// * `to_address` - Contract address to send the message to on the Appchain. - /// * `selector` - The `l1_handler` function selector of the contract on the Appchain to execute. + /// * `selector` - The `l1_handler` function selector of the contract on the Appchain to + /// execute. /// * `payload` - The message payload. /// /// # Returns @@ -42,28 +43,29 @@ trait IMessaging { ) -> MessageHash; /// Checks the status of message sent to the Appchain from Starknet - /// - /// + /// + /// /// # Arguments - /// + /// /// * `message_hash` - The hash of the message that was sent to the Appchain. - /// + /// /// # Returns - /// + /// /// Returns the Nonce used for the message with the given `msgHash`, /// or 0 if no message with such a hash is pending. fn sn_to_appchain_messages(self: @T, message_hash: felt252) -> MessageToAppchainStatus; /// Checks the status of message sent to the Starknet from the Appchain - /// - /// + /// + /// /// # Arguments - /// + /// /// * `message_hash` - The hash of the message that was sent to Starknet. - /// + /// /// # Returns - /// - /// Returns the count of messages with given `msg_hash` that are pending to be consumed on starknet, otherwise 0 + /// + /// Returns the count of messages with given `msg_hash` that are pending to be consumed on + /// starknet, otherwise 0 fn appchain_to_sn_messages(self: @T, message_hash: felt252) -> MessageToStarknetStatus; /// Starts the cancellation procedure for a message sent from @@ -76,7 +78,8 @@ trait IMessaging { /// # Arguments /// /// * `to_address` - Contract address to send the message to on the Appchain. - /// * `selector` - The `l1_handler` function selector of the contract on the Appchain to execute. + /// * `selector` - The `l1_handler` function selector of the contract on the Appchain to + /// execute. /// * `payload` - The message's payload. /// * `nonce` - The message's nonce. /// @@ -98,7 +101,8 @@ trait IMessaging { /// # Arguments /// /// * `to_address` - Contract address to send the message to on the Appchain. - /// * `selector` - The `l1_handler` function selector of the contract on the Appchain to execute. + /// * `selector` - The `l1_handler` function selector of the contract on the Appchain to + /// execute. /// * `payload` - The message's payload. /// * `nonce` - The message's nonce. /// diff --git a/src/messaging/tests/test_messaging.cairo b/src/messaging/tests/test_messaging.cairo index 08da0c8..569cf30 100644 --- a/src/messaging/tests/test_messaging.cairo +++ b/src/messaging/tests/test_messaging.cairo @@ -12,12 +12,16 @@ use piltover::messaging::{ output_process::{MessageToStarknet, MessageToAppchain}, hash, output_process, }; use snforge_std as snf; -use snforge_std::{ContractClassTrait, EventSpy, EventSpyAssertionsTrait}; -use starknet::{ContractAddress, storage::StorageMemberAccessTrait}; +use snforge_std::{ContractClassTrait, EventSpy, EventSpyAssertionsTrait, DeclareResult}; +use starknet::ContractAddress; /// Deploys the mock with a specific cancellation delay. fn deploy_mock_with_delay(cancellation_delay_secs: u64) -> (IMessagingDispatcher, EventSpy) { - let contract = snf::declare("messaging_mock").unwrap(); + let contract = match snf::declare("messaging_mock").unwrap() { + DeclareResult::Success(contract) => contract, + DeclareResult::AlreadyDeclared(contract) => contract, + }; + let calldata = array![cancellation_delay_secs.into()]; let (contract_address, _) = contract.deploy(@calldata).unwrap(); @@ -470,7 +474,7 @@ fn appchain_to_sn_messages_ok() { mock.process_messages_to_starknet(messages); - // Ensure that message is available to consume + // Ensure that message is available to consume let count_after = mock.appchain_to_sn_messages(message_hash); assert(count_after == MessageToStarknetStatus::ReadyToConsume(1), 'message not be present'); } diff --git a/src/messaging/types.cairo b/src/messaging/types.cairo index 83dac37..2e7382d 100644 --- a/src/messaging/types.cairo +++ b/src/messaging/types.cairo @@ -1,13 +1,13 @@ pub type MessageHash = felt252; pub type Nonce = felt252; -#[derive(Serde, Drop, Eq, PartialEq)] +#[derive(Serde, Drop, PartialEq)] pub enum MessageToAppchainStatus { SealedOrNotSent, // sn->appc: The nonce is 0 for the message hash. Pending: Nonce, // sn->appc: The nonce > 0. } -#[derive(Serde, Drop, Eq, PartialEq)] +#[derive(Serde, Drop, PartialEq)] pub enum MessageToStarknetStatus { NothingToConsume, // appc->sn: the ref count is 0. ReadyToConsume: felt252 // appc->sn: the ref count > 0. diff --git a/src/state/component.cairo b/src/state/component.cairo index 76618a1..f345e81 100644 --- a/src/state/component.cairo +++ b/src/state/component.cairo @@ -38,7 +38,8 @@ mod state_cpt { let mut program_output = program_output; let program_output: ProgramOutput = Serde::deserialize(ref program_output).unwrap(); - // Check the blockNumber first as the error is less ambiguous then INVALID_PREVIOUS_ROOT. + // Check the blockNumber first as the error is less ambiguous then + // INVALID_PREVIOUS_ROOT. self.block_number.write(self.block_number.read() + 1); assert( self.block_number.read() == program_output.block_number, diff --git a/src/state/tests/test_state.cairo b/src/state/tests/test_state.cairo index 9e18444..36b6ea9 100644 --- a/src/state/tests/test_state.cairo +++ b/src/state/tests/test_state.cairo @@ -4,13 +4,17 @@ use piltover::state::{ IStateDispatcherTrait, state_mock, }; use snforge_std as snf; -use snforge_std::{ContractClassTrait}; +use snforge_std::{ContractClassTrait, DeclareResult}; /// Deploys the mock with a specific state. fn deploy_mock_with_state( state_root: felt252, block_number: felt252, block_hash: felt252, ) -> IStateDispatcher { - let contract = snf::declare("state_mock").unwrap(); + let contract = match snf::declare("state_mock").unwrap() { + DeclareResult::Success(contract) => contract, + DeclareResult::AlreadyDeclared(contract) => contract, + }; + let calldata = array![state_root, block_number, block_hash]; let (contract_address, _) = contract.deploy(@calldata).unwrap(); IStateDispatcher { contract_address } diff --git a/tests/test_appchain.cairo b/tests/test_appchain.cairo index 053257e..b376d12 100644 --- a/tests/test_appchain.cairo +++ b/tests/test_appchain.cairo @@ -11,12 +11,16 @@ use piltover::mocks::{ }; // To change when Herodotus finishes implementing FactRegistry. use piltover::snos_output::ProgramOutput; use snforge_std as snf; -use snforge_std::{ContractClassTrait, EventSpy, EventSpyAssertionsTrait}; -use starknet::{ContractAddress, storage::StorageMemberAccessTrait}; +use snforge_std::{ContractClassTrait, EventSpy, EventSpyAssertionsTrait, DeclareResult}; +use starknet::ContractAddress; /// Deploys the appchain contract. fn deploy_with_owner(owner: felt252) -> (IAppchainDispatcher, EventSpy) { - let contract = snf::declare("appchain").unwrap(); + let contract = match snf::declare("appchain").unwrap() { + DeclareResult::Success(contract) => contract, + DeclareResult::AlreadyDeclared(contract) => contract, + }; + let calldata = array![owner, 0, 0, 0]; let (contract_address, _) = contract.deploy(@calldata).unwrap(); @@ -29,7 +33,11 @@ fn deploy_with_owner(owner: felt252) -> (IAppchainDispatcher, EventSpy) { fn deploy_with_owner_and_state( owner: felt252, state_root: felt252, block_number: felt252, block_hash: felt252, ) -> (IAppchainDispatcher, EventSpy) { - let contract = snf::declare("appchain").unwrap(); + let contract = match snf::declare("appchain").unwrap() { + DeclareResult::Success(contract) => contract, + DeclareResult::AlreadyDeclared(contract) => contract, + }; + let calldata = array![owner, state_root, block_number, block_hash]; let (contract_address, _) = contract.deploy(@calldata).unwrap(); @@ -40,7 +48,11 @@ fn deploy_with_owner_and_state( /// Deploys the fact registry mock contract. fn deploy_fact_registry_mock() -> IFactRegistryMockDispatcher { - let contract = snf::declare("fact_registry_mock").unwrap(); + let contract = match snf::declare("fact_registry_mock").unwrap() { + DeclareResult::Success(contract) => contract, + DeclareResult::AlreadyDeclared(contract) => contract, + }; + let (contract_address, _) = contract.deploy(@array![]).unwrap(); IFactRegistryMockDispatcher { contract_address } }