diff --git a/cairo-contracts/packages/apps/src/tests/config.cairo b/cairo-contracts/packages/apps/src/tests/config.cairo index 9019c5de..9c0c2888 100644 --- a/cairo-contracts/packages/apps/src/tests/config.cairo +++ b/cairo-contracts/packages/apps/src/tests/config.cairo @@ -5,10 +5,10 @@ use starknet_ibc_apps::transfer::types::PrefixedDenomTrait; use starknet_ibc_apps::transfer::types::{ MsgTransfer, PacketData, PrefixedDenom, Denom, Memo, TracePrefixTrait, Participant }; -use starknet_ibc_core::channel::Packet; -use starknet_ibc_core::client::{Height, Timestamp}; +use starknet_ibc_core::channel::{Packet, MsgRecvPacket}; +use starknet_ibc_core::client::Timestamp; use starknet_ibc_core::host::{PortId, ChannelId, Sequence}; -use starknet_ibc_core::tests::{PORT_ID, CHANNEL_ID}; +use starknet_ibc_core::tests::{PORT_ID, CHANNEL_ID, HEIGHT}; #[derive(Clone, Debug, Drop, Serde)] pub struct TransferAppConfig { @@ -72,11 +72,21 @@ pub impl TransferAppConfigImpl of TransferAppConfigTrait { port_id_on_a: PORT_ID(), chan_id_on_a: self.chan_id_on_a.clone(), packet_data: self.dummy_packet_data(denom, sender, receiver), - timeout_height_on_b: Height { revision_number: 0, revision_height: 1000 }, + timeout_height_on_b: HEIGHT(1000), timeout_timestamp_on_b: Timestamp { timestamp: 1000 } } } + fn dummy_msg_recv_packet( + self: @TransferAppConfig, denom: PrefixedDenom, sender: Participant, receiver: Participant + ) -> MsgRecvPacket { + MsgRecvPacket { + packet: self.dummy_recv_packet(denom, sender, receiver), + proof_commitment_on_a: array![], + proof_height_on_a: HEIGHT(10), + } + } + fn dummy_recv_packet( self: @TransferAppConfig, denom: PrefixedDenom, sender: Participant, receiver: Participant ) -> Packet { @@ -90,7 +100,7 @@ pub impl TransferAppConfigImpl of TransferAppConfigTrait { port_id_on_b: PORT_ID(), chan_id_on_b: self.chan_id_on_b.clone(), data: serialized_data, - timeout_height_on_b: Height { revision_number: 0, revision_height: 1000 }, + timeout_height_on_b: HEIGHT(1000), timeout_timestamp_on_b: Timestamp { timestamp: 1000 } } } diff --git a/cairo-contracts/packages/contracts/src/core.cairo b/cairo-contracts/packages/contracts/src/core.cairo index af01df8d..a04a2cec 100644 --- a/cairo-contracts/packages/contracts/src/core.cairo +++ b/cairo-contracts/packages/contracts/src/core.cairo @@ -4,6 +4,7 @@ pub mod IBCCore { use starknet_ibc_core::channel::ChannelHandlerComponent; use starknet_ibc_core::client::ClientEventEmitterComponent; use starknet_ibc_core::client::ClientHandlerComponent; + use starknet_ibc_core::router::RouterHandlerComponent; // ----------------------------------------------------------- // Setup Client Components @@ -32,8 +33,23 @@ pub mod IBCCore { ); component!(path: ChannelHandlerComponent, storage: channel_handler, event: ChannelHandlerEvent); + #[abi(embed_v0)] + impl CoreChannelHanderImpl = + ChannelHandlerComponent::CoreChannelHandler; impl ChannelInitializerImpl = ChannelHandlerComponent::ChannelInitializerImpl; + // ----------------------------------------------------------- + // Setup Router Components + // ----------------------------------------------------------- + + component!(path: RouterHandlerComponent, storage: router_handler, event: RouterHandlerEvent); + + #[abi(embed_v0)] + impl CoreRouterHanderImpl = + RouterHandlerComponent::CoreRouterHandler; + impl RouterInitializerImpl = RouterHandlerComponent::RouterInitializerImpl; + + #[storage] struct Storage { #[substorage(v0)] @@ -44,6 +60,8 @@ pub mod IBCCore { channel_emitter: ChannelEventEmitterComponent::Storage, #[substorage(v0)] channel_handler: ChannelHandlerComponent::Storage, + #[substorage(v0)] + router_handler: RouterHandlerComponent::Storage, } #[event] @@ -57,11 +75,14 @@ pub mod IBCCore { ChannelEventEmitterEvent: ChannelEventEmitterComponent::Event, #[flat] ChannelHandlerEvent: ChannelHandlerComponent::Event, + #[flat] + RouterHandlerEvent: RouterHandlerComponent::Event, } #[constructor] fn constructor(ref self: ContractState) { self.client_handler.initializer(); self.channel_handler.initializer(); + self.router_handler.initializer(); } } diff --git a/cairo-contracts/packages/contracts/src/lib.cairo b/cairo-contracts/packages/contracts/src/lib.cairo index 7696ce65..e7237e6d 100644 --- a/cairo-contracts/packages/contracts/src/lib.cairo +++ b/cairo-contracts/packages/contracts/src/lib.cairo @@ -9,6 +9,7 @@ pub mod clients { pub use cometbft::CometClient; } mod tests { + mod setup; #[cfg(test)] mod test_channel; #[cfg(test)] @@ -20,6 +21,7 @@ mod tests { pub use handles::client::{ClientHandleImpl, ClientHandle}; pub use handles::core::{CoreContract, CoreHandleImpl, CoreHandle}; pub use handles::erc20::{ERC20HandleImpl, ERC20Handle}; + pub use setup::{Setup, SetupImpl, SetupTrait}; mod handles { pub mod app; diff --git a/cairo-contracts/packages/contracts/src/tests/handles/app.cairo b/cairo-contracts/packages/contracts/src/tests/handles/app.cairo index ae16eaa3..51b664ac 100644 --- a/cairo-contracts/packages/contracts/src/tests/handles/app.cairo +++ b/cairo-contracts/packages/contracts/src/tests/handles/app.cairo @@ -22,7 +22,7 @@ pub struct AppContract { #[generate_trait] pub impl AppHandleImpl of AppHandle { - fn setup_transfer(owner: ContractAddress, erc20_class: ContractClass) -> AppContract { + fn deploy_transfer(owner: ContractAddress, erc20_class: ContractClass) -> AppContract { let mut call_data = array![]; call_data.append_serde(owner); diff --git a/cairo-contracts/packages/contracts/src/tests/handles/client.cairo b/cairo-contracts/packages/contracts/src/tests/handles/client.cairo index 0ad544dd..69ebe0be 100644 --- a/cairo-contracts/packages/contracts/src/tests/handles/client.cairo +++ b/cairo-contracts/packages/contracts/src/tests/handles/client.cairo @@ -3,7 +3,7 @@ use starknet_ibc_core::client::ClientContract; #[generate_trait] pub impl ClientHandleImpl of ClientHandle { - fn setup_cometbft() -> ClientContract { + fn deploy_cometbft() -> ClientContract { let mut call_data = array![]; let address = declare_and_deploy("CometClient", call_data); diff --git a/cairo-contracts/packages/contracts/src/tests/handles/core.cairo b/cairo-contracts/packages/contracts/src/tests/handles/core.cairo index c6324cbf..e4bf808f 100644 --- a/cairo-contracts/packages/contracts/src/tests/handles/core.cairo +++ b/cairo-contracts/packages/contracts/src/tests/handles/core.cairo @@ -21,7 +21,7 @@ pub struct CoreContract { #[generate_trait] pub impl CoreHandleImpl of CoreHandle { - fn setup() -> CoreContract { + fn deploy() -> CoreContract { let mut call_data = array![]; let address = declare_and_deploy("IBCCore", call_data); diff --git a/cairo-contracts/packages/contracts/src/tests/handles/erc20.cairo b/cairo-contracts/packages/contracts/src/tests/handles/erc20.cairo index f2e361e7..0f60dd67 100644 --- a/cairo-contracts/packages/contracts/src/tests/handles/erc20.cairo +++ b/cairo-contracts/packages/contracts/src/tests/handles/erc20.cairo @@ -7,7 +7,7 @@ use starknet_ibc_apps::transfer::{ERC20Contract, ERC20ContractTrait}; #[generate_trait] pub impl ERC20HandleImpl of ERC20Handle { - fn setup(contract_class: ContractClass) -> ERC20Contract { + fn deploy(contract_class: ContractClass) -> ERC20Contract { deploy(contract_class, dummy_erc20_call_data()).into() } diff --git a/cairo-contracts/packages/contracts/src/tests/setup.cairo b/cairo-contracts/packages/contracts/src/tests/setup.cairo new file mode 100644 index 00000000..04e670d0 --- /dev/null +++ b/cairo-contracts/packages/contracts/src/tests/setup.cairo @@ -0,0 +1,51 @@ +use openzeppelin_testing::declare_class; +use snforge_std::ContractClass; +use starknet::ContractAddress; +use starknet_ibc_apps::tests::OWNER; +use starknet_ibc_apps::transfer::ERC20Contract; +use starknet_ibc_contracts::tests::{ + AppContract, CoreContract, CoreHandle, ClientHandle, ERC20Handle, AppHandle +}; +use starknet_ibc_core::client::ClientContract; +use starknet_ibc_core::tests::CLIENT_TYPE; + +#[derive(Drop, Serde)] +pub struct Setup { + pub owner: ContractAddress, + pub erc20_contract_class: ContractClass +} + +#[generate_trait] +pub impl SetupImpl of SetupTrait { + /// Initializes the test setup with default values. + fn default() -> Setup { + Setup { owner: OWNER(), erc20_contract_class: declare_class("ERC20Mintable"), } + } + + /// Deploys an instance of IBC core contract. + fn deploy_core(self: @Setup) -> CoreContract { + CoreHandle::deploy() + } + + /// Deploys an instance of CometBFT client contract. + fn deploy_cometbft(self: @Setup, ref core: CoreContract) -> ClientContract { + // Deploy a Comet client contract. + let comet = ClientHandle::deploy_cometbft(); + + // Register the Comet client into the IBC core contract. + core.register_client(CLIENT_TYPE(), comet.address); + + comet + } + + /// Deploys an instance of ERC20 contract. + fn deploy_erc20(self: @Setup) -> ERC20Contract { + ERC20Handle::deploy(*self.erc20_contract_class) + } + + /// Deploys an instance of ICS-20 Token Transfer contract. + fn deploy_trasnfer(self: @Setup) -> AppContract { + AppHandle::deploy_transfer(self.owner.clone(), *self.erc20_contract_class) + } +} + diff --git a/cairo-contracts/packages/contracts/src/tests/test_channel.cairo b/cairo-contracts/packages/contracts/src/tests/test_channel.cairo index 3c4cd6a1..d6cdc77f 100644 --- a/cairo-contracts/packages/contracts/src/tests/test_channel.cairo +++ b/cairo-contracts/packages/contracts/src/tests/test_channel.cairo @@ -1,35 +1,30 @@ -use openzeppelin_testing::declare_class; -use starknet_ibc_apps::tests::{TransferAppConfigTrait, OWNER}; -use starknet_ibc_apps::transfer::ERC20Contract; -use starknet_ibc_contracts::tests::{ - ClientHandle, ERC20Handle, AppHandle, AppContract, CoreContract, CoreHandle -}; -use starknet_ibc_core::client::ClientContract; - -// Deploys an instance of IBC core, Cometbft ligth client, and Token Transfer -// applicaiton contracts, and registers the client and application into the core -// contract. -fn setup_contracts( - client_type: felt252 -) -> (CoreContract, ClientContract, AppContract, ERC20Contract) { - // Deploy an IBC core contract. - let mut core = CoreHandle::setup(); - - // Deploy a Comet client contract. - let comet = ClientHandle::setup_cometbft(); - - // Register the Comet client into the IBC core contract. - core.register_client(client_type, comet.address); - - // Declare the ERC20 contract class. - let erc20_contract_class = declare_class("ERC20Mintable"); - - // Deploy an ERC20 contract. - let mut erc20 = ERC20Handle::setup(erc20_contract_class); - - // Deploy an ICS20 Token Transfer contract. - let mut ics20 = AppHandle::setup_transfer(OWNER(), erc20_contract_class); - - (core, comet, ics20, erc20) +use starknet_ibc_apps::tests::{TransferAppConfigTrait, COSMOS, STARKNET}; +use starknet_ibc_contracts::tests::{SetupImpl, CoreHandle}; + +#[test] +#[should_panic] +fn test_recv_packet_ok() { + // ----------------------------------------------------------- + // Setup Essentials + // ----------------------------------------------------------- + + let mut transfer_cfg = TransferAppConfigTrait::default(); + + let setup = SetupImpl::default(); + + let mut core = setup.deploy_core(); + + let _comet = setup.deploy_cometbft(ref core); + + let _ics20 = setup.deploy_trasnfer(); + + // ----------------------------------------------------------- + // Receive Packet + // ----------------------------------------------------------- + + let msg = transfer_cfg + .dummy_msg_recv_packet(transfer_cfg.hosted_denom.clone(), COSMOS(), STARKNET()); + + core.recv_packet(msg); } diff --git a/cairo-contracts/packages/contracts/src/tests/test_client.cairo b/cairo-contracts/packages/contracts/src/tests/test_client.cairo index d231ec98..fae8f754 100644 --- a/cairo-contracts/packages/contracts/src/tests/test_client.cairo +++ b/cairo-contracts/packages/contracts/src/tests/test_client.cairo @@ -2,26 +2,11 @@ use openzeppelin_testing::events::EventSpyExt; use snforge_std::cheat_block_timestamp_global; use snforge_std::spy_events; use starknet_ibc_clients::tests::CometClientConfigTrait; -use starknet_ibc_contracts::tests::{CoreContract, CoreHandle, ClientHandle}; +use starknet_ibc_contracts::tests::{CoreHandle, SetupImpl}; use starknet_ibc_core::client::{ UpdateResponse, Height, StatusTrait, ClientContract, ClientContractTrait }; -use starknet_ibc_core::tests::ClientEventSpyExt; - -// Deploys the IBC core and Comet client contracts, and registers the Comet -// client into the IBC core. -fn setup_contracts(client_type: felt252) -> (CoreContract, ClientContract) { - // Deploy an IBC core contract. - let mut core = CoreHandle::setup(); - - // Deploy a Comet client contract. - let comet = ClientHandle::setup_cometbft(); - - // Register the Comet client into the IBC core contract. - core.register_client(client_type, comet.address); - - (core, comet) -} +use starknet_ibc_core::tests::{ClientEventSpyExt, HEIGHT}; #[test] fn test_create_comet_client_ok() { @@ -31,7 +16,11 @@ fn test_create_comet_client_ok() { let mut cfg = CometClientConfigTrait::default(); - let (mut core, comet) = setup_contracts(cfg.client_type); + let setup = SetupImpl::default(); + + let mut core = setup.deploy_core(); + + let mut comet = setup.deploy_cometbft(ref core); let mut spy = spy_events(); @@ -64,12 +53,16 @@ fn test_create_comet_client_ok() { #[test] fn test_update_comet_client_ok() { // ----------------------------------------------------------- - // Setup Contracts + // Setup Essentials // ----------------------------------------------------------- let mut cfg = CometClientConfigTrait::default(); - let (mut core, comet) = setup_contracts(cfg.client_type); + let setup = SetupImpl::default(); + + let mut core = setup.deploy_core(); + + let comet = setup.deploy_cometbft(ref core); let mut spy = spy_events(); @@ -92,8 +85,7 @@ fn test_update_comet_client_ok() { spy.drop_all_events(); // Update the client to a new height. - let updating_height = cfg.latest_height.clone() - + Height { revision_number: 0, revision_height: 5 }; + let updating_height = cfg.latest_height.clone() + HEIGHT(5); // Create a `MsgUpdateClient` message. let msg = cfg diff --git a/cairo-contracts/packages/contracts/src/tests/test_transfer.cairo b/cairo-contracts/packages/contracts/src/tests/test_transfer.cairo index a2eb31e7..490a4b30 100644 --- a/cairo-contracts/packages/contracts/src/tests/test_transfer.cairo +++ b/cairo-contracts/packages/contracts/src/tests/test_transfer.cairo @@ -1,5 +1,3 @@ -use core::traits::TryInto; -use openzeppelin_testing::declare_class; use openzeppelin_testing::events::EventSpyExt; use snforge_std::spy_events; use snforge_std::start_cheat_caller_address; @@ -9,23 +7,9 @@ use starknet_ibc_apps::tests::{ TransferAppConfigTrait, NAME, SYMBOL, SUPPLY, OWNER, COSMOS, STARKNET }; use starknet_ibc_apps::transfer::ERC20Contract; -use starknet_ibc_contracts::tests::{ERC20Handle, AppHandle, AppContract}; +use starknet_ibc_contracts::tests::{SetupImpl, ERC20Handle, AppHandle}; use starknet_ibc_utils::ComputeKeyTrait; -// Deploys an instance of ERC20 and ICS20 Token Transfer contracts. -fn setup_contracts() -> (ERC20Contract, AppContract) { - // Declare the ERC20 contract class. - let erc20_contract_class = declare_class("ERC20Mintable"); - - // Deploy an ERC20 contract. - let mut erc20 = ERC20Handle::setup(erc20_contract_class); - - // Deploy an ICS20 Token Transfer contract. - let mut ics20 = AppHandle::setup_transfer(OWNER(), erc20_contract_class); - - (erc20, ics20) -} - #[test] fn test_escrow_unescrow_roundtrip() { // ----------------------------------------------------------- @@ -34,7 +18,11 @@ fn test_escrow_unescrow_roundtrip() { let mut cfg = TransferAppConfigTrait::default(); - let (mut erc20, mut ics20) = setup_contracts(); + let setup = SetupImpl::default(); + + let mut erc20 = setup.deploy_erc20(); + + let mut ics20 = setup.deploy_trasnfer(); cfg.set_native_denom(erc20.address); @@ -97,7 +85,9 @@ fn test_mint_burn_roundtrip() { let mut cfg = TransferAppConfigTrait::default(); - let (_, mut ics20) = setup_contracts(); + let setup = SetupImpl::default(); + + let mut ics20 = setup.deploy_trasnfer(); // Set the caller address, as callbacks are permissioned. start_cheat_caller_address(ics20.address, OWNER()); diff --git a/cairo-contracts/packages/core/src/channel/components/handler.cairo b/cairo-contracts/packages/core/src/channel/components/handler.cairo index ee3b502e..803bd937 100644 --- a/cairo-contracts/packages/core/src/channel/components/handler.cairo +++ b/cairo-contracts/packages/core/src/channel/components/handler.cairo @@ -51,7 +51,7 @@ pub mod ChannelHandlerComponent { } #[embeddable_as(CoreChannelHandler)] - pub impl CoreChannelHandlerImpl< + impl CoreChannelHandlerImpl< TContractState, +HasComponent, +Drop,