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
chan open init
  • Loading branch information
cezarad committed Jan 7, 2021
commit 1054aee0125af09111c34fe9de90722b9111bb81
32 changes: 16 additions & 16 deletions modules/src/ics04_channel/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,28 @@ pub trait ChannelKeeper {
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(), result.channel_id.clone()),
&(result.port_id.clone(), channel_id.clone()),
&result.channel_end,
)?;

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

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

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

self.store_connection_channels(
&result.channel_end.connection_hops()[0].clone(),
&(result.port_id.clone(), result.channel_id.clone()),
&(result.port_id.clone(), channel_id.clone()),
)?;
}
State::TryOpen => {
self.store_channel(&(result.port_id, result.channel_id), &result.channel_end)?;
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(
Expand All @@ -91,12 +86,17 @@ pub trait ChannelKeeper {
// )?;
}
_ => {
self.store_channel(&(result.port_id, result.channel_id), &result.channel_end)?;
self.store_channel(
&(result.port_id, result.channel_id.clone().unwrap()),
&result.channel_end,
)?;
}
}
Ok(())
}

fn next_channel_id(&mut self) -> ChannelId;

fn store_connection_channels(
&mut self,
conn_id: &ConnectionId,
Expand Down
6 changes: 3 additions & 3 deletions modules/src/ics04_channel/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ pub enum ChannelEvent {
#[derive(Clone, Debug)]
pub struct ChannelResult {
pub port_id: PortId,
pub channel_id: ChannelId,
pub channel_id: Option<ChannelId>,
pub channel_end: ChannelEnd,
}

impl From<ChannelEvent> for Event {
fn from(ev: ChannelEvent) -> Event {
match ev {
ChannelEvent::ChanOpenInit(chan) => Event::new(
ChannelEvent::ChanOpenInit(_chan) => Event::new(
EventType::Custom("channel_open_init".to_string()),
vec![("channel_id".to_string(), chan.channel_id.to_string())],
vec![("channel_id".to_string(), "None".to_string())],
),
// ChannelEvent::ChanOpenTry(conn) => Event::new(
// EventType::Custom("channel_open_try".to_string()),
Expand Down
66 changes: 21 additions & 45 deletions modules/src/ics04_channel/handler/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -15,12 +14,6 @@ pub(crate) fn process(
) -> HandlerResult<ChannelResult, Error> {
let mut output = HandlerOutput::builder();

let pc_id = (msg.port_id().clone(), msg.channel_id().clone());

if ctx.channel_end(&pc_id).is_some() {
return Err(Kind::ChannelExistsAlready(msg.channel_id().clone()).into());
}

let cap = ctx.port_capability(&msg.port_id().clone());
match cap {
Some(key) => {
Expand Down Expand Up @@ -60,7 +53,7 @@ pub(crate) fn process(

let result = ChannelResult {
port_id: msg.port_id().clone(),
channel_id: msg.channel_id().clone(),
channel_id: None,
channel_end: new_channel_end,
};

Expand All @@ -72,23 +65,25 @@ pub(crate) fn process(
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use std::str::FromStr;

use crate::handler::EventType;

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

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::ics04_channel::channel::State;

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

use crate::ics03_connection::version::get_compatible_versions;
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::ics24_host::identifier::ConnectionId;
use crate::mock::context::MockContext;

#[test]
Expand All @@ -105,37 +100,25 @@ mod tests {

let context = MockContext::default();

let init_chan_end = &ChannelEnd::new(
State::Init,
*msg_chan_init.channel().ordering(),
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 = ConnectionEnd::new(
ConnectionState::Init,
msg_conn_init.client_id().clone(),
msg_conn_init.counterparty().clone(),
vec![default_version_string()],
)
.unwrap();
get_compatible_versions(),
msg_conn_init.delay_period,
);

// let cid = ConnectionId::default();
let ccid = <ConnectionId as FromStr>::from_str("srcconnection");
let cid = match ccid {
Ok(v) => v,
Err(_e) => ConnectionId::default(),
};

let tests: Vec<Test> = vec![
Test {
name: "Processing fails because the channel exists in the store already"
.to_string(),
ctx: context.clone().with_channel(
msg_chan_init.port_id().clone(),
msg_chan_init.channel_id().clone(),
init_chan_end.clone(),
),
msg: ChannelMsg::ChannelOpenInit(msg_chan_init.clone()),
want_pass: false,
},
Test {
name: "Processing fails because no connection exists in the context".to_string(),
ctx: context.clone(),
Expand All @@ -145,13 +128,9 @@ mod tests {
Test {
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(),
),
ctx: context
.clone()
.with_connection(cid.clone(), init_conn_end.clone()),
msg: ChannelMsg::ChannelOpenInit(msg_chan_init.clone()),
want_pass: false,
},
Expand All @@ -163,10 +142,7 @@ mod tests {
.unwrap()
.port_id()
.clone(),
MsgConnectionOpenInit::try_from(get_dummy_msg_conn_open_init())
.unwrap()
.connection_id()
.clone(),
cid.clone(),
init_conn_end.clone(),
)
.clone(),
Expand Down Expand Up @@ -194,7 +170,7 @@ mod tests {

// The object in the output is a ConnectionEnd, should have init state.
let res: ChannelResult = proto_output.result;
assert_eq!(res.channel_id, msg_chan_init.channel_id().clone());
//assert_eq!(res.channel_id, msg_chan_init.channel_id().clone());
assert_eq!(res.channel_end.state().clone(), State::Init);

for e in proto_output.events.iter() {
Expand Down
10 changes: 2 additions & 8 deletions modules/src/ics04_channel/msgs/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ pub struct MsgChannelOpenInit {
}

impl MsgChannelOpenInit {
/// Getter: borrow the `connection_id` from this message.
pub fn channel_id(&self) -> &ChannelId {
&self.channel_id
}

/// Getter: borrow the `client_id` from this message.
/// Getter: borrow the `port_id` from this message.
pub fn port_id(&self) -> &PortId {
&self.port_id
}

/// Getter: borrow the `counterparty` from this message.
/// Getter: borrow the `channelEnd` from this message.
pub fn channel(&self) -> &ChannelEnd {
&self.channel
}
Expand Down Expand Up @@ -114,7 +109,6 @@ pub mod test_util {
pub fn get_dummy_raw_msg_chan_open_init_aux_test_missing_connection() -> RawMsgChannelOpenInit {
RawMsgChannelOpenInit {
port_id: "port".to_string(),
channel_id: "testchannel".to_string(),
channel: Some(get_dummy_raw_channel_end_aux_test_missing_connection()),
signer: get_dummy_bech32_account(),
}
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics04_channel/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ics04_channel::error::{Error, Kind};
use std::str::FromStr;

#[derive(Clone, Debug, PartialEq, Eq)]
struct Version {
pub struct Version {
/// unique version identifier
identifier: String,
/// list of features compatible with the specified identifier
Expand Down
35 changes: 12 additions & 23 deletions modules/src/mock/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ pub struct MockContext {
next_sequence_ack: HashMap<(PortId, ChannelId), u64>,

port_capabilities: HashMap<PortId, Capability>,

/// Counter for connection identifiers (see `next_connection_id`).
connection_ids_counter: u32,

channel_ids_counter: u32,
}

/// Returns a MockContext with bare minimum initialization: no clients, no connections and no channels are
Expand Down Expand Up @@ -155,7 +157,7 @@ impl MockContext {
next_sequence_ack: Default::default(),
port_capabilities: Default::default(),
connection_ids_counter: 0,

channel_ids_counter: 0,
}
}

Expand Down Expand Up @@ -245,27 +247,6 @@ impl MockContext {
}
}

// pub fn with_capability(self, port_id: PortId) -> Self {
// let mut port_capabilities = self.port_capabilities.clone();
// port_capabilities.insert(port_id, Capability::new());
// Self {
// port_capabilities,
// ..self
// }
// }

/// Associates a channel to this context.
pub fn with_channel(
self,
port_id: PortId,
channel_id: ChannelId,
channel_end: ChannelEnd,
) -> Self {
let mut channels = self.channels.clone();
channels.insert((port_id, channel_id), channel_end);
Self { channels, ..self }
}

/// Accessor for a block of the local (host) chain from this context.
/// Returns `None` if the block at the requested height does not exist.
fn host_block(&self, target_height: Height) -> Option<&HostBlock> {
Expand Down Expand Up @@ -419,6 +400,14 @@ impl ChannelReader for MockContext {
}

impl ChannelKeeper for MockContext {
fn next_channel_id(&mut self) -> ChannelId {
let prefix = ChannelId::default().to_string();
let suffix = self.channel_ids_counter;
self.channel_ids_counter += 1;

ChannelId::from_str(format!("{}-{}", prefix, suffix).as_str()).unwrap()
}

fn store_channel(
&mut self,
port_channel_id: &(PortId, ChannelId),
Expand Down