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

Relayer CLIs for client messages #251

Merged
merged 14 commits into from
Sep 25, 2020
37 changes: 32 additions & 5 deletions modules/src/ics02_client/client_def.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use prost_types::Any;
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;

use crate::downcast;
use crate::ics02_client::client_type::ClientType;
Expand All @@ -14,10 +16,8 @@ use crate::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProof, Com
use crate::ics24_host::identifier::{ClientId, ConnectionId};

use ::tendermint::block::Height;

use prost_types::Any;
use std::convert::TryFrom;
use tendermint_proto::{DomainType, Error, Kind};

#[cfg(test)]
use {
crate::mock_client::client_def::MockClient,
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Header for AnyHeader {
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnyClientState {
Tendermint(TendermintClientState),

Expand Down Expand Up @@ -191,7 +191,7 @@ impl ClientState for AnyClientState {
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AnyConsensusState {
Tendermint(crate::ics07_tendermint::consensus_state::ConsensusState),

Expand Down Expand Up @@ -464,3 +464,30 @@ impl ClientDef for AnyClient {
}
}
}

#[cfg(test)]
mod tests {
use crate::ics02_client::client_def::AnyClientState;
use crate::ics07_tendermint::client_state::ClientState;
use crate::ics07_tendermint::header::test_util::get_dummy_header;
use prost_types::Any;
use std::convert::TryFrom;
use std::time::Duration;

#[test]
fn to_and_from_any() {
let tm_header = get_dummy_header();
let tm_client_state = AnyClientState::Tendermint(ClientState {
chain_id: tm_header.signed_header.header.chain_id.to_string(),
trusting_period: Duration::from_secs(64000),
unbonding_period: Duration::from_secs(128000),
max_clock_drift: Duration::from_millis(3000),
latest_height: tm_header.signed_header.header.height,
frozen_height: 0.into(),
});

let raw: Any = tm_client_state.clone().into();
let tm_client_state_back = AnyClientState::try_from(raw).unwrap();
assert_eq!(tm_client_state, tm_client_state_back);
}
}
46 changes: 32 additions & 14 deletions modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn process(
client_type,
client_state,
consensus_state,
signer: _,
} = msg;

if ctx.client_state(&client_id).is_some() {
Expand Down Expand Up @@ -60,26 +61,30 @@ pub fn keep(keeper: &mut dyn ClientKeeper, result: CreateClientResult) -> Result

#[cfg(test)]
mod tests {
use std::time::Duration;
use tendermint::block::Height;

use super::*;
use crate::ics02_client::context_mock::MockClientContext;
use crate::ics03_connection::msgs::test_util::get_dummy_account_id;
use crate::ics07_tendermint::client_state::ClientState;
use crate::ics07_tendermint::header::test_util::get_dummy_header;
use crate::ics07_tendermint::msgs::create_client::MsgCreateClient;
use crate::mock_client::header::MockHeader;
use crate::mock_client::state::{MockClientState, MockConsensusState};
use std::str::FromStr;
use std::time::Duration;
use tendermint::block::Height;

#[test]
fn test_create_client_ok() {
let client_id: ClientId = "mockclient".parse().unwrap();
let ctx = MockClientContext::default();

let signer = get_dummy_account_id();

let msg = MsgCreateAnyClient {
client_id,
client_type: ClientType::Mock,
client_state: MockClientState(MockHeader(Height(42))).into(),
consensus_state: MockConsensusState(MockHeader(Height(42))).into(),
signer,
};

let output = process(&ctx, msg.clone());
Expand Down Expand Up @@ -113,6 +118,8 @@ mod tests {
fn test_create_client_existing_client_type() {
let height = Height(42);
let client_id: ClientId = "mockclient".parse().unwrap();
let signer = get_dummy_account_id();

let mut ctx = MockClientContext::default();
ctx.with_client_type(&client_id, ClientType::Mock, height);

Expand All @@ -123,6 +130,7 @@ mod tests {
client_type: ClientType::Mock,
client_state: MockClientState(MockHeader(height)).into(),
consensus_state: MockConsensusState(MockHeader(height)).into(),
signer,
};

let output = process(&ctx, msg.clone());
Expand All @@ -137,6 +145,8 @@ mod tests {
#[test]
fn test_create_client_existing_client_state() {
let client_id: ClientId = "mockclient".parse().unwrap();
let signer = get_dummy_account_id();

let mut ctx = MockClientContext::default();
let height = Height(30);
ctx.with_client_consensus_state(&client_id, height);
Expand All @@ -146,6 +156,7 @@ mod tests {
client_type: ClientType::Tendermint,
client_state: MockClientState(MockHeader(Height(42))).into(),
consensus_state: MockConsensusState(MockHeader(Height(42))).into(),
signer,
};

let output = process(&ctx, msg.clone());
Expand All @@ -160,6 +171,7 @@ mod tests {
#[test]
fn test_create_client_ok_multiple() {
let existing_client_id: ClientId = "existingmockclient".parse().unwrap();
let signer = get_dummy_account_id();
let height = Height(80);
let mut ctx = MockClientContext::default();
ctx.with_client_consensus_state(&existing_client_id, height);
Expand All @@ -170,18 +182,21 @@ mod tests {
client_type: ClientType::Mock,
client_state: MockClientState(MockHeader(Height(42))).into(),
consensus_state: MockConsensusState(MockHeader(Height(42))).into(),
signer,
},
MsgCreateAnyClient {
client_id: "newmockclient2".parse().unwrap(),
client_type: ClientType::Mock,
client_state: MockClientState(MockHeader(Height(42))).into(),
consensus_state: MockConsensusState(MockHeader(Height(42))).into(),
signer,
},
MsgCreateAnyClient {
client_id: "newmockclient3".parse().unwrap(),
client_type: ClientType::Tendermint,
client_state: MockClientState(MockHeader(Height(50))).into(),
consensus_state: MockConsensusState(MockHeader(Height(50))).into(),
signer,
},
]
.into_iter()
Expand Down Expand Up @@ -218,24 +233,27 @@ mod tests {

#[test]
fn test_tm_create_client_ok() {
use tendermint::account::Id as AccountId;
let client_id: ClientId = "tendermint".parse().unwrap();
let signer = get_dummy_account_id();

let ctx = MockClientContext::default();

let ics_msg = MsgCreateClient {
client_id,
header: get_dummy_header(),
let tm_header = get_dummy_header();
let tm_client_state = AnyClientState::Tendermint(ClientState {
chain_id: tm_header.signed_header.header.chain_id.to_string(),
trusting_period: Duration::from_secs(64000),
unbonding_period: Duration::from_secs(128000),
max_clock_drift: Duration::from_millis(3000),
signer: AccountId::from_str("7C2BB42A8BE69791EC763E51F5A49BCD41E82237").unwrap(),
};
latest_height: tm_header.signed_header.header.height,
frozen_height: 0.into(),
});

let msg = MsgCreateAnyClient {
client_id: ics_msg.client_id().clone(),
client_type: ics_msg.client_type(),
client_state: ics_msg.client_state(),
consensus_state: ics_msg.consensus_state(),
client_id,
client_type: ClientType::Tendermint,
client_state: tm_client_state,
consensus_state: AnyConsensusState::Tendermint(tm_header.consensus_state()),
signer,
};

let output = process(&ctx, msg.clone());
Expand Down
18 changes: 16 additions & 2 deletions modules/src/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ pub fn process(
) -> HandlerResult<UpdateClientResult, Error> {
let mut output = HandlerOutput::builder();

let MsgUpdateAnyClient { client_id, header } = msg;
let MsgUpdateAnyClient {
client_id,
header,
signer: _,
} = msg;

let client_type = ctx
.client_type(&client_id)
Expand Down Expand Up @@ -65,21 +69,26 @@ pub fn keep(keeper: &mut dyn ClientKeeper, result: UpdateClientResult) -> Result

#[cfg(test)]
mod tests {
use tendermint::block::Height;

use super::*;
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::context_mock::MockClientContext;
use crate::ics03_connection::msgs::test_util::get_dummy_account_id;
use crate::mock_client::header::MockHeader;
use tendermint::block::Height;

#[test]
fn test_update_client_ok() {
let client_id: ClientId = "mockclient".parse().unwrap();
let signer = get_dummy_account_id();

let mut ctx = MockClientContext::default();
ctx.with_client(&client_id, ClientType::Mock, Height(42));

let msg = MsgUpdateAnyClient {
client_id,
header: MockHeader(Height(46)).into(),
signer,
};

let output = process(&ctx, msg.clone());
Expand All @@ -105,12 +114,15 @@ mod tests {
#[test]
fn test_update_nonexisting_client() {
let client_id: ClientId = "mockclient1".parse().unwrap();
let signer = get_dummy_account_id();

let mut ctx = MockClientContext::default();
ctx.with_client_consensus_state(&client_id, Height(42));

let msg = MsgUpdateAnyClient {
client_id: "nonexistingclient".parse().unwrap(),
header: MockHeader(Height(46)).into(),
signer,
};

let output = process(&ctx, msg.clone());
Expand All @@ -132,6 +144,7 @@ mod tests {
"mockclient2".parse().unwrap(),
"mockclient3".parse().unwrap(),
];
let signer = get_dummy_account_id();

let initial_height = Height(45);
let update_height = Height(49);
Expand All @@ -146,6 +159,7 @@ mod tests {
let msg = MsgUpdateAnyClient {
client_id: cid.clone(),
header: MockHeader(update_height).into(),
signer,
};

let output = process(&ctx, msg.clone());
Expand Down
Loading