Skip to content

Commit

Permalink
Remove mock types and associated tests (informalsystems#3939)
Browse files Browse the repository at this point in the history
  • Loading branch information
romac authored Apr 9, 2024
1 parent 6a3212a commit 1edcb15
Show file tree
Hide file tree
Showing 25 changed files with 3 additions and 1,009 deletions.
3 changes: 0 additions & 3 deletions crates/relayer-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ all-features = true

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

[dependencies]
bytes = { workspace = true }
Expand Down
33 changes: 0 additions & 33 deletions crates/relayer-types/src/clients/ics07_tendermint/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,36 +659,3 @@ mod tests {
}
}
}

#[cfg(any(test, feature = "mocks"))]
pub mod test_util {
use core::time::Duration;

use tendermint::block::Header;

use crate::clients::ics07_tendermint::client_state::{AllowUpdate, ClientState};
use crate::core::ics02_client::height::Height;
use crate::core::ics24_host::identifier::ChainId;

pub fn get_dummy_tendermint_client_state(tm_header: Header) -> ClientState {
ClientState::new(
ChainId::from(tm_header.chain_id.clone()),
Default::default(),
Duration::from_secs(64000),
Duration::from_secs(128000),
Duration::from_millis(3000),
Height::new(
ChainId::chain_version(tm_header.chain_id.as_str()),
u64::from(tm_header.height),
)
.unwrap(),
Default::default(),
vec!["".to_string()],
AllowUpdate {
after_expiry: false,
after_misbehaviour: false,
},
)
.unwrap()
}
}
65 changes: 0 additions & 65 deletions crates/relayer-types/src/clients/ics07_tendermint/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,68 +146,3 @@ impl From<Header> for RawHeader {
}
}
}

#[cfg(any(test, feature = "mocks"))]
pub mod test_util {

use subtle_encoding::hex;
use tendermint::block::signed_header::SignedHeader;
use tendermint::validator::Info as ValidatorInfo;
use tendermint::validator::Set as ValidatorSet;
use tendermint::PublicKey;

use crate::clients::ics07_tendermint::header::Header;
use crate::Height;

pub fn get_dummy_tendermint_header() -> tendermint::block::Header {
serde_json::from_str::<SignedHeader>(include_str!(
"../../../tests/support/signed_header.json"
))
.unwrap()
.header
}

// TODO: This should be replaced with a ::default() or ::produce().
// The implementation of this function comprises duplicate code (code borrowed from
// `tendermint-rs` for assembling a Header).
// See https://github.com/informalsystems/tendermint-rs/issues/381.
//
// The normal flow is:
// - get the (trusted) signed header and the `trusted_validator_set` at a `trusted_height`
// - get the `signed_header` and the `validator_set` at latest height
// - build the ics07 Header
// For testing purposes this function does:
// - get the `signed_header` from a .json file
// - create the `validator_set` with a single validator that is also the proposer
// - assume a `trusted_height` of 1 and no change in the validator set since height 1,
// i.e. `trusted_validator_set` = `validator_set`
pub fn get_dummy_ics07_header() -> Header {
// Build a SignedHeader from a JSON file.
let shdr = serde_json::from_str::<SignedHeader>(include_str!(
"../../../tests/support/signed_header.json"
))
.unwrap();

// Build a set of validators.
// Below are test values inspired form `test_validator_set()` in tendermint-rs.
let v1: ValidatorInfo = ValidatorInfo::new(
PublicKey::from_raw_ed25519(
&hex::decode_upper(
"F349539C7E5EF7C49549B09C4BFC2335318AB0FE51FBFAA2433B4F13E816F4A7",
)
.unwrap(),
)
.unwrap(),
281_815_u64.try_into().unwrap(),
);

let vs = ValidatorSet::new(vec![v1.clone()], Some(v1));

Header {
signed_header: shdr,
validator_set: vs.clone(),
trusted_height: Height::new(0, 1).unwrap(),
trusted_validator_set: vs,
}
}
}
30 changes: 0 additions & 30 deletions crates/relayer-types/src/core/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@ use super::error::Error;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ClientType {
Tendermint = 1,

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

impl ClientType {
const TENDERMINT_STR: &'static str = "07-tendermint";

#[cfg_attr(not(test), allow(dead_code))]
const MOCK_STR: &'static str = "9999-mock";

/// Yields the identifier of this client type as a string
pub fn as_str(&self) -> &'static str {
match self {
Self::Tendermint => Self::TENDERMINT_STR,

#[cfg(any(test, feature = "mocks"))]
Self::Mock => Self::MOCK_STR,
}
}
}
Expand All @@ -42,9 +33,6 @@ impl core::str::FromStr for ClientType {
match s {
Self::TENDERMINT_STR => Ok(Self::Tendermint),

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

_ => Err(Error::unknown_client_type(s.to_string())),
}
}
Expand All @@ -68,16 +56,6 @@ mod tests {
}
}

#[test]
fn parse_mock_client_type() {
let client_type = ClientType::from_str("9999-mock");

match client_type {
Ok(ClientType::Mock) => (),
_ => panic!("parse failed"),
}
}

#[test]
fn parse_unknown_client_type() {
let client_type_str = "some-random-client-type";
Expand All @@ -93,14 +71,6 @@ mod tests {
}
}

#[test]
fn parse_mock_as_string_result() {
let client_type = ClientType::Mock;
let type_string = client_type.as_str();
let client_type_from_str = ClientType::from_str(type_string).unwrap();
assert_eq!(client_type_from_str, client_type);
}

#[test]
fn parse_tendermint_as_string_result() {
let client_type = ClientType::Tendermint;
Expand Down
35 changes: 0 additions & 35 deletions crates/relayer-types/src/core/ics02_client/msgs/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,3 @@ impl From<MsgCreateClient> for RawMsgCreateClient {
}
}
}

#[cfg(test)]
mod tests {

use test_log::test;

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

use crate::clients::ics07_tendermint::client_state::test_util::get_dummy_tendermint_client_state;
use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TmConsensusState;
use crate::clients::ics07_tendermint::header::test_util::get_dummy_tendermint_header;
use crate::core::ics02_client::msgs::create_client::MsgCreateClient;
use crate::test_utils::get_dummy_account_id;

#[test]
fn msg_create_client_serialization() {
let signer = get_dummy_account_id();

let tm_header = get_dummy_tendermint_header();
let tm_client_state = get_dummy_tendermint_client_state(tm_header.clone()).into();

let msg = MsgCreateClient::new(
tm_client_state,
TmConsensusState::from(tm_header).into(),
signer,
)
.unwrap();

let raw = RawMsgCreateClient::from(msg.clone());
let msg_back = MsgCreateClient::try_from(raw.clone()).unwrap();
let raw_back = RawMsgCreateClient::from(msg_back.clone());
assert_eq!(msg, msg_back);
assert_eq!(raw, raw_back);
}
}
28 changes: 0 additions & 28 deletions crates/relayer-types/src/core/ics02_client/msgs/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,3 @@ impl From<MsgUpdateClient> for RawMsgUpdateClient {
}
}
}

#[cfg(test)]
mod tests {

use test_log::test;

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

use crate::clients::ics07_tendermint::header::test_util::get_dummy_ics07_header;
use crate::core::ics02_client::msgs::MsgUpdateClient;
use crate::core::ics24_host::identifier::ClientId;
use crate::test_utils::get_dummy_account_id;

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

let header = get_dummy_ics07_header();

let msg = MsgUpdateClient::new(client_id, header.into(), signer);
let raw = RawMsgUpdateClient::from(msg.clone());
let msg_back = MsgUpdateClient::try_from(raw.clone()).unwrap();
let raw_back = RawMsgUpdateClient::from(msg_back.clone());
assert_eq!(msg, msg_back);
assert_eq!(raw, raw_back);
}
}
80 changes: 0 additions & 80 deletions crates/relayer-types/src/core/ics02_client/msgs/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,83 +112,3 @@ impl TryFrom<RawMsgUpgradeClient> for MsgUpgradeClient {
})
}
}

#[cfg(test)]
pub mod test_util {
use ibc_proto::ibc::core::client::v1::MsgUpgradeClient as RawMsgUpgradeClient;

use crate::{
core::{ics02_client::height::Height, ics24_host::identifier::ClientId},
mock::{
client_state::MockClientState, consensus_state::MockConsensusState, header::MockHeader,
},
test_utils::{get_dummy_bech32_account, get_dummy_proof},
};

use super::MsgUpgradeClient;

/// Extends the implementation with additional helper methods.
impl MsgUpgradeClient {
/// Setter for `client_id`. Amenable to chaining, since it consumes the input message.
pub fn with_client_id(self, client_id: ClientId) -> Self {
MsgUpgradeClient { client_id, ..self }
}
}

/// Returns a dummy `RawMsgUpgradeClient`, for testing only!
pub fn get_dummy_raw_msg_upgrade_client(height: Height) -> RawMsgUpgradeClient {
RawMsgUpgradeClient {
client_id: "tendermint".parse().unwrap(),
client_state: Some(MockClientState::new(MockHeader::new(height)).into()),
consensus_state: Some(MockConsensusState::new(MockHeader::new(height)).into()),
proof_upgrade_client: get_dummy_proof(),
proof_upgrade_consensus_state: get_dummy_proof(),
signer: get_dummy_bech32_account(),
}
}
}

#[cfg(test)]
mod tests {

use ibc_proto::ibc::core::client::v1::MsgUpgradeClient as RawMsgUpgradeClient;

use crate::{
core::{
ics02_client::{height::Height, msgs::upgrade_client::MsgUpgradeClient},
ics23_commitment::commitment::test_util::get_dummy_merkle_proof,
ics24_host::identifier::ClientId,
},
mock::{
client_state::MockClientState, consensus_state::MockConsensusState, header::MockHeader,
},
test_utils::get_dummy_account_id,
};

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

let height = Height::new(1, 1).unwrap();

let client_state = MockClientState::new(MockHeader::new(height));
let consensus_state = MockConsensusState::new(MockHeader::new(height));

let proof = get_dummy_merkle_proof();

let msg = MsgUpgradeClient::new(
client_id,
client_state.into(),
consensus_state.into(),
proof.clone(),
proof,
signer,
);
let raw: RawMsgUpgradeClient = RawMsgUpgradeClient::from(msg.clone());
let msg_back = MsgUpgradeClient::try_from(raw.clone()).unwrap();
let raw_back: RawMsgUpgradeClient = RawMsgUpgradeClient::from(msg_back.clone());
assert_eq!(msg, msg_back);
assert_eq!(raw, raw_back);
}
}
1 change: 0 additions & 1 deletion crates/relayer-types/src/core/ics23_commitment/mock.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/relayer-types/src/core/ics23_commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
pub mod commitment;
pub mod error;
pub mod merkle;
pub mod mock;
pub mod specs;
3 changes: 0 additions & 3 deletions crates/relayer-types/src/core/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ impl ClientId {
pub fn prefix(client_type: ClientType) -> &'static str {
match client_type {
ClientType::Tendermint => ClientType::Tendermint.as_str(),

#[cfg(any(test, feature = "mocks"))]
ClientType::Mock => ClientType::Mock.as_str(),
}
}

Expand Down
5 changes: 1 addition & 4 deletions crates/relayer-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,5 @@ pub type Height = core::ics02_client::height::Height;
#[cfg(test)]
mod test;

#[cfg(any(test, feature = "mocks"))]
#[cfg(test)]
pub mod test_utils;

#[cfg(any(test, feature = "mocks"))]
pub mod mock; // Context mock, the underlying host chain, and client types: for testing all handlers.
Loading

0 comments on commit 1edcb15

Please sign in to comment.