Skip to content

Commit

Permalink
imp: define Setup struct to streamline test setups
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Sep 17, 2024
1 parent 22c13b2 commit 09f2032
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 84 deletions.
20 changes: 15 additions & 5 deletions cairo-contracts/packages/apps/src/tests/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 }
}
}
Expand Down
21 changes: 21 additions & 0 deletions cairo-contracts/packages/contracts/src/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,8 +33,23 @@ pub mod IBCCore {
);
component!(path: ChannelHandlerComponent, storage: channel_handler, event: ChannelHandlerEvent);

#[abi(embed_v0)]
impl CoreChannelHanderImpl =
ChannelHandlerComponent::CoreChannelHandler<ContractState>;
impl ChannelInitializerImpl = ChannelHandlerComponent::ChannelInitializerImpl<ContractState>;

// -----------------------------------------------------------
// Setup Router Components
// -----------------------------------------------------------

component!(path: RouterHandlerComponent, storage: router_handler, event: RouterHandlerEvent);

#[abi(embed_v0)]
impl CoreRouterHanderImpl =
RouterHandlerComponent::CoreRouterHandler<ContractState>;
impl RouterInitializerImpl = RouterHandlerComponent::RouterInitializerImpl<ContractState>;


#[storage]
struct Storage {
#[substorage(v0)]
Expand All @@ -44,6 +60,8 @@ pub mod IBCCore {
channel_emitter: ChannelEventEmitterComponent::Storage,
#[substorage(v0)]
channel_handler: ChannelHandlerComponent::Storage,
#[substorage(v0)]
router_handler: RouterHandlerComponent::Storage,
}

#[event]
Expand All @@ -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();
}
}
2 changes: 2 additions & 0 deletions cairo-contracts/packages/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod clients {
pub use cometbft::CometClient;
}
mod tests {
mod setup;
#[cfg(test)]
mod test_channel;
#[cfg(test)]
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
51 changes: 51 additions & 0 deletions cairo-contracts/packages/contracts/src/tests/setup.cairo
Original file line number Diff line number Diff line change
@@ -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)
}
}

61 changes: 28 additions & 33 deletions cairo-contracts/packages/contracts/src/tests/test_channel.cairo
Original file line number Diff line number Diff line change
@@ -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);
}

36 changes: 14 additions & 22 deletions cairo-contracts/packages/contracts/src/tests/test_client.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand Down
28 changes: 9 additions & 19 deletions cairo-contracts/packages/contracts/src/tests/test_transfer.cairo
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
// -----------------------------------------------------------
Expand All @@ -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);

Expand Down Expand Up @@ -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());
Expand Down
Loading

0 comments on commit 09f2032

Please sign in to comment.