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 all commits
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ Cargo.lock
**/*.db/

# Ignore Jetbrains
.idea
.idea

# Ignore VisualStudio
.vscode
8 changes: 2 additions & 6 deletions modules/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ impl IBCEvent {
IBCEvent::AcknowledgePacketChannel(ev) => ev.height,
IBCEvent::TimeoutPacketChannel(ev) => ev.height,

_ => {
unimplemented!()
}
_ => unimplemented!(),
}
}
pub fn set_height(&mut self, height: ICSHeight) {
Expand All @@ -121,9 +119,7 @@ impl IBCEvent {
IBCEvent::TimeoutPacketChannel(ev) => {
ev.height = Height::try_from(height.revision_height).unwrap()
}
_ => {
unimplemented!()
}
_ => unimplemented!(),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions modules/src/ics03_connection/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub struct Version {
features: Vec<String>,
}

impl Version {
/// Checks whether or not the given feature is supported in this versin
pub fn is_supported_feature(&self, feature: String) -> bool {
self.features.contains(&feature)
}
}

impl Protobuf<RawVersion> for Version {}

impl TryFrom<RawVersion> for Version {
Expand Down
33 changes: 22 additions & 11 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl TryFrom<RawChannel> for ChannelEnd {

fn try_from(value: RawChannel) -> Result<Self, Self::Error> {
// Parse the ordering type. Propagate the error, if any, to our caller.
let chan_state = State::from_i32(value.state)?;
let chan_state: State = State::from_i32(value.state)?;

if chan_state == State::Uninitialized {
return Ok(ChannelEnd::default());
Expand Down Expand Up @@ -82,7 +82,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 @@ -124,12 +124,12 @@ impl ChannelEnd {
&self.ordering
}

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

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

pub fn version(&self) -> String {
Expand Down Expand Up @@ -240,8 +240,8 @@ impl Order {
pub fn as_string(&self) -> &'static str {
match self {
Self::None => "UNINITIALIZED",
Self::Unordered => "UNORDERED",
Self::Ordered => "ORDERED",
Self::Unordered => "ORDER_UNORDERED",
Self::Ordered => "ORDER_ORDERED",
}
}

Expand Down Expand Up @@ -322,6 +322,7 @@ pub fn validate_version(version: String) -> Result<String, Error> {

#[cfg(test)]
pub mod test_util {

use ibc_proto::ibc::core::channel::v1::Channel as RawChannel;
use ibc_proto::ibc::core::channel::v1::Counterparty as RawCounterparty;

Expand All @@ -337,10 +338,20 @@ pub mod test_util {
pub fn get_dummy_raw_channel_end() -> RawChannel {
RawChannel {
state: 1,
ordering: 0,
ordering: 1,
counterparty: Some(get_dummy_raw_counterparty()),
connection_hops: vec![],
version: "".to_string(), // The version is not validated.
connection_hops: vec!["defaultConnection-0".to_string()],
version: "ics20".to_string(), // The version is not validated.
}
}

pub fn get_dummy_raw_channel_end_with_missing_connection() -> RawChannel {
RawChannel {
state: 1,
ordering: 1,
counterparty: Some(get_dummy_raw_counterparty()),
connection_hops: vec!["noconnection".to_string()],
version: "ics20".to_string(), // The version is not validated.
}
}
}
Expand Down
114 changes: 114 additions & 0 deletions modules/src/ics04_channel/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! ICS4 (channel) context. The two traits `ChannelReader ` and `ChannelKeeper` define
//! the interface that any host chain must implement to be able to process any `ChannelMsg`.
//!
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::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 `ChannelMsg`.
pub trait ChannelReader {
/// Returns the ChannelEnd for the given `port_id` and `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;
}

/// A context supplying all the necessary write-only dependencies (i.e., storage writing facility)
/// for processing any `ChannelMsg`.
pub trait ChannelKeeper {
fn next_channel_id(&mut self) -> ChannelId;

fn store_channel_result(&mut self, result: ChannelResult) -> Result<(), Error> {
match result.channel_end.state() {
State::Init => {
let channel_id = self.next_channel_id();
self.store_channel(
&(result.port_id.clone(), channel_id.clone()),
&result.channel_end,
)?;

self.store_next_sequence_send(&(result.port_id.clone(), channel_id.clone()), 1)?;

self.store_next_sequence_recv(&(result.port_id.clone(), channel_id.clone()), 1)?;

self.store_next_sequence_ack(&(result.port_id.clone(), channel_id.clone()), 1)?;

self.store_connection_channels(
&result.channel_end.connection_hops()[0].clone(),
&(result.port_id.clone(), channel_id),
)?;
}
State::TryOpen => {
self.store_channel(
&(result.port_id, result.channel_id.clone().unwrap()),
&result.channel_end,
)?;
// TODO: If this is the first time the handler processed this channel, associate the
// channel end to its client identifier.
// self.store_channel_to_connection(
// &(result.port_id,result.channel_id),
// &result.... ,
// )?;
}
_ => {
self.store_channel(
&(result.port_id, result.channel_id.clone().unwrap()),
&result.channel_end,
)?;
}
}
Ok(())
}

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),
channel_end: &ChannelEnd,
) -> Result<(), Error>;

fn store_next_sequence_send(
&mut self,
port_channel_id: &(PortId, ChannelId),
seq: u64,
) -> Result<(), Error>;

fn store_next_sequence_recv(
&mut self,
port_channel_id: &(PortId, ChannelId),
seq: u64,
) -> Result<(), Error>;

fn store_next_sequence_ack(
&mut self,
port_channel_id: &(PortId, ChannelId),
seq: u64,
) -> Result<(), Error>;
}
20 changes: 20 additions & 0 deletions modules/src/ics04_channel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use thiserror::Error;

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

use crate::ics24_host::identifier::ConnectionId;

#[derive(Clone, Debug, Error)]
pub enum Kind {
#[error("channel state unknown")]
Expand Down Expand Up @@ -44,8 +46,26 @@ pub enum Kind {
#[error("missing counterparty")]
MissingCounterparty,

#[error("no commong version")]
NoCommonVersion,

#[error("missing channel end")]
MissingChannel,

#[error("given connection hop {0} does not exist")]
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,

#[error("single version must be negociated on connection before opening channel")]
InvalidVersionLengthConnection,

#[error("the channel ordering is not supported by connection ")]
ChannelFeatureNotSuportedByConnection,
}

impl Kind {
Expand Down
65 changes: 65 additions & 0 deletions modules/src/ics04_channel/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! This module implements the processing logic for ICS4 (channel) messages.

use crate::handler::{Event, EventType, HandlerOutput};
use crate::ics04_channel::channel::ChannelEnd;
use crate::ics04_channel::context::ChannelReader;
use crate::ics04_channel::error::Error;
use crate::ics04_channel::msgs::ChannelMsg;
use crate::ics05_port::capabilities::Capability;
use crate::ics24_host::identifier::{ChannelId, PortId};

pub mod chan_open_init;

#[derive(Clone, Debug)]
pub enum ChannelEvent {
ChanOpenInit(ChannelResult),
// ConnOpenTry(ConnectionResult),
// ConnOpenAck(ConnectionResult),
// ConnOpenConfirm(ConnectionResult),
}

#[derive(Clone, Debug)]
pub struct ChannelResult {
pub port_id: PortId,
pub channel_id: Option<ChannelId>,
pub channel_cap: Capability,
pub channel_end: ChannelEnd,
}

impl From<ChannelEvent> for Event {
fn from(ev: ChannelEvent) -> Event {
match ev {
ChannelEvent::ChanOpenInit(_chan) => Event::new(
EventType::Custom("channel_open_init".to_string()),
vec![("channel_id".to_string(), "None".to_string())],
),
// ChannelEvent::ChanOpenTry(conn) => Event::new(
// EventType::Custom("channel_open_try".to_string()),
// vec![("channel_id".to_string(), chan.channel_id.to_string())],
// ),
// ChannelEvent::ChanOpenAck(conn) => Event::new(
// EventType::Custom("channel_open_ack".to_string()),
// vec![("channel_id".to_string(), chan.channel_id.to_string())],
// ),
// ChannelEvent::ChanOpenConfirm(conn) => Event::new(
// EventType::Custom("channel_open_confirm".to_string()),
// vec![("channel_id".to_string(), conn.channel _id.to_string())],
// ),
}
}
}

/// General entry point for processing any type of message related to the ICS4 channel open
/// handshake protocol.
pub fn dispatch<Ctx>(ctx: &Ctx, msg: ChannelMsg) -> Result<HandlerOutput<ChannelResult>, Error>
where
Ctx: ChannelReader,
{
Ok(match msg {
ChannelMsg::ChannelOpenInit(msg) => chan_open_init::process(ctx, msg)?,
// ChannelMsg::ChannelOpenTry(msg) => chan_open_try::process(ctx, *msg)?,
// ChannelMsg::ChannelOpenAck(msg) => chan_open_ack::process(ctx, *msg)?,
// ChannelMsg::ChannelOpenConfirm(msg) => chan_open_confirm::process(ctx, msg)?,
_ => panic!(),
})
}
Loading