Skip to content

Commit

Permalink
imp(08-wasm): supersede Bytes with cosmwasm_std::Binary (#1272)
Browse files Browse the repository at this point in the history
* imp: supersede Bytes with cosmwasm_std::Binary

* chore: add unclog

* fix: keep ibc-client-wasm-types as is

* fix: use cosmwasm_std::Checksum

* fix: make clippy happy

* rm unused dependency

* rm redundant import

* fix: move unclog under improvements

---------

Co-authored-by: Ranadeep Biswas <[email protected]>
  • Loading branch information
Farhad-Shabani and rnbguy authored Jul 11, 2024
1 parent c6420c4 commit df9653a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ibc-client-cw] Supersede `Bytes` with `cosmwasm::Binary`.
([\#1271](https://github.com/cosmos/ibc-rs/issues/1271))
29 changes: 17 additions & 12 deletions ibc-clients/cw-context/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod custom_ctx;

use std::str::FromStr;

use cosmwasm_std::{Deps, DepsMut, Empty, Env, Order, Storage};
use cosmwasm_std::{Checksum, Deps, DepsMut, Empty, Env, Order, Storage};
use cw_storage_plus::{Bound, Map};
use ibc_client_wasm_types::client_state::ClientState as WasmClientState;
use ibc_core::client::context::client_state::ClientStateCommon;
Expand All @@ -21,8 +21,6 @@ use crate::api::ClientType;
use crate::types::{ContractError, GenesisMetadata, HeightTravel, MigrationPrefix};
use crate::utils::AnyCodec;

type Checksum = Vec<u8>;

/// - [`Height`] cannot be used directly as keys in the map,
/// as it doesn't implement some cw_storage specific traits.
/// - Only a sorted set is needed. So the value type is set to
Expand Down Expand Up @@ -241,13 +239,13 @@ where

let processed_height_key = self.client_update_height_key(&height);
metadata.push(GenesisMetadata {
key: processed_height_key.clone(),
value: self.retrieve(&processed_height_key)?,
key: processed_height_key.clone().into(),
value: self.retrieve(&processed_height_key)?.into(),
});
let processed_time_key = self.client_update_time_key(&height);
metadata.push(GenesisMetadata {
key: processed_time_key.clone(),
value: self.retrieve(&processed_time_key)?,
key: processed_time_key.clone().into(),
value: self.retrieve(&processed_time_key)?.into(),
});
}

Expand All @@ -265,8 +263,8 @@ where
let height = height_result?;

metadata.push(GenesisMetadata {
key: iteration_key(height.revision_number(), height.revision_height()),
value: height.encode_vec(),
key: iteration_key(height.revision_number(), height.revision_height()).into(),
value: height.encode_vec().into(),
});
}

Expand All @@ -276,7 +274,7 @@ where
/// Returns the checksum of the current contract.
pub fn obtain_checksum(&self) -> Result<Checksum, ClientError> {
match &self.checksum {
Some(checksum) => Ok(checksum.clone()),
Some(checksum) => Ok(*checksum),
None => {
let client_state_value = self.retrieve(ClientStatePath::leaf())?;

Expand All @@ -287,7 +285,14 @@ where
}
})?;

Ok(wasm_client_state.checksum)
let checksum =
Checksum::try_from(wasm_client_state.checksum.as_slice()).map_err(|e| {
ClientError::Other {
description: e.to_string(),
}
})?;

Ok(checksum)
}
}
}
Expand All @@ -298,7 +303,7 @@ where
client_state: C::ClientState,
) -> Result<Vec<u8>, ClientError> {
let wasm_client_state = WasmClientState {
checksum: self.obtain_checksum()?,
checksum: self.obtain_checksum()?.into(),
latest_height: client_state.latest_height(),
data: C::ClientState::encode_to_any_vec(client_state),
};
Expand Down
71 changes: 22 additions & 49 deletions ibc-clients/cw-context/src/types/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use std::str::FromStr;

use cosmwasm_schema::{cw_serde, QueryResponses};
use ibc_client_wasm_types::serializer::Base64;
use ibc_client_wasm_types::Bytes;
use cosmwasm_std::{Binary, Checksum};
use ibc_core::client::types::proto::v1::Height as RawHeight;
use ibc_core::client::types::Height;
use ibc_core::commitment_types::commitment::{CommitmentPrefix, CommitmentProofBytes};
Expand All @@ -20,15 +19,9 @@ use super::error::ContractError;

#[cw_serde]
pub struct InstantiateMsg {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub client_state: Bytes,
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub consensus_state: Bytes,
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub checksum: Bytes,
pub client_state: Binary,
pub consensus_state: Binary,
pub checksum: Checksum,
}

// ------------------------------------------------------------
Expand All @@ -48,9 +41,7 @@ pub enum SudoMsg {

#[cw_serde]
pub struct UpdateStateOnMisbehaviourMsgRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub client_message: Bytes,
pub client_message: Binary,
}

pub struct UpdateStateOnMisbehaviourMsg {
Expand All @@ -69,9 +60,7 @@ impl TryFrom<UpdateStateOnMisbehaviourMsgRaw> for UpdateStateOnMisbehaviourMsg {

#[cw_serde]
pub struct UpdateStateMsgRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub client_message: Bytes,
pub client_message: Binary,
}

pub struct UpdateStateMsg {
Expand All @@ -93,18 +82,10 @@ pub struct CheckSubstituteAndUpdateStateMsg {}

#[cw_serde]
pub struct VerifyUpgradeAndUpdateStateMsgRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub upgrade_client_state: Bytes,
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub upgrade_consensus_state: Bytes,
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub proof_upgrade_client: Bytes,
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub proof_upgrade_consensus_state: Bytes,
pub upgrade_client_state: Binary,
pub upgrade_consensus_state: Binary,
pub proof_upgrade_client: Binary,
pub proof_upgrade_consensus_state: Binary,
}

pub struct VerifyUpgradeAndUpdateStateMsg {
Expand All @@ -125,9 +106,11 @@ impl TryFrom<VerifyUpgradeAndUpdateStateMsgRaw> for VerifyUpgradeAndUpdateStateM
Ok(VerifyUpgradeAndUpdateStateMsg {
upgrade_client_state,
upgrade_consensus_state,
proof_upgrade_client: CommitmentProofBytes::try_from(raw.proof_upgrade_client)?,
proof_upgrade_client: CommitmentProofBytes::try_from(
raw.proof_upgrade_client.to_vec(),
)?,
proof_upgrade_consensus_state: CommitmentProofBytes::try_from(
raw.proof_upgrade_consensus_state,
raw.proof_upgrade_consensus_state.to_vec(),
)?,
})
}
Expand All @@ -140,13 +123,9 @@ pub struct MerklePath {

#[cw_serde]
pub struct VerifyMembershipMsgRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub proof: Bytes,
pub proof: Binary,
pub path: MerklePath,
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub value: Bytes,
pub value: Binary,
pub height: RawHeight,
pub delay_block_period: u64,
pub delay_time_period: u64,
Expand All @@ -166,7 +145,7 @@ impl TryFrom<VerifyMembershipMsgRaw> for VerifyMembershipMsg {
type Error = ContractError;

fn try_from(mut raw: VerifyMembershipMsgRaw) -> Result<Self, Self::Error> {
let proof = CommitmentProofBytes::try_from(raw.proof)?;
let proof = CommitmentProofBytes::try_from(raw.proof.to_vec())?;
let prefix = raw.path.key_path.remove(0).into_bytes();
let path_str = raw.path.key_path.join("");
let path = Path::from_str(&path_str)?;
Expand All @@ -175,7 +154,7 @@ impl TryFrom<VerifyMembershipMsgRaw> for VerifyMembershipMsg {
Ok(Self {
proof,
path,
value: raw.value,
value: raw.value.into(),
height,
prefix: CommitmentPrefix::try_from(prefix)?,
delay_block_period: raw.delay_block_period,
Expand All @@ -186,9 +165,7 @@ impl TryFrom<VerifyMembershipMsgRaw> for VerifyMembershipMsg {

#[cw_serde]
pub struct VerifyNonMembershipMsgRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub proof: Bytes,
pub proof: Binary,
pub path: MerklePath,
pub height: RawHeight,
pub delay_block_period: u64,
Expand All @@ -208,7 +185,7 @@ impl TryFrom<VerifyNonMembershipMsgRaw> for VerifyNonMembershipMsg {
type Error = ContractError;

fn try_from(mut raw: VerifyNonMembershipMsgRaw) -> Result<Self, Self::Error> {
let proof = CommitmentProofBytes::try_from(raw.proof)?;
let proof = CommitmentProofBytes::try_from(raw.proof.to_vec())?;
let prefix = raw.path.key_path.remove(0).into_bytes();
let path_str = raw.path.key_path.join("");
let path = Path::from_str(&path_str)?;
Expand Down Expand Up @@ -259,9 +236,7 @@ pub struct TimestampAtHeightMsg {

#[cw_serde]
pub struct VerifyClientMessageRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub client_message: Bytes,
pub client_message: Binary,
}

pub struct VerifyClientMessageMsg {
Expand All @@ -280,9 +255,7 @@ impl TryFrom<VerifyClientMessageRaw> for VerifyClientMessageMsg {

#[cw_serde]
pub struct CheckForMisbehaviourMsgRaw {
#[schemars(with = "String")]
#[serde(with = "Base64", default)]
pub client_message: Bytes,
pub client_message: Binary,
}

pub struct CheckForMisbehaviourMsg {
Expand Down
6 changes: 3 additions & 3 deletions ibc-clients/cw-context/src/types/response.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Contains the response types for the CosmWasm contract.
use cosmwasm_schema::cw_serde;
use ibc_client_wasm_types::Bytes;
use cosmwasm_std::Binary;
use ibc_core::client::types::Height;

#[cw_serde]
pub struct GenesisMetadata {
pub key: Bytes,
pub value: Bytes,
pub key: Binary,
pub value: Binary,
}

#[cw_serde]
Expand Down
1 change: 0 additions & 1 deletion tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ tendermint-testgen = { workspace = true }

[dev-dependencies]
cosmwasm-std = { workspace = true }
hex = { workspace = true }
rstest = { workspace = true }
test-log = { version = "0.2.16", features = [ "trace" ] }
tendermint-rpc = { workspace = true }
Expand Down
34 changes: 28 additions & 6 deletions tests-integration/tests/cosmwasm/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ impl Fixture {
let tm_consensus_state = dummy_sov_consensus_state(self.trusted_timestamp);

InstantiateMsg {
client_state: TmClientState::encode_to_any_vec(tm_client_state),
consensus_state: TmConsensusState::encode_to_any_vec(tm_consensus_state),
client_state: TmClientState::encode_to_any_vec(tm_client_state).into(),
consensus_state: TmConsensusState::encode_to_any_vec(tm_consensus_state).into(),
checksum: dummy_checksum(),
}
}
Expand Down Expand Up @@ -139,7 +139,13 @@ impl Fixture {

pub fn verify_client_message(&self, deps: Deps<'_>, client_message: Vec<u8>) {
let resp = self
.query(deps, VerifyClientMessageRaw { client_message }.into())
.query(
deps,
VerifyClientMessageRaw {
client_message: client_message.into(),
}
.into(),
)
.unwrap();

assert!(resp.is_valid);
Expand All @@ -149,7 +155,13 @@ impl Fixture {

pub fn check_for_misbehaviour(&self, deps: Deps<'_>, client_message: Vec<u8>) {
let resp = self
.query(deps, CheckForMisbehaviourMsgRaw { client_message }.into())
.query(
deps,
CheckForMisbehaviourMsgRaw {
client_message: client_message.into(),
}
.into(),
)
.unwrap();

assert!(resp.is_valid);
Expand Down Expand Up @@ -199,7 +211,12 @@ impl Fixture {

let mut ctx = self.ctx_mut(deps_mut);

let data = ctx.sudo(UpdateStateMsgRaw { client_message }.into())?;
let data = ctx.sudo(
UpdateStateMsgRaw {
client_message: client_message.into(),
}
.into(),
)?;

Ok(Response::default().set_data(data))
}
Expand All @@ -212,7 +229,12 @@ impl Fixture {
let mut ctx = self.ctx_mut(deps_mut);

let data = ctx
.sudo(UpdateStateOnMisbehaviourMsgRaw { client_message }.into())
.sudo(
UpdateStateOnMisbehaviourMsgRaw {
client_message: client_message.into(),
}
.into(),
)
.unwrap();

Response::default().set_data(data)
Expand Down
6 changes: 3 additions & 3 deletions tests-integration/tests/cosmwasm/helper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
use cosmwasm_std::{coins, Env, MessageInfo, Timestamp as CwTimestamp};
use cosmwasm_std::{coins, Checksum, Env, MessageInfo, Timestamp as CwTimestamp};
use ibc::clients::tendermint::types::ConsensusState;
use ibc::core::primitives::Timestamp as IbcTimestamp;
use tendermint::Hash;
Expand All @@ -13,8 +13,8 @@ pub fn dummy_msg_info() -> MessageInfo {
message_info(&creator, &coins(1000, "ibc"))
}

pub fn dummy_checksum() -> Vec<u8> {
hex::decode("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6")
pub fn dummy_checksum() -> Checksum {
Checksum::from_hex("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6")
.expect("Never fails")
}

Expand Down
2 changes: 1 addition & 1 deletion tests-integration/tests/cosmwasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn test_cw_client_expiry() {
let resp = fxt.query(
deps.as_ref(),
VerifyClientMessageRaw {
client_message: client_message.clone(),
client_message: client_message.into(),
}
.into(),
);
Expand Down

0 comments on commit df9653a

Please sign in to comment.