Skip to content

Commit

Permalink
solana-ibc: impl From/TryFrom for client and consensus state via deri…
Browse files Browse the repository at this point in the history
…ve_more (#94)
  • Loading branch information
mina86 authored Nov 16, 2023
1 parent ea245b4 commit 787efde
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 70 deletions.
15 changes: 3 additions & 12 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use ibc_proto::protobuf::Protobuf;
use crate::consensus_state::AnyConsensusState;
use crate::storage::IbcStorage;

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, derive_more::From)]
pub enum AnyClientState {
Tendermint(TmClientState),
#[cfg(any(test, feature = "mocks"))]
Expand Down Expand Up @@ -353,15 +353,6 @@ impl ClientStateCommon for AnyClientState {
}
}

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) }
}

impl ClientStateExecution<IbcStorage<'_, '_>> for AnyClientState {
fn initialise(
&self,
Expand Down Expand Up @@ -451,7 +442,7 @@ impl ClientStateExecution<IbcStorage<'_, '_>> for AnyClientState {
}

impl ibc::clients::ics07_tendermint::CommonContext for IbcStorage<'_, '_> {
type ConversionError = ClientError;
type ConversionError = &'static str;

type AnyConsensusState = AnyConsensusState;

Expand Down Expand Up @@ -489,7 +480,7 @@ impl ibc::clients::ics07_tendermint::CommonContext for IbcStorage<'_, '_> {

#[cfg(any(test, feature = "mocks"))]
impl MockClientContext for IbcStorage<'_, '_> {
type ConversionError = ClientError;
type ConversionError = &'static str;
type AnyConsensusState = AnyConsensusState;

fn consensus_state(
Expand Down
67 changes: 9 additions & 58 deletions solana/solana-ibc/programs/solana-ibc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ use serde::{Deserialize, Serialize};
const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =
"/ibc.lightclients.tendermint.v1.ConsensusState";

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(
Clone,
Debug,
PartialEq,
Serialize,
Deserialize,
derive_more::From,
derive_more::TryInto,
)]
#[serde(tag = "type")]
pub enum AnyConsensusState {
Tendermint(TmConsensusState),
Expand Down Expand Up @@ -70,19 +78,6 @@ impl From<AnyConsensusState> for Any {
}
}

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

#[cfg(any(test, feature = "mocks"))]
impl From<MockConsensusState> for AnyConsensusState {
fn from(value: MockConsensusState) -> Self {
AnyConsensusState::Mock(value)
}
}

impl ConsensusState for AnyConsensusState {
fn root(&self) -> &CommitmentRoot {
match self {
Expand Down Expand Up @@ -112,47 +107,3 @@ impl ConsensusState for AnyConsensusState {
}
}
}

impl TryInto<ibc::clients::ics07_tendermint::consensus_state::ConsensusState>
for AnyConsensusState
{
type Error = ClientError;

fn try_into(
self,
) -> Result<
ibc::clients::ics07_tendermint::consensus_state::ConsensusState,
Self::Error,
> {
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"
.to_string(),
}),
}
}
}

#[cfg(any(test, feature = "mocks"))]
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(),
}),
}
}
}

0 comments on commit 787efde

Please sign in to comment.