Skip to content

Commit

Permalink
fix: use Binary for MeklePath + some helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Jul 18, 2024
1 parent 4453cbd commit cb83f43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 7 additions & 3 deletions ibc-clients/cw-context/src/types/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,6 +114,11 @@ impl TryFrom<VerifyUpgradeAndUpdateStateMsgRaw> for VerifyUpgradeAndUpdateStateM
}
}

#[cw_serde]

Check warning on line 117 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#L117

Added line #L117 was not covered by tests
pub struct MerklePath {
pub key_path: Vec<Binary>,
}

#[cw_serde]
pub struct VerifyMembershipMsgRaw {
pub proof: Binary,
Expand All @@ -140,7 +144,7 @@ impl TryFrom<VerifyMembershipMsgRaw> for VerifyMembershipMsg {

fn try_from(mut raw: VerifyMembershipMsgRaw) -> Result<Self, Self::Error> {
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));

Check warning on line 147 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#L147

Added line #L147 was not covered by tests
let path = PathBytes::flatten(raw.path.key_path);
let height = Height::try_from(raw.height)?;

Expand Down Expand Up @@ -179,7 +183,7 @@ impl TryFrom<VerifyNonMembershipMsgRaw> for VerifyNonMembershipMsg {

fn try_from(mut raw: VerifyNonMembershipMsgRaw) -> Result<Self, Self::Error> {
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));

Check warning on line 186 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#L186

Added line #L186 was not covered by tests
let path = PathBytes::flatten(raw.path.key_path);
let height = raw.height.try_into()?;

Expand Down
6 changes: 6 additions & 0 deletions ibc-core/ics23-commitment/types/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Check warning on line 156 in ibc-core/ics23-commitment/types/src/commitment.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics23-commitment/types/src/commitment.rs#L152-L156

Added lines #L152 - L156 were not covered by tests

pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
Expand Down
8 changes: 6 additions & 2 deletions ibc-core/ics24-host/types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub const UPGRADED_CLIENT_CONSENSUS_STATE: &str = "upgradedConsState";
pub struct PathBytes(Vec<u8>);

impl PathBytes {
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
Self(bytes.as_ref().to_vec())
}

Check warning on line 56 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L54-L56

Added lines #L54 - L56 were not covered by tests

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
Expand All @@ -60,10 +64,10 @@ impl PathBytes {
}

/// Flattens a list of path bytes into a single path.
pub fn flatten(paths: Vec<PathBytes>) -> Self {
pub fn flatten<T: AsRef<[u8]>>(paths: Vec<T>) -> Self {

Check warning on line 67 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L67

Added line #L67 was not covered by tests
let mut bytes = Vec::new();
paths.iter().for_each(|path| {
bytes.extend_from_slice(&path.0);
bytes.extend_from_slice(path.as_ref());

Check warning on line 70 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L70

Added line #L70 was not covered by tests
});
Self(bytes)
}
Expand Down

0 comments on commit cb83f43

Please sign in to comment.