Skip to content

Commit

Permalink
Implement query packet pending command (#2096)
Browse files Browse the repository at this point in the history
* relayer: typo in packet_acknowledgements

* cli: Remove an unused struct un unreceived_packets

* First crack at `packet pending` cmd

* relayer: Optimize pending_packet_summary

Don't query the set of commitments on the counterparty chain twice.
To achieve this, simplify the low-level functions in chain::counterparty
by accepting previously obtained commitment sets rather than
querying everything inside each function.

* Skip the unreceived acks query on empty ack set

Do the same optimization to unreceived_acknowledgements_sequences
as the one done in unreceived_packets_sequences.

* Swap the directions in pending_packet_summary

* Initial test of pending_packet_summary

Only tests the unreceived packets.

* test framework: impl Copy for Tagged

* test-framework: Add query_identified_channel_end

* testf-framework: conversion from LinkError

* Minimal test of pending_packet_summary

* test-framework: query_identified_connection_end

* query the reverse channel in pending packet test

* Changelog entry for #2096

* Renamed local vars for ChannelConnectionClient

* Rename fields in output of `query packet pending`

* tests: No override of channel order in query_packet

* Renamed PacketSummary members

* Fix clippy warnings

Co-authored-by: Romain Ruetschi <[email protected]>
  • Loading branch information
mzabaluev and romac authored Apr 27, 2022
1 parent d4f4110 commit f7cbe1c
Show file tree
Hide file tree
Showing 16 changed files with 436 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Added `query packet pending` command to list outstanding packet
commitments that are either unreceived or pending acknowledgement
at both ends of a channel.
([#1862](https://github.com/informalsystems/ibc-rs/issues/1862))
7 changes: 3 additions & 4 deletions relayer-cli/src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use alloc::sync::Arc;
use tokio::runtime::Runtime as TokioRuntime;

use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics04_channel::channel::IdentifiedChannelEnd;
use ibc::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc_relayer::chain::counterparty::channel_connection_client;
use ibc_relayer::chain::counterparty::{channel_connection_client, ChannelConnectionClient};
use ibc_relayer::{
chain::{
handle::{BaseChainHandle, ChainHandle},
Expand Down Expand Up @@ -80,7 +79,7 @@ pub fn spawn_chain_counterparty<Chain: ChainHandle>(
chain_id: &ChainId,
port_id: &PortId,
channel_id: &ChannelId,
) -> Result<(ChainHandlePair<Chain>, IdentifiedChannelEnd), Error> {
) -> Result<(ChainHandlePair<Chain>, ChannelConnectionClient), Error> {
let chain = spawn_chain_runtime_generic::<Chain>(config, chain_id)?;
let channel_connection_client =
channel_connection_client(&chain, port_id, channel_id).map_err(Error::supervisor)?;
Expand All @@ -94,6 +93,6 @@ pub fn spawn_chain_counterparty<Chain: ChainHandle>(
src: chain,
dst: counterparty_chain,
},
channel_connection_client.channel,
channel_connection_client,
))
}
4 changes: 4 additions & 0 deletions relayer-cli/src/commands/query/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod ack;
mod acks;
mod commitment;
mod commitments;
mod pending;
mod unreceived_acks;
mod unreceived_packets;

Expand All @@ -27,4 +28,7 @@ pub enum QueryPacketCmds {

/// Query unreceived acknowledgments
UnreceivedAcks(unreceived_acks::QueryUnreceivedAcknowledgementCmd),

/// Output a summary of pending packets in both directions
Pending(pending::QueryPendingPacketsCmd),
}
7 changes: 4 additions & 3 deletions relayer-cli/src/commands/query/packet/acks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ impl QueryPacketAcknowledgementsCmd {

debug!("Options: {:?}", self);

let (chains, channel) = spawn_chain_counterparty::<BaseChainHandle>(
let (chains, chan_conn_cli) = spawn_chain_counterparty::<BaseChainHandle>(
&config,
&self.chain_id,
&self.port_id,
&self.channel_id,
)?;

let (seqs, height) = acknowledgements_on_chain(&chains.src, &chains.dst, &channel)
.map_err(Error::supervisor)?;
let (seqs, height) =
acknowledgements_on_chain(&chains.src, &chains.dst, &chan_conn_cli.channel)
.map_err(Error::supervisor)?;

Ok(PacketSeqs { seqs, height })
}
Expand Down
95 changes: 95 additions & 0 deletions relayer-cli/src/commands/query/packet/pending.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};
use serde::Serialize;

use ibc::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc_relayer::chain::counterparty::{
channel_on_destination, pending_packet_summary, PendingPackets,
};
use ibc_relayer::chain::handle::BaseChainHandle;

use crate::cli_utils::spawn_chain_counterparty;
use crate::conclude::Output;
use crate::error::Error;
use crate::prelude::*;

/// A structure to display pending packet commitment sequence IDs
/// at both ends of a channel.
#[derive(Debug, Serialize)]
struct Summary {
/// The packets sent on the source chain as identified by the command.
src: PendingPackets,
/// The packets sent on the counterparty chain.
dst: PendingPackets,
}

/// This command does the following:
///
/// 1. queries the chain to get its counterparty chain, channel and port identifiers (needed in 2)
/// 2. queries both chains for all packet commitments/ sequences for the given port and channel
/// and its counterparty.
/// 3. queries both chains for the unreceived sequences and acks out of the lists obtained in 2.
#[derive(Clone, Command, Debug, Parser)]
pub struct QueryPendingPacketsCmd {
#[clap(
required = true,
help = "identifier of the chain at one end of the channel"
)]
chain_id: ChainId,

#[clap(
required = true,
help = "port identifier on the chain given by <CHAIN_ID>"
)]
port_id: PortId,

#[clap(
required = true,
help = "channel identifier on the chain given by <CHAIN_ID>"
)]
channel_id: ChannelId,
}

impl QueryPendingPacketsCmd {
fn execute(&self) -> Result<Summary, Error> {
let config = app_config();

let (chains, chan_conn_cli) = spawn_chain_counterparty::<BaseChainHandle>(
&config,
&self.chain_id,
&self.port_id,
&self.channel_id,
)?;

debug!(
chain=%self.chain_id,
"fetched channel from source chain: {:?}",
chan_conn_cli.channel
);

let src_summary = pending_packet_summary(&chains.src, &chains.dst, &chan_conn_cli.channel)
.map_err(Error::supervisor)?;
let counterparty_channel = channel_on_destination(
&chan_conn_cli.channel,
&chan_conn_cli.connection,
&chains.dst,
)
.map_err(Error::supervisor)?
.ok_or_else(|| Error::missing_counterparty_channel_id(chan_conn_cli.channel))?;
let dst_summary = pending_packet_summary(&chains.dst, &chains.src, &counterparty_channel)
.map_err(Error::supervisor)?;
Ok(Summary {
src: src_summary,
dst: dst_summary,
})
}
}

impl Runnable for QueryPendingPacketsCmd {
fn run(&self) {
match self.execute() {
Ok(pending) => Output::success(pending).exit(),
Err(e) => Output::error(format!("{}", e)).exit(),
}
}
}
7 changes: 4 additions & 3 deletions relayer-cli/src/commands/query/packet/unreceived_acks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl QueryUnreceivedAcknowledgementCmd {
let config = app_config();
debug!("Options: {:?}", self);

let (chains, channel) = spawn_chain_counterparty::<BaseChainHandle>(
let (chains, chan_conn_cli) = spawn_chain_counterparty::<BaseChainHandle>(
&config,
&self.chain_id,
&self.port_id,
Expand All @@ -43,10 +43,11 @@ impl QueryUnreceivedAcknowledgementCmd {

debug!(
"fetched from source chain {} the following channel {:?}",
self.chain_id, channel
self.chain_id, chan_conn_cli.channel,
);

unreceived_acknowledgements(&chains.src, &chains.dst, &channel).map_err(Error::supervisor)
unreceived_acknowledgements(&chains.src, &chains.dst, &chan_conn_cli.channel)
.map_err(Error::supervisor)
}
}

Expand Down
15 changes: 4 additions & 11 deletions relayer-cli/src/commands/query/packet/unreceived_packets.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};
use serde::Serialize;

use ibc::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc::Height;
use ibc_relayer::chain::counterparty::unreceived_packets;
use ibc_relayer::chain::handle::BaseChainHandle;

Expand All @@ -12,12 +10,6 @@ use crate::conclude::Output;
use crate::error::Error;
use crate::prelude::*;

#[derive(Serialize, Debug)]
struct PacketSeqs {
height: Height,
seqs: Vec<u64>,
}

/// This command does the following:
/// 1. queries the chain to get its counterparty chain, channel and port identifiers (needed in 2)
/// 2. queries the counterparty chain for all packet commitments/ sequences for a given port and channel
Expand All @@ -42,7 +34,7 @@ impl QueryUnreceivedPacketsCmd {
let config = app_config();
debug!("Options: {:?}", self);

let (chains, channel) = spawn_chain_counterparty::<BaseChainHandle>(
let (chains, chan_conn_cli) = spawn_chain_counterparty::<BaseChainHandle>(
&config,
&self.chain_id,
&self.port_id,
Expand All @@ -51,10 +43,11 @@ impl QueryUnreceivedPacketsCmd {

debug!(
"fetched from source chain {} the following channel {:?}",
self.chain_id, channel
self.chain_id, chan_conn_cli.channel
);

unreceived_packets(&chains.src, &chains.dst, &channel).map_err(Error::supervisor)
unreceived_packets(&chains.src, &chains.dst, &chan_conn_cli.channel)
.map_err(Error::supervisor)
}
}

Expand Down
Loading

0 comments on commit f7cbe1c

Please sign in to comment.