Skip to content

Commit

Permalink
feat : Check config hash and ProgramInfoChanged event (#18)
Browse files Browse the repository at this point in the history
* feat : Implemented check config hash and ProgramInfoChanged event

* Used ProgramOutput struct instead of hardcoded index
  • Loading branch information
thomas192 authored Mar 8, 2024
1 parent 3579a23 commit 96a742d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/appchain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand All @@ -25,6 +26,7 @@ mod appchain {
messaging_cpt, messaging_cpt::InternalTrait as MessagingInternal, IMessaging,
output_process, output_process::{MessageToStarknet, MessageToAppchain},
};
use piltover::snos_output::ProgramOutput;
use piltover::snos_output;
use piltover::state::component::state_cpt::HasComponent;
use piltover::state::{state_cpt, state_cpt::InternalTrait as StateInternal, IState};
Expand Down Expand Up @@ -129,6 +131,15 @@ mod appchain {
errors::SNOS_INVALID_PROGRAM_OUTPUT_SIZE
);

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_struct.config_hash == current_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
Expand Down
4 changes: 2 additions & 2 deletions src/config/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_appchain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fn update_state_ok() {
);

let imsg = IMessagingDispatcher { contract_address: appchain.contract_address };
let iconfig = IConfigDispatcher { contract_address: appchain.contract_address };

let contract_sn = starknet::contract_address_const::<
993696174272377493693496825928908586134624850969
Expand All @@ -156,6 +157,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.
Expand Down

0 comments on commit 96a742d

Please sign in to comment.