Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat-token-extensions' into feat…
Browse files Browse the repository at this point in the history
…-token-extensions
  • Loading branch information
ametel01 committed Sep 4, 2024
2 parents 868e993 + 582068c commit cafca98
Show file tree
Hide file tree
Showing 35 changed files with 1,959 additions and 404 deletions.
5 changes: 0 additions & 5 deletions cairo/src/contracts/client/gas_router_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ pub mod GasRouterComponent {
impl Router: RouterComponent::HasComponent<TContractState>,
+Drop<TContractState>
> of InternalTrait<TContractState> {
fn initialize(ref self: ComponentState<TContractState>, mailbox: ContractAddress) {
let mut router_comp = get_dep_component_mut!(ref self, Router);
router_comp.initialize(mailbox);
}

fn _Gas_router_hook_metadata(
self: @ComponentState<TContractState>, destination: u32
) -> Bytes {
Expand Down
20 changes: 14 additions & 6 deletions cairo/src/contracts/client/mailboxclient.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ mod mailboxClientProxy {
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[abi(embed_v0)]
impl MailboxclientImpl =
MailboxclientComponent::MailboxClientImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
Expand All @@ -27,12 +31,19 @@ mod mailboxClientProxy {
}

#[constructor]
fn constructor(ref self: ContractState, _mailbox: ContractAddress, _owner: ContractAddress,) {
self.mailboxclient.initialize(_mailbox);
fn constructor(
ref self: ContractState,
_mailbox: ContractAddress,
_owner: ContractAddress,
_hook: ContractAddress,
_interchain_security_module: ContractAddress
) {
self.ownable.initializer(_owner);
self
.mailboxclient
.initialize(_mailbox, Option::Some(_hook), Option::Some(_interchain_security_module));
}


#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
Expand All @@ -56,7 +67,4 @@ mod mailboxClientProxy {
self.upgradeable.upgrade(new_class_hash);
}
}
#[abi(embed_v0)]
impl MailboxclientImpl =
MailboxclientComponent::MailboxClientImpl<ContractState>;
}
36 changes: 12 additions & 24 deletions cairo/src/contracts/client/mailboxclient_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,6 @@ pub mod MailboxclientComponent {
self.interchain_security_module.read()
}


/// Initializes the mailbox client configuration.
/// Dev: callable only by the admin
///
/// # Arguments
///
/// * - `_hook` the hook contract address to set
/// * - `_interchain_security_module`- the ISM contract address
fn _MailboxClient_initialize(
ref self: ComponentState<TContractState>,
_hook: ContractAddress,
_interchain_security_module: ContractAddress,
_owner: ContractAddress
) {
let ownable_comp_read = get_dep_component!(@self, Owner);
ownable_comp_read.assert_only_owner();
self.set_hook(_hook);
self.set_interchain_security_module(_interchain_security_module);

let mut ownable_comp_write = get_dep_component_mut!(ref self, Owner);
ownable_comp_write.transfer_ownership(_owner);
}

/// Determines if a message associated to a given id is the mailbox's latest dispatched
///
/// # Arguments
Expand Down Expand Up @@ -219,10 +196,21 @@ pub mod MailboxclientComponent {
/// # Arguments
///
/// * - `_mailbox` - mailbox contract address
fn initialize(ref self: ComponentState<TContractState>, _mailbox: ContractAddress) {
fn initialize(
ref self: ComponentState<TContractState>,
_mailbox: ContractAddress,
_hook: Option<ContractAddress>,
_interchain_security_module: Option<ContractAddress>,
) {
let mailbox = IMailboxDispatcher { contract_address: _mailbox };
self.mailbox.write(mailbox);
self.local_domain.write(mailbox.get_local_domain());
if let Option::Some(hook) = _hook {
self.hook.write(hook);
}
if let Option::Some(ism) = _interchain_security_module {
self.interchain_security_module.write(ism);
}
}
}
}
86 changes: 19 additions & 67 deletions cairo/src/contracts/client/router_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use starknet::ContractAddress;

#[starknet::interface]
pub trait IRouter<TState> {
fn enroll_remote_router(ref self: TState, domain: u32, router: Option<u256>);
fn enroll_remote_router(ref self: TState, domain: u32, router: u256);
fn enroll_remote_routers(ref self: TState, domains: Array<u32>, addresses: Array<u256>);
fn unenroll_remote_router(ref self: TState, domain: u32);
fn unenroll_remote_routers(
ref self: TState, domains: Array<u32>, addresses: Option<Array<u256>>
);
fn unenroll_remote_routers(ref self: TState, domains: Array<u32>);
// fn handle(ref self: TState, origin: u32, sender: u256, message: Bytes);
fn domains(self: @TState) -> Array<u32>;
fn routers(self: @TState, domain: u32) -> u256;
Expand All @@ -17,10 +15,10 @@ pub trait IRouter<TState> {
#[starknet::component]
pub mod RouterComponent {
use alexandria_bytes::Bytes;
use alexandria_storage::{List, ListTrait};
use hyperlane_starknet::contracts::client::mailboxclient_component::{
MailboxclientComponent, MailboxclientComponent::MailboxClientInternalImpl
};
use hyperlane_starknet::contracts::libs::enumerable_map::{EnumerableMap, EnumerableMapTrait};
use hyperlane_starknet::interfaces::{
IMailboxClient, IMailboxDispatcher, IMailboxDispatcherTrait
};
Expand All @@ -31,7 +29,7 @@ pub mod RouterComponent {

#[storage]
struct Storage {
routers: List<u256>,
routers: EnumerableMap<u32, u256>,
gas_router: ContractAddress,
}

Expand All @@ -50,18 +48,11 @@ pub mod RouterComponent {
+Drop<TContractState>
> of super::IRouter<ComponentState<TContractState>> {
fn enroll_remote_router(
ref self: ComponentState<TContractState>, domain: u32, router: Option<u256>
ref self: ComponentState<TContractState>, domain: u32, router: u256
) {
let ownable_comp = get_dep_component!(@self, Owner);
ownable_comp.assert_only_owner();

match router {
Option::Some(router) => { self._enroll_remote_router(domain, router); },
Option::None => {
let router = self.routers.read().get(domain).expect('DOMAIN_NOT_FOUND');
self._enroll_remote_router(domain, router.unwrap());
}
}
self._enroll_remote_router(domain, router);
}

fn enroll_remote_routers(
Expand Down Expand Up @@ -89,31 +80,12 @@ pub mod RouterComponent {
self._unenroll_remote_router(domain);
}

fn unenroll_remote_routers(
ref self: ComponentState<TContractState>,
domains: Array<u32>,
addresses: Option<Array<u256>>
) {
fn unenroll_remote_routers(ref self: ComponentState<TContractState>, domains: Array<u32>,) {
let domains_len = domains.len();
match addresses {
Option::Some(addresses) => {
if addresses.len() != domains_len {
panic!("Addresses array length must match domains array length");
}

let mut i = 0;
while i < domains_len {
self._unenroll_remote_router(*domains.at(i));
i += 1;
}
},
Option::None => {
let mut i = 0;
while i < domains_len {
self._unenroll_remote_router(*domains.at(i));
i += 1;
}
}
let mut i = 0;
while i < domains_len {
self._unenroll_remote_router(*domains.at(i));
i += 1;
}
}

Expand All @@ -127,23 +99,11 @@ pub mod RouterComponent {
// }

fn domains(self: @ComponentState<TContractState>) -> Array<u32> {
let mut keys: Array<u32> = array![];
let routers = self.routers.read().array().expect('ROUTERS_EMPTY');

let mut i = 0;
let len = routers.len();
while i < len {
let element = *routers.at(i);
if element != 0 {
keys.append(i);
}
i += 1;
};
keys
self.routers.read().keys()
}

fn routers(self: @ComponentState<TContractState>, domain: u32) -> u256 {
self.routers.read().get(domain).expect('DOMAIN_NOT_FOUND').unwrap()
self.routers.read().get(domain)
}
}

Expand All @@ -154,11 +114,6 @@ pub mod RouterComponent {
+Drop<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn initialize(ref self: ComponentState<TContractState>, _mailbox: ContractAddress) {
let mut mailbox_comp = get_dep_component_mut!(ref self, MailBoxClient);
mailbox_comp.initialize(_mailbox);
}

// TODO: review later once we have a clear idea of how to handle virtual functions
// fn _handle(
// ref self: ComponentState<TContractState>, origin: u32, sender: u256, message: Bytes
Expand All @@ -178,29 +133,26 @@ pub mod RouterComponent {

fn _unenroll_remote_router(ref self: ComponentState<TContractState>, domain: u32) {
let mut routers = self.routers.read();

let _ = routers.get(domain).expect('DOMAIN_NOT_FOUND');

let _ = routers.set(domain, 0);
routers.remove(domain);
}

fn _is_remote_router(
self: @ComponentState<TContractState>, domain: u32, address: u256
) -> bool {
let routers = self.routers.read();
let router = routers.get(domain).expect('DOMAIN_NOT_FOUND');
router.unwrap() == address
let router = routers.get(domain);
router == address
}

fn _must_have_remote_router(self: @ComponentState<TContractState>, domain: u32) -> u256 {
let routers = self.routers.read();
let router = routers.get(domain).expect('DOMAIN_NOT_FOUND');
let router = routers.get(domain);

if router.is_none() {
if router == 0 {
Err::domain_not_found(domain);
}

router.unwrap()
router
}

fn _Router_dispatch(
Expand Down
4 changes: 3 additions & 1 deletion cairo/src/contracts/hooks/merkle_tree_hook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ pub mod merkle_tree_hook {
/// * `_mailbox` - The mailbox to be associated to the mailbox client
#[constructor]
fn constructor(ref self: ContractState, _mailbox: ContractAddress, _owner: ContractAddress) {
self.mailboxclient.initialize(_mailbox);
self
.mailboxclient
.initialize(_mailbox, Option::<ContractAddress>::None, Option::<ContractAddress>::None);
self.ownable.initializer(_owner);
}

Expand Down
2 changes: 1 addition & 1 deletion cairo/src/contracts/isms/multisig/validator_announce.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub mod validator_announce {

#[constructor]
fn constructor(ref self: ContractState, _mailbox: ContractAddress, _owner: ContractAddress) {
self.mailboxclient.initialize(_mailbox);
self.mailboxclient.initialize(_mailbox, Option::None, Option::None);
self.ownable.initializer(_owner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub mod default_fallback_routing_ism {
#[constructor]
fn constructor(ref self: ContractState, _owner: ContractAddress, _mailbox: ContractAddress) {
self.ownable.initializer(_owner);
self.mailboxclient.initialize(_mailbox);
self.mailboxclient.initialize(_mailbox, Option::None, Option::None);
}

#[abi(embed_v0)]
Expand Down
Loading

0 comments on commit cafca98

Please sign in to comment.