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

imp(ibc-testkit): remove field access of MockContext #1046

Merged
merged 5 commits into from
Jan 18, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ibc-testkit] Remove field access of `MockContext`.
([\#1043](https://github.com/cosmos/ibc-rs/issues/1043))
2 changes: 0 additions & 2 deletions ibc-testkit/src/fixtures/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ impl From<MockContextConfig> for MockContext {
history,
block_time: params.block_time,
ibc_store: Arc::new(Mutex::new(MockIbcStore::default())),
events: Vec::new(),
logs: Vec::new(),
}
}
}
4 changes: 2 additions & 2 deletions ibc-testkit/src/testapp/ibc/core/core_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,12 @@ impl ExecutionContext for MockContext {
}

fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), ContextError> {
self.events.push(event);
self.ibc_store.lock().events.push(event);
Ok(())
}

fn log_message(&mut self, message: String) -> Result<(), ContextError> {
self.logs.push(message);
self.ibc_store.lock().logs.push(message);
Ok(())
}
}
26 changes: 15 additions & 11 deletions ibc-testkit/src/testapp/ibc/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ pub struct MockIbcStore {
/// Constant-size commitments to packets data fields
pub packet_commitment: PortChannelIdMap<BTreeMap<Sequence, PacketCommitment>>,

// Used by unordered channel
/// Used by unordered channel
pub packet_receipt: PortChannelIdMap<BTreeMap<Sequence, Receipt>>,

/// Emitted IBC events in order
pub events: Vec<IbcEvent>,

/// Logs of the IBC module
pub logs: Vec<String>,
}

/// A context implementing the dependencies necessary for testing any IBC module.
Expand All @@ -116,10 +122,6 @@ pub struct MockContext {

/// An object that stores all IBC related data.
pub ibc_store: Arc<Mutex<MockIbcStore>>,

pub events: Vec<IbcEvent>,

pub logs: Vec<String>,
}

#[derive(Debug, TypedBuilder)]
Expand Down Expand Up @@ -170,8 +172,6 @@ impl Clone for MockContext {
history: self.history.clone(),
block_time: self.block_time,
ibc_store,
events: self.events.clone(),
logs: self.logs.clone(),
}
}
}
Expand Down Expand Up @@ -232,8 +232,6 @@ impl MockContext {
.collect(),
block_time,
ibc_store: Arc::new(Mutex::new(MockIbcStore::default())),
events: Vec::new(),
logs: Vec::new(),
}
}

Expand Down Expand Up @@ -299,8 +297,6 @@ impl MockContext {
history,
block_time,
ibc_store: Arc::new(Mutex::new(MockIbcStore::default())),
events: Vec::new(),
logs: Vec::new(),
}
}

Expand Down Expand Up @@ -781,6 +777,14 @@ impl MockContext {
let block_ref = self.host_block(&self.host_height().expect("Never fails"));
block_ref.cloned()
}

pub fn get_events(&self) -> Vec<IbcEvent> {
self.ibc_store.lock().events.clone()
}

pub fn get_logs(&self) -> Vec<String> {
self.ibc_store.lock().logs.clone()
}
}

#[cfg(test)]
Expand Down
13 changes: 8 additions & 5 deletions ibc-testkit/tests/core/ics02_client/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,13 @@ fn test_update_client_events() {
let res = execute(&mut ctx, &mut router, msg_envelope);
assert!(res.is_ok());

let ibc_events = ctx.get_events();

assert!(matches!(
ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Client)
));
let update_client_event = downcast!(&ctx.events[1] => IbcEvent::UpdateClient).unwrap();
let update_client_event = downcast!(&ibc_events[1] => IbcEvent::UpdateClient).unwrap();

assert_eq!(update_client_event.client_id(), &client_id);
assert_eq!(update_client_event.client_type(), &mock_client_type());
Expand All @@ -696,13 +698,14 @@ fn ensure_misbehaviour(ctx: &MockContext, client_id: &ClientId, client_type: &Cl
assert!(status.is_frozen(), "client_state status: {status}");

// check events
assert_eq!(ctx.events.len(), 2);
let ibc_events = ctx.get_events();
assert_eq!(ibc_events.len(), 2);
assert!(matches!(
ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Client),
));
let misbehaviour_client_event =
downcast!(&ctx.events[1] => IbcEvent::ClientMisbehaviour).unwrap();
downcast!(&ibc_events[1] => IbcEvent::ClientMisbehaviour).unwrap();
assert_eq!(misbehaviour_client_event.client_id(), client_id);
assert_eq!(misbehaviour_client_event.client_type(), client_type);
}
Expand Down
5 changes: 3 additions & 2 deletions ibc-testkit/tests/core/ics02_client/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ fn upgrade_client_execute(fxt: &mut Fixture<MsgUpgradeClient>, expect: Expect) {
}
Expect::Success => {
assert!(res.is_ok(), "{err_msg}");
let ibc_events = fxt.ctx.get_events();
assert!(matches!(
fxt.ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Client)
));
let upgrade_client_event =
downcast!(&fxt.ctx.events[1] => IbcEvent::UpgradeClient).unwrap();
downcast!(&ibc_events[1] => IbcEvent::UpgradeClient).unwrap();
let plan_height = Height::new(1, 26).unwrap();

assert_eq!(upgrade_client_event.client_id(), &fxt.msg.client_id);
Expand Down
7 changes: 4 additions & 3 deletions ibc-testkit/tests/core/ics03_connection/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ fn conn_open_ack_execute(fxt: &mut Fixture<MsgConnectionOpenAck>, expect: Expect
assert!(res.is_err(), "{err_msg}");
}
Expect::Success => {
let ibc_events = fxt.ctx.get_events();
assert!(res.is_ok(), "{err_msg}");
assert_eq!(fxt.ctx.events.len(), 2);
assert_eq!(ibc_events.len(), 2);

assert!(matches!(
fxt.ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Connection)
));
let event = &fxt.ctx.events[1];
let event = &ibc_events[1];
assert!(matches!(event, &IbcEvent::OpenAckConnection(_)));

let conn_open_try_event = match event {
Expand Down
7 changes: 4 additions & 3 deletions ibc-testkit/tests/core/ics03_connection/conn_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ fn conn_open_confirm_execute(fxt: &mut Fixture<MsgConnectionOpenConfirm>, expect
assert!(res.is_err(), "{err_msg}");
}
Expect::Success => {
let ibc_events = fxt.ctx.get_events();
assert!(res.is_ok(), "{err_msg}");
assert_eq!(fxt.ctx.events.len(), 2);
assert_eq!(ibc_events.len(), 2);

assert!(matches!(
fxt.ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Connection)
));
let event = &fxt.ctx.events[1];
let event = &ibc_events[1];
assert!(matches!(event, &IbcEvent::OpenConfirmConnection(_)));

let conn_open_try_event = match event {
Expand Down
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics03_connection/conn_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ fn conn_open_init_execute(
assert!(res.is_err(), "{err_msg}")
}
Expect::Success => {
let ibc_events = fxt.ctx.get_events();

assert!(res.is_ok(), "{err_msg}");

assert_eq!(fxt.ctx.connection_counter().unwrap(), 1);

assert_eq!(fxt.ctx.events.len(), 2);
assert_eq!(ibc_events.len(), 2);

assert!(matches!(
fxt.ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Connection)
));
let event = &fxt.ctx.events[1];
let event = &ibc_events[1];
assert!(matches!(event, &IbcEvent::OpenInitConnection(_)));

let conn_open_init_event = match event {
Expand Down
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics03_connection/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ fn conn_open_try_execute(fxt: &mut Fixture<MsgConnectionOpenTry>, expect: Expect

assert_eq!(fxt.ctx.connection_counter().unwrap(), 1);

assert_eq!(fxt.ctx.events.len(), 2);
let ibc_events = fxt.ctx.get_events();

assert_eq!(ibc_events.len(), 2);

assert!(matches!(
fxt.ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Connection)
));
let event = &fxt.ctx.events[1];
let event = &ibc_events[1];
assert!(matches!(event, &IbcEvent::OpenTryConnection(_)));

let conn_open_try_event = match event {
Expand Down
16 changes: 10 additions & 6 deletions ibc-testkit/tests/core/ics04_channel/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,14 @@ fn ack_unordered_chan_execute(fixture: Fixture) {

assert!(res.is_ok());

assert_eq!(ctx.events.len(), 2);
let ibc_events = ctx.get_events();

assert_eq!(ibc_events.len(), 2);
assert!(matches!(
ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(ctx.events[1], IbcEvent::AcknowledgePacket(_)));
assert!(matches!(ibc_events[1], IbcEvent::AcknowledgePacket(_)));
}

#[rstest]
Expand Down Expand Up @@ -256,10 +258,12 @@ fn ack_ordered_chan_execute(fixture: Fixture) {

assert!(res.is_ok());

assert_eq!(ctx.events.len(), 2);
let ibc_events = ctx.get_events();

assert_eq!(ibc_events.len(), 2);
assert!(matches!(
ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(ctx.events[1], IbcEvent::AcknowledgePacket(_)));
assert!(matches!(ibc_events[1], IbcEvent::AcknowledgePacket(_)));
}
11 changes: 5 additions & 6 deletions ibc-testkit/tests/core/ics04_channel/chan_close_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@ fn test_chan_close_confirm_execute() {

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

assert_eq!(context.events.len(), 2);
let ibc_events = context.get_events();

assert_eq!(ibc_events.len(), 2);

assert!(matches!(
context.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));

assert!(matches!(
context.events[1],
IbcEvent::CloseConfirmChannel(_)
));
assert!(matches!(ibc_events[1], IbcEvent::CloseConfirmChannel(_)));
}
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics04_channel/chan_close_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ fn test_chan_close_init_execute() {

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

assert_eq!(context.events.len(), 2);
let ibc_events = context.get_events();

assert_eq!(ibc_events.len(), 2);

assert!(matches!(
context.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));

assert!(matches!(context.events[1], IbcEvent::CloseInitChannel(_)));
assert!(matches!(ibc_events[1], IbcEvent::CloseInitChannel(_)));
}
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics04_channel/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ fn chan_open_ack_execute_happy_path(fixture: Fixture) {

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

assert_eq!(context.events.len(), 2);
let ibc_events = context.get_events();

assert_eq!(ibc_events.len(), 2);
assert!(matches!(
context.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(context.events[1], IbcEvent::OpenAckChannel(_)));
assert!(matches!(ibc_events[1], IbcEvent::OpenAckChannel(_)));
}

#[rstest]
Expand Down
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics04_channel/chan_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ fn chan_open_confirm_execute_happy_path(fixture: Fixture) {

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

assert_eq!(context.events.len(), 2);
let ibc_events = context.get_events();

assert_eq!(ibc_events.len(), 2);

assert!(matches!(
context.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));

assert!(matches!(context.events[1], IbcEvent::OpenConfirmChannel(_)));
assert!(matches!(ibc_events[1], IbcEvent::OpenConfirmChannel(_)));
}

#[rstest]
Expand Down
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics04_channel/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ fn chan_open_init_execute_happy_path(fixture: Fixture) {

assert_eq!(ctx.channel_counter().unwrap(), 1);

assert_eq!(ctx.events.len(), 2);
let ibc_events = ctx.get_events();

assert_eq!(ibc_events.len(), 2);

assert!(matches!(
ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(ctx.events[1], IbcEvent::OpenInitChannel(_)));
assert!(matches!(ibc_events[1], IbcEvent::OpenInitChannel(_)));
}

#[rstest]
Expand Down
8 changes: 5 additions & 3 deletions ibc-testkit/tests/core/ics04_channel/chan_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ fn chan_open_try_execute_happy_path(fixture: Fixture) {

assert_eq!(ctx.channel_counter().unwrap(), 1);

assert_eq!(ctx.events.len(), 2);
let ibc_events = ctx.get_events();

assert_eq!(ibc_events.len(), 2);

assert!(matches!(
ctx.events[0],
ibc_events[0],
IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(ctx.events[1], IbcEvent::OpenTryChannel(_)));
assert!(matches!(ibc_events[1], IbcEvent::OpenTryChannel(_)));
}

#[rstest]
Expand Down
12 changes: 7 additions & 5 deletions ibc-testkit/tests/core/ics04_channel/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,17 @@ fn recv_packet_execute_happy_path(fixture: Fixture) {

assert!(res.is_ok());

assert_eq!(ctx.events.len(), 4);
let ibc_events = ctx.get_events();

assert_eq!(ibc_events.len(), 4);
assert!(matches!(
&ctx.events[0],
&ibc_events[0],
&IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(&ctx.events[1], &IbcEvent::ReceivePacket(_)));
assert!(matches!(&ibc_events[1], &IbcEvent::ReceivePacket(_)));
assert!(matches!(
&ctx.events[2],
&ibc_events[2],
&IbcEvent::Message(MessageEvent::Channel)
));
assert!(matches!(&ctx.events[3], &IbcEvent::WriteAcknowledgement(_)));
assert!(matches!(&ibc_events[3], &IbcEvent::WriteAcknowledgement(_)));
}
Loading
Loading