Skip to content

Commit

Permalink
Expose verify_connection_hops_length for chan_open_init/try
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed May 5, 2023
1 parent 7f65497 commit cb71f2c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
37 changes: 22 additions & 15 deletions crates/ibc/src/core/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,6 @@ impl ChannelEnd {
});
}

// Current IBC version only supports one connection hop.
self.verify_connection_hops_length(1)?;

Ok(())
}

/// Checks if the `connection_hops` has a length of `expected`.
pub fn verify_connection_hops_length(&self, expected: usize) -> Result<(), ChannelError> {
if self.connection_hops.len() != expected {
return Err(ChannelError::InvalidConnectionHopsLength {
expected,
actual: self.connection_hops.len(),
});
}
Ok(())
}

Expand Down Expand Up @@ -310,11 +296,32 @@ impl ChannelEnd {
Ok(())
}

/// Checks if the `connection_hops` has a length of `expected`.
///
/// Note: Current IBC version only supports one connection hop.
pub fn verify_connection_hops_length(&self) -> Result<(), ChannelError> {
verify_connection_hops_length(&self.connection_hops, 1)
}

pub fn version_matches(&self, other: &Version) -> bool {
self.version().eq(other)
}
}

/// Checks if the `connection_hops` has a length of `expected`.
pub(crate) fn verify_connection_hops_length(
connection_hops: &Vec<ConnectionId>,
expected: usize,
) -> Result<(), ChannelError> {
if connection_hops.len() != expected {
return Err(ChannelError::InvalidConnectionHopsLength {
expected,
actual: connection_hops.len(),
});
}
Ok(())
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
Expand Down Expand Up @@ -664,7 +671,7 @@ mod tests {
.collect(),
..raw_channel_end.clone()
},
want_pass: false,
want_pass: true,
},
Test {
name: "Raw channel end with correct params".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
chan_end_on_a.verify_not_closed()?;

// An OPEN IBC connection running on the local (host) chain should exist.
chan_end_on_a.verify_connection_hops_length(1)?;
chan_end_on_a.verify_connection_hops_length()?;

let conn_end_on_a = ctx_a.connection_end(&chan_end_on_a.connection_hops()[0])?;

Expand Down
2 changes: 1 addition & 1 deletion crates/ibc/src/core/ics04_channel/handler/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
chan_end_on_a.verify_state_matches(&ChannelState::Init)?;

// An OPEN IBC connection running on the local (host) chain should exist.
chan_end_on_a.verify_connection_hops_length(1)?;
chan_end_on_a.verify_connection_hops_length()?;

let conn_end_on_a = ctx_a.connection_end(&chan_end_on_a.connection_hops()[0])?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
chan_end_on_b.verify_state_matches(&ChannelState::TryOpen)?;

// An OPEN IBC connection running on the local (host) chain should exist.
chan_end_on_b.verify_connection_hops_length(1)?;
chan_end_on_b.verify_connection_hops_length()?;

let conn_end_on_b = ctx_b.connection_end(&chan_end_on_b.connection_hops()[0])?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ where
{
ctx_a.validate_message_signer(&msg.signer)?;

msg.verify_connection_hops_length()?;
// An IBC connection running on the local (host) chain should exist.
let conn_end_on_a = ctx_a.connection_end(&msg.connection_hops_on_a[0])?;

Expand Down
2 changes: 2 additions & 0 deletions crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ where
{
ctx_b.validate_message_signer(&msg.signer)?;

msg.verify_connection_hops_length()?;

let conn_end_on_b = ctx_b.connection_end(&msg.connection_hops_on_b[0])?;

conn_end_on_b.verify_state_matches(&ConnectionState::Open)?;
Expand Down
10 changes: 10 additions & 0 deletions crates/ibc/src/core/ics04_channel/msgs/chan_open_init.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::ics04_channel::channel::verify_connection_hops_length;
use crate::core::ics04_channel::channel::ChannelEnd;
use crate::core::ics04_channel::channel::Counterparty;
use crate::core::ics04_channel::channel::{Order, State};
Expand Down Expand Up @@ -28,6 +29,15 @@ pub struct MsgChannelOpenInit {
pub version_proposal: Version,
}

impl MsgChannelOpenInit {
/// Checks if the `connection_hops` has a length of `expected`.
///
/// Note: Current IBC version only supports one connection hop.
pub(crate) fn verify_connection_hops_length(&self) -> Result<(), ChannelError> {
verify_connection_hops_length(&self.connection_hops_on_a, 1)
}
}

impl Msg for MsgChannelOpenInit {
type Raw = RawMsgChannelOpenInit;

Expand Down
10 changes: 10 additions & 0 deletions crates/ibc/src/core/ics04_channel/msgs/chan_open_try.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::ics04_channel::channel::verify_connection_hops_length;
use crate::core::ics04_channel::channel::ChannelEnd;
use crate::core::ics04_channel::channel::Counterparty;
use crate::core::ics04_channel::channel::{Order, State};
Expand Down Expand Up @@ -35,6 +36,15 @@ pub struct MsgChannelOpenTry {
pub version_proposal: Version,
}

impl MsgChannelOpenTry {
/// Checks if the `connection_hops` has a length of `expected`.
///
/// Note: Current IBC version only supports one connection hop.
pub(crate) fn verify_connection_hops_length(&self) -> Result<(), ChannelError> {
verify_connection_hops_length(&self.connection_hops_on_b, 1)
}
}

impl Msg for MsgChannelOpenTry {
type Raw = RawMsgChannelOpenTry;

Expand Down

0 comments on commit cb71f2c

Please sign in to comment.