diff --git a/.changelog/unreleased/improvements/1271-supersede-Bytes-with-cosmwasm-Binary.md b/.changelog/unreleased/improvements/1271-supersede-Bytes-with-cosmwasm-Binary.md new file mode 100644 index 000000000..1e27e8ba1 --- /dev/null +++ b/.changelog/unreleased/improvements/1271-supersede-Bytes-with-cosmwasm-Binary.md @@ -0,0 +1,2 @@ +- [ibc-client-cw] Supersede `Bytes` with `cosmwasm::Binary`. + ([\#1271](https://github.com/cosmos/ibc-rs/issues/1271)) diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index 70e534fe6..096115970 100644 --- a/ibc-clients/cw-context/src/context/mod.rs +++ b/ibc-clients/cw-context/src/context/mod.rs @@ -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; @@ -21,8 +21,6 @@ use crate::api::ClientType; use crate::types::{ContractError, GenesisMetadata, HeightTravel, MigrationPrefix}; use crate::utils::AnyCodec; -type Checksum = Vec; - /// - [`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 @@ -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(), }); } @@ -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(), }); } @@ -276,7 +274,7 @@ where /// Returns the checksum of the current contract. pub fn obtain_checksum(&self) -> Result { match &self.checksum { - Some(checksum) => Ok(checksum.clone()), + Some(checksum) => Ok(*checksum), None => { let client_state_value = self.retrieve(ClientStatePath::leaf())?; @@ -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) } } } @@ -298,7 +303,7 @@ where client_state: C::ClientState, ) -> Result, 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), }; diff --git a/ibc-clients/cw-context/src/types/msgs.rs b/ibc-clients/cw-context/src/types/msgs.rs index 506d65695..e5647ee4c 100644 --- a/ibc-clients/cw-context/src/types/msgs.rs +++ b/ibc-clients/cw-context/src/types/msgs.rs @@ -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}; @@ -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, } // ------------------------------------------------------------ @@ -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 { @@ -69,9 +60,7 @@ impl TryFrom 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 { @@ -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 { @@ -125,9 +106,11 @@ impl TryFrom 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(), )?, }) } @@ -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, @@ -166,7 +145,7 @@ impl TryFrom for VerifyMembershipMsg { type Error = ContractError; fn try_from(mut raw: VerifyMembershipMsgRaw) -> Result { - 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)?; @@ -175,7 +154,7 @@ impl TryFrom 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, @@ -186,9 +165,7 @@ impl TryFrom 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, @@ -208,7 +185,7 @@ impl TryFrom for VerifyNonMembershipMsg { type Error = ContractError; fn try_from(mut raw: VerifyNonMembershipMsgRaw) -> Result { - 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)?; @@ -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 { @@ -280,9 +255,7 @@ impl TryFrom 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 { diff --git a/ibc-clients/cw-context/src/types/response.rs b/ibc-clients/cw-context/src/types/response.rs index 4aebdbe63..804585877 100644 --- a/ibc-clients/cw-context/src/types/response.rs +++ b/ibc-clients/cw-context/src/types/response.rs @@ -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] diff --git a/tests-integration/Cargo.toml b/tests-integration/Cargo.toml index 8c3c675d6..cb4a4c7e4 100644 --- a/tests-integration/Cargo.toml +++ b/tests-integration/Cargo.toml @@ -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 } diff --git a/tests-integration/tests/cosmwasm/fixture.rs b/tests-integration/tests/cosmwasm/fixture.rs index 8e8403342..3cc187167 100644 --- a/tests-integration/tests/cosmwasm/fixture.rs +++ b/tests-integration/tests/cosmwasm/fixture.rs @@ -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(), } } @@ -139,7 +139,13 @@ impl Fixture { pub fn verify_client_message(&self, deps: Deps<'_>, client_message: Vec) { let resp = self - .query(deps, VerifyClientMessageRaw { client_message }.into()) + .query( + deps, + VerifyClientMessageRaw { + client_message: client_message.into(), + } + .into(), + ) .unwrap(); assert!(resp.is_valid); @@ -149,7 +155,13 @@ impl Fixture { pub fn check_for_misbehaviour(&self, deps: Deps<'_>, client_message: Vec) { let resp = self - .query(deps, CheckForMisbehaviourMsgRaw { client_message }.into()) + .query( + deps, + CheckForMisbehaviourMsgRaw { + client_message: client_message.into(), + } + .into(), + ) .unwrap(); assert!(resp.is_valid); @@ -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)) } @@ -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) diff --git a/tests-integration/tests/cosmwasm/helper.rs b/tests-integration/tests/cosmwasm/helper.rs index e66e0036a..e32e4d3e4 100644 --- a/tests-integration/tests/cosmwasm/helper.rs +++ b/tests-integration/tests/cosmwasm/helper.rs @@ -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; @@ -13,8 +13,8 @@ pub fn dummy_msg_info() -> MessageInfo { message_info(&creator, &coins(1000, "ibc")) } -pub fn dummy_checksum() -> Vec { - hex::decode("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") +pub fn dummy_checksum() -> Checksum { + Checksum::from_hex("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") .expect("Never fails") } diff --git a/tests-integration/tests/cosmwasm/mod.rs b/tests-integration/tests/cosmwasm/mod.rs index 413981236..e0e3e613f 100644 --- a/tests-integration/tests/cosmwasm/mod.rs +++ b/tests-integration/tests/cosmwasm/mod.rs @@ -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(), );