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

added mock client and consensus state #27

Merged
merged 16 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 2 additions & 2 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
seeds = false
skip-lint = false
[programs.localnet]
solana_ibc = "7MEuaEwNMsjVCJy9N31ZgvQf1dFkRNXYFREaAjMsoE5g"
solana_ibc = "EnfDJsAK7BGgetnmKzBx86CsgC5kfSPcsktFCQ4YLC81"
[programs.devnet]
solana_ibc = "7MEuaEwNMsjVCJy9N31ZgvQf1dFkRNXYFREaAjMsoE5g"
solana_ibc = "EnfDJsAK7BGgetnmKzBx86CsgC5kfSPcsktFCQ4YLC81"

[registry]
url = "https://api.apr.dev"
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ base64 = { version = "0.21", default-features = false, features = ["alloc"] }
bincode = "1.3.3"
borsh = { version = "0.10.3", default-features = false }
derive_more = "0.99.17"
ibc = { version = "0.45.0", default-features = false, features = ["serde"] }
ibc = { version = "0.45.0", default-features = false, features = ["serde", "mocks", "std"] }
ibc-proto = { version = "0.35.0", default-features = false, features = ["serde"] }
pretty_assertions = "1.4.0"
rand = { version = "0.8.5" }
Expand Down
109 changes: 107 additions & 2 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anchor_lang::solana_program::msg;
use ibc::clients::ics07_tendermint::client_state::ClientState as TmClientState;
use ibc::core::ics02_client::client_state::{
ClientStateCommon, ClientStateExecution, ClientStateValidation, UpdateKind,
Expand All @@ -11,8 +12,12 @@ 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};

Expand All @@ -25,6 +30,7 @@ const TENDERMINT_CLIENT_STATE_TYPE_URL: &str =
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum AnyClientState {
Tendermint(TmClientState),
Mock(MockClientState),
}

impl Protobuf<Any> for AnyClientState {}
Expand All @@ -41,6 +47,12 @@ impl TryFrom<Any> for AnyClientState {
},
)?,
)),
MOCK_CLIENT_STATE_TYPE_URL => Ok(AnyClientState::Mock(
Protobuf::<RawMockClientState>::decode_vec(&raw.value)
.map_err(|e| ClientError::ClientSpecific {
description: e.to_string(),
})?,
)),
_ => Err(ClientError::UnknownClientStateType {
client_state_type: raw.type_url,
}),
Expand All @@ -55,6 +67,12 @@ impl From<AnyClientState> for Any {
type_url: TENDERMINT_CLIENT_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawTmClientState>::encode_vec(&client_state),
},
AnyClientState::Mock(mock_client_state) => Any {
type_url: MOCK_CLIENT_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawMockClientState>::encode_vec(
&mock_client_state,
),
},
}
}
}
Expand All @@ -75,6 +93,13 @@ impl ClientStateValidation<SolanaIbcStorage> for AnyClientState {
client_message,
update_kind,
),
AnyClientState::Mock(mock_client_state) => mock_client_state
.verify_client_message(
ctx,
client_id,
client_message,
update_kind,
),
}
}

Expand All @@ -93,6 +118,13 @@ impl ClientStateValidation<SolanaIbcStorage> for AnyClientState {
client_message,
update_kind,
),
AnyClientState::Mock(mock_client_state) => mock_client_state
.check_for_misbehaviour(
ctx,
client_id,
client_message,
update_kind,
),
}
}

Expand All @@ -115,6 +147,9 @@ impl ClientStateCommon for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.verify_consensus_state(consensus_state)
}
AnyClientState::Mock(mock_client_state) => {
mock_client_state.verify_consensus_state(consensus_state)
}
}
}

Expand All @@ -123,15 +158,28 @@ impl ClientStateCommon for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.client_type()
}
AnyClientState::Mock(mock_client_state) => {
mock_client_state.client_type()
}
}
}

fn latest_height(&self) -> Height {
match self {
msg!("Fetching the height");
let height = match self {
AnyClientState::Tendermint(client_state) => {
client_state.latest_height()
}
}
AnyClientState::Mock(mock_client_state) => {
msg!(
"This is latest height {:?}",
mock_client_state.latest_height()
);
mock_client_state.latest_height()
}
};
msg!("This was the height {}", height);
height
}

fn validate_proof_height(
Expand All @@ -142,6 +190,9 @@ impl ClientStateCommon for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.validate_proof_height(proof_height)
}
AnyClientState::Mock(client_state) => {
client_state.validate_proof_height(proof_height)
}
}
}

Expand All @@ -162,6 +213,14 @@ impl ClientStateCommon for AnyClientState {
proof_upgrade_consensus_state,
root,
),
AnyClientState::Mock(client_state) => client_state
.verify_upgrade_client(
upgraded_client_state,
upgraded_consensus_state,
proof_upgrade_client,
proof_upgrade_consensus_state,
root,
),
}
}

Expand All @@ -177,6 +236,9 @@ impl ClientStateCommon for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.verify_membership(prefix, proof, root, path, value)
}
AnyClientState::Mock(client_state) => {
client_state.verify_membership(prefix, proof, root, path, value)
}
}
}

Expand All @@ -191,6 +253,9 @@ impl ClientStateCommon for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.verify_non_membership(prefix, proof, root, path)
}
AnyClientState::Mock(client_state) => {
client_state.verify_non_membership(prefix, proof, root, path)
}
}
}
}
Expand All @@ -199,6 +264,10 @@ impl From<TmClientState> for AnyClientState {
fn from(value: TmClientState) -> Self { AnyClientState::Tendermint(value) }
}

impl From<MockClientState> for AnyClientState {
fn from(value: MockClientState) -> Self { AnyClientState::Mock(value) }
}

impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
fn initialise(
&self,
Expand All @@ -210,6 +279,9 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.initialise(ctx, client_id, consensus_state)
}
AnyClientState::Mock(client_state) => {
client_state.initialise(ctx, client_id, consensus_state)
}
}
}

Expand All @@ -223,6 +295,9 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
AnyClientState::Tendermint(client_state) => {
client_state.update_state(ctx, client_id, header)
}
AnyClientState::Mock(client_state) => {
client_state.update_state(ctx, client_id, header)
}
}
}

Expand All @@ -241,6 +316,13 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
client_message,
update_kind,
),
AnyClientState::Mock(client_state) => client_state
.update_state_on_misbehaviour(
ctx,
client_id,
client_message,
update_kind,
),
}
}

Expand All @@ -259,6 +341,13 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
upgraded_client_state,
upgraded_consensus_state,
),
AnyClientState::Mock(client_state) => client_state
.update_state_on_upgrade(
ctx,
client_id,
upgraded_client_state,
upgraded_consensus_state,
),
}
}
}
Expand All @@ -276,6 +365,22 @@ impl ibc::clients::ics07_tendermint::CommonContext for SolanaIbcStorage {
}
}

impl MockClientContext for SolanaIbcStorage {
type ConversionError = ClientError;
type AnyConsensusState = AnyConsensusState;

fn consensus_state(
&self,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Self::AnyConsensusState, ContextError> {
ValidationContext::consensus_state(self, client_cons_state_path)
}

fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
ValidationContext::host_timestamp(self)
}
}

impl ibc::clients::ics07_tendermint::ValidationContext for SolanaIbcStorage {
fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
ValidationContext::host_timestamp(self)
Expand Down
51 changes: 51 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ 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};

Expand All @@ -15,6 +19,7 @@ const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =
#[serde(tag = "type")]
pub enum AnyConsensusState {
Tendermint(TmConsensusState),
Mock(MockConsensusState),
}

impl Protobuf<Any> for AnyConsensusState {}
Expand All @@ -32,6 +37,12 @@ impl TryFrom<Any> for AnyConsensusState {
})?,
))
}
MOCK_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Mock(
Protobuf::<RawMockConsensusState>::decode_vec(&value.value)
.map_err(|e| ClientError::ClientSpecific {
description: e.to_string(),
})?,
)),
_ => Err(ClientError::UnknownConsensusStateType {
consensus_state_type: value.type_url.clone(),
}),
Expand All @@ -46,6 +57,10 @@ impl From<AnyConsensusState> for Any {
type_url: TENDERMINT_CONSENSUS_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawTmConsensusState>::encode_vec(&value),
},
AnyConsensusState::Mock(value) => Any {
type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawMockConsensusState>::encode_vec(&value),
},
}
}
}
Expand All @@ -56,23 +71,34 @@ impl From<TmConsensusState> for AnyConsensusState {
}
}

impl From<MockConsensusState> for AnyConsensusState {
fn from(value: MockConsensusState) -> Self {
AnyConsensusState::Mock(value)
}
}

impl ConsensusState for AnyConsensusState {
fn root(&self) -> &CommitmentRoot {
match self {
AnyConsensusState::Tendermint(value) => value.root(),
AnyConsensusState::Mock(value) => value.root(),
}
}

fn timestamp(&self) -> Timestamp {
match self {
AnyConsensusState::Tendermint(value) => value.timestamp(),
AnyConsensusState::Mock(value) => value.timestamp(),
}
}

fn encode_vec(&self) -> Vec<u8> {
match self {
AnyConsensusState::Tendermint(value) => {
ibc::core::ics02_client::consensus_state::ConsensusState::encode_vec(value)
},
AnyConsensusState::Mock(value) => {
ibc::core::ics02_client::consensus_state::ConsensusState::encode_vec(value)
}
}
}
Expand All @@ -91,6 +117,31 @@ impl TryInto<ibc::clients::ics07_tendermint::consensus_state::ConsensusState>
> {
match self {
AnyConsensusState::Tendermint(value) => Ok(value),
AnyConsensusState::Mock(_) => Err(ClientError::Other {
description: "Cannot convert mock consensus state to \
tendermint"
.to_string(),
}),
}
}
}

impl TryInto<ibc::mock::consensus_state::MockConsensusState>
for AnyConsensusState
{
type Error = ClientError;

fn try_into(
self,
) -> Result<ibc::mock::consensus_state::MockConsensusState, Self::Error>
{
match self {
AnyConsensusState::Mock(value) => Ok(value),
AnyConsensusState::Tendermint(_) => Err(ClientError::Other {
description: "Cannot convert tendermint consensus state to \
mock"
.to_string(),
}),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ClientExecutionContext for SolanaIbcStorage {
msg!(
"store_client_state - path: {}, client_state: {:?}",
client_state_path,
client_state
client_state,
);
let client_state_key = client_state_path.0.to_string();
let serialized_client_state =
Expand Down Expand Up @@ -84,6 +84,7 @@ impl ExecutionContext for SolanaIbcStorage {
height: Height,
timestamp: Timestamp,
) -> Result {
msg!("I am here inside update time");
msg!(
"store_update_time - client_id: {}, height: {}, timestamp: {}",
client_id,
Expand Down
Loading
Loading