From cb83f438f33b6aa1e20c4395dff2dbf6f6020cf2 Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Thu, 18 Jul 2024 16:56:39 -0700 Subject: [PATCH] fix: use Binary for MeklePath + some helper methods --- ibc-clients/cw-context/src/types/msgs.rs | 10 +++++++--- ibc-core/ics23-commitment/types/src/commitment.rs | 6 ++++++ ibc-core/ics24-host/types/src/path.rs | 8 ++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/ibc-clients/cw-context/src/types/msgs.rs b/ibc-clients/cw-context/src/types/msgs.rs index fbbe4740b..25b55313b 100644 --- a/ibc-clients/cw-context/src/types/msgs.rs +++ b/ibc-clients/cw-context/src/types/msgs.rs @@ -5,7 +5,6 @@ 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; @@ -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()?; 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) }