Skip to content

Commit

Permalink
added mocks feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvja committed Oct 13, 2023
1 parent c733f27 commit a2ab397
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
mocks = []

[dependencies]
anchor-lang.workspace = true
Expand Down
30 changes: 26 additions & 4 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand All @@ -47,6 +52,7 @@ impl TryFrom<Any> for AnyClientState {
},
)?,
)),
#[cfg(any(test, feature = "mocks"))]
MOCK_CLIENT_STATE_TYPE_URL => Ok(AnyClientState::Mock(
Protobuf::<RawMockClientState>::decode_vec(&raw.value)
.map_err(|e| ClientError::ClientSpecific {
Expand All @@ -67,6 +73,7 @@ impl From<AnyClientState> for Any {
type_url: TENDERMINT_CLIENT_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawTmClientState>::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::<RawMockClientState>::encode_vec(
Expand All @@ -93,6 +100,7 @@ impl ClientStateValidation<SolanaIbcStorage> for AnyClientState {
client_message,
update_kind,
),
#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(mock_client_state) => mock_client_state
.verify_client_message(
ctx,
Expand All @@ -118,6 +126,7 @@ impl ClientStateValidation<SolanaIbcStorage> for AnyClientState {
client_message,
update_kind,
),
#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(mock_client_state) => mock_client_state
.check_for_misbehaviour(
ctx,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()
}
Expand All @@ -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 {:?}",
Expand All @@ -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)
}
Expand All @@ -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,
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -264,6 +280,7 @@ impl From<TmClientState> for AnyClientState {
fn from(value: TmClientState) -> Self { AnyClientState::Tendermint(value) }
}

#[cfg(any(test, feature = "mocks"))]
impl From<MockClientState> for AnyClientState {
fn from(value: MockClientState) -> Self { AnyClientState::Mock(value) }
}
Expand All @@ -279,6 +296,7 @@ impl ClientStateExecution<SolanaIbcStorage> 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)
}
Expand All @@ -295,6 +313,7 @@ impl ClientStateExecution<SolanaIbcStorage> 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)
}
Expand All @@ -316,6 +335,7 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
client_message,
update_kind,
),
#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(client_state) => client_state
.update_state_on_misbehaviour(
ctx,
Expand All @@ -341,6 +361,7 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
upgraded_client_state,
upgraded_consensus_state,
),
#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(client_state) => client_state
.update_state_on_upgrade(
ctx,
Expand All @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ 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";

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AnyConsensusState {
Tendermint(TmConsensusState),
#[cfg(any(test, feature = "mocks"))]
Mock(MockConsensusState),
}

Expand All @@ -37,6 +43,7 @@ impl TryFrom<Any> for AnyConsensusState {
})?,
))
}
#[cfg(any(test, feature = "mocks"))]
MOCK_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Mock(
Protobuf::<RawMockConsensusState>::decode_vec(&value.value)
.map_err(|e| ClientError::ClientSpecific {
Expand All @@ -57,6 +64,7 @@ impl From<AnyConsensusState> for Any {
type_url: TENDERMINT_CONSENSUS_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawTmConsensusState>::encode_vec(&value),
},
#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(value) => Any {
type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawMockConsensusState>::encode_vec(&value),
Expand All @@ -71,6 +79,7 @@ impl From<TmConsensusState> for AnyConsensusState {
}
}

#[cfg(any(test, feature = "mocks"))]
impl From<MockConsensusState> for AnyConsensusState {
fn from(value: MockConsensusState) -> Self {
AnyConsensusState::Mock(value)
Expand All @@ -81,13 +90,15 @@ 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(),
}
}

fn timestamp(&self) -> Timestamp {
match self {
AnyConsensusState::Tendermint(value) => value.timestamp(),
#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(value) => value.timestamp(),
}
}
Expand All @@ -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)
}
Expand All @@ -117,6 +129,7 @@ impl TryInto<ibc::clients::ics07_tendermint::consensus_state::ConsensusState>
> {
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"
Expand All @@ -126,6 +139,7 @@ impl TryInto<ibc::clients::ics07_tendermint::consensus_state::ConsensusState>
}
}

#[cfg(any(test, feature = "mocks"))]
impl TryInto<ibc::mock::consensus_state::MockConsensusState>
for AnyConsensusState
{
Expand Down

0 comments on commit a2ab397

Please sign in to comment.