From e58a5dccce77bd45a58166a310e87d94a4b3dc32 Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Mon, 23 Sep 2024 18:19:30 -0700 Subject: [PATCH] feat: refactor core interfaces to improve query exposures (#88) --- .../src/transfer/components/transfer.cairo | 130 ++++++++++++------ .../clients/src/cometbft/component.cairo | 96 +++++++++---- .../src/tests/mocks/mock_cometbft.cairo | 4 +- .../clients/src/tests/test_cometbft.cairo | 2 +- .../contracts/src/clients/cometbft.cairo | 4 +- .../packages/contracts/src/core.cairo | 4 + .../contracts/src/tests/handles/core.cairo | 35 +++-- .../contracts/src/tests/test_channel.cairo | 17 ++- .../core/src/channel/components/handler.cairo | 75 +++++++++- .../packages/core/src/channel/interface.cairo | 17 ++- .../core/src/client/client_call.cairo | 8 +- .../core/src/client/components/handler.cairo | 24 ++++ .../packages/core/src/client/interface.cairo | 24 ++-- cairo-contracts/packages/core/src/lib.cairo | 15 +- .../packages/core/src/router/component.cairo | 16 +++ 15 files changed, 361 insertions(+), 110 deletions(-) diff --git a/cairo-contracts/packages/apps/src/transfer/components/transfer.cairo b/cairo-contracts/packages/apps/src/transfer/components/transfer.cairo index f36b047a..77b95cd7 100644 --- a/cairo-contracts/packages/apps/src/transfer/components/transfer.cairo +++ b/cairo-contracts/packages/apps/src/transfer/components/transfer.cairo @@ -73,6 +73,10 @@ pub mod TokenTransferComponent { pub initial_supply: u256, } + // ----------------------------------------------------------- + // Transfer Initializer + // ----------------------------------------------------------- + #[generate_trait] pub impl TransferInitializerImpl< TContractState, +HasComponent, +Drop, @@ -86,6 +90,10 @@ pub mod TokenTransferComponent { } } + // ----------------------------------------------------------- + // ISendTransfer + // ----------------------------------------------------------- + #[embeddable_as(SendTransfer)] impl SendTransferImpl< TContractState, @@ -98,6 +106,64 @@ pub mod TokenTransferComponent { } } + // ----------------------------------------------------------- + // IAppCallback + // ----------------------------------------------------------- + + #[embeddable_as(TransferAppCallback)] + impl TransferAppCallbackImpl< + TContractState, + +HasComponent, + +ITransferrable, + +Drop, + impl Ownable: OwnableComponent::HasComponent, + > of IAppCallback> { + fn on_recv_packet( + ref self: ComponentState, packet: Packet + ) -> Acknowledgement { + self.assert_owner(); + + self.recv_execute(packet); + + Acknowledgement { ack: '0' } + } + + fn on_acknowledgement_packet( + ref self: ComponentState, packet: Packet, ack: Acknowledgement + ) { + self.assert_owner(); + } + + fn on_timeout_packet(ref self: ComponentState, packet: Packet) { + self.assert_owner(); + } + } + + // ----------------------------------------------------------- + // ITokenAddress + // ----------------------------------------------------------- + + #[embeddable_as(IBCTokenAddress)] + impl ITokenAddressImpl< + TContractState, +HasComponent, +Drop + > of ITokenAddress> { + fn ibc_token_address( + self: @ComponentState, token_key: felt252 + ) -> Option { + let token_address = self.read_ibc_token_address(token_key); + + if token_address.is_non_zero() { + Option::Some(token_address) + } else { + Option::None + } + } + } + + // ----------------------------------------------------------- + // Transfer Handlers + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl SendTransferInternalImpl< TContractState, @@ -168,35 +234,6 @@ pub mod TokenTransferComponent { } } - #[embeddable_as(TransferAppCallback)] - impl TransferAppCallbackImpl< - TContractState, - +HasComponent, - +ITransferrable, - +Drop, - impl Ownable: OwnableComponent::HasComponent, - > of IAppCallback> { - fn on_recv_packet( - ref self: ComponentState, packet: Packet - ) -> Acknowledgement { - self.assert_owner(); - - self.recv_execute(packet); - - Acknowledgement { ack: '0' } - } - - fn on_acknowledgement_packet( - ref self: ComponentState, packet: Packet, ack: Acknowledgement - ) { - self.assert_owner(); - } - - fn on_timeout_packet(ref self: ComponentState, packet: Packet) { - self.assert_owner(); - } - } - #[generate_trait] pub(crate) impl RecvPacketInternalImpl< TContractState, @@ -281,22 +318,9 @@ pub mod TokenTransferComponent { } } - #[embeddable_as(IBCTokenAddress)] - impl ITokenAddressImpl< - TContractState, +HasComponent, +Drop - > of ITokenAddress> { - fn ibc_token_address( - self: @ComponentState, token_key: felt252 - ) -> Option { - let token_address = self.read_ibc_token_address(token_key); - - if token_address.is_non_zero() { - Option::Some(token_address) - } else { - Option::None - } - } - } + // ----------------------------------------------------------- + // Transfer Validation/Execution + // ----------------------------------------------------------- #[generate_trait] pub impl TransferValidationImpl< @@ -415,6 +439,10 @@ pub mod TokenTransferComponent { } } + // ----------------------------------------------------------- + // Transfer Owner Assertion + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl OwnerAssertionImpl< TContractState, @@ -428,6 +456,10 @@ pub mod TokenTransferComponent { } } + // ----------------------------------------------------------- + // Transfer Internal + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl TransferInternalImpl< TContractState, +HasComponent, +Drop @@ -500,6 +532,10 @@ pub mod TokenTransferComponent { } } + // ----------------------------------------------------------- + // Transfer Reader/Writer + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl TransferReaderImpl< TContractState, +HasComponent, +Drop @@ -556,6 +592,10 @@ pub mod TokenTransferComponent { } } + // ----------------------------------------------------------- + // Transfer Event Emitter + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl TransferEventImpl< TContractState, +HasComponent, +Drop diff --git a/cairo-contracts/packages/clients/src/cometbft/component.cairo b/cairo-contracts/packages/clients/src/cometbft/component.cairo index 6d3665e7..ad7113ca 100644 --- a/cairo-contracts/packages/clients/src/cometbft/component.cairo +++ b/cairo-contracts/packages/clients/src/cometbft/component.cairo @@ -9,7 +9,7 @@ pub mod CometClientComponent { use starknet_ibc_core::client::{ MsgCreateClient, MsgUpdateClient, MsgRecoverClient, MsgUpgradeClient, Height, Timestamp, Status, StatusTrait, CreateResponse, CreateResponseImpl, UpdateResponse, IClientHandler, - IClientState, IClientStateValidation, IClientStateExecution + IClientQuery, IClientStateValidation, IClientStateExecution, }; use starknet_ibc_core::host::ClientIdImpl; use starknet_ibc_utils::ValidateBasic; @@ -27,6 +27,10 @@ pub mod CometClientComponent { #[derive(Debug, Drop, starknet::Event)] pub enum Event {} + // ----------------------------------------------------------- + // IClientHandler + // ----------------------------------------------------------- + #[embeddable_as(CometClientHandler)] impl ClientHandlerImpl< TContractState, +HasComponent, +Drop @@ -50,6 +54,60 @@ pub mod CometClientComponent { fn upgrade_client(ref self: ComponentState, msg: MsgUpgradeClient) {} } + // ----------------------------------------------------------- + // IClientQuery + // ----------------------------------------------------------- + + #[embeddable_as(CometClientQuery)] + impl ClientQueryImpl< + TContractState, +HasComponent, +Drop + > of IClientQuery> { + fn client_type(self: @ComponentState) -> felt252 { + '07-cometbft' + } + + fn latest_height(self: @ComponentState, client_sequence: u64) -> Height { + let comet_client_state: CometClientState = self.read_client_state(client_sequence); + + comet_client_state.latest_height + } + + fn status(self: @ComponentState, client_sequence: u64) -> Status { + let comet_client_state: CometClientState = self.read_client_state(client_sequence); + + let latest_consensus_state = self + .read_consensus_state(client_sequence, comet_client_state.latest_height.clone()); + + assert(!latest_consensus_state.is_zero(), CometErrors::ZERO_CONSENSUS_STATE); + + self._status(comet_client_state, latest_consensus_state, client_sequence) + } + + fn client_state( + self: @ComponentState, client_sequence: u64 + ) -> Array { + let mut client_state: Array = ArrayTrait::new(); + + self.read_client_state(client_sequence).serialize(ref client_state); + + client_state + } + + fn consensus_state( + self: @ComponentState, client_sequence: u64, height: Height + ) -> Array { + let mut consensus_state: Array = ArrayTrait::new(); + + self.read_consensus_state(client_sequence, height).serialize(ref consensus_state); + + consensus_state + } + } + + // ----------------------------------------------------------- + // Client handler implementations + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl CreateClientImpl< TContractState, +HasComponent, +Drop @@ -141,31 +199,9 @@ pub mod CometClientComponent { fn upgrade_execute(ref self: ComponentState, msg: MsgUpgradeClient,) {} } - #[embeddable_as(CometCommonClientState)] - impl ClientStateImpl< - TContractState, +HasComponent, +Drop - > of IClientState> { - fn client_type(self: @ComponentState) -> felt252 { - '07-cometbft' - } - - fn latest_height(self: @ComponentState, client_sequence: u64) -> Height { - let comet_client_state: CometClientState = self.read_client_state(client_sequence); - - comet_client_state.latest_height - } - - fn status(self: @ComponentState, client_sequence: u64) -> Status { - let comet_client_state: CometClientState = self.read_client_state(client_sequence); - - let latest_consensus_state = self - .read_consensus_state(client_sequence, comet_client_state.latest_height.clone()); - - assert(!latest_consensus_state.is_zero(), CometErrors::ZERO_CONSENSUS_STATE); - - self._status(comet_client_state, latest_consensus_state, client_sequence) - } - } + // ----------------------------------------------------------- + // Client Validation/Execution + // ----------------------------------------------------------- #[embeddable_as(CometClientValidation)] impl ClientValidationImpl< @@ -293,6 +329,10 @@ pub mod CometClientComponent { ) {} } + // ----------------------------------------------------------- + // Client Internal + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl ClientInternalImpl< TContractState, +HasComponent, +Drop @@ -359,6 +399,10 @@ pub mod CometClientComponent { } } + // ----------------------------------------------------------- + // Client Reader/Writer + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl ClientReaderImpl< TContractState, +HasComponent, +Drop diff --git a/cairo-contracts/packages/clients/src/tests/mocks/mock_cometbft.cairo b/cairo-contracts/packages/clients/src/tests/mocks/mock_cometbft.cairo index 5fa6fbf1..595677e2 100644 --- a/cairo-contracts/packages/clients/src/tests/mocks/mock_cometbft.cairo +++ b/cairo-contracts/packages/clients/src/tests/mocks/mock_cometbft.cairo @@ -8,8 +8,8 @@ pub(crate) mod MockCometClient { impl CometClientHandlerImpl = CometClientComponent::CometClientHandler; #[abi(embed_v0)] - impl CometCommonClientStateImpl = - CometClientComponent::CometCommonClientState; + impl CometClientQueryImpl = + CometClientComponent::CometClientQuery; #[abi(embed_v0)] impl CometClientValidationImpl = diff --git a/cairo-contracts/packages/clients/src/tests/test_cometbft.cairo b/cairo-contracts/packages/clients/src/tests/test_cometbft.cairo index 884ff0e0..40c7fdd0 100644 --- a/cairo-contracts/packages/clients/src/tests/test_cometbft.cairo +++ b/cairo-contracts/packages/clients/src/tests/test_cometbft.cairo @@ -1,6 +1,6 @@ use snforge_std::start_cheat_block_timestamp_global; use starknet_ibc_clients::cometbft::CometClientComponent::{ - CometClientHandler, CometCommonClientState, ClientReaderImpl + CometClientHandler, CometClientQuery, ClientReaderImpl }; use starknet_ibc_clients::cometbft::CometClientComponent; use starknet_ibc_clients::tests::{MockCometClient, CometClientConfigTrait}; diff --git a/cairo-contracts/packages/contracts/src/clients/cometbft.cairo b/cairo-contracts/packages/contracts/src/clients/cometbft.cairo index b404c74b..86fb248a 100644 --- a/cairo-contracts/packages/contracts/src/clients/cometbft.cairo +++ b/cairo-contracts/packages/contracts/src/clients/cometbft.cairo @@ -14,8 +14,8 @@ pub mod CometClient { impl CometClientHandlerImpl = CometClientComponent::CometClientHandler; #[abi(embed_v0)] - impl CometCommonClientStateImpl = - CometClientComponent::CometCommonClientState; + impl CometClientQueryImpl = + CometClientComponent::CometClientQuery; // NOTE: The client state validation interface is exposed for public use. // However, only the handler contract can invoke the execution methods. diff --git a/cairo-contracts/packages/contracts/src/core.cairo b/cairo-contracts/packages/contracts/src/core.cairo index a04a2cec..38c88412 100644 --- a/cairo-contracts/packages/contracts/src/core.cairo +++ b/cairo-contracts/packages/contracts/src/core.cairo @@ -9,6 +9,7 @@ pub mod IBCCore { // ----------------------------------------------------------- // Setup Client Components // ----------------------------------------------------------- + component!( path: ClientEventEmitterComponent, storage: client_emitter, event: ClientEventEmitterEvent ); @@ -36,6 +37,9 @@ pub mod IBCCore { #[abi(embed_v0)] impl CoreChannelHanderImpl = ChannelHandlerComponent::CoreChannelHandler; + #[abi(embed_v0)] + impl CoreChannelQueryImpl = + ChannelHandlerComponent::CoreChannelQuery; impl ChannelInitializerImpl = ChannelHandlerComponent::ChannelInitializerImpl; // ----------------------------------------------------------- diff --git a/cairo-contracts/packages/contracts/src/tests/handles/core.cairo b/cairo-contracts/packages/contracts/src/tests/handles/core.cairo index ec1899a9..761d4fb2 100644 --- a/cairo-contracts/packages/contracts/src/tests/handles/core.cairo +++ b/cairo-contracts/packages/contracts/src/tests/handles/core.cairo @@ -1,12 +1,15 @@ use openzeppelin_testing::declare_and_deploy; use starknet::ContractAddress; use starknet_ibc_core::channel::{ - IChannelHandlerDispatcher, IChannelHandlerDispatcherTrait, MsgRecvPacket + IChannelHandlerDispatcher, IChannelHandlerDispatcherTrait, MsgRecvPacket, + IChannelQueryDispatcher, ChannelEnd, IChannelQueryDispatcherTrait }; use starknet_ibc_core::client::{ IClientHandlerDispatcher, IClientHandlerDispatcherTrait, IRegisterClientDispatcher, - IRegisterClientDispatcherTrait, MsgCreateClient, MsgUpdateClient, CreateResponse, UpdateResponse + IRegisterClientDispatcherTrait, MsgCreateClient, MsgUpdateClient, CreateResponse, + UpdateResponse, }; +use starknet_ibc_core::host::{ChannelId, PortId, Sequence}; use starknet_ibc_core::router::{IRouterDispatcher, IRouterDispatcherTrait}; #[derive(Copy, Drop, Serde)] @@ -24,32 +27,36 @@ pub impl CoreHandleImpl of CoreHandle { CoreContract { address } } - fn client_dispatcher(self: @CoreContract) -> IClientHandlerDispatcher { + fn client_handler_dispatcher(self: @CoreContract) -> IClientHandlerDispatcher { IClientHandlerDispatcher { contract_address: *self.address } } - fn channel_dispatcher(self: @CoreContract) -> IChannelHandlerDispatcher { + fn channel_handler_dispatcher(self: @CoreContract) -> IChannelHandlerDispatcher { IChannelHandlerDispatcher { contract_address: *self.address } } + fn channel_query_dispatcher(self: @CoreContract) -> IChannelQueryDispatcher { + IChannelQueryDispatcher { contract_address: *self.address } + } + fn router_dispatcher(self: @CoreContract) -> IRouterDispatcher { IRouterDispatcher { contract_address: *self.address } } - fn register_dispatcher(self: @CoreContract) -> IRegisterClientDispatcher { + fn register_client_dispatcher(self: @CoreContract) -> IRegisterClientDispatcher { IRegisterClientDispatcher { contract_address: *self.address } } fn create_client(self: @CoreContract, msg: MsgCreateClient) -> CreateResponse { - self.client_dispatcher().create_client(msg) + self.client_handler_dispatcher().create_client(msg) } fn update_client(self: @CoreContract, msg: MsgUpdateClient) -> UpdateResponse { - self.client_dispatcher().update_client(msg) + self.client_handler_dispatcher().update_client(msg) } fn register_client(self: @CoreContract, client_type: felt252, client_address: ContractAddress) { - self.register_dispatcher().register_client(client_type, client_address) + self.register_client_dispatcher().register_client(client_type, client_address) } fn register_app(self: @CoreContract, port_id: ByteArray, app_address: ContractAddress) { @@ -57,6 +64,16 @@ pub impl CoreHandleImpl of CoreHandle { } fn recv_packet(self: @CoreContract, msg: MsgRecvPacket) { - self.channel_dispatcher().recv_packet(msg) + self.channel_handler_dispatcher().recv_packet(msg) + } + + fn channel_end(self: @CoreContract, port_id: PortId, channel_id: ChannelId) -> ChannelEnd { + self.channel_query_dispatcher().channel_end(port_id, channel_id) + } + + fn packet_receipt( + self: @CoreContract, port_id: PortId, channel_id: ChannelId, sequence: Sequence + ) -> bool { + self.channel_query_dispatcher().packet_receipt(port_id, channel_id, sequence) } } diff --git a/cairo-contracts/packages/contracts/src/tests/test_channel.cairo b/cairo-contracts/packages/contracts/src/tests/test_channel.cairo index ca8293e7..5112fcf7 100644 --- a/cairo-contracts/packages/contracts/src/tests/test_channel.cairo +++ b/cairo-contracts/packages/contracts/src/tests/test_channel.cairo @@ -5,6 +5,7 @@ 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_utils::ComputeKey; @@ -49,7 +50,7 @@ fn test_recv_packet_ok() { let msg = transfer_cfg .dummy_msg_recv_packet(transfer_cfg.hosted_denom.clone(), COSMOS(), STARKNET()); - core.recv_packet(msg); + core.recv_packet(msg.clone()); // ----------------------------------------------------------- // Check Results @@ -73,5 +74,19 @@ fn test_recv_packet_ok() { // Check the total supply of the ERC20 contract. erc20.assert_total_supply(transfer_cfg.amount); + + // Retrieve the channel end on Starknet. + let chan_end_on_b = core + .channel_end(msg.packet.port_id_on_b.clone(), msg.packet.chan_id_on_b.clone()); + + // Assert the channel end is open. + assert!(chan_end_on_b.is_open()); + + // Retrieve the packet receipt. + let receipt = core + .packet_receipt(msg.packet.port_id_on_b, msg.packet.chan_id_on_b, msg.packet.seq_on_a,); + + // Assert the packet receipt is true. + assert!(receipt); } diff --git a/cairo-contracts/packages/core/src/channel/components/handler.cairo b/cairo-contracts/packages/core/src/channel/components/handler.cairo index c24b6136..351bbb4d 100644 --- a/cairo-contracts/packages/core/src/channel/components/handler.cairo +++ b/cairo-contracts/packages/core/src/channel/components/handler.cairo @@ -7,9 +7,9 @@ pub mod ChannelHandlerComponent { use starknet::storage::Map; use starknet::{get_block_timestamp, get_block_number}; use starknet_ibc_core::channel::{ - ChannelEventEmitterComponent, IChannelHandler, MsgRecvPacket, ChannelEnd, ChannelEndTrait, - ChannelErrors, PacketTrait, ChannelOrdering, Receipt, AcknowledgementTrait, Packet, - Acknowledgement + ChannelEventEmitterComponent, IChannelHandler, IChannelQuery, MsgRecvPacket, ChannelEnd, + ChannelEndTrait, ChannelErrors, PacketTrait, ChannelOrdering, Receipt, AcknowledgementTrait, + Packet, Acknowledgement }; use starknet_ibc_core::client::{ClientHandlerComponent, ClientContract, ClientContractTrait}; use starknet_ibc_core::host::{ @@ -34,6 +34,10 @@ pub mod ChannelHandlerComponent { #[derive(Debug, Drop, starknet::Event)] pub enum Event {} + // ----------------------------------------------------------- + // Channel Initializer + // ----------------------------------------------------------- + #[generate_trait] pub impl ChannelInitializerImpl< TContractState, +HasComponent, +Drop @@ -47,6 +51,10 @@ pub mod ChannelHandlerComponent { } } + // ----------------------------------------------------------- + // IChannelHandler + // ----------------------------------------------------------- + #[embeddable_as(CoreChannelHandler)] impl CoreChannelHandlerImpl< TContractState, @@ -66,6 +74,55 @@ pub mod ChannelHandlerComponent { } } + // ----------------------------------------------------------- + // IChannelQuery + // ----------------------------------------------------------- + + #[embeddable_as(CoreChannelQuery)] + impl CoreChannelQueryImpl< + TContractState, +HasComponent, +Drop, + > of IChannelQuery> { + fn channel_end( + self: @ComponentState, port_id: PortId, channel_id: ChannelId + ) -> ChannelEnd { + let maybe_chan_end = self.read_channel_end(@port_id, @channel_id); + + assert(maybe_chan_end.is_some(), ChannelErrors::MISSING_CHANNEL_END); + + maybe_chan_end.unwrap() + } + + fn packet_receipt( + self: @ComponentState, + port_id: PortId, + channel_id: ChannelId, + sequence: Sequence + ) -> bool { + let receipt = self.read_packet_receipt(@port_id, @channel_id, @sequence); + + receipt.is_some() + } + + fn packet_acknowledgement( + self: @ComponentState, + port_id: PortId, + channel_id: ChannelId, + sequence: Sequence + ) -> felt252 { + self.read_packet_ack(@port_id, @channel_id, @sequence) + } + + fn next_sequence_recv( + self: @ComponentState, port_id: PortId, channel_id: ChannelId + ) -> Sequence { + self.read_next_sequence_recv(@port_id, @channel_id) + } + } + + // ----------------------------------------------------------- + // Channel handler implementations + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl RecvPacketImpl< TContractState, @@ -222,6 +279,10 @@ pub mod ChannelHandlerComponent { } } + // ----------------------------------------------------------- + // Channel internal + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl ChannelInternalImpl< TContractState, @@ -257,6 +318,10 @@ pub mod ChannelHandlerComponent { } } + // ----------------------------------------------------------- + // Channel reader/writer + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl ChannelReaderImpl< TContractState, +HasComponent, +Drop @@ -339,6 +404,10 @@ pub mod ChannelHandlerComponent { } } + // ----------------------------------------------------------- + // Channel Event Emitter + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl EventEmitterImpl< TContractState, diff --git a/cairo-contracts/packages/core/src/channel/interface.cairo b/cairo-contracts/packages/core/src/channel/interface.cairo index 19fd7014..9a3a5fe6 100644 --- a/cairo-contracts/packages/core/src/channel/interface.cairo +++ b/cairo-contracts/packages/core/src/channel/interface.cairo @@ -1,4 +1,5 @@ -use starknet_ibc_core::channel::{Packet, MsgRecvPacket, Acknowledgement}; +use starknet_ibc_core::channel::{Packet, MsgRecvPacket, Acknowledgement, ChannelEnd}; +use starknet_ibc_core::host::{PortId, ChannelId, Sequence}; #[starknet::interface] pub trait IChannelHandler { @@ -11,3 +12,17 @@ pub trait IAppCallback { fn on_acknowledgement_packet(ref self: TContractState, packet: Packet, ack: Acknowledgement); fn on_timeout_packet(ref self: TContractState, packet: Packet); } + +#[starknet::interface] +pub trait IChannelQuery { + fn channel_end(self: @TContractState, port_id: PortId, channel_id: ChannelId) -> ChannelEnd; + fn packet_receipt( + self: @TContractState, port_id: PortId, channel_id: ChannelId, sequence: Sequence + ) -> bool; + fn packet_acknowledgement( + self: @TContractState, port_id: PortId, channel_id: ChannelId, sequence: Sequence + ) -> felt252; + fn next_sequence_recv( + self: @TContractState, port_id: PortId, channel_id: ChannelId + ) -> Sequence; +} diff --git a/cairo-contracts/packages/core/src/client/client_call.cairo b/cairo-contracts/packages/core/src/client/client_call.cairo index b529d73a..1e09261f 100644 --- a/cairo-contracts/packages/core/src/client/client_call.cairo +++ b/cairo-contracts/packages/core/src/client/client_call.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; use starknet_ibc_core::client::{ - IClientHandlerDispatcher, IClientStateDispatcher, IClientStateDispatcherTrait, + IClientHandlerDispatcher, IClientQueryDispatcher, IClientQueryDispatcherTrait, IClientHandlerDispatcherTrait, IClientStateValidationDispatcher, IClientStateValidationDispatcherTrait, MsgCreateClient, MsgUpdateClient, MsgRecoverClient, MsgUpgradeClient, CreateResponse, UpdateResponse, Height, HeightPartialOrd, Status, StatusTrait, @@ -27,15 +27,15 @@ impl ClientContractIntoFelt252 of Into { #[generate_trait] pub impl ClientContractImpl of ClientContractTrait { fn client_type(self: @ClientContract) -> felt252 { - IClientStateDispatcher { contract_address: *self.address }.client_type() + IClientQueryDispatcher { contract_address: *self.address }.client_type() } fn latest_height(self: @ClientContract, client_sequence: u64) -> Height { - IClientStateDispatcher { contract_address: *self.address }.latest_height(client_sequence) + IClientQueryDispatcher { contract_address: *self.address }.latest_height(client_sequence) } fn status(self: @ClientContract, client_sequence: u64) -> Status { - IClientStateDispatcher { contract_address: *self.address }.status(client_sequence) + IClientQueryDispatcher { contract_address: *self.address }.status(client_sequence) } fn verify_is_active(self: @ClientContract, client_sequence: u64) { diff --git a/cairo-contracts/packages/core/src/client/components/handler.cairo b/cairo-contracts/packages/core/src/client/components/handler.cairo index d578cdf0..5676ca11 100644 --- a/cairo-contracts/packages/core/src/client/components/handler.cairo +++ b/cairo-contracts/packages/core/src/client/components/handler.cairo @@ -21,6 +21,10 @@ pub mod ClientHandlerComponent { #[derive(Debug, Drop, starknet::Event)] pub enum Event {} + // ----------------------------------------------------------- + // Client Initializer + // ----------------------------------------------------------- + #[generate_trait] pub impl ClientInitializerImpl< TContractState, +HasComponent, +Drop @@ -28,6 +32,10 @@ pub mod ClientHandlerComponent { fn initializer(ref self: ComponentState) {} } + // ----------------------------------------------------------- + // IClientHandler + // ----------------------------------------------------------- + #[embeddable_as(CoreClientHandler)] pub impl CoreClientHandlerImpl< TContractState, @@ -80,6 +88,10 @@ pub mod ClientHandlerComponent { } } + // ----------------------------------------------------------- + // IRegisterClient + // ----------------------------------------------------------- + #[embeddable_as(CoreRegisterClient)] pub impl CoreRegisterClientImpl< TContractState, +HasComponent, +Drop @@ -93,6 +105,10 @@ pub mod ClientHandlerComponent { } } + // ----------------------------------------------------------- + // Client Internal + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl ClientInternalImpl< TContractState, +HasComponent, +Drop @@ -108,6 +124,10 @@ pub mod ClientHandlerComponent { } } + // ----------------------------------------------------------- + // Client Reader/Writer + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl ClientReaderImpl< TContractState, +HasComponent, +Drop @@ -132,6 +152,10 @@ pub mod ClientHandlerComponent { } } + // ----------------------------------------------------------- + // Client Event Emitter + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl EventEmitterImpl< TContractState, diff --git a/cairo-contracts/packages/core/src/client/interface.cairo b/cairo-contracts/packages/core/src/client/interface.cairo index 3a763c46..f603ca11 100644 --- a/cairo-contracts/packages/core/src/client/interface.cairo +++ b/cairo-contracts/packages/core/src/client/interface.cairo @@ -22,15 +22,6 @@ pub trait IRegisterClient { ); } -#[starknet::interface] -pub trait IClientState { - fn client_type(self: @TContractState) -> felt252; - - fn latest_height(self: @TContractState, client_sequence: u64) -> Height; - - fn status(self: @TContractState, client_sequence: u64) -> Status; -} - #[starknet::interface] pub trait IClientStateValidation { fn verify_membership( @@ -96,3 +87,18 @@ pub trait IClientStateExecution { new_consensus_state: Array, ); } + +#[starknet::interface] +pub trait IClientQuery { + fn client_type(self: @TContractState) -> felt252; + + fn latest_height(self: @TContractState, client_sequence: u64) -> Height; + + fn status(self: @TContractState, client_sequence: u64) -> Status; + + fn client_state(self: @TContractState, client_sequence: u64) -> Array; + + fn consensus_state( + self: @TContractState, client_sequence: u64, height: Height + ) -> Array; +} diff --git a/cairo-contracts/packages/core/src/lib.cairo b/cairo-contracts/packages/core/src/lib.cairo index 216a163b..ba9aaa7e 100644 --- a/cairo-contracts/packages/core/src/lib.cairo +++ b/cairo-contracts/packages/core/src/lib.cairo @@ -42,7 +42,8 @@ pub mod channel { pub use errors::ChannelErrors; pub use interface::{ IChannelHandler, IChannelHandlerDispatcher, IChannelHandlerDispatcherTrait, IAppCallback, - IAppCallbackDispatcher, IAppCallbackDispatcherTrait + IAppCallbackDispatcher, IAppCallbackDispatcherTrait, IChannelQuery, IChannelQueryDispatcher, + IChannelQueryDispatcherTrait }; pub use msgs::MsgRecvPacket; pub use types::{ @@ -70,12 +71,12 @@ pub mod client { pub use components::handler::ClientHandlerComponent; pub use errors::ClientErrors; pub use interface::{ - IClientHandler, IClientHandlerDispatcher, IClientState, IClientStateDispatcher, - IClientStateDispatcherTrait, IClientHandlerDispatcherTrait, IClientStateValidation, - IClientStateValidationDispatcher, IClientStateValidationDispatcherTrait, - IClientStateExecution, IClientStateExecutionDispatcher, - IClientStateExecutionDispatcherTrait, IRegisterClient, IRegisterClientDispatcher, - IRegisterClientDispatcherTrait + IClientHandler, IClientHandlerDispatcher, IClientHandlerDispatcherTrait, + IClientStateValidation, IClientStateValidationDispatcher, + IClientStateValidationDispatcherTrait, IClientStateExecution, + IClientStateExecutionDispatcher, IClientStateExecutionDispatcherTrait, IRegisterClient, + IRegisterClientDispatcher, IRegisterClientDispatcherTrait, IClientQuery, + IClientQueryDispatcher, IClientQueryDispatcherTrait }; pub use msgs::{MsgCreateClient, MsgRecoverClient, MsgUpdateClient, MsgUpgradeClient}; pub use types::{ diff --git a/cairo-contracts/packages/core/src/router/component.cairo b/cairo-contracts/packages/core/src/router/component.cairo index bed6641c..d4053ef3 100644 --- a/cairo-contracts/packages/core/src/router/component.cairo +++ b/cairo-contracts/packages/core/src/router/component.cairo @@ -16,6 +16,10 @@ pub mod RouterHandlerComponent { #[derive(Debug, Drop, starknet::Event)] pub enum Event {} + // ----------------------------------------------------------- + // Router Initializer + // ----------------------------------------------------------- + #[generate_trait] pub impl RouterInitializerImpl< TContractState, +HasComponent, +Drop @@ -23,6 +27,10 @@ pub mod RouterHandlerComponent { fn initializer(ref self: ComponentState) {} } + // ----------------------------------------------------------- + // IRouter + // ----------------------------------------------------------- + #[embeddable_as(CoreRouterHandler)] impl CoreRouterHandlerImpl< TContractState, +HasComponent, +Drop @@ -52,6 +60,10 @@ pub mod RouterHandlerComponent { } } + // ----------------------------------------------------------- + // Router Internal + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl RouterInternalImpl< TContractState, +HasComponent, +Drop @@ -65,6 +77,10 @@ pub mod RouterHandlerComponent { } } + // ----------------------------------------------------------- + // Router Reader/Writer + // ----------------------------------------------------------- + #[generate_trait] pub(crate) impl RouterReaderImpl< TContractState, +HasComponent, +Drop