Skip to content

Commit

Permalink
Feat: Add Upgradeable Component to relevant contracts and Contract St…
Browse files Browse the repository at this point in the history
…ructure Reorganisation (#40)

* fix: remove unused merkle lib

* fix: mailbox client local domain redirection

* fix: ByteData structure change (is_address -> size)

* fix: remove unused lib

* feat: test changes for ByteData

* fix: bug fix for keccak function

* feat: refactor and add keccak documentation

* fix: refactor and bug merkle tree hook

* Feat: Upgradeable Component Addition and Structure adjustements

* fix(rust): mailboxclient event alias

* fix: format code

---------

Co-authored-by: 0xevolve <[email protected]>
  • Loading branch information
JordyRo1 and EvolveArt authored Jun 18, 2024
1 parent 7bfd99f commit ac4a2e6
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 185 deletions.
26 changes: 24 additions & 2 deletions contracts/src/contracts/client/mailboxclient.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ mod mailboxclient {
};
use openzeppelin::access::ownable::ownable::OwnableComponent::InternalTrait;
use openzeppelin::access::ownable::{OwnableComponent,};
use starknet::ContractAddress;
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent};
use starknet::{ContractAddress, ClassHash};
component!(path: MailboxclientComponent, storage: mailboxclient, event: MailboxclientEvent);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
#[substorage(v0)]
mailboxclient: MailboxclientComponent::Storage,
}

Expand All @@ -31,8 +40,21 @@ mod mailboxclient {
MailboxclientEvent: MailboxclientComponent::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
}
#[abi(embed_v0)]
impl Upgradeable of IUpgradeable<ContractState> {
/// Upgrades the contract to a new implementation.
/// Callable only by the owner
/// # Arguments
///
/// * `new_class_hash` - The class hash of the new implementation.
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.ownable.assert_only_owner();
self.upgradeable._upgrade(new_class_hash);
}
}

#[abi(embed_v0)]
impl MailboxclientImpl =
MailboxclientComponent::MailboxClientImpl<ContractState>;
Expand Down
4 changes: 3 additions & 1 deletion contracts/src/contracts/client/mailboxclient_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ pub mod MailboxclientComponent {
impl Owner: OwnableComponent::HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn initialize(ref self: ComponentState<TContractState>, _mailbox: ContractAddress) {
self.mailbox.write(IMailboxDispatcher { contract_address: _mailbox });
let mailbox = IMailboxDispatcher { contract_address: _mailbox };
self.mailbox.write(mailbox);
self.local_domain.write(mailbox.get_local_domain());
}
}
}
131 changes: 0 additions & 131 deletions contracts/src/contracts/client/router.cairo

This file was deleted.

17 changes: 15 additions & 2 deletions contracts/src/contracts/hooks/merkle_tree_hook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub mod merkle_tree_hook {
use alexandria_bytes::{Bytes, BytesTrait};
use alexandria_math::pow;
use hyperlane_starknet::contracts::client::mailboxclient_component::{
MailboxclientComponent, MailboxclientComponent::MailboxClientInternalImpl
MailboxclientComponent, MailboxclientComponent::MailboxClientInternalImpl,
MailboxclientComponent::MailboxClientImpl
};
use hyperlane_starknet::contracts::hooks::libs::standard_hook_metadata::standard_hook_metadata::{
StandardHookMetadata, VARIANT
Expand All @@ -18,7 +19,7 @@ pub mod merkle_tree_hook {
};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent};
use starknet::ContractAddress;
use starknet::{ContractAddress, ClassHash};

#[derive(Serde, Drop)]
pub struct Tree {
Expand Down Expand Up @@ -82,6 +83,18 @@ pub mod merkle_tree_hook {
self.ownable.initializer(_owner);
}

#[abi(embed_v0)]
impl Upgradeable of IUpgradeable<ContractState> {
/// Upgrades the contract to a new implementation.
/// Callable only by the owner
/// # Arguments
///
/// * `new_class_hash` - The class hash of the new implementation.
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.ownable.assert_only_owner();
self.upgradeable._upgrade(new_class_hash);
}
}

#[abi(embed_v0)]
impl IMerkleTreeHookImpl of IMerkleTreeHook<ContractState> {
Expand Down
6 changes: 0 additions & 6 deletions contracts/src/contracts/hooks/protocol_fee.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ pub mod protocol_fee {
ContractAddress, contract_address_const, get_caller_address, get_contract_address
};
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
Expand All @@ -27,8 +25,6 @@ pub mod protocol_fee {
fee_token: ContractAddress,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
}

mod Errors {
Expand All @@ -44,8 +40,6 @@ pub mod protocol_fee {
enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
}

#[constructor]
Expand Down
42 changes: 30 additions & 12 deletions contracts/src/contracts/isms/multisig/validator_announce.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ pub mod validator_announce {
use alexandria_bytes::{Bytes, BytesTrait};
use alexandria_data_structures::array_ext::ArrayTraitExt;
use core::poseidon::poseidon_hash_span;
use hyperlane_starknet::contracts::client::mailboxclient_component::{
MailboxclientComponent, MailboxclientComponent::MailboxClientInternalImpl,
MailboxclientComponent::MailboxClientImpl
};
use hyperlane_starknet::contracts::libs::checkpoint_lib::checkpoint_lib::{
HYPERLANE_ANNOUNCEMENT
};
Expand All @@ -13,14 +17,25 @@ pub mod validator_announce {
u64_word_size, HASH_SIZE
};
use hyperlane_starknet::utils::store_arrays::StoreFelt252Array;
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent};
use starknet::ContractAddress;
use starknet::EthAddress;
use starknet::eth_signature::is_eth_signature_valid;
use starknet::secp256_trait::{Signature, signature_from_vrs};

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
component!(path: MailboxclientComponent, storage: mailboxclient, event: MailboxclientEvent);

#[storage]
struct Storage {
mailboxclient: ContractAddress,
#[substorage(v0)]
mailboxclient: MailboxclientComponent::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
storage_location: LegacyMap::<EthAddress, Array<felt252>>,
replay_protection: LegacyMap::<felt252, bool>,
validators: LegacyMap::<EthAddress, EthAddress>,
Expand All @@ -30,7 +45,13 @@ pub mod validator_announce {
#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
ValidatorAnnouncement: ValidatorAnnouncement
ValidatorAnnouncement: ValidatorAnnouncement,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
#[flat]
MailboxclientEvent: MailboxclientComponent::Event,
}

#[derive(starknet::Event, Drop)]
Expand All @@ -45,8 +66,8 @@ pub mod validator_announce {
}

#[constructor]
fn constructor(ref self: ContractState, _mailbox_client: ContractAddress) {
self.mailboxclient.write(_mailbox_client);
fn constructor(ref self: ContractState, _mailbox: ContractAddress) {
self.mailboxclient.initialize(_mailbox);
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -154,18 +175,15 @@ pub mod validator_announce {
}

fn domain_hash(self: @ContractState) -> u256 {
let mailboxclient = IMailboxClientDispatcher {
contract_address: self.mailboxclient.read()
};
let mailboxclient_address: felt252 = self.mailboxclient.read().try_into().unwrap();
let mailbox_address: felt252 = self.mailboxclient.mailbox().try_into().unwrap();
let mut input: Array<ByteData> = array![
ByteData {
value: mailboxclient.get_local_domain().into(),
size: u64_word_size(mailboxclient.get_local_domain().into()).into()
value: self.mailboxclient.get_local_domain().into(),
size: u64_word_size(self.mailboxclient.get_local_domain().into()).into()
},
ByteData {
value: mailboxclient_address.try_into().unwrap(),
size: u256_word_size(mailboxclient_address.try_into().unwrap()).into()
value: mailbox_address.try_into().unwrap(),
size: u256_word_size(mailbox_address.try_into().unwrap()).into()
},
ByteData { value: HYPERLANE_ANNOUNCEMENT.into(), size: 22 }
];
Expand Down
Loading

0 comments on commit ac4a2e6

Please sign in to comment.