Skip to content

Commit

Permalink
fix: Address PR comments and print events in order
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jul 15, 2024
1 parent aff0427 commit ed07d09
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 94 deletions.
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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).await;
invoke_log(sandbox, id);
}

fn invoke_hello_world(sandbox: &TestEnv, id: &str) {
Expand Down Expand Up @@ -374,7 +374,7 @@ async fn invoke_prng_u64_in_range_test(sandbox: &TestEnv, id: &str) {
.await
.is_ok());
}
async fn invoke_log(sandbox: &TestEnv, id: &str) {
fn invoke_log(sandbox: &TestEnv, id: &str) {
sandbox
.new_assert_cmd("contract")
.arg("invoke")
Expand Down
37 changes: 2 additions & 35 deletions cmd/soroban-cli/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
use crate::xdr;
pub mod auth;
pub mod budget;
pub mod contract_event;
pub mod cost;
pub mod diagnostic_event;
pub mod events;
pub mod footprint;
pub mod host_event;
#[allow(clippy::module_name_repetitions)]
pub mod log_event;

pub use auth::*;
pub use budget::*;
pub use contract_event::*;
pub use cost::*;
pub use diagnostic_event::*;
pub use events::*;
pub use footprint::*;
pub use host_event::*;
pub use log_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);
let (log_events, other_events): (Vec<_>, Vec<_>) =
other_events.into_iter().partition(|e| is_log_event(e));
log_event::log_events(&log_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 {
Expand All @@ -37,21 +22,3 @@ pub fn extract_events(tx_meta: &xdr::TransactionMeta) -> Vec<xdr::DiagnosticEven
_ => Vec::new(),
}
}

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 => {
topics[0] == xdr::ScVal::Symbol(str_to_sc_string("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)
}
19 changes: 0 additions & 19 deletions cmd/soroban-cli/src/log/contract_event.rs

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/soroban-cli/src/log/diagnostic_event.rs

This file was deleted.

42 changes: 42 additions & 0 deletions cmd/soroban-cli/src/log/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::xdr;
pub fn log(event: &xdr::DiagnosticEvent) {
tracing::info!("{event:#?}");
}

pub fn contract(event: &xdr::DiagnosticEvent) {
tracing::info!("{event:#?}");
}

pub fn diagnostic(event: &xdr::DiagnosticEvent) {
tracing::debug!("{event:#?}");
}

pub fn events(events: &[xdr::DiagnosticEvent]) {
for event in events {
if is_contract_event(event) {
contract(event);
} else if is_log_event(event) {
log(event);
} else {
diagnostic(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 => {
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)
}
19 changes: 0 additions & 19 deletions cmd/soroban-cli/src/log/log_event.rs

This file was deleted.

0 comments on commit ed07d09

Please sign in to comment.