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

MsgConnOpenTry builder and CLI #388

Merged
merged 15 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
32 changes: 7 additions & 25 deletions modules/src/ics03_connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ibc_proto::ibc::core::connection::v1::{
use tendermint_proto::DomainType;

use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::validate_versions;
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
Expand Down Expand Up @@ -45,7 +46,7 @@ impl From<ConnectionEnd> for RawConnectionEnd {
client_id: value.client_id.to_string(),
versions: value.versions,
state: value.state as i32,
counterparty: Some(RawCounterparty::from(value.counterparty)),
counterparty: Some(value.counterparty.into()),
}
}
}
Expand All @@ -65,6 +66,11 @@ impl ConnectionEnd {
})
}

/// Getter for the state of this connection end.
pub fn state(&self) -> &State {
&self.state
}

/// Setter for the `state` field.
pub fn set_state(&mut self, new_state: State) {
self.state = new_state;
Expand All @@ -91,11 +97,6 @@ impl ConnectionEnd {
self.state.eq(other)
}

/// Getter for the state of this connection end.
pub fn state(&self) -> &State {
&self.state
}

/// Getter for the client id on the local party of this connection end.
pub fn client_id(&self) -> &ClientId {
&self.client_id
Expand Down Expand Up @@ -196,25 +197,6 @@ impl Counterparty {
}
}

pub fn validate_versions(versions: Vec<String>) -> Result<Vec<String>, String> {
let v: Vec<String> = versions.to_vec();
if v.is_empty() {
return Err("missing versions".to_string());
}

for v in versions.into_iter() {
validate_version(v)?;
}
Ok(v)
}

pub fn validate_version(version: String) -> Result<String, String> {
if version.trim().is_empty() {
return Err("empty version string".to_string());
}
Ok(version)
}

#[derive(Clone, Debug, PartialEq)]
pub enum State {
Init = 1,
Expand Down
13 changes: 11 additions & 2 deletions modules/src/ics03_connection/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics03_connection::connection::{ConnectionEnd, State};
use crate::ics03_connection::error::Error;
use crate::ics03_connection::handler::ConnectionResult;
use crate::ics03_connection::version::{get_compatible_versions, pick_version};
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::Height;
Expand Down Expand Up @@ -40,11 +41,19 @@ pub trait ConnectionReader {

/// Function required by ICS 03. Returns the list of all possible versions that the connection
/// handshake protocol supports.
fn get_compatible_versions(&self) -> Vec<String>;
fn get_compatible_versions(&self) -> Vec<String> {
get_compatible_versions()
}

/// Function required by ICS 03. Returns one version out of the supplied list of versions, which the
/// connection handshake protocol prefers.
fn pick_version(&self, counterparty_candidate_versions: Vec<String>) -> String;
fn pick_version(
&self,
supported_versions: Vec<String>,
counterparty_candidate_versions: Vec<String>,
) -> Result<String, Error> {
pick_version(supported_versions, counterparty_candidate_versions)
}
}

/// A context supplying all the necessary write-only dependencies (i.e., storage writing facility)
Expand Down
3 changes: 3 additions & 0 deletions modules/src/ics03_connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub enum Kind {
#[error("invalid version")]
InvalidVersion,

#[error("no commong version")]
NoCommonVersion,

#[error("invalid address")]
InvalidAddress,

Expand Down
10 changes: 2 additions & 8 deletions modules/src/ics03_connection/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,8 @@ pub(crate) fn process(
new_connection_end.set_state(State::TryOpen);

// Pick the version.
let local_versions = ctx.get_compatible_versions();
let intersection: Vec<String> = msg
.counterparty_versions()
.iter()
.filter(|cv| local_versions.contains(cv))
.cloned()
.collect();
new_connection_end.set_version(ctx.pick_version(intersection));
new_connection_end
.set_version(ctx.pick_version(ctx.get_compatible_versions(), msg.counterparty_versions())?);
adizere marked this conversation as resolved.
Show resolved Hide resolved

output.log("success: connection verification passed");

Expand Down
1 change: 1 addition & 0 deletions modules/src/ics03_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub mod events;
/// Message processing logic (protocol) for ICS 03.
pub mod handler;
pub mod msgs;
pub mod version;
10 changes: 8 additions & 2 deletions modules/src/ics03_connection/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ pub mod conn_open_confirm;
pub mod conn_open_init;
pub mod conn_open_try;

/// Message type for the `MsgConnectionOpenConfirm` message.
pub const TYPE_MSG_CONNECTION_OPEN_CONFIRM: &str = "connection_open_confirm";
/// Enumeration of all possible message types that the ICS3 protocol processes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConnectionMsgType {
OpenInit,
OpenTry,
OpenAck,
OpenConfirm,
}

/// Enumeration of all possible messages that the ICS3 protocol processes.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
17 changes: 10 additions & 7 deletions modules/src/ics03_connection/msgs/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use tendermint_proto::DomainType;

use tendermint::account::Id as AccountId;

use crate::address::{account_to_string, string_to_account};
use crate::ics02_client::client_def::AnyClientState;
use crate::ics03_connection::connection::validate_version;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::validate_version;
use crate::ics23_commitment::commitment::CommitmentProof;
use crate::ics24_host::identifier::ConnectionId;
use crate::proofs::{ConsensusProof, Proofs};
Expand Down Expand Up @@ -91,6 +92,8 @@ impl TryFrom<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenAck) -> Result<Self, Self::Error> {
let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?;

let consensus_height = msg
.consensus_height
.ok_or_else(|| Kind::MissingConsensusHeight)?
Expand Down Expand Up @@ -134,8 +137,7 @@ impl TryFrom<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {
proof_height,
)
.map_err(|e| Kind::InvalidProof.context(e))?,
signer: AccountId::from_str(msg.signer.as_str())
.map_err(|e| Kind::InvalidSigner.context(e))?,
signer,
})
}
}
Expand Down Expand Up @@ -166,14 +168,15 @@ impl From<MsgConnectionOpenAck> for RawMsgConnectionOpenAck {
.consensus_proof()
.map_or_else(|| None, |h| Some(h.height().into())),
version: ics_msg.version,
signer: ics_msg.signer.to_string(),
signer: account_to_string(ics_msg.signer).unwrap(),
}
}
}

#[cfg(test)]
pub mod test_util {
use crate::test_utils::{get_dummy_account_id_raw, get_dummy_proof};
use crate::ics03_connection::version::default_version_string;
use crate::test_utils::{get_dummy_bech32_account, get_dummy_proof};
use ibc_proto::ibc::core::client::v1::Height;
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenAck as RawMsgConnectionOpenAck;

Expand All @@ -193,8 +196,8 @@ pub mod test_util {
}),
client_state: None,
proof_client: vec![],
version: "1.0.0".to_string(),
signer: get_dummy_account_id_raw(),
version: default_version_string(),
signer: get_dummy_bech32_account(),
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions modules/src/ics03_connection/msgs/conn_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use tendermint_proto::DomainType;

use tendermint::account::Id as AccountId;

use crate::address::{account_to_string, string_to_account};
use crate::ics03_connection::error::{Error, Kind};
use crate::ics24_host::identifier::ConnectionId;
use crate::{proofs::Proofs, tx_msg::Msg};
use std::str::FromStr;

/// Message type for the `MsgConnectionOpenConfirm` message.
pub const TYPE_MSG_CONNECTION_OPEN_CONFIRM: &str = "connection_open_confirm";
Expand Down Expand Up @@ -61,6 +61,8 @@ impl TryFrom<RawMsgConnectionOpenConfirm> for MsgConnectionOpenConfirm {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenConfirm) -> Result<Self, Self::Error> {
let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?;

let proof_height = msg
.proof_height
.ok_or_else(|| Kind::MissingProofHeight)?
Expand All @@ -73,8 +75,7 @@ impl TryFrom<RawMsgConnectionOpenConfirm> for MsgConnectionOpenConfirm {
.map_err(|e| Kind::IdentifierError.context(e))?,
proofs: Proofs::new(msg.proof_ack.into(), None, None, proof_height)
.map_err(|e| Kind::InvalidProof.context(e))?,
signer: AccountId::from_str(msg.signer.as_str())
.map_err(|e| Kind::InvalidSigner.context(e))?,
signer,
})
}
}
Expand All @@ -85,7 +86,7 @@ impl From<MsgConnectionOpenConfirm> for RawMsgConnectionOpenConfirm {
connection_id: ics_msg.connection_id.as_str().to_string(),
proof_ack: ics_msg.proofs.object_proof().clone().into(),
proof_height: Some(ics_msg.proofs.height().into()),
signer: ics_msg.signer.to_string(),
signer: account_to_string(ics_msg.signer).unwrap(),
}
}
}
Expand All @@ -95,7 +96,7 @@ pub mod test_util {
use ibc_proto::ibc::core::client::v1::Height;
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm;

use crate::test_utils::{get_dummy_account_id_raw, get_dummy_proof};
use crate::test_utils::{get_dummy_bech32_account, get_dummy_proof};

pub fn get_dummy_msg_conn_open_confirm() -> RawMsgConnectionOpenConfirm {
RawMsgConnectionOpenConfirm {
Expand All @@ -105,7 +106,7 @@ pub mod test_util {
version_number: 0,
version_height: 10,
}),
signer: get_dummy_account_id_raw(),
signer: get_dummy_bech32_account(),
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions modules/src/ics03_connection/msgs/conn_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use tendermint_proto::DomainType;
use tendermint::account::Id as AccountId;

use crate::address::{account_to_string, string_to_account};
use crate::ics03_connection::connection::{validate_version, Counterparty};
use crate::ics03_connection::connection::Counterparty;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::validate_version;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::tx_msg::Msg;

Expand Down Expand Up @@ -107,8 +108,8 @@ impl From<MsgConnectionOpenInit> for RawMsgConnectionOpenInit {
client_id: ics_msg.client_id.as_str().to_string(),
connection_id: ics_msg.connection_id.as_str().to_string(),
counterparty: Some(ics_msg.counterparty.into()),
signer: account_to_string(ics_msg.signer).unwrap(),
version: ics_msg.version,
signer: account_to_string(ics_msg.signer).unwrap(),
}
}
}
Expand All @@ -118,6 +119,7 @@ pub mod test_util {
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenInit as RawMsgConnectionOpenInit;

use crate::ics03_connection::msgs::test_util::get_dummy_counterparty;
use crate::ics03_connection::version::default_version_string;
use crate::test_utils::get_dummy_bech32_account;

/// Returns a dummy message, for testing only.
Expand All @@ -127,7 +129,7 @@ pub mod test_util {
client_id: "srcclient".to_string(),
connection_id: "srcconnection".to_string(),
counterparty: Some(get_dummy_counterparty()),
version: "1.0.0".to_string(),
version: default_version_string(),
signer: get_dummy_bech32_account(),
}
}
Expand Down
36 changes: 21 additions & 15 deletions modules/src/ics03_connection/msgs/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use tendermint_proto::DomainType;

use tendermint::account::Id as AccountId;

use crate::address::{account_to_string, string_to_account};
use crate::ics02_client::client_def::AnyClientState;
use crate::ics03_connection::connection::{validate_versions, Counterparty};
use crate::ics03_connection::connection::Counterparty;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::validate_versions;
use crate::ics23_commitment::commitment::CommitmentProof;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::proofs::{ConsensusProof, Proofs};
Expand All @@ -23,14 +25,14 @@ pub const TYPE_MSG_CONNECTION_OPEN_TRY: &str = "connection_open_try";
///
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MsgConnectionOpenTry {
connection_id: ConnectionId,
client_id: ClientId,
client_state: Option<AnyClientState>,
counterparty_chosen_connection_id: Option<ConnectionId>,
counterparty: Counterparty,
counterparty_versions: Vec<String>,
proofs: Proofs,
signer: AccountId,
pub connection_id: ConnectionId,
adizere marked this conversation as resolved.
Show resolved Hide resolved
pub client_id: ClientId,
pub client_state: Option<AnyClientState>,
pub counterparty_chosen_connection_id: Option<ConnectionId>,
pub counterparty: Counterparty,
pub counterparty_versions: Vec<String>,
pub proofs: Proofs,
pub signer: AccountId,
}

impl MsgConnectionOpenTry {
Expand Down Expand Up @@ -99,6 +101,10 @@ impl Msg for MsgConnectionOpenTry {
fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}

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

impl DomainType<RawMsgConnectionOpenTry> for MsgConnectionOpenTry {}
Expand Down Expand Up @@ -160,8 +166,7 @@ impl TryFrom<RawMsgConnectionOpenTry> for MsgConnectionOpenTry {
proof_height,
)
.map_err(|e| Kind::InvalidProof.context(e))?,
signer: AccountId::from_str(msg.signer.as_str())
.map_err(|e| Kind::InvalidSigner.context(e))?,
signer: string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?,
})
}
}
Expand Down Expand Up @@ -194,15 +199,16 @@ impl From<MsgConnectionOpenTry> for RawMsgConnectionOpenTry {
.proofs
.consensus_proof()
.map_or_else(|| None, |h| Some(h.height().into())),
signer: ics_msg.signer.to_string(),
signer: account_to_string(ics_msg.signer).unwrap(),
}
}
}

#[cfg(test)]
pub mod test_util {
use crate::ics03_connection::msgs::test_util::get_dummy_counterparty;
use crate::test_utils::{get_dummy_account_id_raw, get_dummy_proof};
use crate::ics03_connection::version::get_compatible_versions;
use crate::test_utils::{get_dummy_bech32_account, get_dummy_proof};
use ibc_proto::ibc::core::client::v1::Height;
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry;

Expand All @@ -219,7 +225,7 @@ pub mod test_util {
desired_connection_id: "srcconnection".to_string(),
client_state: None,
counterparty: Some(get_dummy_counterparty()),
counterparty_versions: vec!["1.0.0".to_string()],
counterparty_versions: get_compatible_versions(),
counterparty_chosen_connection_id: "srcconnection".to_string(),
proof_init: get_dummy_proof(),
proof_height: Some(Height {
Expand All @@ -232,7 +238,7 @@ pub mod test_util {
version_height: consensus_height,
}),
proof_client: vec![],
signer: get_dummy_account_id_raw(),
signer: get_dummy_bech32_account(),
}
}
}
Expand Down
Loading