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

Emit message events #564

Merged
merged 5 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .changelog/unreleased/bug/563-emit-message-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Emit a message event for each IBC handling
([#563](https://github.com/cosmos/ibc-rs/issues/563))
21 changes: 13 additions & 8 deletions crates/ibc/src/core/context/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ where
let conn_id_on_a = &chan_end_on_a.connection_hops()[0];

// In all cases, this event is emitted
ctx_a.emit_ibc_event(IbcEvent::AcknowledgePacket(AcknowledgePacket::new(
let event = IbcEvent::AcknowledgePacket(AcknowledgePacket::new(
msg.packet.clone(),
chan_end_on_a.ordering,
conn_id_on_a.clone(),
)));
));
ctx_a.emit_ibc_event(IbcEvent::Message(event.event_type()));
ctx_a.emit_ibc_event(event);

let commitment_path_on_a = CommitmentPath::new(
&msg.packet.port_id_on_a,
Expand Down Expand Up @@ -136,6 +138,7 @@ mod tests {
},
ics24_host::identifier::{ClientId, ConnectionId},
},
events::IbcEventType,
mock::context::MockContext,
test_utils::DummyTransferModule,
timestamp::ZERO_DURATION,
Expand Down Expand Up @@ -245,11 +248,12 @@ mod tests {

assert!(res.is_ok());

assert_eq!(ctx.events.len(), 1);
assert_eq!(ctx.events.len(), 2);
assert!(matches!(
ctx.events.first().unwrap(),
&IbcEvent::AcknowledgePacket(_)
ctx.events[0],
IbcEvent::Message(IbcEventType::AckPacket)
));
assert!(matches!(ctx.events[1], IbcEvent::AcknowledgePacket(_)));
}

#[rstest]
Expand Down Expand Up @@ -281,10 +285,11 @@ mod tests {

assert!(res.is_ok());

assert_eq!(ctx.events.len(), 1);
assert_eq!(ctx.events.len(), 2);
assert!(matches!(
ctx.events.first().unwrap(),
&IbcEvent::AcknowledgePacket(_)
ctx.events[0],
IbcEvent::Message(IbcEventType::AckPacket)
));
assert!(matches!(ctx.events[1], IbcEvent::AcknowledgePacket(_)));
}
}
13 changes: 9 additions & 4 deletions crates/ibc/src/core/context/chan_close_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ where
conn_id_on_b,
))
};
ctx_b.emit_ibc_event(IbcEvent::Message(core_event.event_type()));
ctx_b.emit_ibc_event(core_event);

for module_event in extras.events {
Expand All @@ -101,7 +102,7 @@ mod tests {
use crate::core::ics04_channel::msgs::chan_close_confirm::MsgChannelCloseConfirm;
use crate::core::ics26_routing::context::ModuleId;
use crate::core::ValidationContext;
use crate::events::IbcEvent;
use crate::events::{IbcEvent, IbcEventType};
use crate::prelude::*;

use crate::core::ics03_connection::connection::ConnectionEnd;
Expand Down Expand Up @@ -167,10 +168,14 @@ mod tests {
let res = chan_close_confirm_execute(&mut context, module_id, msg_chan_close_confirm);
assert!(res.is_ok(), "Execution success: happy path");

assert_eq!(context.events.len(), 1);
assert_eq!(context.events.len(), 2);
assert!(matches!(
context.events.first().unwrap(),
&IbcEvent::CloseConfirmChannel(_)
context.events[0],
IbcEvent::Message(IbcEventType::CloseConfirmChannel)
));
assert!(matches!(
context.events[1],
IbcEvent::CloseConfirmChannel(_)
));
}
}
10 changes: 6 additions & 4 deletions crates/ibc/src/core/context/chan_close_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ where
conn_id_on_a,
))
};
ctx_a.emit_ibc_event(IbcEvent::Message(core_event.event_type()));
ctx_a.emit_ibc_event(core_event);

for module_event in extras.events {
Expand All @@ -100,7 +101,7 @@ mod tests {
use crate::core::ics04_channel::msgs::chan_close_init::MsgChannelCloseInit;
use crate::core::ics26_routing::context::ModuleId;
use crate::core::ValidationContext;
use crate::events::IbcEvent;
use crate::events::{IbcEvent, IbcEventType};
use crate::prelude::*;

use crate::core::ics03_connection::connection::ConnectionEnd;
Expand Down Expand Up @@ -172,10 +173,11 @@ mod tests {
);
assert!(res.is_ok(), "Execution happy path");

assert_eq!(context.events.len(), 1);
assert_eq!(context.events.len(), 2);
assert!(matches!(
context.events.first().unwrap(),
&IbcEvent::CloseInitChannel(_)
context.events[0],
IbcEvent::Message(IbcEventType::CloseInitChannel)
));
assert!(matches!(context.events[1], IbcEvent::CloseInitChannel(_)));
}
}
9 changes: 6 additions & 3 deletions crates/ibc/src/core/context/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ where
conn_id_on_a,
))
};
ctx_a.emit_ibc_event(IbcEvent::Message(core_event.event_type()));
ctx_a.emit_ibc_event(core_event);

for module_event in extras.events {
Expand Down Expand Up @@ -113,6 +114,7 @@ mod tests {
ics24_host::identifier::{ClientId, ConnectionId},
ics26_routing::context::ModuleId,
},
events::IbcEventType,
mock::context::MockContext,
test_utils::DummyTransferModule,
timestamp::ZERO_DURATION,
Expand Down Expand Up @@ -201,10 +203,11 @@ mod tests {

assert!(res.is_ok(), "Execution happy path");

assert_eq!(context.events.len(), 1);
assert_eq!(context.events.len(), 2);
assert!(matches!(
context.events.first().unwrap(),
&IbcEvent::OpenAckChannel(_)
context.events[0],
IbcEvent::Message(IbcEventType::OpenAckChannel)
));
assert!(matches!(context.events[1], IbcEvent::OpenAckChannel(_)));
}
}
10 changes: 6 additions & 4 deletions crates/ibc/src/core/context/chan_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ where
chan_id_on_a,
conn_id_on_b,
));
ctx_b.emit_ibc_event(IbcEvent::Message(core_event.event_type()));
ctx_b.emit_ibc_event(core_event);

for module_event in extras.events {
Expand All @@ -97,7 +98,7 @@ where
mod tests {
use crate::{
core::{context::chan_open_confirm::chan_open_confirm_execute, ics04_channel::Version},
events::IbcEvent,
events::{IbcEvent, IbcEventType},
prelude::*,
Height,
};
Expand Down Expand Up @@ -208,10 +209,11 @@ mod tests {

assert!(res.is_ok(), "Execution happy path");

assert_eq!(context.events.len(), 1);
assert_eq!(context.events.len(), 2);
assert!(matches!(
context.events.first().unwrap(),
&IbcEvent::OpenConfirmChannel(_)
context.events[0],
IbcEvent::Message(IbcEventType::OpenConfirmChannel)
));
assert!(matches!(context.events[1], IbcEvent::OpenConfirmChannel(_)));
}
}
9 changes: 6 additions & 3 deletions crates/ibc/src/core/context/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ where
conn_id_on_a,
version,
));
ctx_a.emit_ibc_event(IbcEvent::Message(core_event.event_type()));
ctx_a.emit_ibc_event(core_event);

for module_event in extras.events {
Expand Down Expand Up @@ -130,6 +131,7 @@ mod tests {
},
ics04_channel::msgs::chan_open_init::test_util::get_dummy_raw_msg_chan_open_init,
},
events::IbcEventType,
mock::context::MockContext,
};

Expand Down Expand Up @@ -186,10 +188,11 @@ mod tests {

assert!(res.is_ok(), "Execution succeeds; good parameters");

assert_eq!(context.events.len(), 1);
assert_eq!(context.events.len(), 2);
assert!(matches!(
context.events.first().unwrap(),
&IbcEvent::OpenInitChannel(_)
context.events[0],
IbcEvent::Message(IbcEventType::OpenInitChannel)
));
assert!(matches!(context.events[1], IbcEvent::OpenInitChannel(_)));
}
}
10 changes: 6 additions & 4 deletions crates/ibc/src/core/context/chan_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ where
conn_id_on_b,
version,
));
ctx_b.emit_ibc_event(IbcEvent::Message(core_event.event_type()));
ctx_b.emit_ibc_event(core_event);

for module_event in extras.events {
Expand All @@ -120,7 +121,7 @@ mod tests {
use crate::{
applications::transfer::MODULE_ID_STR,
core::{context::chan_open_try::chan_open_try_execute, ics26_routing::context::ModuleId},
events::IbcEvent,
events::{IbcEvent, IbcEventType},
prelude::*,
test_utils::DummyTransferModule,
Height,
Expand Down Expand Up @@ -216,10 +217,11 @@ mod tests {

assert!(res.is_ok(), "Execution success: happy path");

assert_eq!(context.events.len(), 1);
assert_eq!(context.events.len(), 2);
assert!(matches!(
context.events.first().unwrap(),
&IbcEvent::OpenTryChannel(_)
context.events[0],
IbcEvent::Message(IbcEventType::OpenTryChannel)
));
assert!(matches!(context.events[1], IbcEvent::OpenTryChannel(_)));
}
}
28 changes: 20 additions & 8 deletions crates/ibc/src/core/context/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,20 @@ where
ctx_b.log_message("success: packet write acknowledgement".to_string());

let conn_id_on_b = &chan_end_on_b.connection_hops()[0];
ctx_b.emit_ibc_event(IbcEvent::ReceivePacket(ReceivePacket::new(
let event = IbcEvent::ReceivePacket(ReceivePacket::new(
msg.packet.clone(),
chan_end_on_b.ordering,
conn_id_on_b.clone(),
)));
ctx_b.emit_ibc_event(IbcEvent::WriteAcknowledgement(WriteAcknowledgement::new(
));
ctx_b.emit_ibc_event(IbcEvent::Message(event.event_type()));
ctx_b.emit_ibc_event(event);
let event = IbcEvent::WriteAcknowledgement(WriteAcknowledgement::new(
msg.packet,
acknowledgement,
conn_id_on_b.clone(),
)));
));
ctx_b.emit_ibc_event(IbcEvent::Message(event.event_type()));
ctx_b.emit_ibc_event(event);

for module_event in extras.events {
ctx_b.emit_ibc_event(IbcEvent::AppModule(module_event));
Expand All @@ -150,7 +154,7 @@ mod tests {
ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId},
ics26_routing::context::ModuleId,
},
events::IbcEvent,
events::{IbcEvent, IbcEventType},
prelude::*,
test_utils::DummyTransferModule,
timestamp::ZERO_DURATION,
Expand Down Expand Up @@ -252,8 +256,16 @@ mod tests {

assert!(res.is_ok());

assert_eq!(ctx.events.len(), 2);
assert!(matches!(&ctx.events[0], &IbcEvent::ReceivePacket(_)));
assert!(matches!(&ctx.events[1], &IbcEvent::WriteAcknowledgement(_)));
assert_eq!(ctx.events.len(), 4);
assert!(matches!(
&ctx.events[0],
&IbcEvent::Message(IbcEventType::ReceivePacket)
));
assert!(matches!(&ctx.events[1], &IbcEvent::ReceivePacket(_)));
assert!(matches!(
&ctx.events[2],
&IbcEvent::Message(IbcEventType::WriteAck)
));
assert!(matches!(&ctx.events[3], &IbcEvent::WriteAcknowledgement(_)));
}
}
35 changes: 23 additions & 12 deletions crates/ibc/src/core/context/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ where
let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;

// In all cases, this event is emitted
ctx_a.emit_ibc_event(IbcEvent::TimeoutPacket(TimeoutPacket::new(
packet.clone(),
chan_end_on_a.ordering,
)));
let event = IbcEvent::TimeoutPacket(TimeoutPacket::new(packet.clone(), chan_end_on_a.ordering));
ctx_a.emit_ibc_event(IbcEvent::Message(event.event_type()));
ctx_a.emit_ibc_event(event);

let commitment_path_on_a =
CommitmentPath::new(&packet.port_id_on_a, &packet.chan_id_on_a, packet.seq_on_a);
Expand Down Expand Up @@ -119,14 +118,16 @@ where
if let Order::Ordered = chan_end_on_a.ordering {
let conn_id_on_a = chan_end_on_a.connection_hops()[0].clone();

ctx_a.emit_ibc_event(IbcEvent::ChannelClosed(ChannelClosed::new(
let event = IbcEvent::ChannelClosed(ChannelClosed::new(
packet.port_id_on_a.clone(),
packet.chan_id_on_a.clone(),
chan_end_on_a.counterparty().port_id.clone(),
chan_end_on_a.counterparty().channel_id.clone(),
conn_id_on_a,
chan_end_on_a.ordering,
)));
));
ctx_a.emit_ibc_event(IbcEvent::Message(event.event_type()));
ctx_a.emit_ibc_event(event);
}

for module_event in extras.events {
Expand Down Expand Up @@ -168,6 +169,7 @@ mod tests {
},
ics24_host::identifier::{ClientId, ConnectionId},
},
events::IbcEventType,
mock::context::MockContext,
};

Expand Down Expand Up @@ -279,11 +281,12 @@ mod tests {
assert!(res.is_ok());

// Unordered channels only emit one event
assert_eq!(ctx.events.len(), 1);
assert_eq!(ctx.events.len(), 2);
assert!(matches!(
ctx.events.first().unwrap(),
&IbcEvent::TimeoutPacket(_)
ctx.events[0],
IbcEvent::Message(IbcEventType::Timeout)
));
assert!(matches!(ctx.events[1], IbcEvent::TimeoutPacket(_)));
}

#[rstest]
Expand Down Expand Up @@ -316,8 +319,16 @@ mod tests {
assert!(res.is_ok());

// Ordered channels emit 2 events
assert_eq!(ctx.events.len(), 2);
assert!(matches!(ctx.events[0], IbcEvent::TimeoutPacket(_)));
assert!(matches!(ctx.events[1], IbcEvent::ChannelClosed(_)));
assert_eq!(ctx.events.len(), 4);
assert!(matches!(
ctx.events[0],
IbcEvent::Message(IbcEventType::Timeout)
));
assert!(matches!(ctx.events[1], IbcEvent::TimeoutPacket(_)));
assert!(matches!(
ctx.events[2],
IbcEvent::Message(IbcEventType::ChannelClosed)
));
assert!(matches!(ctx.events[3], IbcEvent::ChannelClosed(_)));
}
}
Loading