From ed07d09ad1e9e6349582f6a9933a3fb81f3a2d27 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Mon, 15 Jul 2024 15:41:38 -0400 Subject: [PATCH] fix: Address PR comments and print events in order --- .../tests/it/integration/hello_world.rs | 4 +- cmd/soroban-cli/src/log.rs | 37 +--------------- cmd/soroban-cli/src/log/contract_event.rs | 19 --------- cmd/soroban-cli/src/log/diagnostic_event.rs | 19 --------- cmd/soroban-cli/src/log/events.rs | 42 +++++++++++++++++++ cmd/soroban-cli/src/log/log_event.rs | 19 --------- 6 files changed, 46 insertions(+), 94 deletions(-) delete mode 100644 cmd/soroban-cli/src/log/contract_event.rs delete mode 100644 cmd/soroban-cli/src/log/diagnostic_event.rs create mode 100644 cmd/soroban-cli/src/log/events.rs delete mode 100644 cmd/soroban-cli/src/log/log_event.rs diff --git a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs index bd08ee6c5..11ea82b3c 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -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) { @@ -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") diff --git a/cmd/soroban-cli/src/log.rs b/cmd/soroban-cli/src/log.rs index 142b65231..8c5ec34c2 100644 --- a/cmd/soroban-cli/src/log.rs +++ b/cmd/soroban-cli/src/log.rs @@ -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 { match tx_meta { @@ -37,21 +22,3 @@ pub fn extract_events(tx_meta: &xdr::TransactionMeta) -> Vec 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) -} diff --git a/cmd/soroban-cli/src/log/contract_event.rs b/cmd/soroban-cli/src/log/contract_event.rs deleted file mode 100644 index 2bc61e7db..000000000 --- a/cmd/soroban-cli/src/log/contract_event.rs +++ /dev/null @@ -1,19 +0,0 @@ -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:#?}"); - } - _ => {} - } - } -} diff --git a/cmd/soroban-cli/src/log/diagnostic_event.rs b/cmd/soroban-cli/src/log/diagnostic_event.rs deleted file mode 100644 index 7508fc3bd..000000000 --- a/cmd/soroban-cli/src/log/diagnostic_event.rs +++ /dev/null @@ -1,19 +0,0 @@ -pub fn diagnostic_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:#?}"); - } - _ => {} - } - } -} diff --git a/cmd/soroban-cli/src/log/events.rs b/cmd/soroban-cli/src/log/events.rs new file mode 100644 index 000000000..47b4f89ff --- /dev/null +++ b/cmd/soroban-cli/src/log/events.rs @@ -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) +} diff --git a/cmd/soroban-cli/src/log/log_event.rs b/cmd/soroban-cli/src/log/log_event.rs deleted file mode 100644 index 76dfc54a5..000000000 --- a/cmd/soroban-cli/src/log/log_event.rs +++ /dev/null @@ -1,19 +0,0 @@ -pub fn log_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:#?}"); - } - _ => {} - } - } -}