Skip to content

Commit

Permalink
feat: separate contract events from diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jun 19, 2024
1 parent 516bf59 commit d4e38f1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
10 changes: 7 additions & 3 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,14 @@ impl NetworkRunnable for Cmd {
if !no_cache {
data::write(res.clone().try_into()?, &network.rpc_uri()?)?;
}
(res.return_value()?, res.contract_events()?)
let events = res
.result_meta
.as_ref()
.map(crate::log::extract_events)
.unwrap_or_default();
(res.return_value()?, events)
};

crate::log::diagnostic_events(&events, tracing::Level::INFO);
crate::log::events(&events);
output_to_string(&spec, &return_value, &function)
}
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/soroban-cli/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
use crate::xdr;
pub mod auth;
pub mod budget;
pub mod contract_event;
pub mod cost;
pub mod diagnostic_event;
pub mod footprint;
pub mod host_event;

pub use auth::*;
pub use budget::*;
pub use contract_event::*;
pub use cost::*;
pub use diagnostic_event::*;
pub use footprint::*;
pub use host_event::*;

pub fn events(events: &[xdr::DiagnosticEvent]) {
let (contract_events, other_events): (Vec<_>, Vec<_>) =
events.iter().partition(|e| is_contract_event(e));
contract_event::contract_events(&contract_events, tracing::Level::INFO);
diagnostic_event::diagnostic_events(&other_events, tracing::Level::DEBUG);
}

pub fn extract_events(tx_meta: &xdr::TransactionMeta) -> Vec<xdr::DiagnosticEvent> {
match tx_meta {
xdr::TransactionMeta::V3(xdr::TransactionMetaV3 {
soroban_meta: Some(meta),
..
}) => {
let mut events = meta.diagnostic_events.to_vec();
// NOTE: we assume there can only be one operation, since we only send one
if meta.events.len() >= 1 {
events.extend(meta.events.iter().map(|e| xdr::DiagnosticEvent {
in_successful_contract_call: true,
event: e.clone(),
}));
};
events
}
_ => Vec::new(),
}
}

fn is_contract_event(event: &xdr::DiagnosticEvent) -> bool {
matches!(event.event.type_, xdr::ContractEventType::Contract)
}
19 changes: 19 additions & 0 deletions cmd/soroban-cli/src/log/contract_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub fn contract_events(events: &[impl std::fmt::Debug], level: tracing::Level) {
for (i, event) in events.iter().enumerate() {
match level {
tracing::Level::TRACE => {
tracing::trace!("{i}: {event:#?}");
}
tracing::Level::INFO => {
tracing::info!("{i}: {event:#?}");
}
tracing::Level::ERROR => {
tracing::error!("{i}: {event:#?}");
}
tracing::Level::DEBUG => {
tracing::debug!("{i}: {event:#?}");
}
_ => {}
}
}
}
20 changes: 14 additions & 6 deletions cmd/soroban-cli/src/log/diagnostic_event.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
pub fn diagnostic_events(events: &[impl std::fmt::Debug], level: tracing::Level) {
for (i, event) in events.iter().enumerate() {
if level == tracing::Level::TRACE {
tracing::trace!("{i}: {event:#?}");
} else if level == tracing::Level::INFO {
tracing::info!("{i}: {event:#?}");
} else if level == tracing::Level::ERROR {
tracing::error!("{i}: {event:#?}");
match level {
tracing::Level::TRACE => {
tracing::trace!("{i}: {event:#?}");
}
tracing::Level::INFO => {
tracing::info!("{i}: {event:#?}");
}
tracing::Level::ERROR => {
tracing::error!("{i}: {event:#?}");
}
tracing::Level::DEBUG => {
tracing::debug!("{i}: {event:#?}");
}
_ => {}
}
}
}

0 comments on commit d4e38f1

Please sign in to comment.