Skip to content

Commit

Permalink
Represent signer as a String and use account prefix from the config (#…
Browse files Browse the repository at this point in the history
…711)

* Refactoring of signer from AccountId into String (#219)

* Logic to add the account_prefix from config to signer (#219)

* Uncomment tests that are failing

* Move bech32 encoding of accounts to cosmos chain impl

* Use a newtype for the signer

* Allow empty signer in tests

* Add plain constructor to all ICS04 messages

* Remove default implementation of Msg::type_url

* Use associated type on Msg trait instead of type parameter on Msg::to_any

* Formatting

* Remove proto-compiler lockfile

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
andynog and romac committed Mar 11, 2021
1 parent bfd3d8e commit 9c0c5d6
Show file tree
Hide file tree
Showing 43 changed files with 413 additions and 469 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ prost-types = "0.7"
bytes = "1.0.0"
dyn-clonable = "0.9.0"
regex = "1"
bech32 = "0.8.0"
subtle-encoding = "0.5"
sha2 = { version = "0.9.3", optional = true }

Expand Down
24 changes: 0 additions & 24 deletions modules/src/address.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! This is the definition of a transfer messages that an application submits to a chain.

use std::convert::{TryFrom, TryInto};
use tendermint::account::Id as AccountId;
use tendermint_proto::Protobuf;

use ibc_proto::ibc::applications::transfer::v1::MsgTransfer as RawMsgTransfer;
use tendermint_proto::Protobuf;

use crate::address::{account_to_string, string_to_account};
use crate::application::ics20_fungible_token_transfer::error::{Error, Kind};
use crate::ics02_client::height::Height;
use crate::ics24_host::identifier::{ChannelId, PortId};
use crate::signer::Signer;
use crate::tx_msg::Msg;

pub const TYPE_URL: &str = "/ibc.applications.transfer.v1.MsgTransfer";
Expand All @@ -26,9 +25,9 @@ pub struct MsgTransfer {
/// the tokens to be transferred
pub token: Option<ibc_proto::cosmos::base::v1beta1::Coin>,
/// the sender address
pub sender: AccountId,
pub sender: Signer,
/// the recipient address on the destination chain
pub receiver: AccountId,
pub receiver: Signer,
/// Timeout height relative to the current block height.
/// The timeout is disabled when set to 0.
pub timeout_height: Height,
Expand All @@ -39,6 +38,7 @@ pub struct MsgTransfer {

impl Msg for MsgTransfer {
type ValidationError = Error;
type Raw = RawMsgTransfer;

fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
Expand All @@ -47,10 +47,6 @@ impl Msg for MsgTransfer {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.sender]
}
}

impl Protobuf<RawMsgTransfer> for MsgTransfer {}
Expand All @@ -63,8 +59,8 @@ impl TryFrom<RawMsgTransfer> for MsgTransfer {
source_port: raw_msg.source_port.parse().unwrap(),
source_channel: raw_msg.source_channel.parse().unwrap(),
token: raw_msg.token,
sender: string_to_account(raw_msg.sender).unwrap(),
receiver: string_to_account(raw_msg.receiver).unwrap(),
sender: raw_msg.sender.into(),
receiver: raw_msg.receiver.into(),
timeout_height: raw_msg.timeout_height.unwrap().try_into().unwrap(),
timeout_timestamp: 0,
})
Expand All @@ -77,8 +73,8 @@ impl From<MsgTransfer> for RawMsgTransfer {
source_port: domain_msg.source_port.to_string(),
source_channel: domain_msg.source_channel.to_string(),
token: domain_msg.token,
sender: account_to_string(domain_msg.sender).unwrap(),
receiver: account_to_string(domain_msg.receiver).unwrap(),
sender: domain_msg.sender.to_string(),
receiver: domain_msg.receiver.to_string(),
timeout_height: Some(domain_msg.timeout_height.try_into().unwrap()),
timeout_timestamp: 0,
}
Expand Down
4 changes: 2 additions & 2 deletions modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod tests {
..height
}))
.into(),
signer,
signer.clone(),
)
.unwrap(),
MsgCreateAnyClient::new(
Expand All @@ -153,7 +153,7 @@ mod tests {
..height
}))
.into(),
signer,
signer.clone(),
)
.unwrap(),
MsgCreateAnyClient::new(
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod tests {
let msg = MsgUpdateAnyClient {
client_id: cid.clone(),
header: MockHeader::new(update_height).into(),
signer,
signer: signer.clone(),
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
Expand Down
20 changes: 8 additions & 12 deletions modules/src/ics02_client/msgs/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

use std::convert::TryFrom;

use tendermint::account::Id as AccountId;
use tendermint_proto::Protobuf;

use ibc_proto::ibc::core::client::v1::MsgCreateClient as RawMsgCreateClient;

use crate::address::{account_to_string, string_to_account};
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics02_client::error;
use crate::ics02_client::error::{Error, Kind};
use crate::signer::Signer;
use crate::tx_msg::Msg;

pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgCreateClient";
Expand All @@ -24,14 +23,14 @@ pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgCreateClient";
pub struct MsgCreateAnyClient {
pub client_state: AnyClientState,
pub consensus_state: AnyConsensusState,
pub signer: AccountId,
pub signer: Signer,
}

impl MsgCreateAnyClient {
pub fn new(
client_state: AnyClientState,
consensus_state: AnyConsensusState,
signer: AccountId,
signer: Signer,
) -> Result<Self, Error> {
if client_state.client_type() != consensus_state.client_type() {
return Err(error::Kind::RawClientAndConsensusStateTypesMismatch {
Expand All @@ -46,16 +45,19 @@ impl MsgCreateAnyClient {
signer,
})
}

pub fn client_state(&self) -> AnyClientState {
self.client_state.clone()
}

pub fn consensus_state(&self) -> AnyConsensusState {
self.consensus_state.clone()
}
}

impl Msg for MsgCreateAnyClient {
type ValidationError = crate::ics24_host::error::ValidationError;
type Raw = RawMsgCreateClient;

fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
Expand All @@ -64,10 +66,6 @@ impl Msg for MsgCreateAnyClient {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

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

impl Protobuf<RawMsgCreateClient> for MsgCreateAnyClient {}
Expand All @@ -84,14 +82,12 @@ impl TryFrom<RawMsgCreateClient> for MsgCreateAnyClient {
.consensus_state
.ok_or_else(|| Kind::InvalidRawConsensusState.context("missing consensus state"))?;

let signer = string_to_account(raw.signer).map_err(|e| Kind::InvalidAddress.context(e))?;

MsgCreateAnyClient::new(
AnyClientState::try_from(raw_client_state)
.map_err(|e| Kind::InvalidRawClientState.context(e))?,
AnyConsensusState::try_from(raw_consensus_state)
.map_err(|e| Kind::InvalidRawConsensusState.context(e))?,
signer,
raw.signer.into(),
)
}
}
Expand All @@ -101,7 +97,7 @@ impl From<MsgCreateAnyClient> for RawMsgCreateClient {
RawMsgCreateClient {
client_state: Some(ics_msg.client_state.into()),
consensus_state: Some(ics_msg.consensus_state.into()),
signer: account_to_string(ics_msg.signer).unwrap(),
signer: ics_msg.signer.to_string(),
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions modules/src/ics02_client/msgs/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

use std::convert::TryFrom;

use tendermint::account::Id as AccountId;
use tendermint_proto::Protobuf;

use ibc_proto::ibc::core::client::v1::MsgUpdateClient as RawMsgUpdateClient;

use crate::address::{account_to_string, string_to_account};
use crate::ics02_client::client_def::AnyHeader;
use crate::ics02_client::error::{Error, Kind};
use crate::ics24_host::identifier::ClientId;
use crate::signer::Signer;
use crate::tx_msg::Msg;

pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgUpdateClient";
Expand All @@ -24,11 +23,11 @@ pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgUpdateClient";
pub struct MsgUpdateAnyClient {
pub client_id: ClientId,
pub header: AnyHeader,
pub signer: AccountId,
pub signer: Signer,
}

impl MsgUpdateAnyClient {
pub fn new(client_id: ClientId, header: AnyHeader, signer: AccountId) -> Self {
pub fn new(client_id: ClientId, header: AnyHeader, signer: Signer) -> Self {
MsgUpdateAnyClient {
client_id,
header,
Expand All @@ -39,6 +38,7 @@ impl MsgUpdateAnyClient {

impl Msg for MsgUpdateAnyClient {
type ValidationError = crate::ics24_host::error::ValidationError;
type Raw = RawMsgUpdateClient;

fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
Expand All @@ -47,10 +47,6 @@ impl Msg for MsgUpdateAnyClient {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

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

impl Protobuf<RawMsgUpdateClient> for MsgUpdateAnyClient {}
Expand All @@ -60,12 +56,11 @@ impl TryFrom<RawMsgUpdateClient> for MsgUpdateAnyClient {

fn try_from(raw: RawMsgUpdateClient) -> Result<Self, Self::Error> {
let raw_header = raw.header.ok_or(Kind::InvalidRawHeader)?;
let signer = string_to_account(raw.signer).map_err(|e| Kind::InvalidAddress.context(e))?;

Ok(MsgUpdateAnyClient {
client_id: raw.client_id.parse().unwrap(),
header: AnyHeader::try_from(raw_header).unwrap(),
signer,
signer: raw.signer.into(),
})
}
}
Expand All @@ -75,7 +70,7 @@ impl From<MsgUpdateAnyClient> for RawMsgUpdateClient {
RawMsgUpdateClient {
client_id: ics_msg.client_id.to_string(),
header: Some(ics_msg.header.into()),
signer: account_to_string(ics_msg.signer).unwrap(),
signer: ics_msg.signer.to_string(),
}
}
}
Expand Down
16 changes: 5 additions & 11 deletions modules/src/ics03_connection/msgs/conn_open_ack.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::convert::{TryFrom, TryInto};

use tendermint::account::Id as AccountId;
use tendermint_proto::Protobuf;

use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenAck as RawMsgConnectionOpenAck;

use crate::address::{account_to_string, string_to_account};
use crate::ics02_client::client_def::AnyClientState;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::Version;
use crate::ics23_commitment::commitment::CommitmentProofBytes;
use crate::ics24_host::identifier::ConnectionId;
use crate::proofs::{ConsensusProof, Proofs};
use crate::signer::Signer;
use crate::tx_msg::Msg;
use crate::Height;

Expand All @@ -25,7 +24,7 @@ pub struct MsgConnectionOpenAck {
pub client_state: Option<AnyClientState>,
pub proofs: Proofs,
pub version: Version,
pub signer: AccountId,
pub signer: Signer,
}

impl MsgConnectionOpenAck {
Expand Down Expand Up @@ -66,6 +65,7 @@ impl MsgConnectionOpenAck {

impl Msg for MsgConnectionOpenAck {
type ValidationError = Error;
type Raw = RawMsgConnectionOpenAck;

fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
Expand All @@ -74,10 +74,6 @@ impl Msg for MsgConnectionOpenAck {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

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

impl Protobuf<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {}
Expand All @@ -86,8 +82,6 @@ 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(Kind::MissingConsensusHeight)?
Expand Down Expand Up @@ -133,7 +127,7 @@ impl TryFrom<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {
proof_height,
)
.map_err(|e| Kind::InvalidProof.context(e))?,
signer,
signer: msg.signer.into(),
})
}
}
Expand Down Expand Up @@ -162,7 +156,7 @@ impl From<MsgConnectionOpenAck> for RawMsgConnectionOpenAck {
.consensus_proof()
.map_or_else(|| None, |h| Some(h.height().into())),
version: Some(ics_msg.version.into()),
signer: account_to_string(ics_msg.signer).unwrap(),
signer: ics_msg.signer.to_string(),
}
}
}
Expand Down
Loading

0 comments on commit 9c0c5d6

Please sign in to comment.