Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler for channel open init #452

Merged
merged 35 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
79caad1
handler for channel open init
cezarad Dec 17, 2020
b177fe5
formated
cezarad Dec 17, 2020
3cd7b8f
Update modules/src/ics04_channel/handler/chan_open_init.rs
cezarad Dec 21, 2020
d2e1dff
Update modules/src/ics04_channel/handler/chan_open_init.rs
cezarad Dec 21, 2020
4beb025
Update modules/src/ics04_channel/context.rs
cezarad Dec 21, 2020
0a7aa38
chan open init Romains comments
cezarad Dec 21, 2020
af514ec
added port verification
cezarad Jan 7, 2021
5a62fd5
Merge branch 'master' into Cezara
cezarad Jan 7, 2021
1054aee
chan open init
cezarad Jan 7, 2021
d899101
chan open init
cezarad Jan 7, 2021
08b54fb
minor fmt
cezarad Jan 8, 2021
0f5f16d
chan open init
cezarad Jan 8, 2021
17fe71f
Update modules/src/ics04_channel/channel.rs
cezarad Jan 12, 2021
791f5a1
Update modules/src/ics04_channel/channel.rs
cezarad Jan 12, 2021
bffcb1a
Update modules/src/ics04_channel/context.rs
cezarad Jan 12, 2021
8580c2c
Update modules/src/ics04_channel/context.rs
cezarad Jan 12, 2021
b2f8105
Update error.rs
cezarad Jan 12, 2021
f57b658
Update modules/src/ics04_channel/error.rs
cezarad Jan 12, 2021
e0afeb1
Update modules/src/ics04_channel/channel.rs
cezarad Jan 14, 2021
823eb49
Update modules/src/ics04_channel/context.rs
cezarad Jan 14, 2021
0f37d37
Update context.rs
cezarad Jan 14, 2021
3e40a36
minor
cezarad Jan 14, 2021
cbcaa41
minor
cezarad Jan 14, 2021
e4876e1
test for port
cezarad Jan 14, 2021
2448106
connetion features
cezarad Jan 15, 2021
24a3ae8
connetion features
cezarad Jan 15, 2021
839f675
connetion features
cezarad Jan 15, 2021
a9611fa
review comments
cezarad Jan 15, 2021
ea84402
Merge branch 'master' into Cezara
cezarad Jan 15, 2021
053f0e0
Small refactor to improve readability
romac Jan 15, 2021
1426985
Cleanup ics05_port::capabilities
romac Jan 15, 2021
2d7bc35
Replace panic! with unwrap and avoid some clones in mock context
romac Jan 15, 2021
107236e
Avoid clones in MockContext::add_port
romac Jan 15, 2021
768dfd6
Only clone the channel id
romac Jan 15, 2021
05017f5
Fix a couple comments
romac Jan 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added port verification
  • Loading branch information
cezarad committed Jan 7, 2021
commit af514ec7c63ca0a655d33e9c20b86914a17d0062
17 changes: 1 addition & 16 deletions modules/src/ics03_connection/msgs/conn_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,9 @@ impl From<MsgConnectionOpenInit> for RawMsgConnectionOpenInit {
pub mod test_util {
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenInit as RawMsgConnectionOpenInit;

use crate::ics03_connection::connection::{ConnectionEnd, Counterparty, State};
use crate::ics03_connection::error::Error;
use crate::ics03_connection::msgs::test_util::get_dummy_counterparty;
use crate::ics03_connection::version::default_version_string;
use crate::test_utils::get_dummy_bech32_account;
use crate::{
ics03_connection::msgs::test_util::get_dummy_counterparty, ics24_host::identifier::ClientId,
};

/// Returns a dummy message, for testing only.
/// Other unit tests may import this if they depend on a MsgConnectionOpenInit.
Expand All @@ -134,17 +130,6 @@ pub mod test_util {
}
}

pub fn make_default_connection_end_for_client(
client_id: ClientId,
counterparty: Counterparty,
) -> Result<ConnectionEnd, Error> {
return ConnectionEnd::new(
State::Init,
client_id,
counterparty,
vec![default_version_string()],
);
}
}

#[cfg(test)]
Expand Down
10 changes: 5 additions & 5 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl From<ChannelEnd> for RawChannel {
RawChannel {
state: value.state.clone() as i32,
ordering: value.ordering as i32,
counterparty: Some(value.counterparty().into()),
counterparty: Some(value.counterparty().clone().into()),
connection_hops: value
.connection_hops
.iter()
Expand Down Expand Up @@ -104,12 +104,12 @@ impl ChannelEnd {
&self.ordering
}

pub fn counterparty(&self) -> Counterparty {
self.remote.clone()
pub fn counterparty(&self) -> &Counterparty {
&self.remote //.clone()
}

pub fn connection_hops(&self) -> Vec<ConnectionId> {
self.connection_hops.clone()
pub fn connection_hops(&self) -> &Vec<ConnectionId> {
&self.connection_hops //.clone()
}

pub fn version(&self) -> String {
Expand Down
35 changes: 32 additions & 3 deletions modules/src/ics04_channel/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@
//! the interface that any host chain must implement to be able to process any `ChannelMsg`.
//! TODO make "ADR 004: IBC protocol implementation" for more details.
//!
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics03_connection::connection::ConnectionEnd;
use crate::ics04_channel::channel::{ChannelEnd, State};
use crate::ics04_channel::error::Error;
use crate::ics04_channel::handler::ChannelResult;
use crate::ics04_channel::version::{get_compatible_versions, pick_version};
use crate::ics05_port::capabilities::Capability;
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::Height;

/// A context supplying all the necessary read-only dependencies for processing any `ConnectionMsg`.
/// A context supplying all the necessary read-only dependencies for processing any `ChannelMsg`.
pub trait ChannelReader {
/// Returns the ChannelEnd for the given identifier `chan_id`.
fn channel_end(&self, port_channel_id: &(PortId, ChannelId)) -> Option<ChannelEnd>;

/// Returns the ConnectionState for the given identifier `connection_id`.
fn connection_state(&self, connection_id: &ConnectionId) -> Option<ConnectionEnd>;

fn connection_channels(&self, cid: &ConnectionId) -> Option<Vec<(PortId, ChannelId)>>;

fn channel_client_state(&self, port_channel_id: &(PortId, ChannelId))
-> Option<AnyClientState>;

fn channel_consensus_state(
&self,
port_channel_id: &(PortId, ChannelId),
height: Height,
) -> Option<AnyConsensusState>;

fn port_capability(&self, port_id: &PortId) -> Option<Capability>;

fn capability_authentification(&self, port_id: &PortId, cap: &Capability) -> bool;

/// Function required by ICS 04. Returns the list of all possible versions that the channels handshake protocol supports.
fn get_compatible_versions(&self) -> Vec<String> {
get_compatible_versions()
Expand All @@ -33,7 +51,7 @@ pub trait ChannelReader {
}

/// A context supplying all the necessary write-only dependencies (i.e., storage writing facility)
/// for processing any `ConnectionMsg`.
/// for processing any `ChannelMsg`.
pub trait ChannelKeeper {
fn store_channel_result(&mut self, result: ChannelResult) -> Result<(), Error> {
match result.channel_end.state() {
Expand All @@ -57,6 +75,11 @@ pub trait ChannelKeeper {
&(result.port_id.clone(), result.channel_id.clone()),
1,
)?;

self.store_connection_channels(
&result.channel_end.connection_hops()[0].clone(),
&(result.port_id.clone(), result.channel_id.clone()),
)?;
}
State::TryOpen => {
self.store_channel(&(result.port_id, result.channel_id), &result.channel_end)?;
Expand All @@ -74,7 +97,13 @@ pub trait ChannelKeeper {
Ok(())
}

/// Stores the given connection_end at a path associated with the connection_id.
fn store_connection_channels(
&mut self,
conn_id: &ConnectionId,
port_channel_id: &(PortId, ChannelId),
) -> Result<(), Error>;

/// Stores the given channel_end at a path associated with the port_id and channel_id.
fn store_channel(
&mut self,
port_channel_id: &(PortId, ChannelId),
Expand Down
6 changes: 6 additions & 0 deletions modules/src/ics04_channel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ pub enum Kind {

#[error("given connection {0} for channel innexitant ")]
MissingConnection(ConnectionId),

#[error("the port has no capability associated")]
NoPortCapability,

#[error("the module associated with the port does not have the capability it needs")]
InvalidPortCapability,
}

impl Kind {
Expand Down
53 changes: 43 additions & 10 deletions modules/src/ics04_channel/handler/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::ics04_channel::channel::{ChannelEnd, State};
use crate::ics04_channel::context::ChannelReader;
use crate::ics04_channel::error::{Error, Kind};
use crate::ics04_channel::handler::ChannelEvent::ChanOpenInit;
//use crate::ics05_port::context::PortReader;
use crate::ics04_channel::handler::ChannelResult;
use crate::ics04_channel::msgs::chan_open_init::MsgChannelOpenInit;

Expand All @@ -19,7 +20,16 @@ pub(crate) fn process(
if ctx.channel_end(&pc_id).is_some() {
return Err(Kind::ChannelExistsAlready(msg.channel_id().clone()).into());
}
//TODO what about the port capabilities ?

let cap = ctx.port_capability(&msg.port_id().clone());
match cap {
Some(key) => {
if !ctx.capability_authentification(&msg.port_id().clone(), &key) {
return Err(Kind::InvalidPortCapability.into());
}
}
None => return Err(Kind::NoPortCapability.into()),
}

if msg.channel().connection_hops().len() != 1 {
return Err(Kind::InvalidConnectionHopsLength.into());
Expand All @@ -41,8 +51,8 @@ pub(crate) fn process(
let new_channel_end = ChannelEnd::new(
State::Init,
*msg.channel().ordering(),
msg.channel().counterparty(),
msg.channel().connection_hops(),
msg.channel().counterparty().clone(),
msg.channel().connection_hops().clone(),
ctx.get_compatible_versions()[0].clone(),
);

Expand All @@ -66,15 +76,17 @@ mod tests {
use crate::handler::EventType;

use crate::ics03_connection::msgs::conn_open_init::test_util::get_dummy_msg_conn_open_init;
use crate::ics03_connection::msgs::conn_open_init::test_util::make_default_connection_end_for_client;

use crate::ics03_connection::connection::State as ConnectionState;
use crate::ics03_connection::msgs::conn_open_init::MsgConnectionOpenInit;

use crate::ics04_channel::channel::{ChannelEnd, State};

use crate::ics03_connection::connection::ConnectionEnd;

use crate::ics04_channel::handler::{dispatch, ChannelResult};
use crate::ics04_channel::msgs::chan_open_init::test_util::get_dummy_raw_msg_chan_open_init;

use crate::ics03_connection::version::default_version_string;
use crate::ics04_channel::msgs::chan_open_init::MsgChannelOpenInit;
use crate::ics04_channel::msgs::ChannelMsg;
use crate::mock::context::MockContext;
Expand All @@ -96,17 +108,19 @@ mod tests {
let init_chan_end = &ChannelEnd::new(
State::Init,
*msg_chan_init.channel().ordering(),
msg_chan_init.channel().counterparty(),
msg_chan_init.channel().connection_hops(),
msg_chan_init.channel().counterparty().clone(),
msg_chan_init.channel().connection_hops().clone(),
"ics20".to_string(),
);

let msg_conn_init =
MsgConnectionOpenInit::try_from(get_dummy_msg_conn_open_init()).unwrap();

let init_conn_end = make_default_connection_end_for_client(
let init_conn_end = ConnectionEnd::new(
ConnectionState::Init,
msg_conn_init.client_id().clone(),
msg_conn_init.counterparty().clone(),
vec![default_version_string()],
)
.unwrap();

Expand All @@ -129,15 +143,34 @@ mod tests {
want_pass: false,
},
Test {
name: "Good parameters".to_string(),
ctx: context.with_connection(
name: "Processing fails because port does not have a capability associated"
.to_string(),
ctx: context.clone().with_connection(
MsgConnectionOpenInit::try_from(get_dummy_msg_conn_open_init())
.unwrap()
.connection_id()
.clone(),
init_conn_end.clone(),
),
msg: ChannelMsg::ChannelOpenInit(msg_chan_init.clone()),
want_pass: false,
},
Test {
name: "Good parameters".to_string(),
ctx: context
.with_connection_capability(
MsgChannelOpenInit::try_from(get_dummy_raw_msg_chan_open_init())
.unwrap()
.port_id()
.clone(),
MsgConnectionOpenInit::try_from(get_dummy_msg_conn_open_init())
.unwrap()
.connection_id()
.clone(),
init_conn_end.clone(),
)
.clone(),
msg: ChannelMsg::ChannelOpenInit(msg_chan_init.clone()),
want_pass: true,
},
]
Expand Down
16 changes: 16 additions & 0 deletions modules/src/ics05_port/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//TODO: this is a module on it's own !

///
/// Capabilities: this is a placeholder.
///

#[derive(Clone, Debug, PartialEq)]
pub struct Capability {
index: u64,
}

impl Capability {
pub fn new() -> Capability {
Self { index: 0x0 }
}
}
10 changes: 10 additions & 0 deletions modules/src/ics05_port/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::ics05_port::capabilities::Capability;
use crate::ics24_host::identifier::PortId;

// A context supplying all the necessary read-only dependencies for processing any information regarding a port.
pub trait PortReader {
fn lookup_module_by_port(&self, port_id: &PortId) -> Option<Capability>;
fn autenthenticate(&self, key: &Capability, port_id: &PortId) -> bool;
}

// Result<Capability, Error>//return Ok(Capability::new());
16 changes: 16 additions & 0 deletions modules/src/ics05_port/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use anomaly::{BoxError, Context};
use thiserror::Error;

pub type Error = anomaly::Error<Kind>;

#[derive(Clone, Debug, Error)]
pub enum Kind {
#[error("port unknown")]
UnknownPort,
}

impl Kind {
pub fn context(self, source: impl Into<BoxError>) -> Context<Self> {
Context::new(self, Some(source.into()))
}
}
3 changes: 3 additions & 0 deletions modules/src/ics05_port/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod capabilities;
pub mod context;
pub mod error;
13 changes: 9 additions & 4 deletions modules/src/ics26_routing/context.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::ics02_client::context::{ClientKeeper, ClientReader};
use crate::ics03_connection::context::{ConnectionKeeper, ConnectionReader};

use crate::ics04_channel::context::{ChannelKeeper, ChannelReader};

use crate::ics05_port::context::PortReader;
/// This trait captures all the functional dependencies (i.e., context) which the ICS26 module
/// requires to be able to dispatch and process IBC messages. In other words, this is the
/// representation of a chain from the perspective of the IBC module of that chain.
pub trait ICS26Context:
ClientReader + ClientKeeper + ConnectionReader + ConnectionKeeper +
ChannelKeeper + ChannelReader + Clone
ClientReader
+ ClientKeeper
+ ConnectionReader
+ ConnectionKeeper
+ ChannelKeeper
+ ChannelReader
+ PortReader
+ Clone
{
}
2 changes: 2 additions & 0 deletions modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! - ICS 02: Client
//! - ICS 03: Connection
//! - ICS 04: Channel
//! - ICS 05: Port
//! - ICS 07: Tendermint Client
//! - ICS 18: Basic relayer functions
//! - ICS 20: Fungible Token
Expand All @@ -27,6 +28,7 @@ pub mod handler;
pub mod ics02_client;
pub mod ics03_connection;
pub mod ics04_channel;
pub mod ics05_port;
pub mod ics07_tendermint;
pub mod ics18_relayer;
pub mod ics20_fungible_token_transfer;
Expand Down
Loading