Skip to content

Commit

Permalink
imp: introduce starknet_ibc_testkit for shared testing utilities ac…
Browse files Browse the repository at this point in the history
…ross IBC packages (#95)

* imp: introduce starknet_ibc_testkit

* nitpicking
  • Loading branch information
Farhad-Shabani authored Oct 23, 2024
1 parent 84cea7d commit ff13029
Show file tree
Hide file tree
Showing 35 changed files with 201 additions and 111 deletions.
17 changes: 17 additions & 0 deletions cairo-contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ dependencies = [
"openzeppelin_utils",
"snforge_std",
"starknet_ibc_core",
"starknet_ibc_testkit",
"starknet_ibc_utils",
]

Expand All @@ -97,6 +98,7 @@ name = "starknet_ibc_clients"
version = "0.1.0"
dependencies = [
"starknet_ibc_core",
"starknet_ibc_testkit",
]

[[package]]
Expand All @@ -105,9 +107,11 @@ version = "0.1.0"
dependencies = [
"openzeppelin_access",
"openzeppelin_token",
"snforge_std",
"starknet_ibc_apps",
"starknet_ibc_clients",
"starknet_ibc_core",
"starknet_ibc_testkit",
"starknet_ibc_utils",
]

Expand All @@ -117,9 +121,22 @@ version = "0.1.0"
dependencies = [
"openzeppelin_testing",
"snforge_std",
"starknet_ibc_testkit",
"starknet_ibc_utils",
]

[[package]]
name = "starknet_ibc_testkit"
version = "0.1.0"
dependencies = [
"openzeppelin_access",
"openzeppelin_testing",
"openzeppelin_utils",
"starknet_ibc_apps",
"starknet_ibc_clients",
"starknet_ibc_core",
]

[[package]]
name = "starknet_ibc_utils"
version = "0.1.0"
Expand Down
2 changes: 2 additions & 0 deletions cairo-contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"packages/clients",
"packages/contracts",
"packages/core",
"packages/testkit",
"packages/utils",
]

Expand Down Expand Up @@ -37,6 +38,7 @@ starknet_ibc_apps = { path = "packages/apps" }
starknet_ibc_clients = { path = "packages/clients" }
starknet_ibc_contracts = { path = "packages/contracts" }
starknet_ibc_core = { path = "packages/core" }
starknet_ibc_testkit = { path = "packages/testkit" }
starknet_ibc_utils = { path = "packages/utils" }

[workspace.tool.fmt]
Expand Down
4 changes: 4 additions & 0 deletions cairo-contracts/packages/apps/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ starknet_ibc_core = { workspace = true }
starknet_ibc_utils = { workspace = true }

[dev-dependencies]
# external dependencies
openzeppelin_testing = { workspace = true }
snforge_std = { workspace = true }

# ibc dependencies
starknet_ibc_testkit = { workspace = true }
21 changes: 3 additions & 18 deletions cairo-contracts/packages/apps/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,8 @@ pub mod transfer {
"transfer"
}
}
pub mod tests {
mod config;
mod dummy;
mod extend_spy;
#[cfg(test)]
mod test_transfer;
#[cfg(test)]
pub use mocks::mock_transfer::MockTransferApp;
pub use config::{TransferAppConfig, TransferAppConfigImpl, TransferAppConfigTrait};
pub use dummy::{
NAME, SYMBOL, PUBKEY, AMOUNT, SUPPLY, OWNER, STARKNET, COSMOS, SALT, DECIMALS, CLASS_HASH,
EMPTY_MEMO
};

pub use extend_spy::{TransferEventSpyExtImpl, TransferEventSpyExt};
#[cfg(test)]
mod mocks {
pub mod mock_transfer;
}
#[cfg(test)]
mod tests {
mod transfer;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use starknet_ibc_apps::tests::{MockTransferApp, CLASS_HASH};
use starknet_ibc_apps::transfer::TokenTransferComponent::{
TransferInitializerImpl, TransferReaderImpl
};
use starknet_ibc_apps::transfer::TokenTransferComponent;
use starknet_ibc_testkit::dummies::CLASS_HASH;
use starknet_ibc_testkit::mocks::MockTransferApp;

type ComponentState = TokenTransferComponent::ComponentState<MockTransferApp::ContractState>;

Expand Down
3 changes: 3 additions & 0 deletions cairo-contracts/packages/clients/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ starknet = { workspace = true }

# ibc dependencies
starknet_ibc_core = { workspace = true }

[dev-dependencies]
starknet_ibc_testkit = { workspace = true }
14 changes: 3 additions & 11 deletions cairo-contracts/packages/clients/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@ pub mod cometbft {
CometHeader, CometHeaderImpl, CometHeaderIntoConsensusState, CometHeaderTrait, SignedHeader
};
}
pub mod tests {
mod config;
#[cfg(test)]
mod test_cometbft;
#[cfg(test)]
pub use mocks::mock_cometbft::MockCometClient;

pub use config::{CometClientConfig, CometClientConfigImpl, CometClientConfigTrait};
#[cfg(test)]
mod mocks {
pub mod mock_cometbft;
}
#[cfg(test)]
mod tests {
mod cometbft;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use starknet_ibc_clients::cometbft::CometClientComponent::{
CometClientHandler, CometClientQuery, ClientReaderImpl
};
use starknet_ibc_clients::cometbft::CometClientComponent;
use starknet_ibc_clients::tests::{MockCometClient, CometClientConfigTrait};
use starknet_ibc_core::client::StatusTrait;
use starknet_ibc_core::tests::HEIGHT;
use starknet_ibc_testkit::configs::CometClientConfigTrait;
use starknet_ibc_testkit::dummies::HEIGHT;
use starknet_ibc_testkit::mocks::MockCometClient;

type ComponentState = CometClientComponent::ComponentState<MockCometClient::ContractState>;

Expand Down
4 changes: 4 additions & 0 deletions cairo-contracts/packages/contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ starknet_ibc_core = { workspace = true }
starknet_ibc_clients = { workspace = true }
starknet_ibc_utils = { workspace = true }

[dev-dependencies]
snforge_std = { workspace = true }
starknet_ibc_testkit = { workspace = true }

[[target.starknet-contract]]
allowed-libfuncs-list.name = "experimental"
sierra = true
Expand Down
24 changes: 4 additions & 20 deletions cairo-contracts/packages/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,9 @@ pub mod clients {
mod cometbft;
pub use cometbft::CometClient;
}
#[cfg(test)]
mod tests {
mod setup;
#[cfg(test)]
mod test_channel;
#[cfg(test)]
mod test_client;
#[cfg(test)]
mod test_transfer;

pub use handles::app::{AppContract, AppHandleImpl, AppHandle};
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;
pub mod client;
pub mod core;
pub mod erc20;
}
mod channel;
mod client;
mod transfer;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use snforge_std::spy_events;
use starknet_ibc_apps::tests::TransferEventSpyExt;
use starknet_ibc_apps::tests::{TransferAppConfigTrait, COSMOS, STARKNET, OWNER};
use starknet_ibc_apps::transfer::ERC20Contract;
use starknet_ibc_apps::transfer::TRANSFER_PORT_ID;
use starknet_ibc_clients::tests::CometClientConfigTrait;
use starknet_ibc_contracts::tests::{SetupImpl, CoreHandle, AppHandle, ERC20Handle};
use starknet_ibc_core::channel::ChannelEndTrait;
use starknet_ibc_core::tests::CLIENT_TYPE;
use starknet_ibc_testkit::configs::{TransferAppConfigTrait, CometClientConfigTrait};
use starknet_ibc_testkit::dummies::{COSMOS, STARKNET, OWNER, CLIENT_TYPE};
use starknet_ibc_testkit::event_spy::TransferEventSpyExt;
use starknet_ibc_testkit::handles::{CoreHandle, AppHandle, ERC20Handle};
use starknet_ibc_testkit::setup::SetupImpl;
use starknet_ibc_utils::ComputeKey;

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use openzeppelin_testing::events::EventSpyExt;
use snforge_std::spy_events;
use starknet_ibc_clients::tests::CometClientConfigTrait;
use starknet_ibc_contracts::tests::{CoreHandle, SetupImpl};
use starknet_ibc_core::client::{UpdateResponse, StatusTrait, ClientContractTrait};
use starknet_ibc_core::tests::{ClientEventSpyExt, HEIGHT, CLIENT_TYPE};
use starknet_ibc_testkit::configs::CometClientConfigTrait;
use starknet_ibc_testkit::dummies::{HEIGHT, CLIENT_TYPE};
use starknet_ibc_testkit::event_spy::ClientEventSpyExt;
use starknet_ibc_testkit::handles::CoreHandle;
use starknet_ibc_testkit::setup::SetupImpl;

#[test]
fn test_create_comet_client_ok() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use openzeppelin_testing::events::EventSpyExt;
use snforge_std::{start_cheat_caller_address, spy_events};
use starknet_ibc_apps::tests::TransferEventSpyExt;
use starknet_ibc_apps::tests::{
TransferAppConfigTrait, NAME, SYMBOL, SUPPLY, OWNER, COSMOS, STARKNET
};
use starknet_ibc_apps::transfer::{ERC20Contract, TRANSFER_PORT_ID};
use starknet_ibc_clients::tests::CometClientConfigTrait;
use starknet_ibc_contracts::tests::{SetupImpl, CoreHandle, ERC20Handle, AppHandle};
use starknet_ibc_core::tests::CLIENT_TYPE;
use starknet_ibc_testkit::configs::{TransferAppConfigTrait, CometClientConfigTrait};
use starknet_ibc_testkit::dummies::{NAME, SYMBOL, SUPPLY, OWNER, COSMOS, STARKNET, CLIENT_TYPE};
use starknet_ibc_testkit::event_spy::TransferEventSpyExt;
use starknet_ibc_testkit::handles::{CoreHandle, AppHandle, ERC20Handle};
use starknet_ibc_testkit::setup::SetupImpl;
use starknet_ibc_utils::ComputeKey;

#[test]
Expand Down
12 changes: 8 additions & 4 deletions cairo-contracts/packages/core/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ fmt = { workspace = true }

[dependencies]
# external dependencies
openzeppelin_testing = { workspace = true }
starknet = { workspace = true }
starknet = { workspace = true }

# ibc dependencies
starknet_ibc_utils = { workspace = true }

[dev-dependencies]
cairo_test = { workspace = true }
snforge_std = { workspace = true }
# external dependencies
openzeppelin_testing = { workspace = true }
cairo_test = { workspace = true }
snforge_std = { workspace = true }

# ibc dependencies
starknet_ibc_testkit = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use starknet_ibc_core::channel::{ChannelEnd, ChannelState, ChannelOrdering, Counterparty};
use starknet_ibc_core::host::{ClientId, PortId, ChannelId, SequencePartialOrd, SequenceZero};

#[starknet::component]
pub mod ChannelHandlerComponent {
use ChannelEventEmitterComponent::ChannelEventEmitterTrait;
Expand All @@ -23,8 +26,8 @@ pub mod ChannelHandlerComponent {
use starknet_ibc_core::router::{
RouterHandlerComponent, ApplicationContractTrait, ApplicationContract
};
use starknet_ibc_core::tests::{PORT_ID, CHANNEL_ID, CHANNEL_END};
use starknet_ibc_utils::ValidateBasic;
use super::{PORT_ID, CHANNEL_ID, CHANNEL_END};

#[storage]
pub struct Storage {
Expand Down Expand Up @@ -580,3 +583,27 @@ pub mod ChannelHandlerComponent {
}
}
}

// ----------------- Temporary until handshakes are implemented ---------------
pub(crate) fn CLIENT_ID() -> ClientId {
ClientId { client_type: '07-cometbft', sequence: 0 }
}

pub(crate) fn PORT_ID() -> PortId {
PortId { port_id: "transfer" }
}

pub(crate) fn CHANNEL_ID(sequence: u64) -> ChannelId {
ChannelId { channel_id: format!("channel-{sequence}") }
}

pub(crate) fn CHANNEL_END(counterparty_channel_sequence: u64) -> ChannelEnd {
ChannelEnd {
state: ChannelState::Open,
ordering: ChannelOrdering::Unordered,
remote: Counterparty {
port_id: PORT_ID(), channel_id: CHANNEL_ID(counterparty_channel_sequence),
},
client_id: CLIENT_ID(),
}
}
25 changes: 4 additions & 21 deletions cairo-contracts/packages/core/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
pub mod tests {
mod dummy;
mod extend_spy;
#[cfg(test)]
mod test_channel;
#[cfg(test)]
mod test_client;
#[cfg(test)]
pub use mocks::mock_channel::MockChannelHandler;
#[cfg(test)]
pub use mocks::mock_client::MockClientHandler;

pub use dummy::{
HEIGHT, CLIENT, CLIENT_TYPE, CLIENT_ID, PORT_ID, CHANNEL_ID, SEQUENCE, CHANNEL_END
};
pub use extend_spy::ClientEventSpyExt;
#[cfg(test)]
pub mod mocks {
pub mod mock_channel;
pub mod mock_client;
}
#[cfg(test)]
mod tests {
mod channel;
mod client;
}
pub mod router {
mod app_call;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use starknet_ibc_core::channel::ChannelHandlerComponent::{
ChannelInitializerImpl, ChannelWriterTrait
};
use starknet_ibc_core::channel::ChannelHandlerComponent;
use starknet_ibc_core::tests::{CHANNEL_END, CHANNEL_ID, PORT_ID, SEQUENCE, MockChannelHandler};
use starknet_ibc_testkit::dummies::{CHANNEL_END, CHANNEL_ID, PORT_ID, SEQUENCE};
use starknet_ibc_testkit::mocks::MockChannelHandler;

type ComponentState = ChannelHandlerComponent::ComponentState<MockChannelHandler::ContractState>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use starknet_ibc_core::client::ClientHandlerComponent::{
ClientInternalImpl
};
use starknet_ibc_core::client::{ClientHandlerComponent, CreateResponse};
use starknet_ibc_core::tests::{
CLIENT, CLIENT_TYPE, CLIENT_ID, HEIGHT, ClientEventSpyExt, MockClientHandler
};
use starknet_ibc_testkit::dummies::{CLIENT, CLIENT_TYPE, CLIENT_ID, HEIGHT};
use starknet_ibc_testkit::event_spy::ClientEventSpyExt;
use starknet_ibc_testkit::mocks::MockClientHandler;

type ComponentState = ClientHandlerComponent::ComponentState<MockClientHandler::ContractState>;

Expand Down
Loading

0 comments on commit ff13029

Please sign in to comment.