Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : Check config hash and ProgramInfoChanged event #18

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 starknet::ContractAddress;
use super::errors;
Expand Down Expand Up @@ -94,6 +96,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
2 changes: 1 addition & 1 deletion src/snos_output.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const MESSAGE_TO_APPCHAIN_HEADER_SIZE: usize = 5;
/// <https://github.com/starkware-libs/cairo-lang/blob/caba294d82eeeccc3d86a158adb8ba209bf2d8fc/src/starkware/starknet/core/os/output.cairo#L52>.
/// The names are taken from SNOS repository:
/// <https://github.com/keep-starknet-strange/snos/blob/ad9a7df5fdbb63c813db285346eb667e032762e0/src/io/output.rs#L17>.
#[derive(Serde)]
#[derive(Drop, Serde)]
struct ProgramOutput {
/// The state commitment before this block.
prev_state_root: felt252,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_appchain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down