Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel open handshake message relaying/CLIs #403

Merged
merged 8 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,24 @@ bech32 = "0.7.2"

[dependencies.tendermint]
version = "0.17.0-rc2"
git = "https://github.com/informalsystems/tendermint-rs"
branch = "master"

[dependencies.tendermint-rpc]
version = "0.17.0-rc2"
git = "https://github.com/informalsystems/tendermint-rs"
branch = "master"
features = ["http-client", "websocket-client"]

[dependencies.tendermint-light-client]
version = "0.17.0-rc2"
git = "https://github.com/informalsystems/tendermint-rs"
branch = "master"

[dependencies.tendermint-proto]
version = "0.17.0-rc2"
git = "https://github.com/informalsystems/tendermint-rs"
branch = "master"

[dev-dependencies]
tokio = { version = "0.2", features = ["macros"] }
Expand Down
106 changes: 68 additions & 38 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::ics04_channel::error::{self, Error, Kind};
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};

use ibc_proto::ibc::core::channel::v1::Channel as RawChannel;
use ibc_proto::ibc::core::channel::v1::Counterparty as RawCounterparty;

use tendermint_proto::DomainType;

use anomaly::fail;
Expand All @@ -28,19 +30,12 @@ impl TryFrom<RawChannel> for ChannelEnd {

let chan_state = State::from_i32(value.state)?;

// Pop out the Counterparty from the Option.
let counterparty = match value.counterparty {
Some(cp) => cp,
None => return Err(Kind::MissingCounterparty.into()),
};

// Assemble the 'remote' attribute of the Channel, which represents the Counterparty.
let remote = Counterparty {
port_id: PortId::from_str(counterparty.port_id.as_str())
.map_err(|e| Kind::IdentifierError.context(e))?,
channel_id: ChannelId::from_str(counterparty.channel_id.as_str())
.map_err(|e| Kind::IdentifierError.context(e))?,
};
let remote = Counterparty::try_from(
value
.counterparty
.ok_or_else(|| Kind::MissingCounterparty)?,
)?;
ancazamfir marked this conversation as resolved.
Show resolved Hide resolved

// Parse each item in connection_hops into a ConnectionId.
let connection_hops = value
Expand All @@ -52,22 +47,22 @@ impl TryFrom<RawChannel> for ChannelEnd {

let version = validate_version(value.version)?;

let mut channel_end = ChannelEnd::new(chan_ordering, remote, connection_hops, version);
channel_end.set_state(chan_state);

Ok(channel_end)
Ok(ChannelEnd::new(
chan_state,
chan_ordering,
remote,
connection_hops,
version,
))
}
}

impl From<ChannelEnd> for RawChannel {
fn from(value: ChannelEnd) -> Self {
RawChannel {
state: value.state.clone() as i32,
ordering: value.ordering.clone() as i32,
counterparty: Some(ibc_proto::ibc::core::channel::v1::Counterparty {
port_id: value.counterparty().port_id.to_string(),
channel_id: value.counterparty().channel_id.to_string(),
}),
ordering: value.ordering as i32,
counterparty: Some(value.counterparty().into()),
connection_hops: value
.connection_hops
.iter()
Expand All @@ -81,13 +76,14 @@ impl From<ChannelEnd> for RawChannel {
impl ChannelEnd {
/// Creates a new ChannelEnd in state Uninitialized and other fields parametrized.
pub fn new(
state: State,
ordering: Order,
remote: Counterparty,
connection_hops: Vec<ConnectionId>,
version: String,
) -> Self {
Self {
state: State::Uninitialized,
state,
ordering,
remote,
connection_hops,
Expand Down Expand Up @@ -135,42 +131,76 @@ impl ChannelEnd {

#[derive(Clone, Debug, PartialEq)]
pub struct Counterparty {
port_id: PortId,
channel_id: ChannelId,
pub port_id: PortId,
pub channel_id: Option<ChannelId>,
}

impl Counterparty {
pub fn new(port_id: String, channel_id: String) -> Result<Self, Error> {
Ok(Self {
port_id: port_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
channel_id: channel_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
})
pub fn new(port_id: PortId, channel_id: Option<ChannelId>) -> Self {
Self {
port_id,
channel_id,
}
}

pub fn port_id(&self) -> String {
self.port_id.as_str().into()
pub fn port_id(&self) -> &PortId {
&self.port_id
}

pub fn channel_id(&self) -> String {
self.channel_id.as_str().into()
pub fn channel_id(&self) -> Option<&ChannelId> {
self.channel_id.as_ref()
}

pub fn validate_basic(&self) -> Result<(), Error> {
Ok(())
}
}

#[derive(Clone, Debug, PartialEq)]
impl DomainType<RawCounterparty> for Counterparty {}

impl TryFrom<RawCounterparty> for Counterparty {
type Error = anomaly::Error<Kind>;

fn try_from(value: RawCounterparty) -> Result<Self, Self::Error> {
let channel_id = Some(value.channel_id)
.filter(|x| !x.is_empty())
.map(|v| FromStr::from_str(v.as_str()))
.transpose()
.map_err(|e| Kind::IdentifierError.context(e))?;
Ok(Counterparty::new(
value
.port_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
channel_id,
))
}
}

impl From<Counterparty> for RawCounterparty {
fn from(value: Counterparty) -> Self {
RawCounterparty {
port_id: value.port_id.as_str().to_string(),
channel_id: value
.channel_id
.map_or_else(|| "".to_string(), |v| v.as_str().to_string()),
}
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Order {
None = 0,
Unordered,
Ordered,
}

impl Default for Order {
fn default() -> Self {
Order::Unordered
}
}

impl Order {
/// Yields the Order as a string
pub fn as_string(&self) -> &'static str {
Expand Down
15 changes: 9 additions & 6 deletions modules/src/ics04_channel/msgs/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const TYPE_MSG_CHANNEL_OPEN_ACK: &str = "channel_open_ack";
///
#[derive(Clone, Debug, PartialEq)]
pub struct MsgChannelOpenAck {
port_id: PortId,
channel_id: ChannelId,
counterparty_channel_id: ChannelId,
counterparty_version: String,
proofs: Proofs,
signer: AccountId,
pub port_id: PortId,
pub channel_id: ChannelId,
pub counterparty_channel_id: ChannelId,
pub counterparty_version: String,
pub proofs: Proofs,
pub signer: AccountId,
}

impl MsgChannelOpenAck {
Expand Down Expand Up @@ -71,6 +71,9 @@ impl Msg for MsgChannelOpenAck {
// All the validation is performed on creation
Ok(())
}
fn type_url(&self) -> String {
"/ibc.core.channel.v1.MsgChannelOpenAck".to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
Expand Down
12 changes: 8 additions & 4 deletions modules/src/ics04_channel/msgs/chan_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const TYPE_MSG_CHANNEL_OPEN_CONFIRM: &str = "channel_open_confirm";
///
#[derive(Clone, Debug, PartialEq)]
pub struct MsgChannelOpenConfirm {
port_id: PortId,
channel_id: ChannelId,
proofs: Proofs,
signer: AccountId,
pub port_id: PortId,
pub channel_id: ChannelId,
pub proofs: Proofs,
pub signer: AccountId,
}

impl MsgChannelOpenConfirm {
Expand Down Expand Up @@ -66,6 +66,10 @@ impl Msg for MsgChannelOpenConfirm {
Ok(())
}

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

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
Expand Down
56 changes: 10 additions & 46 deletions modules/src/ics04_channel/msgs/chan_open_init.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::address::{account_to_string, string_to_account};
use crate::ics04_channel::channel::{ChannelEnd, Counterparty, Order};
use crate::ics04_channel::channel::ChannelEnd;
use crate::ics04_channel::error::{Error, Kind};
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::ics24_host::identifier::{ChannelId, PortId};
use crate::tx_msg::Msg;

use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit;
use tendermint::account::Id as AccountId;
use tendermint_proto::DomainType;

use std::convert::{TryFrom, TryInto};
use std::str::FromStr;

/// Message type for the `MsgChannelOpenInit` message.
const TYPE_MSG_CHANNEL_OPEN_INIT: &str = "channel_open_init";
Expand All @@ -19,49 +18,10 @@ const TYPE_MSG_CHANNEL_OPEN_INIT: &str = "channel_open_init";
///
#[derive(Clone, Debug, PartialEq)]
pub struct MsgChannelOpenInit {
port_id: PortId,
channel_id: ChannelId,
channel: ChannelEnd,
signer: AccountId,
}

impl MsgChannelOpenInit {
// TODO: Constructors for domain types are in flux.
// Relayer will need this constructor. Validation here should be identical to `try_from` method.
// https://github.com/informalsystems/ibc-rs/issues/219
#[allow(dead_code, clippy::too_many_arguments)]
fn new(
port_id: String,
channel_id: String,
version: String,
order: i32,
connection_hops: Vec<String>,
counterparty_port_id: String,
counterparty_channel_id: String,
signer: AccountId,
) -> Result<MsgChannelOpenInit, Error> {
let connection_hops: Result<Vec<_>, _> = connection_hops
.into_iter()
.map(|s| ConnectionId::from_str(s.as_str()))
.collect();

Ok(Self {
port_id: port_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
channel_id: channel_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
channel: ChannelEnd::new(
Order::from_i32(order)?,
Counterparty::new(counterparty_port_id, counterparty_channel_id)
.map_err(|e| Kind::IdentifierError.context(e))?,
connection_hops.map_err(|e| Kind::IdentifierError.context(e))?,
version,
),
signer,
})
}
pub port_id: PortId,
pub channel_id: ChannelId,
pub channel: ChannelEnd,
pub signer: AccountId,
}

impl Msg for MsgChannelOpenInit {
Expand All @@ -79,6 +39,10 @@ impl Msg for MsgChannelOpenInit {
self.channel.validate_basic()
}

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

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
Expand Down
Loading