Skip to content

Commit

Permalink
feat: implement query methods for unreceived packets and acks (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani authored Jan 23, 2025
1 parent 4c63248 commit d505358
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
39 changes: 36 additions & 3 deletions cairo-contracts/packages/core/src/channel/components/handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ pub mod ChannelHandlerComponent {
use ConnectionHandlerComponent::CoreConnectionQuery;
use RouterHandlerComponent::RouterInternalTrait;
use core::num::traits::Zero;
use starknet::storage::Map;
use starknet::storage::{
StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};
use starknet::{get_block_timestamp, get_block_number};
Expand Down Expand Up @@ -164,7 +163,6 @@ pub mod ChannelHandlerComponent {
self.read_packet_ack(@port_id, @channel_id, @sequence)
}


fn is_packet_received(
self: @ComponentState<TContractState>,
port_id: PortId,
Expand All @@ -174,6 +172,41 @@ pub mod ChannelHandlerComponent {
self.packet_ack_exists(@port_id, @channel_id, @sequence)
}

fn unreceived_packet_sequences(
self: @ComponentState<TContractState>,
port_id: PortId,
channel_id: ChannelId,
sequences: Array<Sequence>
) -> Array<Sequence> {
assert(sequences.len() > 0, ChannelErrors::EMPTY_SEQUENCE_LIST);
let mut unreceived_sequences = ArrayTrait::new();
for seq in sequences {
if self.read_packet_receipt(@port_id, @channel_id, @seq).is_none() {
unreceived_sequences.append(seq);
}
};
unreceived_sequences
}

fn unreceived_ack_sequences(
self: @ComponentState<TContractState>,
port_id: PortId,
channel_id: ChannelId,
sequences: Array<Sequence>
) -> Array<Sequence> {
assert(sequences.len() > 0, ChannelErrors::EMPTY_SEQUENCE_LIST);
let mut unreceived_sequences = ArrayTrait::new();
for seq in sequences {
if self
.packet_commitments
.read(commitment_key(@port_id, @channel_id, @seq))
.is_non_zero() {
unreceived_sequences.append(seq)
};
};
unreceived_sequences
}

fn next_sequence_send(
self: @ComponentState<TContractState>, port_id: PortId, channel_id: ChannelId
) -> Sequence {
Expand Down
1 change: 1 addition & 0 deletions cairo-contracts/packages/core/src/channel/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod ChannelErrors {
pub const EMPTY_ACK: felt252 = 'ICS04: empty acknowledgement';
pub const EMPTY_UNRECEIVED_PROOF: felt252 = 'ICS04: empty unreceived proof';
pub const EMPTY_ACK_PROOF: felt252 = 'ICS04: empty ack proof';
pub const EMPTY_SEQUENCE_LIST: felt252 = 'ICS04: empty sequence list';
pub const ZERO_PROOF_HEIGHT: felt252 = 'ICS04: zero proof height';
pub const UNSUPPORTED_ORDERING: felt252 = 'ICS04: unsupported ordering';
pub const INVALID_CHANNEL_STATE: felt252 = 'ICS04: invalid channel state';
Expand Down
6 changes: 6 additions & 0 deletions cairo-contracts/packages/core/src/channel/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ pub trait IChannelQuery<TContractState> {
fn is_packet_received(
self: @TContractState, port_id: PortId, channel_id: ChannelId, sequence: Sequence
) -> bool;
fn unreceived_packet_sequences(
self: @TContractState, port_id: PortId, channel_id: ChannelId, sequences: Array<Sequence>
) -> Array<Sequence>;
fn unreceived_ack_sequences(
self: @TContractState, port_id: PortId, channel_id: ChannelId, sequences: Array<Sequence>
) -> Array<Sequence>;
fn next_sequence_send(
self: @TContractState, port_id: PortId, channel_id: ChannelId
) -> Sequence;
Expand Down

0 comments on commit d505358

Please sign in to comment.