From 73fde7f22d133ae86fbf73a35d711dd581b50acc Mon Sep 17 00:00:00 2001 From: thomas192 Date: Thu, 22 Feb 2024 14:50:44 +0100 Subject: [PATCH 1/5] feat : Implemented check config hash and ProgramInfoChanged event --- src/appchain.cairo | 7 +++++++ src/snos_output.cairo | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/appchain.cairo b/src/appchain.cairo index 769871d..20e2fed 100644 --- a/src/appchain.cairo +++ b/src/appchain.cairo @@ -5,6 +5,7 @@ mod errors { const INVALID_ADDRESS: felt252 = 'Config: invalid address'; const SNOS_INVALID_PROGRAM_OUTPUT_SIZE: felt252 = 'snos: invalid output size'; + const SNOS_INVALID_CONFIG_HASH: felt252 = 'snos: invalid config hash'; const SNOS_INVALID_MESSAGES_SEGMENTS: felt252 = 'snos: invalid messages segments'; } @@ -94,6 +95,12 @@ mod appchain { errors::SNOS_INVALID_PROGRAM_OUTPUT_SIZE ); + let (_, config_hash): (felt252, felt252) = self.config.program_info.read(); + assert( + *program_output.at(snos_output::CONFIG_HASH_OFFSET) == config_hash, + errors::SNOS_INVALID_CONFIG_HASH + ); + let mut offset = snos_output::HEADER_SIZE; // TODO(#7): We should update SNOS output to have the messages count diff --git a/src/snos_output.cairo b/src/snos_output.cairo index e27c381..58854b3 100644 --- a/src/snos_output.cairo +++ b/src/snos_output.cairo @@ -1,6 +1,8 @@ //! SNOS output related types and variables. //! +/// Offset of the configuration hash value in the SNOS output. +const CONFIG_HASH_OFFSET: usize = 4; /// Size of the header of the output of SNOS. const HEADER_SIZE: usize = 5; /// Size of the header of a message to Starknet, which is From 19e2a4bf13ef378f5cd05735b72067e64af8f456 Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 25 Feb 2024 07:32:57 +0000 Subject: [PATCH 2/5] Fixed tests --- tests/test_appchain.cairo | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_appchain.cairo b/tests/test_appchain.cairo index c118fba..69bac0b 100644 --- a/tests/test_appchain.cairo +++ b/tests/test_appchain.cairo @@ -117,6 +117,7 @@ fn update_state_ok() { let (appchain, _spy) = deploy_with_owner(c::OWNER().into()); let imsg = IMessagingDispatcher { contract_address: appchain.contract_address }; + let iconfig = IConfigDispatcher { contract_address: appchain.contract_address }; let contract_sn = starknet::contract_address_const::< 993696174272377493693496825928908586134624850969 @@ -137,6 +138,13 @@ fn update_state_ok() { ] .span(); + snf::start_prank(CheatTarget::One(appchain.contract_address), c::OWNER()); + iconfig + .set_program_info( + program_hash: 0x11, + config_hash: 2590421891839256512113614983194993186457498815986333310670788206383913888162 + ); + // The state update contains a message to appchain, therefore, before // being sealed, it must be sent first. // The nonce must be adjusted to ensure the correct message to be sent. From d4fa873ac50bd592d0682f0b8350371c57e344cd Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 25 Feb 2024 07:57:25 +0000 Subject: [PATCH 3/5] Used ProgramOutput struct instead of hardcoded index --- src/appchain.cairo | 9 ++++++--- src/snos_output.cairo | 4 +--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/appchain.cairo b/src/appchain.cairo index 20e2fed..754c82f 100644 --- a/src/appchain.cairo +++ b/src/appchain.cairo @@ -27,6 +27,7 @@ mod appchain { output_process, output_process::{MessageToStarknet, MessageToAppchain}, }; use piltover::snos_output; + use piltover::snos_output::ProgramOutput; use starknet::ContractAddress; use super::errors; @@ -95,10 +96,12 @@ mod appchain { errors::SNOS_INVALID_PROGRAM_OUTPUT_SIZE ); - let (_, config_hash): (felt252, felt252) = self.config.program_info.read(); + let mut program_output_mut = program_output; + let program_output_struct: ProgramOutput = Serde::deserialize(ref program_output_mut) + .unwrap(); + let (_, current_config_hash): (felt252, felt252) = self.config.program_info.read(); assert( - *program_output.at(snos_output::CONFIG_HASH_OFFSET) == config_hash, - errors::SNOS_INVALID_CONFIG_HASH + program_output_struct.config_hash == current_config_hash, errors::SNOS_INVALID_CONFIG_HASH ); let mut offset = snos_output::HEADER_SIZE; diff --git a/src/snos_output.cairo b/src/snos_output.cairo index 58854b3..b963d77 100644 --- a/src/snos_output.cairo +++ b/src/snos_output.cairo @@ -1,8 +1,6 @@ //! SNOS output related types and variables. //! -/// Offset of the configuration hash value in the SNOS output. -const CONFIG_HASH_OFFSET: usize = 4; /// Size of the header of the output of SNOS. const HEADER_SIZE: usize = 5; /// Size of the header of a message to Starknet, which is @@ -17,7 +15,7 @@ const MESSAGE_TO_APPCHAIN_HEADER_SIZE: usize = 5; /// . /// The names are taken from SNOS repository: /// . -#[derive(Serde)] +#[derive(Drop, Serde)] struct ProgramOutput { /// The state commitment before this block. prev_state_root: felt252, From 1ca9870fa1983bd61e2c22d63c4536734b61ad55 Mon Sep 17 00:00:00 2001 From: thomas192 Date: Sun, 25 Feb 2024 09:04:08 +0100 Subject: [PATCH 4/5] Formatting --- src/appchain.cairo | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/appchain.cairo b/src/appchain.cairo index 754c82f..fd88d70 100644 --- a/src/appchain.cairo +++ b/src/appchain.cairo @@ -26,8 +26,8 @@ mod appchain { messaging_cpt, messaging_cpt::InternalTrait as MessagingInternal, IMessaging, output_process, output_process::{MessageToStarknet, MessageToAppchain}, }; - use piltover::snos_output; use piltover::snos_output::ProgramOutput; + use piltover::snos_output; use starknet::ContractAddress; use super::errors; @@ -101,7 +101,8 @@ mod appchain { .unwrap(); let (_, current_config_hash): (felt252, felt252) = self.config.program_info.read(); assert( - program_output_struct.config_hash == current_config_hash, errors::SNOS_INVALID_CONFIG_HASH + program_output_struct.config_hash == current_config_hash, + errors::SNOS_INVALID_CONFIG_HASH ); let mut offset = snos_output::HEADER_SIZE; From eab2dad24f9a9d9ae91fda7849b268970e10e072 Mon Sep 17 00:00:00 2001 From: debian Date: Tue, 27 Feb 2024 14:42:54 +0000 Subject: [PATCH 5/5] Update --- src/config/component.cairo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/component.cairo b/src/config/component.cairo index c2471e8..5f04a95 100644 --- a/src/config/component.cairo +++ b/src/config/component.cairo @@ -20,7 +20,7 @@ mod config_cpt { interface::IOwnable, }; use piltover::config::interface::IConfig; - use starknet::{ContractAddress, get_caller_address}; + use starknet::ContractAddress; use super::errors; #[storage] @@ -79,7 +79,7 @@ mod config_cpt { self .emit( ProgramInfoChanged { - changed_by: get_caller_address(), + changed_by: starknet::get_caller_address(), old_program_hash: old_program_hash, new_program_hash: program_hash, old_config_hash: old_config_hash,