From c46f09169bd90281b061ef5899863c99b888665e Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Mon, 8 Jul 2024 08:20:11 -0700 Subject: [PATCH 1/8] imp: supersede Bytes with cosmwasm_std::Binary --- ibc-clients/cw-context/Cargo.toml | 2 +- .../cw-context/src/context/client_ctx.rs | 6 +- ibc-clients/cw-context/src/context/mod.rs | 16 ++--- ibc-clients/cw-context/src/types/msgs.rs | 71 ++++++------------- ibc-clients/cw-context/src/types/response.rs | 6 +- ibc-clients/ics08-wasm/types/Cargo.toml | 6 +- .../ics08-wasm/types/src/client_message.rs | 13 ++-- .../ics08-wasm/types/src/client_state.rs | 26 +++---- .../ics08-wasm/types/src/consensus_state.rs | 33 ++++----- ibc-clients/ics08-wasm/types/src/lib.rs | 7 -- .../types/src/msgs/migrate_contract.rs | 14 ++-- .../types/src/msgs/remove_checksum.rs | 9 ++- .../ics08-wasm/types/src/msgs/store_code.rs | 9 ++- .../ics08-wasm/types/src/serializer.rs | 46 ------------ ibc-core/ics03-connection/src/handler/mod.rs | 2 +- ibc-data-types/Cargo.toml | 4 +- tests-integration/tests/cosmwasm/fixture.rs | 34 +++++++-- tests-integration/tests/cosmwasm/helper.rs | 5 +- tests-integration/tests/cosmwasm/mod.rs | 2 +- 19 files changed, 121 insertions(+), 190 deletions(-) delete mode 100644 ibc-clients/ics08-wasm/types/src/serializer.rs diff --git a/ibc-clients/cw-context/Cargo.toml b/ibc-clients/cw-context/Cargo.toml index 7cb2ef06f..acea0aba3 100644 --- a/ibc-clients/cw-context/Cargo.toml +++ b/ibc-clients/cw-context/Cargo.toml @@ -24,7 +24,7 @@ serde = { workspace = true, features = [ "derive" ] } # ibc dependencies ibc-core = { workspace = true } -ibc-client-wasm-types = { workspace = true, features = [ "cosmwasm" ] } +ibc-client-wasm-types = { workspace = true } # cosmwasm dependencies cosmwasm-schema = { workspace = true } diff --git a/ibc-clients/cw-context/src/context/client_ctx.rs b/ibc-clients/cw-context/src/context/client_ctx.rs index 414217590..24cfa2f5b 100644 --- a/ibc-clients/cw-context/src/context/client_ctx.rs +++ b/ibc-clients/cw-context/src/context/client_ctx.rs @@ -32,7 +32,7 @@ where description: e.to_string(), })?; - let sov_client_state = C::ClientState::decode_any_vec(any_wasm.data)?; + let sov_client_state = C::ClientState::decode_any_vec(any_wasm.data.into())?; Ok(sov_client_state) } @@ -46,7 +46,7 @@ where let any_wasm: WasmConsensusState = C::ConsensusState::decode_any_vec(consensus_state_value)?; - let consensus_state = C::ConsensusState::decode_any_vec(any_wasm.data)?; + let consensus_state = C::ConsensusState::decode_any_vec(any_wasm.data.into())?; Ok(consensus_state) } @@ -118,7 +118,7 @@ where let encoded_consensus_state = C::ConsensusState::encode_to_any_vec(consensus_state); let wasm_consensus_state = WasmConsensusState { - data: encoded_consensus_state, + data: encoded_consensus_state.into(), }; let encoded_wasm_consensus_state = diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index 70e534fe6..4490e438e 100644 --- a/ibc-clients/cw-context/src/context/mod.rs +++ b/ibc-clients/cw-context/src/context/mod.rs @@ -21,7 +21,7 @@ use crate::api::ClientType; use crate::types::{ContractError, GenesisMetadata, HeightTravel, MigrationPrefix}; use crate::utils::AnyCodec; -type Checksum = Vec; +type Checksum = cosmwasm_std::Binary; /// - [`Height`] cannot be used directly as keys in the map, /// as it doesn't implement some cw_storage specific traits. @@ -241,13 +241,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 +265,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(), }); } @@ -300,7 +300,7 @@ where let wasm_client_state = WasmClientState { checksum: self.obtain_checksum()?, latest_height: client_state.latest_height(), - data: C::ClientState::encode_to_any_vec(client_state), + data: C::ClientState::encode_to_any_vec(client_state).into(), }; Ok(Any::from(wasm_client_state).encode_to_vec()) diff --git a/ibc-clients/cw-context/src/types/msgs.rs b/ibc-clients/cw-context/src/types/msgs.rs index 506d65695..89a2ef3d1 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; 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: Binary, } // ------------------------------------------------------------ @@ -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/ibc-clients/ics08-wasm/types/Cargo.toml b/ibc-clients/ics08-wasm/types/Cargo.toml index 70b62e6a1..2bdbdc1a2 100644 --- a/ibc-clients/ics08-wasm/types/Cargo.toml +++ b/ibc-clients/ics08-wasm/types/Cargo.toml @@ -21,6 +21,7 @@ base64 = { workspace = true, features = [ "alloc" ] } displaydoc = { workspace = true } serde = { workspace = true, optional = true } cosmwasm-schema = { workspace = true, optional = true } +cosmwasm-std = { workspace = true } # ibc dependencies ibc-core-client = { workspace = true } @@ -56,8 +57,5 @@ schema = [ "ibc-proto/json-schema", "serde", "std", -] -cosmwasm = [ - "cosmwasm-schema", - "schema", + "cosmwasm-schema" ] diff --git a/ibc-clients/ics08-wasm/types/src/client_message.rs b/ibc-clients/ics08-wasm/types/src/client_message.rs index 242dcd653..0a318f022 100644 --- a/ibc-clients/ics08-wasm/types/src/client_message.rs +++ b/ibc-clients/ics08-wasm/types/src/client_message.rs @@ -1,29 +1,32 @@ //! Defines the client message type for the ICS-08 Wasm light client. +use cosmwasm_std::Binary; use ibc_primitives::proto::Protobuf; use ibc_proto::ibc::lightclients::wasm::v1::ClientMessage as RawClientMessage; -use crate::Bytes; - pub const WASM_CLIENT_MESSAGE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.ClientMessage"; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub struct ClientMessage { - pub data: Bytes, + pub data: Binary, } impl Protobuf for ClientMessage {} impl From for ClientMessage { fn from(raw: RawClientMessage) -> Self { - Self { data: raw.data } + Self { + data: raw.data.into(), + } } } impl From for RawClientMessage { fn from(value: ClientMessage) -> Self { - RawClientMessage { data: value.data } + RawClientMessage { + data: value.data.into(), + } } } diff --git a/ibc-clients/ics08-wasm/types/src/client_state.rs b/ibc-clients/ics08-wasm/types/src/client_state.rs index 4dda117a0..4ad06c649 100644 --- a/ibc-clients/ics08-wasm/types/src/client_state.rs +++ b/ibc-clients/ics08-wasm/types/src/client_state.rs @@ -1,29 +1,23 @@ //! Defines the client state type for the ICS-08 Wasm light client. -#[cfg(feature = "cosmwasm")] +#[cfg(feature = "schema")] use cosmwasm_schema::cw_serde; +use cosmwasm_std::Binary; use ibc_core_client::types::Height; use ibc_primitives::prelude::*; use ibc_primitives::proto::{Any, Protobuf}; use ibc_proto::ibc::lightclients::wasm::v1::ClientState as RawClientState; use crate::error::Error; -#[cfg(feature = "cosmwasm")] -use crate::serializer::Base64; -use crate::Bytes; pub const WASM_CLIENT_STATE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.ClientState"; -#[cfg_attr(feature = "cosmwasm", cw_serde)] -#[cfg_attr(not(feature = "cosmwasm"), derive(Clone, Debug, PartialEq))] +#[cfg_attr(feature = "schema", cw_serde)] +#[cfg_attr(not(feature = "schema"), derive(Clone, Debug, PartialEq))] #[derive(Eq)] pub struct ClientState { - #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] - #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] - pub data: Bytes, - #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] - #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] - pub checksum: Bytes, + pub data: Binary, + pub checksum: Binary, pub latest_height: Height, } @@ -32,8 +26,8 @@ impl Protobuf for ClientState {} impl From for RawClientState { fn from(value: ClientState) -> Self { Self { - data: value.data, - checksum: value.checksum, + data: value.data.into(), + checksum: value.checksum.into(), latest_height: Some(value.latest_height.into()), } } @@ -53,8 +47,8 @@ impl TryFrom for ClientState { reason: "invalid protobuf latest height".to_string(), })?; Ok(Self { - data: raw.data, - checksum: raw.checksum, + data: raw.data.into(), + checksum: raw.checksum.into(), latest_height, }) } diff --git a/ibc-clients/ics08-wasm/types/src/consensus_state.rs b/ibc-clients/ics08-wasm/types/src/consensus_state.rs index 32e6476a9..5ee5bc125 100644 --- a/ibc-clients/ics08-wasm/types/src/consensus_state.rs +++ b/ibc-clients/ics08-wasm/types/src/consensus_state.rs @@ -1,29 +1,24 @@ //! Defines the consensus state type for the ICS-08 Wasm light client. -#[cfg(feature = "cosmwasm")] +#[cfg(feature = "schema")] use cosmwasm_schema::cw_serde; +use cosmwasm_std::Binary; use ibc_core_client::types::error::ClientError; use ibc_primitives::prelude::*; use ibc_primitives::proto::{Any, Protobuf}; use ibc_proto::ibc::lightclients::wasm::v1::ConsensusState as RawConsensusState; -#[cfg(feature = "cosmwasm")] -use crate::serializer::Base64; -use crate::Bytes; - pub const WASM_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.ConsensusState"; -#[cfg_attr(feature = "cosmwasm", cw_serde)] -#[cfg_attr(not(feature = "cosmwasm"), derive(Clone, Debug, PartialEq))] +#[cfg_attr(feature = "schema", cw_serde)] +#[cfg_attr(not(feature = "schema"), derive(Clone, Debug, PartialEq))] #[derive(Eq)] pub struct ConsensusState { - #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] - #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] - pub data: Bytes, + pub data: Binary, } impl ConsensusState { - pub fn new(data: Bytes) -> Self { + pub fn new(data: Binary) -> Self { Self { data } } } @@ -32,15 +27,17 @@ impl Protobuf for ConsensusState {} impl From for RawConsensusState { fn from(value: ConsensusState) -> Self { - Self { data: value.data } + Self { + data: value.data.into(), + } } } -impl TryFrom for ConsensusState { - type Error = ClientError; - - fn try_from(value: RawConsensusState) -> Result { - Ok(Self { data: value.data }) +impl From for ConsensusState { + fn from(value: RawConsensusState) -> Self { + Self { + data: value.data.into(), + } } } @@ -87,7 +84,7 @@ mod tests { let raw_msg = RawConsensusState { data: data.to_vec(), }; - let msg = ConsensusState::try_from(raw_msg.clone()).unwrap(); + let msg = ConsensusState::from(raw_msg.clone()); assert_eq!(RawConsensusState::from(msg.clone()), raw_msg); assert_eq!( ConsensusState::try_from(Any::from(msg.clone())).unwrap(), diff --git a/ibc-clients/ics08-wasm/types/src/lib.rs b/ibc-clients/ics08-wasm/types/src/lib.rs index 98bd27915..79e113b00 100644 --- a/ibc-clients/ics08-wasm/types/src/lib.rs +++ b/ibc-clients/ics08-wasm/types/src/lib.rs @@ -18,16 +18,9 @@ pub mod consensus_state; pub mod error; pub mod msgs; -#[cfg(feature = "cosmwasm")] -pub mod serializer; - use core::str::FromStr; use ibc_core_host_types::identifiers::ClientType; -#[cfg(not(feature = "std"))] -use ibc_primitives::prelude::Vec; - -pub type Bytes = Vec; /// Re-exports ICS-08 Wasm light client proto types from `ibc-proto` crate. pub mod proto { diff --git a/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs b/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs index 40c737457..7918b78fa 100644 --- a/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs +++ b/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs @@ -1,5 +1,6 @@ use core::str::FromStr; +use cosmwasm_std::Binary; use ibc_core_host_types::identifiers::ClientId; use ibc_primitives::prelude::*; use ibc_primitives::Signer; @@ -7,7 +8,6 @@ use ibc_proto::ibc::lightclients::wasm::v1::MsgMigrateContract as RawMsgMigrateC use ibc_proto::Protobuf; use crate::error::Error; -use crate::Bytes; pub const MIGRATE_CONTRACT_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgMigrateContract"; @@ -16,8 +16,8 @@ pub const MIGRATE_CONTRACT_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgMigrat pub struct MsgMigrateContract { pub signer: Signer, pub client_id: ClientId, - pub checksum: Bytes, - pub msg: Bytes, + pub checksum: Binary, + pub msg: Binary, } impl Protobuf for MsgMigrateContract {} @@ -27,8 +27,8 @@ impl From for RawMsgMigrateContract { Self { signer: value.signer.to_string(), client_id: value.client_id.to_string(), - checksum: value.checksum, - msg: value.msg, + checksum: value.checksum.into(), + msg: value.msg.into(), } } } @@ -40,8 +40,8 @@ impl TryFrom for MsgMigrateContract { Ok(Self { signer: Signer::from(value.signer), client_id: ClientId::from_str(&value.client_id)?, - checksum: value.checksum, - msg: value.msg, + checksum: value.checksum.into(), + msg: value.msg.into(), }) } } diff --git a/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs b/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs index 9b84aac87..341f1fa8b 100644 --- a/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs +++ b/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs @@ -1,17 +1,16 @@ +use cosmwasm_std::Binary; use ibc_primitives::prelude::*; use ibc_primitives::Signer; use ibc_proto::ibc::lightclients::wasm::v1::MsgRemoveChecksum as RawMsgRemoveChecksum; use ibc_proto::Protobuf; -use crate::Bytes; - pub const REMOVE_CHECKSUM_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgRemoveChecksum"; /// Defines the message type for removing a checksum from the chain. #[derive(Clone, PartialEq, Debug, Eq)] pub struct MsgRemoveChecksum { pub signer: Signer, - pub checksum: Bytes, + pub checksum: Binary, } impl Protobuf for MsgRemoveChecksum {} @@ -20,7 +19,7 @@ impl From for RawMsgRemoveChecksum { fn from(value: MsgRemoveChecksum) -> Self { Self { signer: value.signer.to_string(), - checksum: value.checksum, + checksum: value.checksum.into(), } } } @@ -29,7 +28,7 @@ impl From for MsgRemoveChecksum { fn from(value: RawMsgRemoveChecksum) -> Self { Self { signer: Signer::from(value.signer), - checksum: value.checksum, + checksum: value.checksum.into(), } } } diff --git a/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs b/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs index ca7818eac..834bc7e76 100644 --- a/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs +++ b/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs @@ -1,17 +1,16 @@ +use cosmwasm_std::Binary; use ibc_primitives::prelude::*; use ibc_primitives::Signer; use ibc_proto::ibc::lightclients::wasm::v1::MsgStoreCode as RawMsgStoreCode; use ibc_proto::Protobuf; -use crate::Bytes; - pub const STORE_CODE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgStoreCode"; /// Defines the message type for storing the Wasm byte code on the chain. #[derive(Clone, PartialEq, Debug, Eq)] pub struct MsgStoreCode { pub signer: Signer, - pub wasm_byte_code: Bytes, + pub wasm_byte_code: Binary, } impl Protobuf for MsgStoreCode {} @@ -20,7 +19,7 @@ impl From for RawMsgStoreCode { fn from(value: MsgStoreCode) -> Self { Self { signer: value.signer.to_string(), - wasm_byte_code: value.wasm_byte_code, + wasm_byte_code: value.wasm_byte_code.into(), } } } @@ -29,7 +28,7 @@ impl From for MsgStoreCode { fn from(value: RawMsgStoreCode) -> Self { Self { signer: Signer::from(value.signer), - wasm_byte_code: value.wasm_byte_code, + wasm_byte_code: value.wasm_byte_code.into(), } } } diff --git a/ibc-clients/ics08-wasm/types/src/serializer.rs b/ibc-clients/ics08-wasm/types/src/serializer.rs deleted file mode 100644 index c44dc7325..000000000 --- a/ibc-clients/ics08-wasm/types/src/serializer.rs +++ /dev/null @@ -1,46 +0,0 @@ -use base64::prelude::BASE64_STANDARD; -use base64::Engine; -use ibc_primitives::prelude::*; -use serde::de::Error; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; - -pub struct Base64; - -impl Base64 { - pub fn serialize(bytes: &[u8], serializer: S) -> Result { - let encoded = BASE64_STANDARD.encode(bytes); - String::serialize(&encoded, serializer) - } - - pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { - let base64 = String::deserialize(deserializer)?; - let bytes = BASE64_STANDARD - .decode(base64.as_bytes()) - .map_err(Error::custom)?; - - Ok(bytes) - } -} - -#[cfg(test)] -mod tests { - use rstest::rstest; - - use super::*; - - #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] - struct Foo(#[serde(with = "Base64")] crate::Bytes); - - // Ensures Base64 serialize and deserialize work as expected - #[rstest] - #[case(b"", "")] - #[case(&[118], "dg==")] - #[case(&[0x1, 0x2, 0x3, 0x4, 0x5], "AQIDBAU=")] - #[case(b"hello world", "aGVsbG8gd29ybGQ=")] - pub fn test_ser_and_deser(#[case] bytes: &[u8], #[case] hash: &str) { - let foo = Foo(bytes.to_vec()); - let json = format!("\"{hash}\""); - assert_eq!(serde_json::to_string(&foo).unwrap(), json); - assert_eq!(serde_json::from_str::(&json).unwrap(), foo); - } -} diff --git a/ibc-core/ics03-connection/src/handler/mod.rs b/ibc-core/ics03-connection/src/handler/mod.rs index 241136411..d36b5b866 100644 --- a/ibc-core/ics03-connection/src/handler/mod.rs +++ b/ibc-core/ics03-connection/src/handler/mod.rs @@ -73,7 +73,7 @@ where use ibc_client_wasm_types::consensus_state::ConsensusState as WasmConsensusState; use prost::Message; - let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec()); + let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec().into()); wasm_consensus_state.into() } else { diff --git a/ibc-data-types/Cargo.toml b/ibc-data-types/Cargo.toml index da618570d..ed2ccbd91 100644 --- a/ibc-data-types/Cargo.toml +++ b/ibc-data-types/Cargo.toml @@ -95,6 +95,4 @@ parity-scale-codec = [ "ibc-client-tendermint-types/parity-scale-codec", "ibc-primitives/parity-scale-codec", ] -cosmwasm = [ - "ibc-client-wasm-types/cosmwasm", -] + 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..e1323e13b 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, Binary, Env, MessageInfo, Timestamp as CwTimestamp}; use ibc::clients::tendermint::types::ConsensusState; use ibc::core::primitives::Timestamp as IbcTimestamp; use tendermint::Hash; @@ -13,9 +13,10 @@ pub fn dummy_msg_info() -> MessageInfo { message_info(&creator, &coins(1000, "ibc")) } -pub fn dummy_checksum() -> Vec { +pub fn dummy_checksum() -> Binary { hex::decode("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") .expect("Never fails") + .into() } pub fn dummy_sov_consensus_state(timestamp: IbcTimestamp) -> ConsensusState { 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(), ); From e76bd3fb16f4ed96865c85835c7b7056df60ea1b Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Mon, 8 Jul 2024 08:22:33 -0700 Subject: [PATCH 2/8] chore: add unclog --- .../1271-supersede-Bytes-with-cosmwasm-Binary.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/breaking-changes/1271-supersede-Bytes-with-cosmwasm-Binary.md diff --git a/.changelog/unreleased/breaking-changes/1271-supersede-Bytes-with-cosmwasm-Binary.md b/.changelog/unreleased/breaking-changes/1271-supersede-Bytes-with-cosmwasm-Binary.md new file mode 100644 index 000000000..1e27e8ba1 --- /dev/null +++ b/.changelog/unreleased/breaking-changes/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)) From b490455ce90e492fdc32a091cd6b407b2fb25b9a Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Mon, 8 Jul 2024 09:05:06 -0700 Subject: [PATCH 3/8] fix: keep ibc-client-wasm-types as is --- ci/cw-check/src/lib.rs | 4 +- ibc-clients/cw-context/Cargo.toml | 2 +- .../cw-context/src/context/client_ctx.rs | 6 +-- ibc-clients/cw-context/src/context/mod.rs | 6 +-- ibc-clients/ics08-wasm/types/Cargo.toml | 6 ++- .../ics08-wasm/types/src/client_message.rs | 13 ++---- .../ics08-wasm/types/src/client_state.rs | 26 +++++++---- .../ics08-wasm/types/src/consensus_state.rs | 33 +++++++------ ibc-clients/ics08-wasm/types/src/lib.rs | 7 +++ .../types/src/msgs/migrate_contract.rs | 14 +++--- .../types/src/msgs/remove_checksum.rs | 9 ++-- .../ics08-wasm/types/src/msgs/store_code.rs | 9 ++-- .../ics08-wasm/types/src/serializer.rs | 46 +++++++++++++++++++ ibc-core/ics03-connection/src/handler/mod.rs | 2 +- ibc-data-types/Cargo.toml | 4 +- 15 files changed, 126 insertions(+), 61 deletions(-) create mode 100644 ibc-clients/ics08-wasm/types/src/serializer.rs diff --git a/ci/cw-check/src/lib.rs b/ci/cw-check/src/lib.rs index e567d844e..2d40f6c8c 100644 --- a/ci/cw-check/src/lib.rs +++ b/ci/cw-check/src/lib.rs @@ -14,7 +14,7 @@ pub struct Msg { pub test: String, } -#[cfg_attr(not(feature = "library"), cosmwasm_std::entry_point)] +#[cosmwasm_std::entry_point] pub fn instantiate( _deps: DepsMut, _env: Env, @@ -49,7 +49,7 @@ pub fn instantiate( .add_attribute(msg.test.clone(), msg.test.clone())) } -#[cfg_attr(not(feature = "library"), cosmwasm_std::entry_point)] +#[cosmwasm_std::entry_point] pub fn execute( _deps: DepsMut, _env: Env, diff --git a/ibc-clients/cw-context/Cargo.toml b/ibc-clients/cw-context/Cargo.toml index acea0aba3..7cb2ef06f 100644 --- a/ibc-clients/cw-context/Cargo.toml +++ b/ibc-clients/cw-context/Cargo.toml @@ -24,7 +24,7 @@ serde = { workspace = true, features = [ "derive" ] } # ibc dependencies ibc-core = { workspace = true } -ibc-client-wasm-types = { workspace = true } +ibc-client-wasm-types = { workspace = true, features = [ "cosmwasm" ] } # cosmwasm dependencies cosmwasm-schema = { workspace = true } diff --git a/ibc-clients/cw-context/src/context/client_ctx.rs b/ibc-clients/cw-context/src/context/client_ctx.rs index 24cfa2f5b..414217590 100644 --- a/ibc-clients/cw-context/src/context/client_ctx.rs +++ b/ibc-clients/cw-context/src/context/client_ctx.rs @@ -32,7 +32,7 @@ where description: e.to_string(), })?; - let sov_client_state = C::ClientState::decode_any_vec(any_wasm.data.into())?; + let sov_client_state = C::ClientState::decode_any_vec(any_wasm.data)?; Ok(sov_client_state) } @@ -46,7 +46,7 @@ where let any_wasm: WasmConsensusState = C::ConsensusState::decode_any_vec(consensus_state_value)?; - let consensus_state = C::ConsensusState::decode_any_vec(any_wasm.data.into())?; + let consensus_state = C::ConsensusState::decode_any_vec(any_wasm.data)?; Ok(consensus_state) } @@ -118,7 +118,7 @@ where let encoded_consensus_state = C::ConsensusState::encode_to_any_vec(consensus_state); let wasm_consensus_state = WasmConsensusState { - data: encoded_consensus_state.into(), + data: encoded_consensus_state, }; let encoded_wasm_consensus_state = diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index 4490e438e..2e8ce9fe5 100644 --- a/ibc-clients/cw-context/src/context/mod.rs +++ b/ibc-clients/cw-context/src/context/mod.rs @@ -287,7 +287,7 @@ where } })?; - Ok(wasm_client_state.checksum) + Ok(wasm_client_state.checksum.into()) } } } @@ -298,9 +298,9 @@ 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).into(), + data: C::ClientState::encode_to_any_vec(client_state), }; Ok(Any::from(wasm_client_state).encode_to_vec()) diff --git a/ibc-clients/ics08-wasm/types/Cargo.toml b/ibc-clients/ics08-wasm/types/Cargo.toml index 2bdbdc1a2..70b62e6a1 100644 --- a/ibc-clients/ics08-wasm/types/Cargo.toml +++ b/ibc-clients/ics08-wasm/types/Cargo.toml @@ -21,7 +21,6 @@ base64 = { workspace = true, features = [ "alloc" ] } displaydoc = { workspace = true } serde = { workspace = true, optional = true } cosmwasm-schema = { workspace = true, optional = true } -cosmwasm-std = { workspace = true } # ibc dependencies ibc-core-client = { workspace = true } @@ -57,5 +56,8 @@ schema = [ "ibc-proto/json-schema", "serde", "std", - "cosmwasm-schema" +] +cosmwasm = [ + "cosmwasm-schema", + "schema", ] diff --git a/ibc-clients/ics08-wasm/types/src/client_message.rs b/ibc-clients/ics08-wasm/types/src/client_message.rs index 0a318f022..242dcd653 100644 --- a/ibc-clients/ics08-wasm/types/src/client_message.rs +++ b/ibc-clients/ics08-wasm/types/src/client_message.rs @@ -1,32 +1,29 @@ //! Defines the client message type for the ICS-08 Wasm light client. -use cosmwasm_std::Binary; use ibc_primitives::proto::Protobuf; use ibc_proto::ibc::lightclients::wasm::v1::ClientMessage as RawClientMessage; +use crate::Bytes; + pub const WASM_CLIENT_MESSAGE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.ClientMessage"; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub struct ClientMessage { - pub data: Binary, + pub data: Bytes, } impl Protobuf for ClientMessage {} impl From for ClientMessage { fn from(raw: RawClientMessage) -> Self { - Self { - data: raw.data.into(), - } + Self { data: raw.data } } } impl From for RawClientMessage { fn from(value: ClientMessage) -> Self { - RawClientMessage { - data: value.data.into(), - } + RawClientMessage { data: value.data } } } diff --git a/ibc-clients/ics08-wasm/types/src/client_state.rs b/ibc-clients/ics08-wasm/types/src/client_state.rs index 4ad06c649..4dda117a0 100644 --- a/ibc-clients/ics08-wasm/types/src/client_state.rs +++ b/ibc-clients/ics08-wasm/types/src/client_state.rs @@ -1,23 +1,29 @@ //! Defines the client state type for the ICS-08 Wasm light client. -#[cfg(feature = "schema")] +#[cfg(feature = "cosmwasm")] use cosmwasm_schema::cw_serde; -use cosmwasm_std::Binary; use ibc_core_client::types::Height; use ibc_primitives::prelude::*; use ibc_primitives::proto::{Any, Protobuf}; use ibc_proto::ibc::lightclients::wasm::v1::ClientState as RawClientState; use crate::error::Error; +#[cfg(feature = "cosmwasm")] +use crate::serializer::Base64; +use crate::Bytes; pub const WASM_CLIENT_STATE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.ClientState"; -#[cfg_attr(feature = "schema", cw_serde)] -#[cfg_attr(not(feature = "schema"), derive(Clone, Debug, PartialEq))] +#[cfg_attr(feature = "cosmwasm", cw_serde)] +#[cfg_attr(not(feature = "cosmwasm"), derive(Clone, Debug, PartialEq))] #[derive(Eq)] pub struct ClientState { - pub data: Binary, - pub checksum: Binary, + #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] + #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] + pub data: Bytes, + #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] + #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] + pub checksum: Bytes, pub latest_height: Height, } @@ -26,8 +32,8 @@ impl Protobuf for ClientState {} impl From for RawClientState { fn from(value: ClientState) -> Self { Self { - data: value.data.into(), - checksum: value.checksum.into(), + data: value.data, + checksum: value.checksum, latest_height: Some(value.latest_height.into()), } } @@ -47,8 +53,8 @@ impl TryFrom for ClientState { reason: "invalid protobuf latest height".to_string(), })?; Ok(Self { - data: raw.data.into(), - checksum: raw.checksum.into(), + data: raw.data, + checksum: raw.checksum, latest_height, }) } diff --git a/ibc-clients/ics08-wasm/types/src/consensus_state.rs b/ibc-clients/ics08-wasm/types/src/consensus_state.rs index 5ee5bc125..32e6476a9 100644 --- a/ibc-clients/ics08-wasm/types/src/consensus_state.rs +++ b/ibc-clients/ics08-wasm/types/src/consensus_state.rs @@ -1,24 +1,29 @@ //! Defines the consensus state type for the ICS-08 Wasm light client. -#[cfg(feature = "schema")] +#[cfg(feature = "cosmwasm")] use cosmwasm_schema::cw_serde; -use cosmwasm_std::Binary; use ibc_core_client::types::error::ClientError; use ibc_primitives::prelude::*; use ibc_primitives::proto::{Any, Protobuf}; use ibc_proto::ibc::lightclients::wasm::v1::ConsensusState as RawConsensusState; +#[cfg(feature = "cosmwasm")] +use crate::serializer::Base64; +use crate::Bytes; + pub const WASM_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.ConsensusState"; -#[cfg_attr(feature = "schema", cw_serde)] -#[cfg_attr(not(feature = "schema"), derive(Clone, Debug, PartialEq))] +#[cfg_attr(feature = "cosmwasm", cw_serde)] +#[cfg_attr(not(feature = "cosmwasm"), derive(Clone, Debug, PartialEq))] #[derive(Eq)] pub struct ConsensusState { - pub data: Binary, + #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] + #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] + pub data: Bytes, } impl ConsensusState { - pub fn new(data: Binary) -> Self { + pub fn new(data: Bytes) -> Self { Self { data } } } @@ -27,17 +32,15 @@ impl Protobuf for ConsensusState {} impl From for RawConsensusState { fn from(value: ConsensusState) -> Self { - Self { - data: value.data.into(), - } + Self { data: value.data } } } -impl From for ConsensusState { - fn from(value: RawConsensusState) -> Self { - Self { - data: value.data.into(), - } +impl TryFrom for ConsensusState { + type Error = ClientError; + + fn try_from(value: RawConsensusState) -> Result { + Ok(Self { data: value.data }) } } @@ -84,7 +87,7 @@ mod tests { let raw_msg = RawConsensusState { data: data.to_vec(), }; - let msg = ConsensusState::from(raw_msg.clone()); + let msg = ConsensusState::try_from(raw_msg.clone()).unwrap(); assert_eq!(RawConsensusState::from(msg.clone()), raw_msg); assert_eq!( ConsensusState::try_from(Any::from(msg.clone())).unwrap(), diff --git a/ibc-clients/ics08-wasm/types/src/lib.rs b/ibc-clients/ics08-wasm/types/src/lib.rs index 79e113b00..98bd27915 100644 --- a/ibc-clients/ics08-wasm/types/src/lib.rs +++ b/ibc-clients/ics08-wasm/types/src/lib.rs @@ -18,9 +18,16 @@ pub mod consensus_state; pub mod error; pub mod msgs; +#[cfg(feature = "cosmwasm")] +pub mod serializer; + use core::str::FromStr; use ibc_core_host_types::identifiers::ClientType; +#[cfg(not(feature = "std"))] +use ibc_primitives::prelude::Vec; + +pub type Bytes = Vec; /// Re-exports ICS-08 Wasm light client proto types from `ibc-proto` crate. pub mod proto { diff --git a/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs b/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs index 7918b78fa..40c737457 100644 --- a/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs +++ b/ibc-clients/ics08-wasm/types/src/msgs/migrate_contract.rs @@ -1,6 +1,5 @@ use core::str::FromStr; -use cosmwasm_std::Binary; use ibc_core_host_types::identifiers::ClientId; use ibc_primitives::prelude::*; use ibc_primitives::Signer; @@ -8,6 +7,7 @@ use ibc_proto::ibc::lightclients::wasm::v1::MsgMigrateContract as RawMsgMigrateC use ibc_proto::Protobuf; use crate::error::Error; +use crate::Bytes; pub const MIGRATE_CONTRACT_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgMigrateContract"; @@ -16,8 +16,8 @@ pub const MIGRATE_CONTRACT_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgMigrat pub struct MsgMigrateContract { pub signer: Signer, pub client_id: ClientId, - pub checksum: Binary, - pub msg: Binary, + pub checksum: Bytes, + pub msg: Bytes, } impl Protobuf for MsgMigrateContract {} @@ -27,8 +27,8 @@ impl From for RawMsgMigrateContract { Self { signer: value.signer.to_string(), client_id: value.client_id.to_string(), - checksum: value.checksum.into(), - msg: value.msg.into(), + checksum: value.checksum, + msg: value.msg, } } } @@ -40,8 +40,8 @@ impl TryFrom for MsgMigrateContract { Ok(Self { signer: Signer::from(value.signer), client_id: ClientId::from_str(&value.client_id)?, - checksum: value.checksum.into(), - msg: value.msg.into(), + checksum: value.checksum, + msg: value.msg, }) } } diff --git a/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs b/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs index 341f1fa8b..9b84aac87 100644 --- a/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs +++ b/ibc-clients/ics08-wasm/types/src/msgs/remove_checksum.rs @@ -1,16 +1,17 @@ -use cosmwasm_std::Binary; use ibc_primitives::prelude::*; use ibc_primitives::Signer; use ibc_proto::ibc::lightclients::wasm::v1::MsgRemoveChecksum as RawMsgRemoveChecksum; use ibc_proto::Protobuf; +use crate::Bytes; + pub const REMOVE_CHECKSUM_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgRemoveChecksum"; /// Defines the message type for removing a checksum from the chain. #[derive(Clone, PartialEq, Debug, Eq)] pub struct MsgRemoveChecksum { pub signer: Signer, - pub checksum: Binary, + pub checksum: Bytes, } impl Protobuf for MsgRemoveChecksum {} @@ -19,7 +20,7 @@ impl From for RawMsgRemoveChecksum { fn from(value: MsgRemoveChecksum) -> Self { Self { signer: value.signer.to_string(), - checksum: value.checksum.into(), + checksum: value.checksum, } } } @@ -28,7 +29,7 @@ impl From for MsgRemoveChecksum { fn from(value: RawMsgRemoveChecksum) -> Self { Self { signer: Signer::from(value.signer), - checksum: value.checksum.into(), + checksum: value.checksum, } } } diff --git a/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs b/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs index 834bc7e76..ca7818eac 100644 --- a/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs +++ b/ibc-clients/ics08-wasm/types/src/msgs/store_code.rs @@ -1,16 +1,17 @@ -use cosmwasm_std::Binary; use ibc_primitives::prelude::*; use ibc_primitives::Signer; use ibc_proto::ibc::lightclients::wasm::v1::MsgStoreCode as RawMsgStoreCode; use ibc_proto::Protobuf; +use crate::Bytes; + pub const STORE_CODE_TYPE_URL: &str = "/ibc.lightclients.wasm.v1.MsgStoreCode"; /// Defines the message type for storing the Wasm byte code on the chain. #[derive(Clone, PartialEq, Debug, Eq)] pub struct MsgStoreCode { pub signer: Signer, - pub wasm_byte_code: Binary, + pub wasm_byte_code: Bytes, } impl Protobuf for MsgStoreCode {} @@ -19,7 +20,7 @@ impl From for RawMsgStoreCode { fn from(value: MsgStoreCode) -> Self { Self { signer: value.signer.to_string(), - wasm_byte_code: value.wasm_byte_code.into(), + wasm_byte_code: value.wasm_byte_code, } } } @@ -28,7 +29,7 @@ impl From for MsgStoreCode { fn from(value: RawMsgStoreCode) -> Self { Self { signer: Signer::from(value.signer), - wasm_byte_code: value.wasm_byte_code.into(), + wasm_byte_code: value.wasm_byte_code, } } } diff --git a/ibc-clients/ics08-wasm/types/src/serializer.rs b/ibc-clients/ics08-wasm/types/src/serializer.rs new file mode 100644 index 000000000..c44dc7325 --- /dev/null +++ b/ibc-clients/ics08-wasm/types/src/serializer.rs @@ -0,0 +1,46 @@ +use base64::prelude::BASE64_STANDARD; +use base64::Engine; +use ibc_primitives::prelude::*; +use serde::de::Error; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +pub struct Base64; + +impl Base64 { + pub fn serialize(bytes: &[u8], serializer: S) -> Result { + let encoded = BASE64_STANDARD.encode(bytes); + String::serialize(&encoded, serializer) + } + + pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + let base64 = String::deserialize(deserializer)?; + let bytes = BASE64_STANDARD + .decode(base64.as_bytes()) + .map_err(Error::custom)?; + + Ok(bytes) + } +} + +#[cfg(test)] +mod tests { + use rstest::rstest; + + use super::*; + + #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] + struct Foo(#[serde(with = "Base64")] crate::Bytes); + + // Ensures Base64 serialize and deserialize work as expected + #[rstest] + #[case(b"", "")] + #[case(&[118], "dg==")] + #[case(&[0x1, 0x2, 0x3, 0x4, 0x5], "AQIDBAU=")] + #[case(b"hello world", "aGVsbG8gd29ybGQ=")] + pub fn test_ser_and_deser(#[case] bytes: &[u8], #[case] hash: &str) { + let foo = Foo(bytes.to_vec()); + let json = format!("\"{hash}\""); + assert_eq!(serde_json::to_string(&foo).unwrap(), json); + assert_eq!(serde_json::from_str::(&json).unwrap(), foo); + } +} diff --git a/ibc-core/ics03-connection/src/handler/mod.rs b/ibc-core/ics03-connection/src/handler/mod.rs index d36b5b866..241136411 100644 --- a/ibc-core/ics03-connection/src/handler/mod.rs +++ b/ibc-core/ics03-connection/src/handler/mod.rs @@ -73,7 +73,7 @@ where use ibc_client_wasm_types::consensus_state::ConsensusState as WasmConsensusState; use prost::Message; - let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec().into()); + let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec()); wasm_consensus_state.into() } else { diff --git a/ibc-data-types/Cargo.toml b/ibc-data-types/Cargo.toml index ed2ccbd91..da618570d 100644 --- a/ibc-data-types/Cargo.toml +++ b/ibc-data-types/Cargo.toml @@ -95,4 +95,6 @@ parity-scale-codec = [ "ibc-client-tendermint-types/parity-scale-codec", "ibc-primitives/parity-scale-codec", ] - +cosmwasm = [ + "ibc-client-wasm-types/cosmwasm", +] From 8181b44a01d619d7a946f2390ae2b36fdec8c6d2 Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Wed, 10 Jul 2024 10:44:26 -0700 Subject: [PATCH 4/8] fix: use cosmwasm_std::Checksum --- ibc-clients/cw-context/src/context/mod.rs | 12 ++++++++---- ibc-clients/cw-context/src/types/msgs.rs | 4 ++-- tests-integration/tests/cosmwasm/helper.rs | 7 +++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index 2e8ce9fe5..9e686918d 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, ChecksumError, 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 = cosmwasm_std::Binary; - /// - [`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 @@ -287,7 +285,13 @@ where } })?; - Ok(wasm_client_state.checksum.into()) + let checksum = wasm_client_state.checksum.as_slice().try_into().map_err( + |e: ChecksumError| ClientError::Other { + description: e.to_string(), + }, + )?; + + Ok(checksum) } } } diff --git a/ibc-clients/cw-context/src/types/msgs.rs b/ibc-clients/cw-context/src/types/msgs.rs index 89a2ef3d1..e5647ee4c 100644 --- a/ibc-clients/cw-context/src/types/msgs.rs +++ b/ibc-clients/cw-context/src/types/msgs.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::Binary; +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}; @@ -21,7 +21,7 @@ use super::error::ContractError; pub struct InstantiateMsg { pub client_state: Binary, pub consensus_state: Binary, - pub checksum: Binary, + pub checksum: Checksum, } // ------------------------------------------------------------ diff --git a/tests-integration/tests/cosmwasm/helper.rs b/tests-integration/tests/cosmwasm/helper.rs index e1323e13b..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, Binary, 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,10 +13,9 @@ pub fn dummy_msg_info() -> MessageInfo { message_info(&creator, &coins(1000, "ibc")) } -pub fn dummy_checksum() -> Binary { - hex::decode("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") +pub fn dummy_checksum() -> Checksum { + Checksum::from_hex("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") .expect("Never fails") - .into() } pub fn dummy_sov_consensus_state(timestamp: IbcTimestamp) -> ConsensusState { From 0d8830b5fb22fb41c5893519b216c1080f3651fd Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Wed, 10 Jul 2024 10:51:16 -0700 Subject: [PATCH 5/8] fix: make clippy happy --- ibc-clients/cw-context/src/context/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index 9e686918d..ac020ffe3 100644 --- a/ibc-clients/cw-context/src/context/mod.rs +++ b/ibc-clients/cw-context/src/context/mod.rs @@ -274,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())?; From 35d8cdd81ada7708e48d358ae6b350b61274f10d Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Thu, 11 Jul 2024 13:26:19 +0200 Subject: [PATCH 6/8] rm unused dependency --- tests-integration/Cargo.toml | 1 - 1 file changed, 1 deletion(-) 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 } From 256cbd93c7c4447d339ed23415da43e512f0b278 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Thu, 11 Jul 2024 13:30:15 +0200 Subject: [PATCH 7/8] rm redundant import --- ibc-clients/cw-context/src/context/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index ac020ffe3..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::{Checksum, ChecksumError, 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; @@ -285,11 +285,12 @@ where } })?; - let checksum = wasm_client_state.checksum.as_slice().try_into().map_err( - |e: ChecksumError| ClientError::Other { - description: e.to_string(), - }, - )?; + let checksum = + Checksum::try_from(wasm_client_state.checksum.as_slice()).map_err(|e| { + ClientError::Other { + description: e.to_string(), + } + })?; Ok(checksum) } From 9515258ae09a79cf68d58ea428024549b2583612 Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Thu, 11 Jul 2024 08:29:21 -0700 Subject: [PATCH 8/8] fix: move unclog under improvements --- .../1271-supersede-Bytes-with-cosmwasm-Binary.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/unreleased/{breaking-changes => improvements}/1271-supersede-Bytes-with-cosmwasm-Binary.md (100%) diff --git a/.changelog/unreleased/breaking-changes/1271-supersede-Bytes-with-cosmwasm-Binary.md b/.changelog/unreleased/improvements/1271-supersede-Bytes-with-cosmwasm-Binary.md similarity index 100% rename from .changelog/unreleased/breaking-changes/1271-supersede-Bytes-with-cosmwasm-Binary.md rename to .changelog/unreleased/improvements/1271-supersede-Bytes-with-cosmwasm-Binary.md