diff --git a/.changelog/unreleased/improvements/1283-remove-cosmwasm-feature-flag.md b/.changelog/unreleased/improvements/1283-remove-cosmwasm-feature-flag.md new file mode 100644 index 000000000..17e4c9285 --- /dev/null +++ b/.changelog/unreleased/improvements/1283-remove-cosmwasm-feature-flag.md @@ -0,0 +1,3 @@ +- [ibc-client-wasm-type] Remove the `cosmwasm` feature for consistent feature + flags across ibc-rs, and use existing `serde` and `schema` features. + ([\#1283](https://github.com/cosmos/ibc-rs/pull/1283)) diff --git a/Cargo.toml b/Cargo.toml index f1b8dc3ec..c0aaa0252 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,7 @@ sha2 = { version = "0.10.8", default-features = false } serde = { version = "1.0", default-features = false } serde-json = { package = "serde-json-wasm", version = "1.0.1", default-features = false } subtle-encoding = { version = "0.5", default-features = false } -hex = { version = "0.4.3" } +hex = { version = "0.4.3", default-features = false } # ibc dependencies ibc = { version = "0.53.0", path = "./ibc", default-features = false } diff --git a/ci/no-std-check/Cargo.lock b/ci/no-std-check/Cargo.lock index 01ea501f8..baad9f1a6 100644 --- a/ci/no-std-check/Cargo.lock +++ b/ci/no-std-check/Cargo.lock @@ -1247,6 +1247,7 @@ version = "0.53.0" dependencies = [ "base64 0.22.1", "displaydoc", + "hex", "ibc-core-client", "ibc-core-host-types", "ibc-primitives", diff --git a/ibc-clients/cw-context/Cargo.toml b/ibc-clients/cw-context/Cargo.toml index 7cb2ef06f..ead5e2601 100644 --- a/ibc-clients/cw-context/Cargo.toml +++ b/ibc-clients/cw-context/Cargo.toml @@ -24,13 +24,16 @@ 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, features = [ "schema" ] } # cosmwasm dependencies cosmwasm-schema = { workspace = true } cosmwasm-std = { workspace = true } cw-storage-plus = { workspace = true } +[dev-dependencies] +serde-json = { workspace = true } + [features] default = [ "std" ] std = [ diff --git a/ibc-clients/cw-context/src/context/mod.rs b/ibc-clients/cw-context/src/context/mod.rs index 096115970..45d410265 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, Deps, DepsMut, Empty, Env, Order, Storage}; +use cosmwasm_std::{Binary, 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; @@ -40,7 +40,7 @@ where deps_mut: Option>, env: Env, client_id: ClientId, - checksum: Option, + checksum: Option, migration_prefix: MigrationPrefix, client_type: std::marker::PhantomData, } @@ -96,7 +96,7 @@ where } /// Sets the checksum of the context. - pub fn set_checksum(&mut self, checksum: Checksum) { + pub fn set_checksum(&mut self, checksum: Binary) { self.checksum = Some(checksum); } @@ -272,9 +272,9 @@ where } /// Returns the checksum of the current contract. - pub fn obtain_checksum(&self) -> Result { + pub fn obtain_checksum(&self) -> Result { match &self.checksum { - Some(checksum) => Ok(*checksum), + Some(checksum) => Ok(checksum.clone()), None => { let client_state_value = self.retrieve(ClientStatePath::leaf())?; @@ -285,14 +285,7 @@ where } })?; - let checksum = - Checksum::try_from(wasm_client_state.checksum.as_slice()).map_err(|e| { - ClientError::Other { - description: e.to_string(), - } - })?; - - Ok(checksum) + Ok(wasm_client_state.checksum.into()) } } } diff --git a/ibc-clients/cw-context/src/types/msgs.rs b/ibc-clients/cw-context/src/types/msgs.rs index 1a7da1a4c..232bd0595 100644 --- a/ibc-clients/cw-context/src/types/msgs.rs +++ b/ibc-clients/cw-context/src/types/msgs.rs @@ -1,11 +1,10 @@ //! Defines the messages sent to the CosmWasm contract by the 08-wasm proxy //! light client. use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::{Binary, Checksum}; +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}; -use ibc_core::commitment_types::merkle::MerklePath; use ibc_core::host::types::path::PathBytes; use ibc_core::primitives::proto::Any; use prost::Message; @@ -20,7 +19,7 @@ use super::error::ContractError; pub struct InstantiateMsg { pub client_state: Binary, pub consensus_state: Binary, - pub checksum: Checksum, + pub checksum: Binary, } // ------------------------------------------------------------ @@ -115,6 +114,11 @@ impl TryFrom for VerifyUpgradeAndUpdateStateM } } +#[cw_serde] +pub struct MerklePath { + pub key_path: Vec, +} + #[cw_serde] pub struct VerifyMembershipMsgRaw { pub proof: Binary, @@ -140,7 +144,7 @@ impl TryFrom for VerifyMembershipMsg { fn try_from(mut raw: VerifyMembershipMsgRaw) -> Result { let proof = CommitmentProofBytes::try_from(raw.proof.to_vec())?; - let prefix = CommitmentPrefix::from(raw.path.key_path.remove(0).into_vec()); + let prefix = CommitmentPrefix::from_bytes(raw.path.key_path.remove(0)); let path = PathBytes::flatten(raw.path.key_path); let height = Height::try_from(raw.height)?; @@ -179,7 +183,7 @@ impl TryFrom for VerifyNonMembershipMsg { fn try_from(mut raw: VerifyNonMembershipMsgRaw) -> Result { let proof = CommitmentProofBytes::try_from(raw.proof.to_vec())?; - let prefix = CommitmentPrefix::from(raw.path.key_path.remove(0).into_vec()); + let prefix = CommitmentPrefix::from_bytes(raw.path.key_path.remove(0)); let path = PathBytes::flatten(raw.path.key_path); let height = raw.height.try_into()?; @@ -264,3 +268,37 @@ impl TryFrom for CheckForMisbehaviourMsg { Ok(Self { client_message }) } } + +#[cfg(test)] +mod test { + use super::{InstantiateMsg, SudoMsg}; + + #[test] + fn verify_membership_from_json() { + let sudo_msg = r#"{ + "verify_membership":{ + "height": + {"revision_height":57}, + "delay_time_period":0, + "delay_block_period":0, + "proof":"CuECCt4CChhjb25uZWN0aW9ucy9jb25uZWN0aW9uLTASWgoPMDctdGVuZGVybWludC0wEiMKATESDU9SREVSX09SREVSRUQSD09SREVSX1VOT1JERVJFRBgCIiAKCTA4LXdhc20tMBIMY29ubmVjdGlvbi0wGgUKA2liYxoLCAEYASABKgMAAkgiKQgBEiUCBHAg3HTYmBAMxlr6u0mv6wCpm3ur2WQc7A3Af6aV7Ye0Fe0gIisIARIEBAZwIBohIHXEkQ9RIH08ZZYBIP6THxOOJiRmjXWGn1G4RCWT3V6rIisIARIEBgxwIBohIEUjGWV7YLPEzdFVLAb0lv4VvP7A+l1TqFkjpx1kDKAPIikIARIlCBhwILWsAKEot+2MknVyn5zcS0qsqVhRj4AHpgDx7fNPbfhtICIpCAESJQxAcCCzyYMGE+CdCltudr1ddHvCJrqv3kl/i7YnMLx3XWJt/yAK/AEK+QEKA2liYxIg2nvqL76rejXXGlX6ng/UKrbw+72C8uKKgM2vP0JKj1QaCQgBGAEgASoBACIlCAESIQEGuZwNgRn/HtvL4WXQ8ZM327wIDmd8iOV6oq52fr8PDyInCAESAQEaIKplBAbqDXbjndQ9LqapHj/aockI/CGnymjl5izIEVY5IiUIARIhAdt4G8DCLINAaaJnhUMIzv74AV3zZiugAyyZ/lWYRv+cIiUIARIhAf+sohoEV+uWeKThAPEbqCUivWT4H8KNT7Giw9//LsyvIicIARIBARogNHO4HC5KxPCwBdQGgBCscVtEKw+YSn2pnf654Y3Oxik=", + "path":{"key_path":["aWJj","Y29ubmVjdGlvbnMvY29ubmVjdGlvbi0w"]}, + "value":"Cg8wNy10ZW5kZXJtaW50LTASIwoBMRINT1JERVJfT1JERVJFRBIPT1JERVJfVU5PUkRFUkVEGAIiIAoJMDgtd2FzbS0wEgxjb25uZWN0aW9uLTAaBQoDaWJj" + } + }"#; + assert!(matches!( + serde_json::from_str::(sudo_msg).unwrap(), + SudoMsg::VerifyMembership(_) + )); + } + + #[test] + fn instantiate_msg_from_json() { + let instantiate_msg = r#"{ + "client_state":"Y2xpZW50X3N0YXRlCg==", + "consensus_state":"Y29uc2Vuc3VzX3N0YXRlCg==", + "checksum":"Y2hlY2tzdW0K" + }"#; + serde_json::from_str::(instantiate_msg).unwrap(); + } +} diff --git a/ibc-clients/ics08-wasm/types/Cargo.toml b/ibc-clients/ics08-wasm/types/Cargo.toml index 70b62e6a1..393c5815f 100644 --- a/ibc-clients/ics08-wasm/types/Cargo.toml +++ b/ibc-clients/ics08-wasm/types/Cargo.toml @@ -17,10 +17,10 @@ description = """ [dependencies] # external dependencies -base64 = { workspace = true, features = [ "alloc" ] } -displaydoc = { workspace = true } -serde = { workspace = true, optional = true } -cosmwasm-schema = { workspace = true, optional = true } +base64 = { workspace = true, features = [ "alloc" ] } +displaydoc = { workspace = true } +schemars = { workspace = true, optional = true } +serde = { workspace = true, optional = true } # ibc dependencies ibc-core-client = { workspace = true } @@ -54,10 +54,7 @@ schema = [ "ibc-core-host-types/schema", "ibc-primitives/schema", "ibc-proto/json-schema", + "schemars", "serde", "std", ] -cosmwasm = [ - "cosmwasm-schema", - "schema", -] diff --git a/ibc-clients/ics08-wasm/types/src/client_state.rs b/ibc-clients/ics08-wasm/types/src/client_state.rs index 4dda117a0..e9570b320 100644 --- a/ibc-clients/ics08-wasm/types/src/client_state.rs +++ b/ibc-clients/ics08-wasm/types/src/client_state.rs @@ -1,28 +1,26 @@ //! Defines the client state type for the ICS-08 Wasm light client. -#[cfg(feature = "cosmwasm")] -use cosmwasm_schema::cw_serde; 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")] +#[cfg(feature = "serde")] 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))] -#[derive(Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ClientState { - #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] - #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] + #[cfg_attr(feature = "schema", schemars(with = "String"))] + #[cfg_attr(feature = "serde", serde(with = "Base64", default))] pub data: Bytes, - #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] - #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] + #[cfg_attr(feature = "schema", schemars(with = "String"))] + #[cfg_attr(feature = "serde", serde(with = "Base64", default))] pub checksum: Bytes, pub latest_height: 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..597cc4e87 100644 --- a/ibc-clients/ics08-wasm/types/src/consensus_state.rs +++ b/ibc-clients/ics08-wasm/types/src/consensus_state.rs @@ -1,24 +1,22 @@ //! Defines the consensus state type for the ICS-08 Wasm light client. -#[cfg(feature = "cosmwasm")] -use cosmwasm_schema::cw_serde; 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")] +#[cfg(feature = "serde")] 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))] -#[derive(Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ConsensusState { - #[cfg_attr(feature = "cosmwasm", schemars(with = "String"))] - #[cfg_attr(feature = "cosmwasm", serde(with = "Base64", default))] + #[cfg_attr(feature = "schema", schemars(with = "String"))] + #[cfg_attr(feature = "serde", serde(with = "Base64", default))] pub data: Bytes, } diff --git a/ibc-clients/ics08-wasm/types/src/lib.rs b/ibc-clients/ics08-wasm/types/src/lib.rs index 98bd27915..169eb3b30 100644 --- a/ibc-clients/ics08-wasm/types/src/lib.rs +++ b/ibc-clients/ics08-wasm/types/src/lib.rs @@ -18,7 +18,7 @@ pub mod consensus_state; pub mod error; pub mod msgs; -#[cfg(feature = "cosmwasm")] +#[cfg(feature = "serde")] pub mod serializer; use core::str::FromStr; diff --git a/ibc-core/ics23-commitment/types/src/commitment.rs b/ibc-core/ics23-commitment/types/src/commitment.rs index d9a911135..2555f89ac 100644 --- a/ibc-core/ics23-commitment/types/src/commitment.rs +++ b/ibc-core/ics23-commitment/types/src/commitment.rs @@ -149,6 +149,12 @@ pub struct CommitmentPrefix { } impl CommitmentPrefix { + pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Self { + Self { + bytes: bytes.as_ref().to_vec(), + } + } + pub fn as_bytes(&self) -> &[u8] { &self.bytes } diff --git a/ibc-core/ics24-host/types/src/path.rs b/ibc-core/ics24-host/types/src/path.rs index c9e384989..bd6b120de 100644 --- a/ibc-core/ics24-host/types/src/path.rs +++ b/ibc-core/ics24-host/types/src/path.rs @@ -51,6 +51,10 @@ pub const UPGRADED_CLIENT_CONSENSUS_STATE: &str = "upgradedConsState"; pub struct PathBytes(Vec); impl PathBytes { + pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Self { + Self(bytes.as_ref().to_vec()) + } + pub fn is_empty(&self) -> bool { self.0.is_empty() } @@ -60,10 +64,10 @@ impl PathBytes { } /// Flattens a list of path bytes into a single path. - pub fn flatten(paths: Vec) -> Self { + pub fn flatten>(paths: Vec) -> Self { let mut bytes = Vec::new(); paths.iter().for_each(|path| { - bytes.extend_from_slice(&path.0); + bytes.extend_from_slice(path.as_ref()); }); Self(bytes) } diff --git a/ibc-data-types/Cargo.toml b/ibc-data-types/Cargo.toml index da618570d..3c4338dc4 100644 --- a/ibc-data-types/Cargo.toml +++ b/ibc-data-types/Cargo.toml @@ -95,6 +95,3 @@ parity-scale-codec = [ "ibc-client-tendermint-types/parity-scale-codec", "ibc-primitives/parity-scale-codec", ] -cosmwasm = [ - "ibc-client-wasm-types/cosmwasm", -] diff --git a/ibc-testkit/Cargo.toml b/ibc-testkit/Cargo.toml index 05310e28b..54830887e 100644 --- a/ibc-testkit/Cargo.toml +++ b/ibc-testkit/Cargo.toml @@ -46,6 +46,7 @@ rstest = { workspace = true } [features] default = [ "std" ] std = [ + "hex/std", "serde/std", "serde-json/std", "ibc/std", diff --git a/tests-integration/tests/cosmwasm/helper.rs b/tests-integration/tests/cosmwasm/helper.rs index e32e4d3e4..b0ba53b0c 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, Checksum, Env, MessageInfo, Timestamp as CwTimestamp}; +use cosmwasm_std::{coins, Binary, Checksum, Env, MessageInfo, Timestamp as CwTimestamp}; use ibc::clients::tendermint::types::ConsensusState; use ibc::core::primitives::Timestamp as IbcTimestamp; use tendermint::Hash; @@ -13,9 +13,12 @@ pub fn dummy_msg_info() -> MessageInfo { message_info(&creator, &coins(1000, "ibc")) } -pub fn dummy_checksum() -> Checksum { - Checksum::from_hex("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") - .expect("Never fails") +pub fn dummy_checksum() -> Binary { + let hex_bytes = + Checksum::from_hex("2469f43c3ca20d476442bd3d98cbd97a180776ab37332aa7b02cae5a620acfc6") + .expect("Never fails"); + + hex_bytes.as_slice().into() } pub fn dummy_sov_consensus_state(timestamp: IbcTimestamp) -> ConsensusState {