Skip to content

Commit

Permalink
Unambiguous packet query
Browse files Browse the repository at this point in the history
Before, a `query_txs` for a packet on a given chain would filter only
by the packet source port, source channel and sequence.
With 2 chains, there can be at most a single packet matching such
filter, and this fact was assumed in the code.

However, this assumption does not hold with more than 2 chains. To
understand why, consider the following example with chains `ibc-0`,
`ibc-1` and `ibc-2`:
- a channel handshake occurs between `ibc-0` and `ibc-1`; the channel
identifiers are `channel-0` on both ends
- a channel handshake occurs between `ibc-1` and `ibc-2`; the channel
identifier on `ibc-1` is `channel-1` and on `ibc-2` is `channel-0`

If now both `ibc-0` and `ibc-2` send packets to `ibc-1` and we do a
packet query filtering only by the source, such query will return
packets from both `ibc-0` and `ibc-1` (as both have `channel-0` as
the source channel in their packets).

In this commit, we start filtering also by the destination port and
destination channel. This makes the packet query unambiguous.
  • Loading branch information
vitorenesduarte committed Feb 5, 2021
1 parent b1b9dac commit a6315e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ pub struct QueryPacketEventDataRequest {
pub event_id: IBCEventType,
pub source_channel_id: ChannelId,
pub source_port_id: PortId,
pub destination_channel_id: ChannelId,
pub destination_port_id: PortId,
pub sequences: Vec<Sequence>,
pub height: Height,
}
Expand Down
11 changes: 11 additions & 0 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,14 @@ fn packet_query(request: &QueryPacketEventDataRequest, seq: &Sequence) -> Result
format!("{}.packet_src_port", request.event_id.as_str()),
request.source_port_id.to_string(),
)
.and_eq(
format!("{}.packet_dst_channel", request.event_id.as_str()),
request.destination_channel_id.to_string(),
)
.and_eq(
format!("{}.packet_dst_port", request.event_id.as_str()),
request.destination_port_id.to_string(),
)
.and_eq(
format!("{}.packet_sequence", request.event_id.as_str()),
seq.to_string(),
Expand All @@ -840,6 +848,7 @@ fn packet_from_tx_search_response(
seq: Sequence,
response: &tendermint_rpc::endpoint::tx_search::Response,
) -> Result<Option<IBCEvent>, Error> {
// TODO: remove loop as `response.txs.len() <= 1`
for r in response.txs.iter() {
let height = r.height;
if height.value() > request.height.revision_height {
Expand Down Expand Up @@ -869,6 +878,8 @@ fn packet_from_tx_search_response(
let packet = packet.unwrap();
if packet.source_port != request.source_port_id
|| packet.source_channel != request.source_channel_id
|| packet.destination_port != request.destination_port_id
|| packet.destination_channel != request.destination_channel_id
|| packet.sequence != seq
{
continue;
Expand Down
4 changes: 4 additions & 0 deletions relayer/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ impl RelayPath {
event_id: IBCEventType::SendPacket,
source_port_id: self.src_port_id().clone(),
source_channel_id: self.src_channel_id().clone(),
destination_port_id: self.dst_port_id().clone(),
destination_channel_id: self.dst_channel_id().clone(),
sequences,
height: self.src_height,
})?;
Expand Down Expand Up @@ -442,6 +444,8 @@ impl RelayPath {
event_id: IBCEventType::WriteAck,
source_port_id: self.dst_port_id().clone(),
source_channel_id: self.dst_channel_id().clone(),
destination_port_id: self.src_port_id().clone(),
destination_channel_id: self.src_channel_id().clone(),
sequences,
height: query_height,
})
Expand Down

0 comments on commit a6315e2

Please sign in to comment.