Skip to content

Commit

Permalink
feat: allow empty CommitmentPrefix (#1274)
Browse files Browse the repository at this point in the history
* feat: allow empty CommitmentPrefix

* fix: use from instead of try_from

* use empty commitmentprefix

* fix: cargo msrv by requireing cosmwasm to stay at 2.0.4

* fix: specify cosmwasm-derive version

* fix: remove now unnecessary EmptyPrefix error variant

---------

Co-authored-by: Ranadeep Biswas <[email protected]>
  • Loading branch information
Farhad-Shabani and rnbguy authored Jul 11, 2024
1 parent df9653a commit 0202dec
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ibc-core-commitment-types] Allow empty `CommitmentPrefix`
([#1006](https://github.com/cosmos/ibc-rs/issues/1006)).
4 changes: 2 additions & 2 deletions ibc-clients/cw-context/src/types/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl TryFrom<VerifyMembershipMsgRaw> for VerifyMembershipMsg {
path,
value: raw.value.into(),
height,
prefix: CommitmentPrefix::try_from(prefix)?,
prefix: CommitmentPrefix::from(prefix),
delay_block_period: raw.delay_block_period,
delay_time_period: raw.delay_time_period,
})
Expand Down Expand Up @@ -194,7 +194,7 @@ impl TryFrom<VerifyNonMembershipMsgRaw> for VerifyNonMembershipMsg {
proof,
path,
height,
prefix: CommitmentPrefix::try_from(prefix)?,
prefix: CommitmentPrefix::from(prefix),
delay_block_period: raw.delay_block_period,
delay_time_period: raw.delay_time_period,
})
Expand Down
10 changes: 8 additions & 2 deletions ibc-clients/ics07-tendermint/src/client_state/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ibc_core_client::types::Height;
use ibc_core_commitment_types::commitment::{
CommitmentPrefix, CommitmentProofBytes, CommitmentRoot,
};
use ibc_core_commitment_types::error::CommitmentError;
use ibc_core_commitment_types::merkle::{apply_prefix, MerkleProof};
use ibc_core_commitment_types::proto::ics23::{HostFunctionsManager, HostFunctionsProvider};
use ibc_core_commitment_types::specs::ProofSpecs;
Expand Down Expand Up @@ -175,8 +176,7 @@ pub fn verify_upgrade_client<H: HostFunctionsProvider>(
});
};

let upgrade_path_prefix = CommitmentPrefix::try_from(upgrade_path[0].clone().into_bytes())
.map_err(ClientError::InvalidCommitmentProof)?;
let upgrade_path_prefix = CommitmentPrefix::from(upgrade_path[0].clone().into_bytes());

let last_height = latest_height.revision_height();

Expand Down Expand Up @@ -216,6 +216,12 @@ pub fn verify_membership<H: HostFunctionsProvider>(
path: Path,
value: Vec<u8>,
) -> Result<(), ClientError> {
if prefix.is_empty() {
return Err(ClientError::Ics23Verification(
CommitmentError::EmptyCommitmentPrefix,
));
}

let merkle_path = apply_prefix(prefix, vec![path.to_string()]);
let merkle_proof = MerkleProof::try_from(proof).map_err(ClientError::InvalidCommitmentProof)?;

Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics02-client/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ pub enum ClientError {
FailedTrustThresholdConversion { numerator: u64, denominator: u64 },
/// unknown client state type: `{client_state_type}`
UnknownClientStateType { client_state_type: String },
/// empty prefix
EmptyPrefix,
/// unknown client consensus state type: `{consensus_state_type}`
UnknownConsensusStateType { consensus_state_type: String },
/// unknown header type: `{header_type}`
Expand Down
4 changes: 1 addition & 3 deletions ibc-core/ics03-connection/types/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use core::fmt::{Display, Error as FmtError, Formatter};
use core::time::Duration;

use ibc_core_client_types::error::ClientError;
use ibc_core_commitment_types::commitment::CommitmentPrefix;
use ibc_core_host_types::identifiers::{ClientId, ConnectionId};
use ibc_primitives::prelude::*;
Expand Down Expand Up @@ -392,8 +391,7 @@ impl TryFrom<RawCounterparty> for Counterparty {
.prefix
.ok_or(ConnectionError::MissingCounterparty)?
.key_prefix
.try_into()
.map_err(|_| ConnectionError::Client(ClientError::EmptyPrefix))?,
.into(),
))
}
}
Expand Down
14 changes: 3 additions & 11 deletions ibc-core/ics23-commitment/types/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'a> TryFrom<&'a CommitmentProofBytes> for MerkleProof {
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, derive_more::From)]
pub struct CommitmentPrefix {
bytes: Vec<u8>,
}
Expand All @@ -160,17 +160,9 @@ impl CommitmentPrefix {
pub fn empty() -> Self {
Self { bytes: Vec::new() }
}
}

impl TryFrom<Vec<u8>> for CommitmentPrefix {
type Error = CommitmentError;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
if bytes.is_empty() {
Err(Self::Error::EmptyCommitmentPrefix)
} else {
Ok(Self { bytes })
}
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
}

Expand Down
2 changes: 1 addition & 1 deletion ibc-testkit/src/testapp/ibc/core/core_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
fn commitment_prefix(&self) -> CommitmentPrefix {
// this is prefix of ibc store
// using a dummy prefix as in our mock context
CommitmentPrefix::try_from(b"mock".to_vec()).expect("Never fails")
CommitmentPrefix::from(b"mock".to_vec())
}

fn connection_counter(&self) -> Result<u64, ContextError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn conn_open_ack_fixture(ctx: Ctx) -> Fixture<MsgConnectionOpenAck> {
Counterparty::new(
client_id.clone(),
Some(msg.conn_id_on_b.clone()),
CommitmentPrefix::try_from(b"ibc".to_vec()).unwrap(),
CommitmentPrefix::from(b"ibc".to_vec()),
),
vec![msg.version.clone()],
ZERO_DURATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn conn_open_confirm_fixture(ctx: Ctx) -> Fixture<MsgConnectionOpenConfirm> {
let counterparty = Counterparty::new(
client_id.clone(),
Some(msg.conn_id_on_b.clone()),
CommitmentPrefix::try_from(b"ibc".to_vec()).unwrap(),
CommitmentPrefix::from(b"ibc".to_vec()),
);

let ctx_default = MockContext::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn fixture() -> Fixture {
ConnectionCounterparty::new(
default_client_id,
Some(ConnectionId::zero()),
CommitmentPrefix::try_from(vec![0]).expect("no error"),
CommitmentPrefix::empty(),
),
ConnectionVersion::compatibles(),
ZERO_DURATION,
Expand Down
2 changes: 1 addition & 1 deletion tests-integration/tests/core/ics04_channel/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn fixture() -> Fixture {
ConnectionCounterparty::new(
client_id.clone(),
Some(ConnectionId::zero()),
CommitmentPrefix::try_from(vec![0]).expect("no error"),
CommitmentPrefix::empty(),
),
ConnectionVersion::compatibles(),
ZERO_DURATION,
Expand Down
2 changes: 1 addition & 1 deletion tests-integration/tests/core/ics04_channel/send_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn send_packet_processing() {
ConnectionCounterparty::new(
default_client_id,
Some(ConnectionId::zero()),
CommitmentPrefix::try_from(vec![0]).expect("no error"),
CommitmentPrefix::empty(),
),
ConnectionVersion::compatibles(),
ZERO_DURATION,
Expand Down
2 changes: 1 addition & 1 deletion tests-integration/tests/core/ics04_channel/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn fixture() -> Fixture {
ConnectionCounterparty::new(
client_id.clone(),
Some(ConnectionId::zero()),
CommitmentPrefix::try_from(vec![0]).expect("no error"),
CommitmentPrefix::empty(),
),
ConnectionVersion::compatibles(),
ZERO_DURATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn fixture() -> Fixture {
ConnectionCounterparty::new(
default_client_id,
Some(ConnectionId::zero()),
CommitmentPrefix::try_from(vec![0]).expect("no error"),
CommitmentPrefix::empty(),
),
ConnectionVersion::compatibles(),
ZERO_DURATION,
Expand Down

0 comments on commit 0202dec

Please sign in to comment.