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

Added Program hash changed event #13

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 13 additions & 2 deletions src/appchain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ mod appchain {
OwnableComponent as ownable_cpt, OwnableComponent::InternalTrait as OwnableInternal,
interface::IOwnable
};
use openzeppelin::security::reentrancyguard::{
ReentrancyGuardComponent,
ReentrancyGuardComponent::InternalTrait as InternalReentrancyGuardImpl
};
use piltover::config::{config_cpt, config_cpt::InternalTrait as ConfigInternal, IConfig};
use piltover::interface::IAppchain;
use piltover::messaging::{
Expand All @@ -31,6 +35,9 @@ mod appchain {
component!(path: ownable_cpt, storage: ownable, event: OwnableEvent);
component!(path: config_cpt, storage: config, event: ConfigEvent);
component!(path: messaging_cpt, storage: messaging, event: MessagingEvent);
component!(
path: ReentrancyGuardComponent, storage: reentrancy_guard, event: ReentrancyGuardEvent
);

#[abi(embed_v0)]
impl ConfigImpl = config_cpt::ConfigImpl<ContractState>;
Expand All @@ -45,6 +52,8 @@ mod appchain {
config: config_cpt::Storage,
#[substorage(v0)]
messaging: messaging_cpt::Storage,
#[substorage(v0)]
reentrancy_guard: ReentrancyGuardComponent::Storage,
}

#[event]
Expand All @@ -56,6 +65,8 @@ mod appchain {
ConfigEvent: config_cpt::Event,
#[flat]
MessagingEvent: messaging_cpt::Event,
#[flat]
ReentrancyGuardEvent: ReentrancyGuardComponent::Event,
}

/// Initializes the contract.
Expand All @@ -72,9 +83,8 @@ mod appchain {
#[abi(embed_v0)]
impl Appchain of IAppchain<ContractState> {
fn update_state(ref self: ContractState, program_output: Span<felt252>) {
self.reentrancy_guard.start();
self.config.assert_only_owner_or_operator();

// TODO(#2): reentrancy guard.
// TODO(#3): facts verification.
// TODO(#4): update the current state (component needed).

Expand All @@ -98,6 +108,7 @@ mod appchain {

self.messaging.process_messages_to_starknet(messages_to_starknet);
self.messaging.process_messages_to_appchain(messages_to_appchain);
self.reentrancy_guard.end();
}
}
}
27 changes: 24 additions & 3 deletions src/config/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod config_cpt {
interface::IOwnable,
};
use piltover::config::interface::IConfig;
use starknet::ContractAddress;
use starknet::{ContractAddress, get_caller_address};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our current style is to not import functions like this, instead when you use the function do Starknet::get_caller_address

Copy link
Contributor Author

@Akashneelesh Akashneelesh Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is how its been done in other projects as well,

use starknet::{ContractAddress, get_caller_address};

I feel this is like a best practices thing, instead if we have another instance we dont need to do

Starknet::get_caller_address

Rather just call get_caller_address

use super::errors;

#[storage]
Expand All @@ -32,6 +32,19 @@ mod config_cpt {
facts_registry: ContractAddress,
}

#[event]
#[derive(Copy, Drop, starknet::Event)]
enum Event {
ProgramHashChanged: ProgramHashChanged,
}

#[derive(Copy, Drop, starknet::Event)]
struct ProgramHashChanged {
changed_by: ContractAddress,
old_program_hash: felt252,
new_program_hash: felt252,
}

#[embeddable_as(ConfigImpl)]
impl Config<
TContractState,
Expand All @@ -51,8 +64,16 @@ mod config_cpt {
ref self: ComponentState<TContractState>, program_hash: felt252, config_hash: felt252
) {
self.assert_only_owner_or_operator();
self.program_info.write((program_hash, config_hash))
// TODO(#6): ProgramHashChanged Event
let (old_program_hash, _): (felt252, felt252) = self.program_info.read();
self.program_info.write((program_hash, config_hash));
self
.emit(
ProgramHashChanged {
changed_by: get_caller_address(),
old_program_hash: old_program_hash,
new_program_hash: program_hash,
Copy link
Collaborator

@glihm glihm Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in this contract the program info contains both the config hash and the program hash (as design that may be challenged @drspacemn if we should be more like solidity version), we should have a new event.

Something like:

struct ProgramInfoChanged {
    changed_by: ContractAddress,
    old_program_hash: ...,
    new_program_hash: ...,
    old_config_hash: ...,
    new_config_hash: ...,
}

WDYT guys @Akashneelesh @drspacemn @b-j-roberts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep true, it will have both the program_hash and config_hash

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say either create a ProgramInfoChanged event or add the ConfigHashChanged event like Solidity and emit both events instead. Up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll just change it into ProgramInfoChanged and commit it now

}
);
}

fn get_program_info(self: @ComponentState<TContractState>) -> (felt252, felt252) {
Expand Down