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

WIP: Localchain feature #289

Closed
wants to merge 19 commits into from
Closed
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
13 changes: 13 additions & 0 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ description = """
Implementation of the Inter-Blockchain Communication Protocol (IBC).
"""

[features]
# This feature grants access to development-time mocking libraries, such as `MockContext` or `MockHeader`.
# Dependens on the testgen suite for generating Tendermint light blocks.
mocks = [ "tendermint-testgen" ]

[dependencies]
tendermint-proto = "0.1.0"

Expand All @@ -35,11 +40,19 @@ regex = "1"

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

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

[dependencies.tendermint-testgen]
version = "0.17.0-rc1"
git = "https://github.com/informalsystems/tendermint-rs"
optional = true

[dev-dependencies]
tokio = { version = "0.2", features = ["macros"] }
subtle-encoding = { version = "0.5" }
tendermint-testgen = { version = "0.17.0-rc1", git = "https://github.com/informalsystems/tendermint-rs" } # Needed for generating (synthetic) light blocks.
40 changes: 20 additions & 20 deletions modules/src/ics02_client/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =
pub const MOCK_CLIENT_STATE_TYPE_URL: &str = "/ibc.mock.ClientState";
pub const MOCK_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.mock.ConsensusState";

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
use {
crate::mock_client::client_def::MockClient,
crate::mock_client::header::MockHeader,
Expand Down Expand Up @@ -92,7 +92,7 @@ pub trait ClientDef: Clone {
pub enum AnyHeader {
Tendermint(tendermint::header::Header),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Mock(MockHeader),
}

Expand All @@ -101,7 +101,7 @@ impl Header for AnyHeader {
match self {
Self::Tendermint(header) => header.client_type(),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(header) => header.client_type(),
}
}
Expand All @@ -110,7 +110,7 @@ impl Header for AnyHeader {
match self {
Self::Tendermint(header) => header.height(),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(header) => header.height(),
}
}
Expand All @@ -120,7 +120,7 @@ impl Header for AnyHeader {
pub enum AnyClientState {
Tendermint(TendermintClientState),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Mock(MockClientState),
}

Expand All @@ -129,15 +129,15 @@ impl AnyClientState {
match self {
Self::Tendermint(tm_state) => tm_state.latest_height(),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(mock_state) => mock_state.latest_height(),
}
}
pub fn client_type(&self) -> ClientType {
match self {
Self::Tendermint(state) => state.client_type(),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(state) => state.client_type(),
}
}
Expand All @@ -155,7 +155,7 @@ impl TryFrom<Any> for AnyClientState {
TendermintClientState::decode_vec(&raw.value)?,
)),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
MOCK_CLIENT_STATE_TYPE_URL => Ok(AnyClientState::Mock(MockClientState::decode_vec(
&raw.value,
)?)),
Expand All @@ -174,7 +174,7 @@ impl From<AnyClientState> for Any {
type_url: TENDERMINT_CLIENT_STATE_TYPE_URL.to_string(),
value: value.encode_vec().unwrap(),
},
#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(value) => Any {
type_url: MOCK_CLIENT_STATE_TYPE_URL.to_string(),
value: value.encode_vec().unwrap(),
Expand All @@ -200,7 +200,7 @@ impl ClientState for AnyClientState {
match self {
AnyClientState::Tendermint(tm_state) => tm_state.is_frozen(),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(mock_state) => mock_state.is_frozen(),
}
}
Expand All @@ -210,7 +210,7 @@ impl ClientState for AnyClientState {
pub enum AnyConsensusState {
Tendermint(crate::ics07_tendermint::consensus_state::ConsensusState),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Mock(MockConsensusState),
}

Expand All @@ -219,7 +219,7 @@ impl AnyConsensusState {
match self {
AnyConsensusState::Tendermint(_cs) => ClientType::Tendermint,

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(_cs) => ClientType::Mock,
}
}
Expand All @@ -236,7 +236,7 @@ impl TryFrom<Any> for AnyConsensusState {
TendermintConsensusState::decode_vec(&value.value)?,
)),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
MOCK_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Mock(
MockConsensusState::decode_vec(&value.value)?,
)),
Expand All @@ -255,7 +255,7 @@ impl From<AnyConsensusState> for Any {
type_url: TENDERMINT_CONSENSUS_STATE_TYPE_URL.to_string(),
value: value.encode_vec().unwrap(),
},
#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(value) => Any {
type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
value: value.encode_vec().unwrap(),
Expand All @@ -282,7 +282,7 @@ impl ConsensusState for AnyConsensusState {
pub enum AnyClient {
Tendermint(TendermintClient),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Mock(MockClient),
}

Expand All @@ -291,7 +291,7 @@ impl AnyClient {
match client_type {
ClientType::Tendermint => Self::Tendermint(TendermintClient),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
ClientType::Mock => Self::Mock(MockClient),
}
}
Expand Down Expand Up @@ -325,7 +325,7 @@ impl ClientDef for AnyClient {
))
}

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
let (client_state, header) = downcast!(
client_state => AnyClientState::Mock,
Expand Down Expand Up @@ -372,7 +372,7 @@ impl ClientDef for AnyClient {
)
}

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
let client_state = downcast!(
client_state => AnyClientState::Mock
Expand Down Expand Up @@ -416,7 +416,7 @@ impl ClientDef for AnyClient {
)
}

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
let client_state = downcast!(client_state => AnyClientState::Mock)
.ok_or_else(|| error::Kind::ClientArgsTypeMismatch(ClientType::Mock))?;
Expand Down Expand Up @@ -461,7 +461,7 @@ impl ClientDef for AnyClient {
)
}

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
let client_state = downcast!(
client_state => AnyClientState::Mock
Expand Down
6 changes: 3 additions & 3 deletions modules/src/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde_derive::{Deserialize, Serialize};
pub enum ClientType {
Tendermint = 1,

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Mock = 9999,
}

Expand All @@ -16,7 +16,7 @@ impl ClientType {
match self {
Self::Tendermint => "Tendermint",

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
Self::Mock => "mock",
}
}
Expand All @@ -29,7 +29,7 @@ impl std::str::FromStr for ClientType {
match s {
"Tendermint" => Ok(Self::Tendermint),

#[cfg(test)]
#[cfg(any(test, feature = "mocks"))]
"mock" => Ok(Self::Mock),

_ => Err(error::Kind::UnknownClientType(s.to_string()).into()),
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ mod tests {
use crate::ics07_tendermint::client_state::ClientState;
use crate::ics07_tendermint::header::test_util::get_dummy_header;
use crate::ics24_host::identifier::ClientId;
use crate::mock::context::MockContext;
use crate::mock_client::header::MockHeader;
use crate::mock_client::state::{MockClientState, MockConsensusState};
use crate::mock_context::MockContext;
use crate::Height;
use std::str::FromStr;
use std::time::Duration;
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 @@ -71,9 +71,9 @@ mod tests {
use crate::ics02_client::msgs::{ClientMsg, MsgUpdateAnyClient};
use crate::ics03_connection::msgs::test_util::get_dummy_account_id;
use crate::ics24_host::identifier::ClientId;
use crate::mock::context::MockContext;
use crate::mock_client::header::MockHeader;
use crate::mock_client::state::MockClientState;
use crate::mock_context::MockContext;
use crate::Height;
use std::str::FromStr;

Expand Down
7 changes: 5 additions & 2 deletions modules/src/ics03_connection/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ mod tests {
use crate::ics03_connection::msgs::conn_open_ack::MsgConnectionOpenAck;
use crate::ics03_connection::msgs::ConnectionMsg;
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::ClientId;
use crate::mock_context::MockContext;
use crate::ics24_host::identifier::{ChainId, ClientId};
use crate::mock::context::MockContext;
use crate::mock::host::HostType;
use crate::Height;

#[test]
Expand Down Expand Up @@ -163,6 +164,8 @@ mod tests {
// Parametrize the (correct) host chain to have a height at least as recent as the
// the height of the proofs in the Ack msg.
let correct_context = MockContext::new(
ChainId::new("mockgaia", 1).unwrap(),
HostType::Mock,
5,
Height::new(1, msg_ack.proofs().height().increment().version_height),
);
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 @@ -86,7 +86,7 @@ mod tests {
use crate::ics03_connection::msgs::ConnectionMsg;
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::ClientId;
use crate::mock_context::MockContext;
use crate::mock::context::MockContext;
use crate::Height;

#[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 @@ -54,7 +54,7 @@ mod tests {
use crate::ics03_connection::msgs::conn_open_init::test_util::get_dummy_msg_conn_open_init;
use crate::ics03_connection::msgs::conn_open_init::MsgConnectionOpenInit;
use crate::ics03_connection::msgs::ConnectionMsg;
use crate::mock_context::MockContext;
use crate::mock::context::MockContext;
use crate::Height;

#[test]
Expand Down
11 changes: 9 additions & 2 deletions modules/src/ics03_connection/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ mod tests {
use crate::ics03_connection::msgs::conn_open_try::test_util::get_dummy_msg_conn_open_try;
use crate::ics03_connection::msgs::conn_open_try::MsgConnectionOpenTry;
use crate::ics03_connection::msgs::ConnectionMsg;
use crate::mock_context::MockContext;
use crate::ics24_host::identifier::ChainId;
use crate::mock::context::MockContext;
use crate::mock::host::HostType;
use crate::Height;

#[test]
Expand All @@ -127,7 +129,12 @@ mod tests {
}

let host_chain_height = Height::new(1, 35);
let context = MockContext::new(5, host_chain_height);
let context = MockContext::new(
ChainId::new("mockgaia", 1).unwrap(),
HostType::Mock,
5,
host_chain_height,
);
let pruning_window = context.host_chain_history_size() as u64;
let client_consensus_state_height = 10;

Expand Down
15 changes: 15 additions & 0 deletions modules/src/ics07_tendermint/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ impl From<ClientState> for RawClientState {
}
}

#[cfg(test)]
pub mod test_util {
// pub fn get_dummy_msg_conn_open_confirm() -> RawMsgConnectionOpenConfirm {
// RawMsgConnectionOpenConfirm {
// connection_id: "srcconnection".to_string(),
// proof_ack: get_dummy_proof(),
// proof_height: Some(Height {
// version_number: 0,
// version_height: 10,
// }),
// signer: get_dummy_account_id_raw(),
// }
// }
}

#[cfg(test)]
mod tests {
use std::time::Duration;
Expand Down
9 changes: 7 additions & 2 deletions modules/src/ics07_tendermint/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use tendermint::block::signed_header::SignedHeader;
use tendermint::validator::Set as ValidatorSet;

use crate::ics02_client::client_type::ClientType;
use crate::ics07_tendermint::consensus_state::ConsensusState;
use crate::ics23_commitment::commitment::CommitmentRoot;
use crate::ics24_host::identifier::ChainId;
use crate::Height;

#[cfg(test)]
use {
crate::ics07_tendermint::consensus_state::ConsensusState,
crate::ics23_commitment::commitment::CommitmentRoot,
};

/// Tendermint consensus header
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Header {
Expand All @@ -18,6 +22,7 @@ pub struct Header {
pub trusted_validator_set: ValidatorSet, // the last trusted validator set at trusted height
}

#[cfg(test)]
impl Header {
pub(crate) fn consensus_state(&self) -> ConsensusState {
ConsensusState {
Expand Down
3 changes: 1 addition & 2 deletions modules/src/ics18_relayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
pub mod context;
pub mod error;

#[cfg(test)]
pub mod utils; // Currently only used in tests.
pub mod utils;
Loading