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

Make all handlers emit an IbcEvent with height ctx.host_height() #2043

Merged
merged 25 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Make all handlers emit an IbcEvent with current host chain height as height parameter value.
([#2035](https://github.com/informalsystems/ibc-rs/issues/2035))
3 changes: 3 additions & 0 deletions modules/src/core/ics02_client/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ impl UpgradeClient {
pub fn set_height(&mut self, height: Height) {
self.0.height = height;
}
pub fn height(&self) -> Height {
self.0.height
}
pub fn client_id(&self) -> &ClientId {
&self.0.client_id
}
Expand Down
11 changes: 8 additions & 3 deletions modules/src/core/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn process(

let event_attributes = Attributes {
client_id,
height: ctx.host_height(),
..Default::default()
};
output.emit(IbcEvent::CreateClient(event_attributes.into()));
Expand All @@ -77,6 +78,7 @@ mod tests {
use crate::core::ics02_client::client_consensus::AnyConsensusState;
use crate::core::ics02_client::client_state::ClientState;
use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics02_client::context::ClientReader;
use crate::core::ics02_client::handler::{dispatch, ClientResult};
use crate::core::ics02_client::msgs::create_client::MsgCreateAnyClient;
use crate::core::ics02_client::msgs::ClientMsg;
Expand Down Expand Up @@ -114,8 +116,9 @@ mod tests {
let event = events.pop().unwrap();
let expected_client_id = ClientId::new(ClientType::Mock, 0).unwrap();
assert!(
matches!(event, IbcEvent::CreateClient(e) if e.client_id() == &expected_client_id)
matches!(event, IbcEvent::CreateClient(ref e) if e.client_id() == &expected_client_id)
);
assert_eq!(event.height(), ctx.host_height());
match result {
ClientResult::Create(create_result) => {
assert_eq!(create_result.client_type, ClientType::Mock);
Expand Down Expand Up @@ -204,8 +207,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::CreateClient(e) if e.client_id() == &expected_client_id)
matches!(event, IbcEvent::CreateClient(ref e) if e.client_id() == &expected_client_id)
);
assert_eq!(event.height(), ctx.host_height());
match result {
ClientResult::Create(create_res) => {
assert_eq!(create_res.client_type, msg.client_state.client_type());
Expand Down Expand Up @@ -267,8 +271,9 @@ mod tests {
let event = events.pop().unwrap();
let expected_client_id = ClientId::new(ClientType::Tendermint, 0).unwrap();
assert!(
matches!(event, IbcEvent::CreateClient(e) if e.client_id() == &expected_client_id)
matches!(event, IbcEvent::CreateClient(ref e) if e.client_id() == &expected_client_id)
);
assert_eq!(event.height(), ctx.host_height());
match result {
ClientResult::Create(create_res) => {
assert_eq!(create_res.client_type, ClientType::Tendermint);
Expand Down
17 changes: 12 additions & 5 deletions modules/src/core/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub fn process(

let event_attributes = Attributes {
client_id,
height: ctx.host_height(),
..Default::default()
};
output.emit(IbcEvent::UpdateClient(event_attributes.into()));
Expand All @@ -108,6 +109,7 @@ mod tests {
use crate::core::ics02_client::client_consensus::AnyConsensusState;
use crate::core::ics02_client::client_state::{AnyClientState, ClientState};
use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics02_client::context::ClientReader;
use crate::core::ics02_client::error::{Error, ErrorDetail};
use crate::core::ics02_client::handler::dispatch;
use crate::core::ics02_client::handler::ClientResult::Update;
Expand Down Expand Up @@ -153,8 +155,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::UpdateClient(e) if e.client_id() == &msg.client_id)
matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id)
);
assert_eq!(event.height(), ctx.host_height());
assert!(log.is_empty());
// Check the result
match result {
Expand Down Expand Up @@ -236,8 +239,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::UpdateClient(e) if e.client_id() == &msg.client_id)
matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id)
);
assert_eq!(event.height(), ctx.host_height());
assert!(log.is_empty());
}
Err(err) => {
Expand Down Expand Up @@ -303,8 +307,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::UpdateClient(e) if e.client_id() == &msg.client_id)
matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id)
);
assert_eq!(event.height(), ctx.host_height());
assert!(log.is_empty());
// Check the result
match result {
Expand Down Expand Up @@ -380,8 +385,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::UpdateClient(e) if e.client_id() == &msg.client_id)
matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id)
);
assert_eq!(event.height(), ctx.host_height());
assert!(log.is_empty());
// Check the result
match result {
Expand Down Expand Up @@ -460,8 +466,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::UpdateClient(e) if e.client_id() == &msg.client_id)
matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id)
);
assert_eq!(event.height(), ctx.host_height());
assert!(log.is_empty());
// Check the result
match result {
Expand Down
5 changes: 4 additions & 1 deletion modules/src/core/ics02_client/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn process(
});
let event_attributes = Attributes {
client_id,
height: ctx.host_height(),
..Default::default()
};

Expand All @@ -79,6 +80,7 @@ mod tests {

use core::str::FromStr;

use crate::core::ics02_client::context::ClientReader;
use crate::core::ics02_client::error::{Error, ErrorDetail};
use crate::core::ics02_client::handler::dispatch;
use crate::core::ics02_client::handler::ClientResult::Upgrade;
Expand Down Expand Up @@ -120,8 +122,9 @@ mod tests {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IbcEvent::UpgradeClient(e) if e.client_id() == &msg.client_id)
matches!(event, IbcEvent::UpgradeClient(ref e) if e.client_id() == &msg.client_id)
);
assert_eq!(event.height(), ctx.host_height());
assert!(log.is_empty());
// Check the result
match result {
Expand Down
3 changes: 3 additions & 0 deletions modules/src/core/ics03_connection/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub(crate) fn process(

let event_attributes = Attributes {
connection_id: Some(result.connection_id.clone()),
height: ctx.host_current_height(),
..Default::default()
};
output.emit(IbcEvent::OpenAckConnection(event_attributes.into()));
Expand All @@ -97,6 +98,7 @@ mod tests {
use test_log::test;

use crate::core::ics03_connection::connection::{ConnectionEnd, Counterparty, State};
use crate::core::ics03_connection::context::ConnectionReader;
use crate::core::ics03_connection::error;
use crate::core::ics03_connection::handler::{dispatch, ConnectionResult};
use crate::core::ics03_connection::msgs::conn_open_ack::test_util::get_dummy_raw_msg_conn_open_ack;
Expand Down Expand Up @@ -238,6 +240,7 @@ mod tests {

for e in proto_output.events.iter() {
assert!(matches!(e, &IbcEvent::OpenAckConnection(_)));
assert_eq!(e.height(), test.ctx.host_current_height());
}
}
Err(e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(crate) fn process(

let event_attributes = Attributes {
connection_id: Some(result.connection_id.clone()),
height: ctx.host_current_height(),
..Default::default()
};
output.emit(IbcEvent::OpenConfirmConnection(event_attributes.into()));
Expand Down Expand Up @@ -169,6 +170,7 @@ mod tests {

for e in proto_output.events.iter() {
assert!(matches!(e, &IbcEvent::OpenConfirmConnection(_)));
assert_eq!(e.height(), test.ctx.host_current_height());
}
}
Err(e) => {
Expand Down
3 changes: 3 additions & 0 deletions modules/src/core/ics03_connection/handler/conn_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(crate) fn process(

let event_attributes = Attributes {
connection_id: Some(conn_id),
height: ctx.host_current_height(),
..Default::default()
};
output.emit(IbcEvent::OpenInitConnection(event_attributes.into()));
Expand All @@ -59,6 +60,7 @@ mod tests {
use test_log::test;

use crate::core::ics03_connection::connection::State;
use crate::core::ics03_connection::context::ConnectionReader;
use crate::core::ics03_connection::handler::{dispatch, ConnectionResult};
use crate::core::ics03_connection::msgs::conn_open_init::test_util::get_dummy_raw_msg_conn_open_init;
use crate::core::ics03_connection::msgs::conn_open_init::MsgConnectionOpenInit;
Expand Down Expand Up @@ -118,6 +120,7 @@ mod tests {

for e in proto_output.events.iter() {
assert!(matches!(e, &IbcEvent::OpenInitConnection(_)));
assert_eq!(e.height(), test.ctx.host_current_height());
}
}
Err(e) => {
Expand Down
3 changes: 3 additions & 0 deletions modules/src/core/ics03_connection/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub(crate) fn process(

let event_attributes = Attributes {
connection_id: Some(conn_id),
height: ctx.host_current_height(),
..Default::default()
};
output.emit(IbcEvent::OpenTryConnection(event_attributes.into()));
Expand All @@ -126,6 +127,7 @@ mod tests {
use test_log::test;

use crate::core::ics03_connection::connection::State;
use crate::core::ics03_connection::context::ConnectionReader;
use crate::core::ics03_connection::handler::{dispatch, ConnectionResult};
use crate::core::ics03_connection::msgs::conn_open_try::test_util::get_dummy_raw_msg_conn_open_try;
use crate::core::ics03_connection::msgs::conn_open_try::MsgConnectionOpenTry;
Expand Down Expand Up @@ -248,6 +250,7 @@ mod tests {

for e in proto_output.events.iter() {
assert!(matches!(e, &IbcEvent::OpenTryConnection(_)));
assert_eq!(e.height(), test.ctx.host_current_height());
}
}
Err(e) => {
Expand Down
4 changes: 2 additions & 2 deletions modules/src/core/ics04_channel/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::core::ics02_client::height::Height;
use crate::core::ics03_connection::connection::State as ConnectionState;
use crate::core::ics04_channel::channel::State;
use crate::core::ics04_channel::channel::{Counterparty, Order};
Expand Down Expand Up @@ -112,7 +111,7 @@ pub fn process(
output.log("success: packet ack");

output.emit(IbcEvent::AcknowledgePacket(AcknowledgePacket {
height: Height::zero(),
height: ctx.host_height(),
packet: packet.clone(),
}));

Expand Down Expand Up @@ -254,6 +253,7 @@ mod tests {

for e in proto_output.events.iter() {
assert!(matches!(e, &IbcEvent::AcknowledgePacket(_)));
assert_eq!(e.height(), test.ctx.host_height());
}
}
Err(e) => {
Expand Down
82 changes: 82 additions & 0 deletions modules/src/core/ics04_channel/handler/chan_close_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub(crate) fn process(

let event_attributes = Attributes {
channel_id: Some(msg.channel_id),
height: ctx.host_height(),
..Default::default()
};
output.emit(IbcEvent::CloseConfirmChannel(
Expand All @@ -99,3 +100,84 @@ pub(crate) fn process(

Ok(output.with_result(result))
}

#[cfg(test)]
mod tests {
use crate::core::ics04_channel::context::ChannelReader;
use crate::core::ics04_channel::msgs::chan_close_confirm::test_util::get_dummy_raw_msg_chan_close_confirm;
use crate::core::ics04_channel::msgs::chan_close_confirm::MsgChannelCloseConfirm;
use crate::core::ics04_channel::msgs::ChannelMsg;
use crate::events::IbcEvent;
use crate::prelude::*;

use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics03_connection::connection::ConnectionEnd;
use crate::core::ics03_connection::connection::Counterparty as ConnectionCounterparty;
use crate::core::ics03_connection::connection::State as ConnectionState;
use crate::core::ics03_connection::msgs::test_util::get_dummy_raw_counterparty;
use crate::core::ics03_connection::version::get_compatible_versions;
use crate::core::ics04_channel::channel::{
ChannelEnd, Counterparty, Order, State as ChannelState,
};
use crate::core::ics04_channel::handler::channel_dispatch;
use crate::core::ics04_channel::Version;
use crate::core::ics24_host::identifier::{ClientId, ConnectionId};

use crate::mock::context::MockContext;
use crate::timestamp::ZERO_DURATION;

#[test]
fn chan_close_confirm_event_height() {
let client_id = ClientId::new(ClientType::Mock, 24).unwrap();
let conn_id = ConnectionId::new(2);
let default_context = MockContext::default();
let client_consensus_state_height = default_context.host_height();

let conn_end = ConnectionEnd::new(
ConnectionState::Open,
client_id.clone(),
ConnectionCounterparty::try_from(get_dummy_raw_counterparty()).unwrap(),
get_compatible_versions(),
ZERO_DURATION,
);

let msg_chan_close_confirm = MsgChannelCloseConfirm::try_from(
get_dummy_raw_msg_chan_close_confirm(client_consensus_state_height.revision_height),
)
.unwrap();

let chan_end = ChannelEnd::new(
ChannelState::Open,
Order::default(),
Counterparty::new(
msg_chan_close_confirm.port_id.clone(),
Some(msg_chan_close_confirm.channel_id.clone()),
),
vec![conn_id.clone()],
Version::default(),
);

let context = default_context
.with_client(&client_id, client_consensus_state_height)
.with_connection(conn_id, conn_end)
.with_port_capability(msg_chan_close_confirm.port_id.clone())
.with_channel(
msg_chan_close_confirm.port_id.clone(),
msg_chan_close_confirm.channel_id.clone(),
chan_end,
);

let handler_output = channel_dispatch(
&context,
ChannelMsg::ChannelCloseConfirm(msg_chan_close_confirm),
)
.unwrap();

assert!(!handler_output.events.is_empty()); // Some events must exist.

for event in handler_output.events.iter() {
assert!(matches!(event, &IbcEvent::CloseConfirmChannel(_)));
assert_eq!(event.height(), context.host_height());
}
}
}
Loading