Skip to content

Commit

Permalink
recv_packet relay (#444)
Browse files Browse the repository at this point in the history
* packet CLIs

* recv packet relay from loop and CLIs

* Add limit on number of messages in a Tx, reorg the code

* add packet files

* fix rev event timestamp, change debug to info temporarily

* Streamline events, multi message Tx-es, cleanup

* Review comments, cleanup, validate events before processing in the relayer loop

* cargo fmt

* Remove files added by mistake in the last commit.

* Update changelog
  • Loading branch information
ancazamfir authored Dec 10, 2020
1 parent 65e4f26 commit 3a42c0c
Show file tree
Hide file tree
Showing 38 changed files with 1,611 additions and 474 deletions.
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
## Unreleased Changes

### FEATURES

- [relayer]
- Implement relaying for recv_packet ([#379])

- [relayer-cli]
- Packet CLIs for recv_packet ([#443])

### IMPROVEMENTS

- Mock chain (implementing IBC handlers) and integration against CLI ([#158])
- Relayer tests for client update (ping pong) against MockChain ([#381])

- [relayer]
- Mock chain (implementing IBC handlers) and integration against CLI ([#158])
- Relayer tests for client update (ping pong) against MockChain ([#381])

[#158]: https://github.com/informalsystems/ibc-rs/issues/158
[#379]: https://github.com/informalsystems/ibc-rs/issues/379
[#381]: https://github.com/informalsystems/ibc-rs/issues/381
[#443]: https://github.com/informalsystems/ibc-rs/issues/443

## v0.0.5
*December 2, 2020*
Expand Down
38 changes: 38 additions & 0 deletions modules/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::ics02_client::events::NewBlock;
use crate::ics03_connection::events as ConnectionEvents;
use crate::ics04_channel::events as ChannelEvents;
use crate::ics20_fungible_token_transfer::events as TransferEvents;
use crate::Height as ICSHeight;

use tendermint_rpc::event::{Event as RpcEvent, EventData as RpcEventData};

Expand All @@ -14,6 +15,22 @@ use tendermint::block::Height;

use tracing::warn;

/// Events types
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum IBCEventType {
SendPacket,
RecvPacket,
}

impl IBCEventType {
pub fn as_str(&self) -> &'static str {
match *self {
IBCEventType::SendPacket => "send_packet",
_ => "unhandled",
}
}
}

/// Events created by the IBC component of a chain, destined for a relayer.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum IBCEvent {
Expand Down Expand Up @@ -50,6 +67,27 @@ impl IBCEvent {
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
pub fn height(&self) -> Height {
match self {
IBCEvent::NewBlock(bl) => bl.height,
IBCEvent::UpdateClient(uc) => uc.height,
IBCEvent::SendPacketChannel(ev) => ev.envelope.height,
IBCEvent::ReceivePacketChannel(ev) => ev.height,
_ => {
unimplemented!()
}
}
}
pub fn set_height(&mut self, height: ICSHeight) {
match self {
IBCEvent::SendPacketChannel(ev) => {
ev.envelope.height = Height::try_from(height.version_height).unwrap()
}
_ => {
unimplemented!()
}
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
21 changes: 15 additions & 6 deletions modules/src/ics02_client/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use tendermint_proto::Protobuf;

use crate::ics02_client::error::{Error, Kind};
use ibc_proto::ibc::core::client::v1::Height as RawHeight;
use serde_derive::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Height {
/// Previously known as "epoch", and will be renamed to "revision" soon
pub version_number: u64,
Expand Down Expand Up @@ -121,10 +122,18 @@ impl From<Height> for RawHeight {

impl std::fmt::Display for Height {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"epoch: {}, height: {}",
self.version_number, self.version_height
)
write!(f, "{}-{}", self.version_number, self.version_height)
}
}

impl TryFrom<String> for Height {
type Error = anomaly::Error<Kind>;

fn try_from(value: String) -> Result<Self, Self::Error> {
let split: Vec<&str> = value.split('-').collect();
Ok(Height {
version_number: split[0].parse::<u64>().unwrap(),
version_height: split[1].parse::<u64>().unwrap(),
})
}
}
13 changes: 13 additions & 0 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use ibc_proto::ibc::core::channel::v1::Counterparty as RawCounterparty;

use tendermint_proto::Protobuf;

use crate::events::IBCEventType;
use crate::ics02_client::height::Height;
use anomaly::fail;
use std::convert::{TryFrom, TryInto};
use std::str::FromStr;
Expand Down Expand Up @@ -270,6 +272,17 @@ impl State {
}
}

/// Used for queries and not yet standardized in channel's query.proto
// todo -move to channel.rs?
#[derive(Clone, Debug)]
pub struct QueryPacketEventDataRequest {
pub event_id: IBCEventType,
pub channel_id: String,
pub port_id: String,
pub sequences: Vec<u64>,
pub height: Height,
}

/// Version validation, specific for channel (ICS4) opening handshake protocol.
/// This field is supposed to be opaque to the core IBC protocol. No explicit validation necessary,
/// and empty version is currently allowed by the specification (cf. ICS 004, v1).
Expand Down
35 changes: 26 additions & 9 deletions modules/src/ics04_channel/events.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Types for the IBC events emitted from Tendermint Websocket by the channels module.
use crate::attribute;
use crate::events::{IBCEvent, RawObject};
use crate::ics02_client::height::Height;
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};
use tendermint::block;

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -164,33 +165,51 @@ impl From<CloseConfirm> for IBCEvent {
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SendPacket {
pub struct PacketEnvelope {
pub height: block::Height,
pub packet_src_port: PortId,
pub packet_src_channel: ChannelId,
pub packet_dst_port: PortId,
pub packet_dst_channel: ChannelId,
pub packet_sequence: u64,
pub packet_timeout_height: u64,
pub packet_timeout_height: Height,
pub packet_timeout_stamp: u64,
}

impl TryFrom<RawObject> for SendPacket {
impl TryFrom<RawObject> for PacketEnvelope {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
Ok(SendPacket {
let height_str: String = attribute!(obj, "send_packet.packet_timeout_height");
Ok(PacketEnvelope {
height: obj.height,
packet_src_port: attribute!(obj, "send_packet.packet_src_port"),
packet_src_channel: attribute!(obj, "send_packet.packet_src_channel"),
packet_dst_port: attribute!(obj, "send_packet.packet_dst_port"),
packet_dst_channel: attribute!(obj, "send_packet.packet_dst_channel"),
packet_sequence: attribute!(obj, "send_packet.packet_sequence"),
packet_timeout_height: attribute!(obj, "send_packet.packet_timeout_height"),
packet_timeout_height: height_str.try_into()?,
packet_timeout_stamp: attribute!(obj, "send_packet.packet_timeout_timestamp"),
})
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SendPacket {
pub envelope: PacketEnvelope,
pub data: Vec<u8>,
}

impl TryFrom<RawObject> for SendPacket {
type Error = BoxError;
fn try_from(obj: RawObject) -> Result<Self, Self::Error> {
let data_str: String = attribute!(obj, "send_packet.packet_data");
Ok(SendPacket {
envelope: PacketEnvelope::try_from(obj)?,
data: Vec::from(data_str.as_str().as_bytes()),
})
}
}

impl From<SendPacket> for IBCEvent {
fn from(v: SendPacket) -> Self {
IBCEvent::SendPacketChannel(v)
Expand All @@ -205,9 +224,8 @@ pub struct ReceivePacket {
pub packet_dst_port: PortId,
pub packet_dst_channel: ChannelId,
pub packet_sequence: u64,
pub packet_timeout_height: u64,
pub packet_timeout_height: String,
pub packet_timeout_stamp: u64,
pub packet_ack: String,
}

impl TryFrom<RawObject> for ReceivePacket {
Expand All @@ -222,7 +240,6 @@ impl TryFrom<RawObject> for ReceivePacket {
packet_sequence: attribute!(obj, "recv_packet.packet_sequence"),
packet_timeout_height: attribute!(obj, "recv_packet.packet_timeout_height"),
packet_timeout_stamp: attribute!(obj, "recv_packet.packet_timeout_timestamp"),
packet_ack: attribute!(obj, "recv_packet.packet_ack"),
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions modules/src/ics04_channel/msgs/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ pub struct MsgRecvPacket {
}

impl MsgRecvPacket {
// todo: Constructor not used yet.
#[allow(dead_code, unreachable_code, unused_variables)]
fn new(
pub fn new(
packet: Packet,
proof: CommitmentProof,
proof_height: Height,
signer: AccountId,
) -> Result<MsgRecvPacket, Error> {
Ok(Self {
packet: todo!(),
packet,
proofs: Proofs::new(proof, None, None, proof_height)
.map_err(|e| Kind::InvalidProof.context(e))?,
signer,
Expand All @@ -58,6 +57,10 @@ impl Msg for MsgRecvPacket {
Ok(())
}

fn type_url(&self) -> String {
"/ibc.core.channel.v1.MsgRecvPacket".to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
Expand Down
16 changes: 8 additions & 8 deletions modules/src/ics04_channel/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ impl From<Sequence> for u64 {

#[derive(Clone, Debug, PartialEq)]
pub struct Packet {
sequence: Sequence,
source_port: PortId,
source_channel: ChannelId,
destination_port: PortId,
destination_channel: ChannelId,
data: Vec<u8>,
timeout_height: Height,
timeout_timestamp: u64,
pub sequence: Sequence,
pub source_port: PortId,
pub source_channel: ChannelId,
pub destination_port: PortId,
pub destination_channel: ChannelId,
pub data: Vec<u8>,
pub timeout_height: Height,
pub timeout_timestamp: u64,
}

impl TryFrom<RawPacket> for Packet {
Expand Down
6 changes: 6 additions & 0 deletions modules/src/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ impl From<tendermint::chain::Id> for ChainId {
}
}

impl Default for ChainId {
fn default() -> Self {
"defaultChainId".to_string().parse().unwrap()
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ClientId(String);

Expand Down
2 changes: 1 addition & 1 deletion proto-compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The `ibc-proto-compiler` is a simple command-line tool to automate the compilati
From within the `proto-compiler` directory, run the following command to clone the Cosmos SDK repository, and check out a specific commit:

```bash
$ cargo run -- clone-sdk --out /tmp/sdk --commit ce3994020a0d5c246016c8832ba4a668e8b7c77b
$ cargo run -- clone-sdk --out /tmp/sdk --commit 15324920548c2629e51d837bcefc1cbc40797c5d
```

Note: the full commit hash must be specified.
Expand Down
1 change: 1 addition & 0 deletions proto-compiler/src/cmd/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl CompileCmd {
let proto_services_path = [
sdk_dir.join("proto/cosmos/auth/v1beta1/query.proto"),
sdk_dir.join("proto/cosmos/staking/v1beta1/query.proto"),
sdk_dir.join("proto/ibc/core/channel/v1/query.proto"),
];

// List available paths for dependencies
Expand Down
Loading

0 comments on commit 3a42c0c

Please sign in to comment.