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

Removed TryFromRaw, implemented DomainType where needed #252

Merged
merged 3 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions docs/architecture/adr-001-repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,25 @@ name prefixed with "Raw", for example:
use ibc_proto::channel::Channel as RawChannel;
```

For any Raw data type that is defined in `ibc-proto` we implement the `TryFromRaw` trait, which serves as a translation
For any Raw data type that is defined in `ibc-proto` we implement the `DomainType` trait, which serves as a translation
& validation layer between the proto ("Raw") types and the domain types. For example, for a `Channel` we do as follows:

```Rust
impl TryFromRaw for ChannelEnd {
type RawType = RawChannel;
impl DomainType<RawChannel> for ChannelEnd {}

impl TryFrom<RawChannel> for ChannelEnd {
type Error = anomaly::Error<Kind>;

fn try_from(value: RawChannel) -> Result<Self, Self::Error> {
// Translate, validate each field from RawChannel into a Channel.
}
}

impl From<ChannelEnd> for RawChannel {
fn from(value: ChannelEnd) -> Self {
// Translate Channel into a RawChannel
}
}
```

This issue [#130](https://github.com/informalsystems/ibc-rs/issues/130) is a good starting place for more context
Expand Down
22 changes: 18 additions & 4 deletions modules/src/ics02_client/raw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::ics03_connection::error::Kind;
use crate::ics24_host::identifier::ConnectionId;
use crate::try_from_raw::TryFromRaw;
use std::convert::TryFrom;
use std::str::FromStr;
use tendermint_proto::DomainType;

//TODO: This might need to be migrated to ibc-proto crate. But ClientConnections (as array of strings)
// might not be part of an official proto file
Expand All @@ -11,9 +12,14 @@ pub struct RawClientConnections {
pub connections: ::std::vec::Vec<String>,
}

impl TryFromRaw for Vec<ConnectionId> {
type RawType = RawClientConnections;
#[derive(Clone, Debug)]
pub struct ConnectionIds(pub Vec<ConnectionId>);

impl DomainType<RawClientConnections> for ConnectionIds {}

impl TryFrom<RawClientConnections> for ConnectionIds {
type Error = anomaly::Error<Kind>;

fn try_from(value: RawClientConnections) -> Result<Self, Self::Error> {
if !value.connections.is_empty() {
let mut connections: Vec<ConnectionId> = vec![];
Expand All @@ -24,9 +30,17 @@ impl TryFromRaw for Vec<ConnectionId> {
Err(_e) => return Err(Kind::IdentifierError.into()),
}
}
Ok(connections)
Ok(ConnectionIds(connections))
} else {
Err(Kind::ConnectionNotFound.into())
}
}
}

impl From<ConnectionIds> for RawClientConnections {
fn from(value: ConnectionIds) -> Self {
RawClientConnections {
connections: value.0.iter().map(|v| v.to_string()).collect(),
}
}
}
30 changes: 27 additions & 3 deletions modules/src/ics03_connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::ics03_connection::error::{Error, Kind};
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::try_from_raw::TryFromRaw;
use ibc_proto::ibc::connection::{
ConnectionEnd as RawConnectionEnd, Counterparty as RawCounterparty,
};
use serde_derive::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use tendermint_proto::DomainType;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ConnectionEnd {
Expand All @@ -17,8 +17,9 @@ pub struct ConnectionEnd {
versions: Vec<String>,
}

impl TryFromRaw for ConnectionEnd {
type RawType = RawConnectionEnd;
impl DomainType<RawConnectionEnd> for ConnectionEnd {}

impl TryFrom<RawConnectionEnd> for ConnectionEnd {
type Error = anomaly::Error<Kind>;
fn try_from(value: RawConnectionEnd) -> Result<Self, Self::Error> {
Ok(Self::new(
Expand All @@ -36,6 +37,17 @@ impl TryFromRaw for ConnectionEnd {
}
}

impl From<ConnectionEnd> for RawConnectionEnd {
fn from(value: ConnectionEnd) -> Self {
RawConnectionEnd {
client_id: value.client_id.to_string(),
versions: value.versions,
state: value.state as i32,
counterparty: Some(RawCounterparty::from(value.counterparty)),
}
}
}

impl ConnectionEnd {
pub fn new(
state: State,
Expand Down Expand Up @@ -134,6 +146,18 @@ impl TryFrom<RawCounterparty> for Counterparty {
}
}

impl From<Counterparty> for RawCounterparty {
fn from(value: Counterparty) -> Self {
RawCounterparty {
client_id: value.client_id.as_str().to_string(),
connection_id: value.connection_id.as_str().to_string(),
prefix: Some(ibc_proto::ibc::commitment::MerklePrefix {
key_prefix: value.prefix.0,
}),
}
}
}

impl Counterparty {
pub fn new(
client_id: ClientId,
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics03_connection/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mod tests {
use crate::ics03_connection::msgs::{ConnectionMsg, MsgConnectionOpenAck};
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::ClientId;
use crate::try_from_raw::TryFromRaw;
use std::convert::TryFrom;
use std::str::FromStr;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics03_connection/handler/conn_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod tests {
use crate::ics03_connection::msgs::{ConnectionMsg, MsgConnectionOpenConfirm};
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::ClientId;
use crate::try_from_raw::TryFromRaw;
use std::convert::TryFrom;
use std::str::FromStr;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics03_connection/handler/conn_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod tests {
use crate::ics03_connection::handler::{dispatch, ConnectionResult};
use crate::ics03_connection::msgs::test_util::get_dummy_msg_conn_open_init;
use crate::ics03_connection::msgs::{ConnectionMsg, MsgConnectionOpenInit};
use crate::try_from_raw::TryFromRaw;
use std::convert::TryFrom;

#[test]
fn conn_open_init_msg_processing() {
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics03_connection/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod tests {
use crate::ics03_connection::handler::{dispatch, ConnectionResult};
use crate::ics03_connection::msgs::test_util::get_dummy_msg_conn_open_try;
use crate::ics03_connection::msgs::{ConnectionMsg, MsgConnectionOpenTry};
use crate::try_from_raw::TryFromRaw;
use std::convert::TryFrom;

#[test]
fn conn_open_try_msg_processing() {
Expand Down
30 changes: 20 additions & 10 deletions modules/src/ics03_connection/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::ics03_connection::connection::{validate_version, validate_versions, C
use crate::ics03_connection::error::{Error, Kind};
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::proofs::{ConsensusProof, Proofs};
use crate::try_from_raw::TryFromRaw;
use crate::tx_msg::Msg;

use ibc_proto::ibc::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck;
Expand All @@ -34,6 +33,7 @@ use tendermint::block::Height;
use serde_derive::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::str::{from_utf8, FromStr};
use tendermint_proto::DomainType;

/// Message type for the `MsgConnectionOpenInit` message.
pub const TYPE_MSG_CONNECTION_OPEN_INIT: &str = "connection_open_init";
Expand Down Expand Up @@ -84,9 +84,11 @@ impl MsgConnectionOpenInit {
}
}

impl TryFromRaw for MsgConnectionOpenInit {
type RawType = RawMsgConnectionOpenInit;
impl DomainType<RawMsgConnectionOpenInit> for MsgConnectionOpenInit {}

impl TryFrom<RawMsgConnectionOpenInit> for MsgConnectionOpenInit {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenInit) -> Result<Self, Self::Error> {
Ok(Self {
connection_id: msg
Expand All @@ -109,6 +111,17 @@ impl TryFromRaw for MsgConnectionOpenInit {
}
}

impl From<MsgConnectionOpenInit> for RawMsgConnectionOpenInit {
fn from(value: MsgConnectionOpenInit) -> Self {
RawMsgConnectionOpenInit {
client_id: value.client_id.as_str().to_string(),
connection_id: value.connection_id.as_str().to_string(),
counterparty: Some(value.counterparty.into()),
signer: value.signer.as_bytes().to_vec(),
}
}
}

impl Msg for MsgConnectionOpenInit {
type ValidationError = Error;

Expand Down Expand Up @@ -217,8 +230,7 @@ impl Msg for MsgConnectionOpenTry {
}
}

impl TryFromRaw for MsgConnectionOpenTry {
type RawType = RawMsgConnectionOpenTry;
impl TryFrom<RawMsgConnectionOpenTry> for MsgConnectionOpenTry {
type Error = Error;

fn try_from(msg: RawMsgConnectionOpenTry) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -340,8 +352,7 @@ impl Msg for MsgConnectionOpenAck {
}
}

impl TryFromRaw for MsgConnectionOpenAck {
type RawType = RawMsgConnectionOpenAck;
impl TryFrom<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenAck) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -433,8 +444,7 @@ impl Msg for MsgConnectionOpenConfirm {
}
}

impl TryFromRaw for MsgConnectionOpenConfirm {
type RawType = RawMsgConnectionOpenConfirm;
impl TryFrom<RawMsgConnectionOpenConfirm> for MsgConnectionOpenConfirm {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenConfirm) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -569,13 +579,13 @@ mod tests {
use crate::ics03_connection::msgs::{
MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenTry,
};
use crate::try_from_raw::TryFromRaw;
use ibc_proto::ibc::client::Height;
use ibc_proto::ibc::connection::Counterparty as RawCounterparty;
use ibc_proto::ibc::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck;
use ibc_proto::ibc::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm;
use ibc_proto::ibc::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit;
use ibc_proto::ibc::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry;
use std::convert::TryFrom;

#[test]
fn parse_connection_open_init_msg() {
Expand Down
29 changes: 25 additions & 4 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::ics04_channel::error::{self, Error, Kind};
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::try_from_raw::TryFromRaw;

use ibc_proto::ibc::channel::Channel as RawChannel;

use anomaly::fail;
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::str::FromStr;
use tendermint_proto::DomainType;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChannelEnd {
Expand All @@ -17,8 +18,9 @@ pub struct ChannelEnd {
version: String,
}

impl TryFromRaw for ChannelEnd {
type RawType = RawChannel;
impl DomainType<RawChannel> for ChannelEnd {}

impl TryFrom<RawChannel> for ChannelEnd {
type Error = anomaly::Error<Kind>;

fn try_from(value: RawChannel) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -60,6 +62,25 @@ impl TryFromRaw for ChannelEnd {
}
}

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::channel::Counterparty {
port_id: value.counterparty().port_id.to_string(),
channel_id: value.counterparty().channel_id.to_string(),
}),
connection_hops: value
.connection_hops
.iter()
.map(|v| v.as_str().to_string())
.collect(),
version: value.version,
}
}
}

impl ChannelEnd {
/// Creates a new ChannelEnd in state Uninitialized and other fields parametrized.
pub fn new(
Expand Down Expand Up @@ -226,10 +247,10 @@ mod tests {
use std::str::FromStr;

use crate::ics04_channel::channel::ChannelEnd;
use crate::try_from_raw::TryFromRaw;

use ibc_proto::ibc::channel::Channel as RawChannel;
use ibc_proto::ibc::channel::Counterparty as RawCounterparty;
use std::convert::TryFrom;

#[test]
fn channel_end_try_from_raw() {
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics23_commitment/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl From<Vec<u8>> for CommitmentProof {
}

#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct CommitmentPrefix(Vec<u8>);
pub struct CommitmentPrefix(pub Vec<u8>); // Todo: decent getter or DomainType trait implementation

impl CommitmentPrefix {
pub fn is_empty(&self) -> bool {
Expand Down
1 change: 0 additions & 1 deletion modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub mod ics24_host;
pub mod keys;
pub mod macros;
pub mod proofs;
pub mod try_from_raw;
pub mod tx_msg;

#[cfg(test)]
Expand Down
21 changes: 0 additions & 21 deletions modules/src/try_from_raw.rs

This file was deleted.

Loading