From 2b8e4802c928bb39b8497c4be5876cd10426c7a7 Mon Sep 17 00:00:00 2001 From: Rivers Yang Date: Mon, 29 Jan 2024 12:02:36 +0800 Subject: [PATCH] Revise a view function. --- Cargo.lock | 2 +- near-ibc/Cargo.toml | 2 +- near-ibc/src/viewer.rs | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac872af..7f88ba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1930,7 +1930,7 @@ dependencies = [ [[package]] name = "near-ibc" -version = "1.0.2" +version = "1.0.3" dependencies = [ "getrandom 0.2.11", "hex", diff --git a/near-ibc/Cargo.toml b/near-ibc/Cargo.toml index c0f70e1..5024574 100644 --- a/near-ibc/Cargo.toml +++ b/near-ibc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "near-ibc" -version = "1.0.2" +version = "1.0.3" edition = "2021" [lib] diff --git a/near-ibc/src/viewer.rs b/near-ibc/src/viewer.rs index b1b6018..17fd656 100644 --- a/near-ibc/src/viewer.rs +++ b/near-ibc/src/viewer.rs @@ -8,7 +8,7 @@ use ibc::{ clients::tendermint::context::CommonContext, core::{ channel::types::{ - channel::{ChannelEnd, IdentifiedChannelEnd}, + channel::{ChannelEnd, IdentifiedChannelEnd, Order}, commitment::{AcknowledgementCommitment, PacketCommitment}, }, client::types::Height, @@ -61,7 +61,7 @@ pub trait Viewer { /// Get the packet receipt associated with the given port, channel, and sequence. fn get_packet_receipt(&self, port_id: PortId, channel_id: ChannelId, seq: Sequence) -> Vec; /// Get the unreceived packet sequences associated with the given port and channel. - fn get_unreceipt_packet( + fn get_unreceived_packets( &self, port_id: PortId, channel_id: ChannelId, @@ -221,22 +221,40 @@ impl Viewer for NearIbcContract { .map_or(vec![], |receipt| borsh::to_vec(&receipt).unwrap()) } // - fn get_unreceipt_packet( + fn get_unreceived_packets( &self, port_id: PortId, channel_id: ChannelId, sequences: Vec, ) -> Vec { let near_ibc_store = self.near_ibc_store.get().unwrap(); - let stored_sequences = near_ibc_store - .packet_receipt_sequence_sets - .get(&(port_id, channel_id)) - .map_or_else(|| vec![], |receipts| receipts.iter().collect()); - sequences - .iter() - .filter(|sequence| !stored_sequences.contains(&sequence)) - .cloned() - .collect() + if let Some(channel_end) = near_ibc_store + .channel_end(&ChannelEndPath::new(&port_id, &channel_id)) + .map_or(None, |ce| Some(ce)) + { + if *channel_end.ordering() == Order::Ordered { + let next_sequence_recv = near_ibc_store + .get_next_sequence_recv(&SeqRecvPath::new(&port_id, &channel_id)) + .map_or(Sequence::default(), |sq| sq); + sequences + .iter() + .filter(|sequence| sequence >= &&next_sequence_recv) + .cloned() + .collect() + } else { + let stored_sequences = near_ibc_store + .packet_receipt_sequence_sets + .get(&(port_id, channel_id)) + .map_or_else(|| vec![], |receipts| receipts.iter().collect()); + sequences + .iter() + .filter(|sequence| !stored_sequences.contains(&sequence)) + .cloned() + .collect() + } + } else { + panic!("Channel not found"); + } } // fn get_clients(&self) -> Vec<(ClientId, Vec)> {