Skip to content

Commit

Permalink
feat: separate out different events to only log user defined ones by …
Browse files Browse the repository at this point in the history
…default (#1394)

* feat: separate contract events from diagnostic

* feat: separate out log events

* fix: fmt

* fix: clippy
  • Loading branch information
willemneal authored Aug 5, 2024
1 parent aba5bdd commit 00826cf
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
22 changes: 22 additions & 0 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ async fn invoke() {
handles_kebab_case(sandbox, id).await;
fetch(sandbox, id).await;
invoke_prng_u64_in_range_test(sandbox, id).await;
invoke_log(sandbox, id);
}

fn invoke_hello_world(sandbox: &TestEnv, id: &str) {
Expand Down Expand Up @@ -381,3 +382,24 @@ async fn invoke_prng_u64_in_range_test(sandbox: &TestEnv, id: &str) {
.await
.is_ok());
}
fn invoke_log(sandbox: &TestEnv, id: &str) {
sandbox
.new_assert_cmd("contract")
.arg("invoke")
.arg("--id")
.arg(id)
.arg("--")
.arg("log")
.arg("--str=world")
.assert()
.success()
.stderr(predicates::str::contains(
"INFO contract_event: soroban_cli::log::event: 1: DiagnosticEvent {",
))
.stderr(predicates::str::contains("StringM(hello)"))
.stderr(predicates::str::contains(
"INFO log_event: soroban_cli::log::event: 2: DiagnosticEvent",
))
.stderr(predicates::str::contains("StringM(hello {})"))
.stderr(predicates::str::contains("StringM(world)"));
}
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 @@ -386,10 +386,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
15 changes: 13 additions & 2 deletions cmd/soroban-cli/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use crate::xdr;
pub mod auth;
pub mod budget;
pub mod cost;
pub mod diagnostic_event;
pub mod event;
pub mod footprint;
pub mod host_event;

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

pub fn extract_events(tx_meta: &xdr::TransactionMeta) -> Vec<xdr::DiagnosticEvent> {
match tx_meta {
xdr::TransactionMeta::V3(xdr::TransactionMetaV3 {
soroban_meta: Some(meta),
..
}) => meta.diagnostic_events.to_vec(),
_ => Vec::new(),
}
}
11 changes: 0 additions & 11 deletions cmd/soroban-cli/src/log/diagnostic_event.rs

This file was deleted.

44 changes: 44 additions & 0 deletions cmd/soroban-cli/src/log/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use tracing::{debug, info, span, Level};

use crate::xdr;

pub fn events(events: &[xdr::DiagnosticEvent]) {
for (i, event) in events.iter().enumerate() {
let span = if is_contract_event(event) {
span!(Level::INFO, "contract_event")
} else if is_log_event(event) {
span!(Level::INFO, "log_event")
} else {
span!(Level::DEBUG, "diagnostic_event")
};

let _enter = span.enter();

if span.metadata().unwrap().level() == &Level::INFO {
info!("{i}: {event:#?}");
} else {
debug!("{i}: {event:#?}");
}
}
}

fn is_contract_event(event: &xdr::DiagnosticEvent) -> bool {
matches!(event.event.type_, xdr::ContractEventType::Contract)
}

fn is_log_event(event: &xdr::DiagnosticEvent) -> bool {
match &event.event.body {
xdr::ContractEventBody::V0(xdr::ContractEventV0 { topics, .. })
if topics.len() == 1
&& matches!(event.event.type_, xdr::ContractEventType::Diagnostic) =>
{
topics[0] == xdr::ScVal::Symbol(str_to_sc_symbol("log"))
}
xdr::ContractEventBody::V0(_) => false,
}
}

fn str_to_sc_symbol(s: &str) -> xdr::ScSymbol {
let inner: xdr::StringM<32> = s.try_into().unwrap();
xdr::ScSymbol(inner)
}

0 comments on commit 00826cf

Please sign in to comment.