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 2 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
17 changes: 17 additions & 0 deletions modules/src/ics03_connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tendermint_proto::Protobuf;

use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::validate_versions;
use crate::ics03_connection::version::default_version_string;
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
Expand Down Expand Up @@ -51,6 +52,7 @@ impl From<ConnectionEnd> for RawConnectionEnd {
}
}


impl ConnectionEnd {
pub fn new(
state: State,
Expand All @@ -66,6 +68,21 @@ impl ConnectionEnd {
})
}

pub fn test_channel_new(
client_id: ClientId,
counterparty: Counterparty,
) -> Result<Self, Error> {

ConnectionEnd::new(
State::Init,
client_id,
counterparty,
vec![default_version_string()],
)
}



/// Getter for the state of this connection end.
pub fn state(&self) -> &State {
&self.state
Expand Down
20 changes: 18 additions & 2 deletions modules/src/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ics04_channel::error::{self, Error, Kind};

use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};

use ibc_proto::ibc::core::channel::v1::Channel as RawChannel;
Expand Down Expand Up @@ -279,8 +280,12 @@ 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;
// use crate::ics03_connection::msgs::conn_open_init::MsgConnectionOpenInit;
// use
//crate::ics03_connection::msgs::conn_open_init::test_util::get_dummy_msg_conn_open_init;

/// Returns a dummy `RawCounterparty`, for testing only!
pub fn get_dummy_raw_counterparty() -> RawCounterparty {
Expand All @@ -296,11 +301,22 @@ pub mod test_util {
state: 1,
ordering: 0,
counterparty: Some(get_dummy_raw_counterparty()),
connection_hops: vec![],
version: "".to_string(), // The version is not validated.
connection_hops: vec!["srcconnection".to_string()],
version: "ics20".to_string(), // The version is not validated.
}
}

pub fn get_dummy_raw_channel_end_aux_test_missing_connection() -> RawChannel {
RawChannel {
state: 1,
ordering: 0,
counterparty: Some(get_dummy_raw_counterparty()),
connection_hops: vec!["noconnection".to_string()],
version: "ics20".to_string(), // The version is not validated.
}
}
}
//MsgConnectionOpenInit::try_from(get_dummy_msg_conn_open_init()).unwrap().connection_id()

#[cfg(test)]
mod tests {
Expand Down
104 changes: 104 additions & 0 deletions modules/src/ics04_channel/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! ICS4 (channel) context. The two traits `ConnectionReader` and `ConnectionKeeper` define
//! 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::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
//use crate::Height;

/// A context supplying all the necessary read-only dependencies for processing any `ConnectionMsg`.
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>;

/// 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()
}

/// Function required by ICS 04. Returns one version out of the supplied list of versions, which the channel handshake protocol prefers.
fn pick_version(
&self,
supported_versions: Vec<String>,
counterparty_candidate_versions: Vec<String>,
) -> Result<String, Error> {
pick_version(supported_versions, counterparty_candidate_versions)
}
}

/// A context supplying all the necessary write-only dependencies (i.e., storage writing facility)
/// for processing any `ConnectionMsg`.
pub trait ChannelKeeper {
fn store_channel_result(&mut self, result: ChannelResult) -> Result<(), Error> {
match result.channel_end.state() {
State::Init => {
self.store_channel(
&(result.port_id.clone(), result.channel_id.clone()),
&result.channel_end,
)?;

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

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

self.store_nextsequence_ack(
&(result.port_id.clone(), result.channel_id.clone()),
1,
)?;
}
State::TryOpen => {
self.store_channel(&(result.port_id, result.channel_id), &result.channel_end)?;
// TODOIf 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), &result.channel_end)?;
}
}
Ok(())
}

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

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

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

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

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

use crate::ics24_host::identifier::{ConnectionId,ChannelId};

#[derive(Clone, Debug, Error)]
pub enum Kind {
#[error("not implemented yet")]
Niy,

#[error("channel state unknown")]
UnknownState,

Expand Down Expand Up @@ -44,8 +49,17 @@ pub enum Kind {
#[error("missing counterparty")]
MissingCounterparty,

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

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

#[error("channel exists (was initialized) already: {0}")]
ChannelExistsAlready(ChannelId),

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

impl Kind {
Expand Down
63 changes: 63 additions & 0 deletions modules/src/ics04_channel/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! 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::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: ChannelId,
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(), chan.channel_id.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