-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add some channel handler component tests
- Loading branch information
1 parent
20ae040
commit a4f244a
Showing
9 changed files
with
172 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
pub mod ChannelErrors { | ||
pub const ACK_ALREADY_EXISTS: felt252 = 'ICS04: ack already exists'; | ||
pub const EMPTY_COMMITMENT_PROOF: felt252 = 'ICS04: empty commitment proof'; | ||
pub const INACTIVE_CLIENT: felt252 = 'ICS04: inactive client'; | ||
pub const INVALID_CHANNEL_STATE: felt252 = 'ICS04: invalid channel state'; | ||
pub const INVALID_COUNTERPARTY: felt252 = 'ICS04: invalid counterparty'; | ||
pub const TIMED_OUT_PACKET: felt252 = 'ICS04: packet timed out'; | ||
pub const INACTIVE_CLIENT: felt252 = 'ICS04: inactive client'; | ||
pub const INVALID_PROOF_HEIGHT: felt252 = 'ICS04: invalid proof height'; | ||
pub const MISSING_CHANNEL_END: felt252 = 'ICS04: missing channel end'; | ||
pub const TIMED_OUT_PACKET: felt252 = 'ICS04: packet timed out'; | ||
pub const UNSUPPORTED_PORT_ID: felt252 = 'ICS04: unsupported port id'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod dummy; | ||
pub(crate) mod mocks; | ||
|
||
#[cfg(test)] | ||
mod test_channel_handler; | ||
pub(crate) use dummy::{CLIENT_ID, PORT_ID, CHANNEL_ID, SEQUENCE, CHANNEL_END}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use starknet_ibc_core::channel::{ChannelEnd, ChannelState, ChannelOrdering, Counterparty}; | ||
use starknet_ibc_core::host::{ClientId, PortId, ChannelId, Sequence}; | ||
|
||
pub fn CLIENT_ID() -> ClientId { | ||
ClientId { client_type: '07-cometbft', sequence: 0 } | ||
} | ||
|
||
pub fn PORT_ID() -> PortId { | ||
PortId { port_id: "transfer" } | ||
} | ||
|
||
pub fn CHANNEL_ID() -> ChannelId { | ||
ChannelId { channel_id: "channel-0" } | ||
} | ||
|
||
pub fn SEQUENCE() -> Sequence { | ||
Sequence { sequence: 1 } | ||
} | ||
|
||
pub fn CHANNEL_END() -> ChannelEnd { | ||
ChannelEnd { | ||
state: ChannelState::Open, | ||
ordering: ChannelOrdering::Ordered, | ||
remote: Counterparty { port_id: PORT_ID(), channel_id: CHANNEL_ID(), }, | ||
client_id: CLIENT_ID(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod channel_handler; |
35 changes: 35 additions & 0 deletions
35
cairo-contracts/packages/core/src/tests/mocks/channel_handler.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#[starknet::contract] | ||
pub(crate) mod MockChannelHandler { | ||
use starknet_ibc_core::channel::{ChannelHandlerComponent, ChannelEventEmitterComponent}; | ||
|
||
component!( | ||
path: ChannelEventEmitterComponent, | ||
storage: channel_emitter, | ||
event: ChannelEventEmitterEvent | ||
); | ||
component!(path: ChannelHandlerComponent, storage: channel_handler, event: ChannelHandlerEvent); | ||
|
||
impl ChannelInitializerImpl = ChannelHandlerComponent::ChannelInitializerImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
channel_emitter: ChannelEventEmitterComponent::Storage, | ||
#[substorage(v0)] | ||
channel_handler: ChannelHandlerComponent::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
ChannelEventEmitterEvent: ChannelEventEmitterComponent::Event, | ||
#[flat] | ||
ChannelHandlerEvent: ChannelHandlerComponent::Event | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState) { | ||
self.channel_handler.initializer(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
cairo-contracts/packages/core/src/tests/test_channel_handler.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use ChannelHandlerComponent::ChannelReaderTrait; | ||
use snforge_std::{spy_events, test_address, start_cheat_caller_address}; | ||
use starknet::ContractAddress; | ||
use starknet::contract_address_const; | ||
use starknet_ibc_core::channel::ChannelHandlerComponent::{ | ||
ChannelInitializerImpl, ChannelWriterTrait | ||
}; | ||
use starknet_ibc_core::channel::ChannelHandlerComponent; | ||
use starknet_ibc_core::tests::mocks::channel_handler::MockChannelHandler; | ||
use starknet_ibc_core::tests::{CHANNEL_END, CHANNEL_ID, CLIENT_ID, PORT_ID, SEQUENCE}; | ||
|
||
type ComponentState = ChannelHandlerComponent::ComponentState<MockChannelHandler::ContractState>; | ||
|
||
fn COMPONENT_STATE() -> ComponentState { | ||
ChannelHandlerComponent::component_state_for_testing() | ||
} | ||
|
||
fn setup() -> ComponentState { | ||
let mut state = COMPONENT_STATE(); | ||
state.initializer(); | ||
state | ||
} | ||
|
||
#[test] | ||
fn test_read_empty_packet_receipt() { | ||
let state = setup(); | ||
let receipt_res = state.read_packet_receipt(@PORT_ID(), @CHANNEL_ID(), @SEQUENCE()); | ||
assert!(receipt_res.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_read_empty_packet_ack() { | ||
let state = setup(); | ||
let ack_res = state.read_packet_ack(@PORT_ID(), @CHANNEL_ID(), @SEQUENCE()); | ||
assert!(ack_res.len() == 0); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_read_empty_channel_end() { | ||
let state = setup(); | ||
state.read_channel_end(@PORT_ID(), @CHANNEL_ID()); | ||
} | ||
|
||
#[test] | ||
fn test_channel_end_storage() { | ||
let mut state = setup(); | ||
state.write_channel_end(@PORT_ID(), @CHANNEL_ID(), CHANNEL_END()); | ||
let chan_end_res = state.read_channel_end(@PORT_ID(), @CHANNEL_ID()); | ||
assert_eq!(chan_end_res, CHANNEL_END()); | ||
} |