Skip to content

Commit

Permalink
imp(ics-07): simplify ClientType impl in cw contract (#1202)
Browse files Browse the repository at this point in the history
* impl TryFrom

* simplify Tendermint ClientType impl

* add comment

* rm Clone bound

* Implement From<ConsensusState>

* Remove unneeded dependency

* Format cargo.toml file

* nit

* update gh workflow titles

---------

Signed-off-by: Rano | Ranadeep <[email protected]>
Co-authored-by: Sean Chen <[email protected]>
  • Loading branch information
rnbguy and seanchen1991 authored May 3, 2024
1 parent 534a465 commit 4ea4dcb
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/upload-cw-clients.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Upload CosmWasm clients as Github workflow artifact
name: Upload precompiled CosmWasm clients
on:
pull_request:
paths:
Expand Down Expand Up @@ -36,7 +36,7 @@ concurrency:

jobs:
upload-tendermint-cw-client:
name: Compile and upload Tendermint CosmWasm client
name: Upload precompiled Tendermint CosmWasm client
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
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;
}
12 changes: 2 additions & 10 deletions ibc-clients/ics07-tendermint/cw-contract/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use cosmwasm_std::{
entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
};
use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Response};
use ibc_client_cw::context::Context;
use ibc_client_cw::types::{ContractError, InstantiateMsg, QueryMsg, SudoMsg};

Expand All @@ -16,25 +14,19 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let mut ctx = TendermintContext::new_mut(deps, env)?;

let data = ctx.instantiate(msg)?;

Ok(Response::default().set_data(data))
}

#[entry_point]
pub fn sudo(deps: DepsMut<'_>, env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
let mut ctx = TendermintContext::new_mut(deps, env)?;

let data = ctx.sudo(msg)?;

Ok(Response::default().set_data(data))
}

#[entry_point]
pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
let ctx = TendermintContext::new_ref(deps, env)?;

ctx.query(msg)
.map_err(|e| StdError::generic_err(e.to_string()))
}
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

0 comments on commit 4ea4dcb

Please sign in to comment.