From a2ab39707701748ed2f3f6dd62c48e761664eff3 Mon Sep 17 00:00:00 2001 From: dhruvja Date: Fri, 13 Oct 2023 22:38:14 +0530 Subject: [PATCH] added mocks feature --- .../solana-ibc/programs/solana-ibc/Cargo.toml | 1 + .../programs/solana-ibc/src/client_state.rs | 30 ++++++++++++++++--- .../solana-ibc/src/consensus_state.rs | 22 +++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/Cargo.toml b/solana/solana-ibc/programs/solana-ibc/Cargo.toml index 6714b97d..bb4690d7 100644 --- a/solana/solana-ibc/programs/solana-ibc/Cargo.toml +++ b/solana/solana-ibc/programs/solana-ibc/Cargo.toml @@ -13,6 +13,7 @@ no-entrypoint = [] no-idl = [] no-log-ix-name = [] cpi = ["no-entrypoint"] +mocks = [] [dependencies] anchor-lang.workspace = true diff --git a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs index 53f04b59..b1e42bc6 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs @@ -12,24 +12,29 @@ use ibc::core::ics24_host::identifier::ClientId; use ibc::core::ics24_host::path::{ClientConsensusStatePath, Path}; use ibc::core::timestamp::Timestamp; use ibc::core::{ContextError, ValidationContext}; -use ibc::mock::client_state::{ - MockClientContext, MockClientState, MOCK_CLIENT_STATE_TYPE_URL, -}; + use ibc::{Any, Height}; use ibc_proto::ibc::lightclients::tendermint::v1::ClientState as RawTmClientState; -use ibc_proto::ibc::mock::ClientState as RawMockClientState; use ibc_proto::protobuf::Protobuf; use serde::{Deserialize, Serialize}; use super::consensus_state::AnyConsensusState; use crate::SolanaIbcStorage; +#[cfg(any(test, feature = "mocks"))] +use ibc_proto::ibc::mock::ClientState as RawMockClientState; +#[cfg(any(test, feature = "mocks"))] +use ibc::mock::client_state::{ + MockClientContext, MockClientState, MOCK_CLIENT_STATE_TYPE_URL, +}; + const TENDERMINT_CLIENT_STATE_TYPE_URL: &str = "/ibc.lightclients.tendermint.v1.ClientState"; #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub enum AnyClientState { Tendermint(TmClientState), + #[cfg(any(test, feature = "mocks"))] Mock(MockClientState), } @@ -47,6 +52,7 @@ impl TryFrom for AnyClientState { }, )?, )), + #[cfg(any(test, feature = "mocks"))] MOCK_CLIENT_STATE_TYPE_URL => Ok(AnyClientState::Mock( Protobuf::::decode_vec(&raw.value) .map_err(|e| ClientError::ClientSpecific { @@ -67,6 +73,7 @@ impl From for Any { type_url: TENDERMINT_CLIENT_STATE_TYPE_URL.to_string(), value: Protobuf::::encode_vec(&client_state), }, + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(mock_client_state) => Any { type_url: MOCK_CLIENT_STATE_TYPE_URL.to_string(), value: Protobuf::::encode_vec( @@ -93,6 +100,7 @@ impl ClientStateValidation for AnyClientState { client_message, update_kind, ), + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(mock_client_state) => mock_client_state .verify_client_message( ctx, @@ -118,6 +126,7 @@ impl ClientStateValidation for AnyClientState { client_message, update_kind, ), + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(mock_client_state) => mock_client_state .check_for_misbehaviour( ctx, @@ -147,6 +156,7 @@ impl ClientStateCommon for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.verify_consensus_state(consensus_state) } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(mock_client_state) => { mock_client_state.verify_consensus_state(consensus_state) } @@ -158,6 +168,7 @@ impl ClientStateCommon for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.client_type() } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(mock_client_state) => { mock_client_state.client_type() } @@ -170,6 +181,7 @@ impl ClientStateCommon for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.latest_height() } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(mock_client_state) => { msg!( "This is latest height {:?}", @@ -190,6 +202,7 @@ impl ClientStateCommon for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.validate_proof_height(proof_height) } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => { client_state.validate_proof_height(proof_height) } @@ -213,6 +226,7 @@ impl ClientStateCommon for AnyClientState { proof_upgrade_consensus_state, root, ), + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => client_state .verify_upgrade_client( upgraded_client_state, @@ -236,6 +250,7 @@ impl ClientStateCommon for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.verify_membership(prefix, proof, root, path, value) } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => { client_state.verify_membership(prefix, proof, root, path, value) } @@ -253,6 +268,7 @@ impl ClientStateCommon for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.verify_non_membership(prefix, proof, root, path) } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => { client_state.verify_non_membership(prefix, proof, root, path) } @@ -264,6 +280,7 @@ impl From for AnyClientState { fn from(value: TmClientState) -> Self { AnyClientState::Tendermint(value) } } +#[cfg(any(test, feature = "mocks"))] impl From for AnyClientState { fn from(value: MockClientState) -> Self { AnyClientState::Mock(value) } } @@ -279,6 +296,7 @@ impl ClientStateExecution for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.initialise(ctx, client_id, consensus_state) } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => { client_state.initialise(ctx, client_id, consensus_state) } @@ -295,6 +313,7 @@ impl ClientStateExecution for AnyClientState { AnyClientState::Tendermint(client_state) => { client_state.update_state(ctx, client_id, header) } + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => { client_state.update_state(ctx, client_id, header) } @@ -316,6 +335,7 @@ impl ClientStateExecution for AnyClientState { client_message, update_kind, ), + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => client_state .update_state_on_misbehaviour( ctx, @@ -341,6 +361,7 @@ impl ClientStateExecution for AnyClientState { upgraded_client_state, upgraded_consensus_state, ), + #[cfg(any(test, feature = "mocks"))] AnyClientState::Mock(client_state) => client_state .update_state_on_upgrade( ctx, @@ -365,6 +386,7 @@ impl ibc::clients::ics07_tendermint::CommonContext for SolanaIbcStorage { } } +#[cfg(any(test, feature = "mocks"))] impl MockClientContext for SolanaIbcStorage { type ConversionError = ClientError; type AnyConsensusState = AnyConsensusState; diff --git a/solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs b/solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs index 06d1d59c..545efbf7 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs @@ -3,15 +3,20 @@ use ibc::core::ics02_client::consensus_state::ConsensusState; use ibc::core::ics02_client::error::ClientError; use ibc::core::ics23_commitment::commitment::CommitmentRoot; use ibc::core::timestamp::Timestamp; -use ibc::mock::consensus_state::{ - MockConsensusState, MOCK_CONSENSUS_STATE_TYPE_URL, -}; + use ibc_proto::google::protobuf::Any; use ibc_proto::ibc::lightclients::tendermint::v1::ConsensusState as RawTmConsensusState; -use ibc_proto::ibc::mock::ConsensusState as RawMockConsensusState; use ibc_proto::protobuf::Protobuf; use serde::{Deserialize, Serialize}; + +#[cfg(any(test, feature = "mocks"))] +use ibc::mock::consensus_state::{ + MockConsensusState, MOCK_CONSENSUS_STATE_TYPE_URL, +}; +#[cfg(any(test, feature = "mocks"))] +use ibc_proto::ibc::mock::ConsensusState as RawMockConsensusState; + const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.lightclients.tendermint.v1.ConsensusState"; @@ -19,6 +24,7 @@ const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str = #[serde(tag = "type")] pub enum AnyConsensusState { Tendermint(TmConsensusState), + #[cfg(any(test, feature = "mocks"))] Mock(MockConsensusState), } @@ -37,6 +43,7 @@ impl TryFrom for AnyConsensusState { })?, )) } + #[cfg(any(test, feature = "mocks"))] MOCK_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Mock( Protobuf::::decode_vec(&value.value) .map_err(|e| ClientError::ClientSpecific { @@ -57,6 +64,7 @@ impl From for Any { type_url: TENDERMINT_CONSENSUS_STATE_TYPE_URL.to_string(), value: Protobuf::::encode_vec(&value), }, + #[cfg(any(test, feature = "mocks"))] AnyConsensusState::Mock(value) => Any { type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(), value: Protobuf::::encode_vec(&value), @@ -71,6 +79,7 @@ impl From for AnyConsensusState { } } +#[cfg(any(test, feature = "mocks"))] impl From for AnyConsensusState { fn from(value: MockConsensusState) -> Self { AnyConsensusState::Mock(value) @@ -81,6 +90,7 @@ impl ConsensusState for AnyConsensusState { fn root(&self) -> &CommitmentRoot { match self { AnyConsensusState::Tendermint(value) => value.root(), + #[cfg(any(test, feature = "mocks"))] AnyConsensusState::Mock(value) => value.root(), } } @@ -88,6 +98,7 @@ impl ConsensusState for AnyConsensusState { fn timestamp(&self) -> Timestamp { match self { AnyConsensusState::Tendermint(value) => value.timestamp(), + #[cfg(any(test, feature = "mocks"))] AnyConsensusState::Mock(value) => value.timestamp(), } } @@ -97,6 +108,7 @@ impl ConsensusState for AnyConsensusState { AnyConsensusState::Tendermint(value) => { ibc::core::ics02_client::consensus_state::ConsensusState::encode_vec(value) }, + #[cfg(any(test, feature = "mocks"))] AnyConsensusState::Mock(value) => { ibc::core::ics02_client::consensus_state::ConsensusState::encode_vec(value) } @@ -117,6 +129,7 @@ impl TryInto > { match self { AnyConsensusState::Tendermint(value) => Ok(value), + #[cfg(any(test, feature = "mocks"))] AnyConsensusState::Mock(_) => Err(ClientError::Other { description: "Cannot convert mock consensus state to \ tendermint" @@ -126,6 +139,7 @@ impl TryInto } } +#[cfg(any(test, feature = "mocks"))] impl TryInto for AnyConsensusState {