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 @@ -9,6 +9,6 @@ use crate::context::Context;
/// their client state and consensus state types into the generic [`Context`]
/// object.
pub trait ClientType<'a>: Sized {
type ClientState: ClientStateExecution<Context<'a, Self>> + Clone;
type ClientState: ClientStateExecution<Context<'a, Self>>;
type ConsensusState: ConsensusStateTrait + Into<Any> + TryFrom<Any, Error = ClientError>;
}
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 @@ -290,9 +290,9 @@ impl<'a, C: ClientType<'a>> Context<'a, C> {
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 @@ -133,13 +133,15 @@ impl<'a, C: ClientType<'a>> Context<'a, C> {
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;
}
9 changes: 9 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,15 @@ impl ConsensusState {
}
}

/// Can't use [`Infallible`](core::convert::Infallible) because TryFrom<Error = ClientError> is required by
/// [`ConsensusStateDecoder`](ibc_core_client::context::consensus_state::ConsensusStateDecoder).
impl TryFrom<ConsensusState> for ConsensusStateType {
type Error = ClientError;
fn try_from(value: ConsensusState) -> Result<Self, Self::Error> {
Ok(value.0)
}
}
rnbguy marked this conversation as resolved.
Show resolved Hide resolved

impl Protobuf<RawTmConsensusState> for ConsensusState {}

impl TryFrom<RawTmConsensusState> for ConsensusState {
Expand Down
Loading