Skip to content

Commit

Permalink
Disallow empty CommitmentPrefix and CommitmentProofBytes (informa…
Browse files Browse the repository at this point in the history
…lsystems#1761)

* Disallow empty CommitmentPrefix and CommitmentProofBytes

* Fix errors in tests

* Fix failing tests

* Fix clippy errors

* Add .changelog entry

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
hu55a1n1 and romac authored Jan 17, 2022
1 parent 6ba901e commit 71cea7d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Disallow empty `CommitmentPrefix` and `CommitmentProofBytes`
([#1761](https://github.com/informalsystems/ibc-rs/issues/1761))
27 changes: 19 additions & 8 deletions relayer/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloc::sync::Arc;
use core::convert::TryFrom;

use prost_types::Any;
use tendermint::block::Height;
use tokio::runtime::Runtime as TokioRuntime;

Expand Down Expand Up @@ -372,15 +375,19 @@ pub trait ChainEndpoint: Sized {
let (client_state_value, client_state_proof) =
self.proven_client_state(client_id, height)?;

client_proof = Some(CommitmentProofBytes::from(client_state_proof));
client_proof = Some(
CommitmentProofBytes::try_from(client_state_proof)
.map_err(Error::malformed_proof)?,
);

let consensus_state_proof = self
.proven_client_consensus(client_id, client_state_value.latest_height(), height)?
.1;

consensus_proof = Option::from(
ConsensusProof::new(
CommitmentProofBytes::from(consensus_state_proof),
CommitmentProofBytes::try_from(consensus_state_proof)
.map_err(Error::malformed_proof)?,
client_state_value.latest_height(),
)
.map_err(Error::consensus_proof)?,
Expand All @@ -394,7 +401,7 @@ pub trait ChainEndpoint: Sized {
Ok((
client_state,
Proofs::new(
CommitmentProofBytes::from(connection_proof),
CommitmentProofBytes::try_from(connection_proof).map_err(Error::malformed_proof)?,
client_proof,
consensus_proof,
None,
Expand All @@ -413,7 +420,8 @@ pub trait ChainEndpoint: Sized {
) -> Result<Proofs, Error> {
// Collect all proofs as required
let channel_proof =
CommitmentProofBytes::from(self.proven_channel(port_id, channel_id, height)?.1);
CommitmentProofBytes::try_from(self.proven_channel(port_id, channel_id, height)?.1)
.map_err(Error::malformed_proof)?;

Proofs::new(channel_proof, None, None, None, height.increment())
.map_err(Error::malformed_proof)
Expand All @@ -429,9 +437,12 @@ pub trait ChainEndpoint: Sized {
height: ICSHeight,
) -> Result<(Vec<u8>, Proofs), Error> {
let channel_proof = if packet_type == PacketMsgType::TimeoutOnClose {
Some(CommitmentProofBytes::from(
self.proven_channel(&port_id, &channel_id, height)?.1,
))
Some(
CommitmentProofBytes::try_from(
self.proven_channel(&port_id, &channel_id, height)?.1,
)
.map_err(Error::malformed_proof)?,
)
} else {
None
};
Expand All @@ -440,7 +451,7 @@ pub trait ChainEndpoint: Sized {
self.proven_packet(packet_type, port_id, channel_id, sequence, height)?;

let proofs = Proofs::new(
CommitmentProofBytes::from(packet_proof),
CommitmentProofBytes::try_from(packet_proof).map_err(Error::malformed_proof)?,
None,
None,
channel_proof,
Expand Down
6 changes: 3 additions & 3 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use ibc::core::ics02_client::client_consensus::{
};
use ibc::core::ics02_client::client_state::{AnyClientState, IdentifiedAnyClientState};
use ibc::core::ics02_client::client_type::ClientType;
use ibc::core::ics02_client::error::Error as ClientError;
use ibc::core::ics02_client::events as ClientEvents;
use ibc::core::ics03_connection::connection::{ConnectionEnd, IdentifiedConnectionEnd};
use ibc::core::ics04_channel;
Expand Down Expand Up @@ -1191,9 +1192,8 @@ impl ChainEndpoint for CosmosSdkChain {
crate::time!("query_commitment_prefix");

// TODO - do a real chain query
Ok(CommitmentPrefix::from(
self.config().store_prefix.as_bytes().to_vec(),
))
CommitmentPrefix::try_from(self.config().store_prefix.as_bytes().to_vec())
.map_err(|_| Error::ics02(ClientError::empty_prefix()))
}

/// Query the chain status
Expand Down

0 comments on commit 71cea7d

Please sign in to comment.