Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(08-wasm): supersede Bytes with cosmwasm_std::Binary #1272

Merged
merged 9 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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::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 @@

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(),

Check warning on line 243 in ibc-clients/cw-context/src/context/mod.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/context/mod.rs#L242-L243

Added lines #L242 - L243 were not covered by tests
});
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(),

Check warning on line 248 in ibc-clients/cw-context/src/context/mod.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/context/mod.rs#L247-L248

Added lines #L247 - L248 were not covered by tests
});
}

Expand All @@ -265,8 +263,8 @@
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(),

Check warning on line 267 in ibc-clients/cw-context/src/context/mod.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/context/mod.rs#L266-L267

Added lines #L266 - L267 were not covered by tests
});
}

Expand All @@ -276,7 +274,7 @@
/// 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 @@
}
})?;

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(),
}

Check warning on line 292 in ibc-clients/cw-context/src/context/mod.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/context/mod.rs#L290-L292

Added lines #L290 - L292 were not covered by tests
})?;

Ok(checksum)
}
}
}
Expand All @@ -298,7 +303,7 @@
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 @@

#[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 @@

#[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 @@

#[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 @@

#[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 @@
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(),
)?,

Check warning on line 111 in ibc-clients/cw-context/src/types/msgs.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/types/msgs.rs#L109-L111

Added lines #L109 - L111 were not covered by tests
proof_upgrade_consensus_state: CommitmentProofBytes::try_from(
raw.proof_upgrade_consensus_state,
raw.proof_upgrade_consensus_state.to_vec(),

Check warning on line 113 in ibc-clients/cw-context/src/types/msgs.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/types/msgs.rs#L113

Added line #L113 was not covered by tests
)?,
})
}
Expand All @@ -140,13 +123,9 @@

#[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 @@
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())?;

Check warning on line 148 in ibc-clients/cw-context/src/types/msgs.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/types/msgs.rs#L148

Added line #L148 was not covered by tests
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 @@
Ok(Self {
proof,
path,
value: raw.value,
value: raw.value.into(),

Check warning on line 157 in ibc-clients/cw-context/src/types/msgs.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/types/msgs.rs#L157

Added line #L157 was not covered by tests
height,
prefix: CommitmentPrefix::try_from(prefix)?,
delay_block_period: raw.delay_block_period,
Expand All @@ -186,9 +165,7 @@

#[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 @@
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())?;

Check warning on line 188 in ibc-clients/cw-context/src/types/msgs.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/cw-context/src/types/msgs.rs#L188

Added line #L188 was not covered by tests
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 @@

#[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 @@

#[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;
rnbguy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading