Skip to content

Commit

Permalink
Add list of sequence numbers to clear pending CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ancazamfir committed Jan 5, 2024
1 parent c979f92 commit 19374ec
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
20 changes: 16 additions & 4 deletions crates/relayer-cli/src/commands/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::config::Config;
use ibc_relayer::link::error::LinkError;
use ibc_relayer::link::{Link, LinkParameters};
use ibc_relayer_types::core::ics04_channel::packet::Sequence;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc_relayer_types::events::IbcEvent;

Expand Down Expand Up @@ -52,6 +53,13 @@ pub struct ClearPacketsCmd {
)]
channel_id: ChannelId,

#[clap(
long = "packet-sequences",
help = "Sequences of packets to be cleared",
value_delimiter = ','
)]
packet_sequences: Vec<Sequence>,

#[clap(
long = "key-name",
help = "use the given signing key for the specified chain (default: `key_name` config)"
Expand Down Expand Up @@ -134,18 +142,18 @@ impl Runnable for ClearPacketsCmd {
// Schedule RecvPacket messages for pending packets in both directions.
// This may produce pending acks which will be processed in the next phase.
run_and_collect_events("forward recv and timeout", &mut ev_list, || {
fwd_link.relay_recv_packet_and_timeout_messages()
fwd_link.relay_recv_packet_and_timeout_messages(self.packet_sequences.clone())
});
run_and_collect_events("reverse recv and timeout", &mut ev_list, || {
rev_link.relay_recv_packet_and_timeout_messages()
rev_link.relay_recv_packet_and_timeout_messages(self.packet_sequences.clone())
});

// Schedule AckPacket messages in both directions.
run_and_collect_events("forward ack", &mut ev_list, || {
fwd_link.relay_ack_packet_messages()
fwd_link.relay_ack_packet_messages(self.packet_sequences.clone())
});
run_and_collect_events("reverse ack", &mut ev_list, || {
rev_link.relay_ack_packet_messages()
rev_link.relay_ack_packet_messages(self.packet_sequences.clone())
});

Output::success(ev_list).exit()
Expand Down Expand Up @@ -178,6 +186,7 @@ mod tests {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap(),
packet_sequences: vec![],
key_name: None,
counterparty_key_name: None,
},
Expand All @@ -200,6 +209,7 @@ mod tests {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap(),
packet_sequences: vec![],
key_name: None,
counterparty_key_name: None
},
Expand All @@ -222,6 +232,7 @@ mod tests {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap(),
packet_sequences: vec![],
key_name: Some("key_name".to_owned()),
counterparty_key_name: None,
},
Expand All @@ -246,6 +257,7 @@ mod tests {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap(),
packet_sequences: vec![],
key_name: None,
counterparty_key_name: Some("counterparty_key_name".to_owned()),
},
Expand Down
6 changes: 5 additions & 1 deletion crates/relayer-cli/src/commands/tx/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl Runnable for TxPacketRecvCmd {

let res: Result<Vec<IbcEvent>, Error> = link
.relay_recv_packet_and_timeout_messages_with_packet_data_query_height(
vec![],
packet_data_query_height,
)
.map_err(Error::link);
Expand Down Expand Up @@ -162,7 +163,10 @@ impl Runnable for TxPacketAckCmd {
.map(|height| Height::new(link.a_to_b.src_chain().id().version(), height).unwrap());

let res: Result<Vec<IbcEvent>, Error> = link
.relay_ack_packet_messages_with_packet_data_query_height(packet_data_query_height)
.relay_ack_packet_messages_with_packet_data_query_height(
vec![],
packet_data_query_height,
)
.map_err(Error::link);

match res {
Expand Down
59 changes: 40 additions & 19 deletions crates/relayer/src/link/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> RelayPath<ChainA, ChainB> {
}

impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
pub fn relay_recv_packet_and_timeout_messages(&self) -> Result<Vec<IbcEvent>, LinkError> {
self.relay_recv_packet_and_timeout_messages_with_packet_data_query_height(None)
pub fn relay_recv_packet_and_timeout_messages(
&self,
sequences: Vec<Sequence>,
) -> Result<Vec<IbcEvent>, LinkError> {
self.relay_recv_packet_and_timeout_messages_with_packet_data_query_height(sequences, None)
}
/// Implements the `packet-recv` CLI
pub fn relay_recv_packet_and_timeout_messages_with_packet_data_query_height(
&self,
maybe_sequences: Vec<Sequence>,
packet_data_query_height: Option<Height>,
) -> Result<Vec<IbcEvent>, LinkError> {
let _span = error_span!(
Expand All @@ -88,13 +92,20 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
)
.entered();

// Find the sequence numbers of unreceived packets
let (sequences, src_response_height) = unreceived_packets(
self.a_to_b.dst_chain(),
self.a_to_b.src_chain(),
&self.a_to_b.path_id,
)
.map_err(LinkError::supervisor)?;
let (sequences, src_response_height) = if maybe_sequences.is_empty() {
// Find the sequence numbers of unreceived packets
unreceived_packets(
self.a_to_b.dst_chain(),
self.a_to_b.src_chain(),
&self.a_to_b.path_id,
)
.map_err(LinkError::supervisor)?
} else {
(
maybe_sequences,
self.a_to_b.src_chain().query_latest_height().unwrap(),
)
};

if sequences.is_empty() {
return Ok(vec![]);
Expand All @@ -119,13 +130,17 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
)
}

pub fn relay_ack_packet_messages(&self) -> Result<Vec<IbcEvent>, LinkError> {
self.relay_ack_packet_messages_with_packet_data_query_height(None)
pub fn relay_ack_packet_messages(
&self,
maybe_sequences: Vec<Sequence>,
) -> Result<Vec<IbcEvent>, LinkError> {
self.relay_ack_packet_messages_with_packet_data_query_height(maybe_sequences, None)
}

/// Implements the `packet-ack` CLI
pub fn relay_ack_packet_messages_with_packet_data_query_height(
&self,
maybe_sequences: Vec<Sequence>,
packet_data_query_height: Option<Height>,
) -> Result<Vec<IbcEvent>, LinkError> {
let _span = error_span!(
Expand All @@ -137,14 +152,20 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
)
.entered();

// Find the sequence numbers of unreceived acknowledgements
let Some((sequences, src_response_height)) = unreceived_acknowledgements(
self.a_to_b.dst_chain(),
self.a_to_b.src_chain(),
&self.a_to_b.path_id,
)
.map_err(LinkError::supervisor)?
else {
let Some((sequences, src_response_height)) = (if maybe_sequences.is_empty() {
// Find the sequence numbers of unreceived acknowledgements
unreceived_acknowledgements(
self.a_to_b.dst_chain(),
self.a_to_b.src_chain(),
&self.a_to_b.path_id,
)
.map_err(LinkError::supervisor)?
} else {
Some((
maybe_sequences,
self.a_to_b.src_chain().query_latest_height().unwrap(),
))
}) else {
return Ok(vec![]);
};

Expand Down
7 changes: 4 additions & 3 deletions tools/integration-test/src/tests/ordered_channel_clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ impl BinaryChannelTest for OrderedChannelClearEqualCLITest {
)?;

let events_returned: Vec<IbcEvent> = chain_a_link
.relay_recv_packet_and_timeout_messages_with_packet_data_query_height(Some(
clear_height,
))
.relay_recv_packet_and_timeout_messages_with_packet_data_query_height(
vec![],
Some(clear_height),
)
.unwrap();

info!("recv packets sent, chain events: {:?}", events_returned);
Expand Down
4 changes: 2 additions & 2 deletions tools/integration-test/src/tests/query_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl BinaryChannelTest for QueryPacketPendingTest {
assert!(summary.unreceived_acks.is_empty());

// Receive the packet on the destination chain
link.relay_recv_packet_and_timeout_messages()?;
link.relay_recv_packet_and_timeout_messages(vec![])?;

let summary =
pending_packet_summary(chains.handle_a(), chains.handle_b(), channel_end.value())?;
Expand All @@ -91,7 +91,7 @@ impl BinaryChannelTest for QueryPacketPendingTest {

// Acknowledge the packet on the source chain
let link = link.reverse(false, false)?;
link.relay_ack_packet_messages()?;
link.relay_ack_packet_messages(vec![])?;

let summary =
pending_packet_summary(chains.handle_a(), chains.handle_b(), channel_end.value())?;
Expand Down

0 comments on commit 19374ec

Please sign in to comment.