Skip to content

Commit

Permalink
imp: return Result type in logger and event emitter APIs (cosmos#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani authored Sep 7, 2023
1 parent 366b394 commit 133863d
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- logger and event emitter methods return `Result<(), ContextError>` type.
([#859](https://github.com/cosmos/ibc-rs/issues/859))
6 changes: 3 additions & 3 deletions crates/ibc/src/applications/transfer/relay/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ where
send_packet_ctx_a.log_message(format!(
"IBC fungible token transfer: {} --({})--> {}",
msg.packet_data.sender, token, msg.packet_data.receiver
));
))?;

let transfer_event = TransferEvent {
sender: msg.packet_data.sender,
Expand All @@ -179,9 +179,9 @@ where
denom: msg.packet_data.token.denom,
memo: msg.packet_data.memo,
};
send_packet_ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into());
send_packet_ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into())?;

send_packet_ctx_a.emit_ibc_event(MessageEvent::Module(MODULE_ID_STR.to_string()).into());
send_packet_ctx_a.emit_ibc_event(MessageEvent::Module(MODULE_ID_STR.to_string()).into())?;
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/ibc/src/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ pub trait ExecutionContext: ValidationContext {
fn increase_channel_counter(&mut self) -> Result<(), ContextError>;

/// Emit the given IBC event
fn emit_ibc_event(&mut self, event: IbcEvent);
fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), ContextError>;

/// Log the given message.
fn log_message(&mut self, message: String);
fn log_message(&mut self, message: String) -> Result<(), ContextError>;
}
6 changes: 3 additions & 3 deletions crates/ibc/src/core/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ where
client_type,
latest_height,
));
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client));
ctx.emit_ibc_event(event);
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client))?;
ctx.emit_ibc_event(event)?;

ctx.log_message(format!(
"success: generated new client identifier: {client_id}"
));
))?;

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ibc/src/core/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ where
client_id,
client_state.client_type(),
));
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client));
ctx.emit_ibc_event(event);
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client))?;
ctx.emit_ibc_event(event)?;
} else {
if !matches!(update_kind, UpdateKind::UpdateClient) {
return Err(ClientError::MisbehaviourHandlingFailure {
Expand Down Expand Up @@ -122,8 +122,8 @@ where
header.encode_to_vec(),
))
};
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client));
ctx.emit_ibc_event(event);
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client))?;
ctx.emit_ibc_event(event)?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ibc/src/core/ics02_client/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ where
old_client_state.client_type(),
latest_height,
));
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client));
ctx.emit_ibc_event(event);
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client))?;
ctx.emit_ibc_event(event)?;

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ibc/src/core/ics03_connection/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ where
msg.conn_id_on_b.clone(),
vars.client_id_on_b().clone(),
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection));
ctx_a.emit_ibc_event(event);
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection))?;
ctx_a.emit_ibc_event(event)?;

ctx_a.log_message("success: conn_open_ack verification passed".to_string());
ctx_a.log_message("success: conn_open_ack verification passed".to_string())?;

{
let new_conn_end_on_a = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ where
conn_id_on_a.clone(),
client_id_on_a.clone(),
));
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection));
ctx_b.emit_ibc_event(event);
ctx_b.log_message("success: conn_open_confirm verification passed".to_string());
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection))?;
ctx_b.emit_ibc_event(event)?;
ctx_b.log_message("success: conn_open_confirm verification passed".to_string())?;

{
let new_conn_end_on_b = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where

ctx_a.log_message(format!(
"success: conn_open_init: generated new connection identifier: {conn_id_on_a}"
));
))?;

{
let client_id_on_b = msg.counterparty.client_id().clone();
Expand All @@ -73,8 +73,8 @@ where
msg.client_id_on_a.clone(),
client_id_on_b,
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection));
ctx_a.emit_ibc_event(event);
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection))?;
ctx_a.emit_ibc_event(event)?;
}

ctx_a.increase_connection_counter()?;
Expand Down
6 changes: 3 additions & 3 deletions crates/ibc/src/core/ics03_connection/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ where
conn_id_on_a.clone(),
vars.client_id_on_a.clone(),
));
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection));
ctx_b.emit_ibc_event(event);
ctx_b.log_message("success: conn_open_try verification passed".to_string());
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Connection))?;
ctx_b.emit_ibc_event(event)?;
ctx_b.log_message("success: conn_open_try verification passed".to_string())?;

ctx_b.increase_connection_counter()?;
ctx_b.store_connection_to_client(
Expand Down
8 changes: 4 additions & 4 deletions crates/ibc/src/core/ics04_channel/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ pub trait SendPacketExecutionContext: SendPacketValidationContext {
) -> Result<(), ContextError>;

/// Ibc events
fn emit_ibc_event(&mut self, event: IbcEvent);
fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), ContextError>;

/// Logging facility
fn log_message(&mut self, message: String);
fn log_message(&mut self, message: String) -> Result<(), ContextError>;
}

impl<T> SendPacketExecutionContext for T
Expand All @@ -129,11 +129,11 @@ where
self.store_packet_commitment(commitment_path, commitment)
}

fn emit_ibc_event(&mut self, event: IbcEvent) {
fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), ContextError> {
self.emit_ibc_event(event)
}

fn log_message(&mut self, message: String) {
fn log_message(&mut self, message: String) -> Result<(), ContextError> {
self.log_message(message)
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ where
chan_end_on_a.ordering,
conn_id_on_a.clone(),
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_a.emit_ibc_event(event);
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(event)?;

let commitment_path_on_a = CommitmentPath::new(
&msg.packet.port_id_on_a,
Expand Down Expand Up @@ -93,16 +93,16 @@ where

// emit events and logs
{
ctx_a.log_message("success: packet acknowledgement".to_string());
ctx_a.log_message("success: packet acknowledgement".to_string())?;

// Note: Acknowledgement event was emitted at the beginning

for module_event in extras.events {
ctx_a.emit_ibc_event(IbcEvent::Module(module_event));
ctx_a.emit_ibc_event(IbcEvent::Module(module_event))?
}

for log_message in extras.log {
ctx_a.log_message(log_message);
ctx_a.log_message(log_message)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/chan_close_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where

// emit events and logs
{
ctx_b.log_message("success: channel close confirm".to_string());
ctx_b.log_message("success: channel close confirm".to_string())?;

let core_event = {
let port_id_on_a = chan_end_on_b.counterparty().port_id.clone();
Expand All @@ -78,15 +78,15 @@ where
conn_id_on_b,
))
};
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_b.emit_ibc_event(core_event);
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_b.emit_ibc_event(core_event)?;

for module_event in extras.events {
ctx_b.emit_ibc_event(IbcEvent::Module(module_event));
ctx_b.emit_ibc_event(IbcEvent::Module(module_event))?;
}

for log_message in extras.log {
ctx_b.log_message(log_message);
ctx_b.log_message(log_message)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/chan_close_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where

// emit events and logs
{
ctx_a.log_message("success: channel close init".to_string());
ctx_a.log_message("success: channel close init".to_string())?;

let core_event = {
let port_id_on_b = chan_end_on_a.counterparty().port_id.clone();
Expand All @@ -75,15 +75,15 @@ where
conn_id_on_a,
))
};
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_a.emit_ibc_event(core_event);
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(core_event)?;

for module_event in extras.events {
ctx_a.emit_ibc_event(IbcEvent::Module(module_event));
ctx_a.emit_ibc_event(IbcEvent::Module(module_event))?;
}

for log_message in extras.log {
ctx_a.log_message(log_message);
ctx_a.log_message(log_message)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where

// emit events and logs
{
ctx_a.log_message("success: channel open ack".to_string());
ctx_a.log_message("success: channel open ack".to_string())?;

let core_event = {
let port_id_on_b = chan_end_on_a.counterparty().port_id.clone();
Expand All @@ -74,15 +74,15 @@ where
conn_id_on_a,
))
};
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_a.emit_ibc_event(core_event);
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(core_event)?;

for module_event in extras.events {
ctx_a.emit_ibc_event(IbcEvent::Module(module_event));
ctx_a.emit_ibc_event(IbcEvent::Module(module_event))?;
}

for log_message in extras.log {
ctx_a.log_message(log_message);
ctx_a.log_message(log_message)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/chan_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where

// emit events and logs
{
ctx_b.log_message("success: channel open confirm".to_string());
ctx_b.log_message("success: channel open confirm".to_string())?;

let conn_id_on_b = chan_end_on_b.connection_hops[0].clone();
let port_id_on_a = chan_end_on_b.counterparty().port_id.clone();
Expand All @@ -77,15 +77,15 @@ where
chan_id_on_a,
conn_id_on_b,
));
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_b.emit_ibc_event(core_event);
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_b.emit_ibc_event(core_event)?;

for module_event in extras.events {
ctx_b.emit_ibc_event(IbcEvent::Module(module_event));
ctx_b.emit_ibc_event(IbcEvent::Module(module_event))?;
}

for log_message in extras.log {
ctx_b.log_message(log_message);
ctx_b.log_message(log_message)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ where
{
ctx_a.log_message(format!(
"success: channel open init with channel identifier: {chan_id_on_a}"
));
))?;
let core_event = IbcEvent::OpenInitChannel(OpenInit::new(
msg.port_id_on_a.clone(),
chan_id_on_a.clone(),
msg.port_id_on_b,
conn_id_on_a,
version,
));
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_a.emit_ibc_event(core_event);
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_a.emit_ibc_event(core_event)?;

for module_event in extras.events {
ctx_a.emit_ibc_event(IbcEvent::Module(module_event));
ctx_a.emit_ibc_event(IbcEvent::Module(module_event))?;
}

for log_message in extras.log {
ctx_a.log_message(log_message);
ctx_a.log_message(log_message)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ibc/src/core/ics04_channel/handler/chan_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
{
ctx_b.log_message(format!(
"success: channel open try with channel identifier: {chan_id_on_b}"
));
))?;

let core_event = IbcEvent::OpenTryChannel(OpenTry::new(
msg.port_id_on_b.clone(),
Expand All @@ -102,15 +102,15 @@ where
conn_id_on_b,
version,
));
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel));
ctx_b.emit_ibc_event(core_event);
ctx_b.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
ctx_b.emit_ibc_event(core_event)?;

for module_event in extras.events {
ctx_b.emit_ibc_event(IbcEvent::Module(module_event));
ctx_b.emit_ibc_event(IbcEvent::Module(module_event))?;
}

for log_message in extras.log {
ctx_b.log_message(log_message);
ctx_b.log_message(log_message)?;
}
}

Expand Down
Loading

0 comments on commit 133863d

Please sign in to comment.