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

imp(ics-07): simplify ClientType impl in cw contract #1202

Merged
merged 12 commits into from
May 3, 2024
2 changes: 1 addition & 1 deletion ibc-clients/cw-context/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ where
<Self::ClientState as TryFrom<Any>>::Error: Into<ClientError>,
<Self::ConsensusState as TryFrom<Any>>::Error: Into<ClientError>,
{
type ClientState: ClientStateExecution<Context<'a, Self>> + Clone;
type ClientState: ClientStateExecution<Context<'a, Self>>;
type ConsensusState: ConsensusStateTrait;
}
2 changes: 1 addition & 1 deletion ibc-clients/cw-context/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ where
client_state: C::ClientState,
) -> Result<Vec<u8>, ClientError> {
let wasm_client_state = WasmClientState {
data: C::ClientState::encode_to_any_vec(client_state.clone()),
checksum: self.obtain_checksum()?,
latest_height: client_state.latest_height(),
data: C::ClientState::encode_to_any_vec(client_state),
};

Ok(Any::from(wasm_client_state).encode_to_vec())
Expand Down
6 changes: 4 additions & 2 deletions ibc-clients/cw-context/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ where
substitute_client_state.latest_height().revision_height(),
))?;

let substitute_client_state_any = substitute_client_state.into();

self.set_subject_prefix();
client_state.check_substitute(self, substitute_client_state.clone().into())?;
client_state.check_substitute(self, substitute_client_state_any.clone())?;

client_state.update_on_recovery(
self,
&self.client_id(),
substitute_client_state.into(),
substitute_client_state_any,
substitute_consensus_state.into(),
)?;

Expand Down
53 changes: 1 addition & 52 deletions ibc-clients/ics07-tendermint/cw-contract/src/client_type.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,12 @@
use ibc_client_cw::api::ClientType;
use ibc_client_tendermint::client_state::ClientState;
use ibc_client_tendermint::consensus_state::ConsensusState;
use ibc_client_tendermint::types::{
ConsensusState as ConsensusStateType, TENDERMINT_CONSENSUS_STATE_TYPE_URL,
};
use ibc_core::client::types::error::ClientError;
use ibc_core::derive::ConsensusState as ConsensusStateDerive;
use ibc_core::primitives::proto::Any;

/// A unit struct that represents the Tendermint client type.
#[derive(Clone, Debug)]
pub struct TendermintClient;

impl<'a> ClientType<'a> for TendermintClient {
type ClientState = ClientState;
type ConsensusState = AnyConsensusState;
}

#[derive(Clone, Debug, ConsensusStateDerive)]
pub enum AnyConsensusState {
Tendermint(ConsensusState),
}

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

impl TryFrom<AnyConsensusState> for ConsensusStateType {
type Error = ClientError;

fn try_from(value: AnyConsensusState) -> Result<Self, Self::Error> {
match value {
AnyConsensusState::Tendermint(state) => Ok(state.into_inner()),
}
}
}

impl From<AnyConsensusState> for Any {
fn from(value: AnyConsensusState) -> Self {
match value {
AnyConsensusState::Tendermint(cs) => cs.into(),
}
}
}

impl TryFrom<Any> for AnyConsensusState {
type Error = ClientError;

fn try_from(raw: Any) -> Result<Self, Self::Error> {
match raw.type_url.as_str() {
TENDERMINT_CONSENSUS_STATE_TYPE_URL => {
let cs = ConsensusState::try_from(raw)?;
Ok(AnyConsensusState::Tendermint(cs))
}
_ => Err(ClientError::UnknownConsensusStateType {
consensus_state_type: raw.type_url,
}),
}
}
type ConsensusState = ConsensusState;
}
6 changes: 6 additions & 0 deletions ibc-clients/ics07-tendermint/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ impl ConsensusState {
}
}

impl From<ConsensusState> for ConsensusStateType {
fn from(value: ConsensusState) -> Self {
value.0
}
}

impl Protobuf<RawTmConsensusState> for ConsensusState {}

impl TryFrom<RawTmConsensusState> for ConsensusState {
Expand Down
8 changes: 8 additions & 0 deletions ibc-core/ics02-client/types/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Defines the client error type

use core::convert::Infallible;

use displaydoc::Display;
use ibc_core_commitment_types::error::CommitmentError;
use ibc_core_host_types::error::IdentifierError;
Expand Down Expand Up @@ -113,6 +115,12 @@ impl From<&'static str> for ClientError {
}
}

impl From<Infallible> for ClientError {
fn from(value: Infallible) -> Self {
match value {}
}
}

#[cfg(feature = "std")]
impl std::error::Error for ClientError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Expand Down
Loading