From 5888f0ab416db6a92c7d6a6f8f4756c2a7d61e37 Mon Sep 17 00:00:00 2001 From: Blaine Heffron Date: Tue, 30 Jul 2024 11:00:53 -0400 Subject: [PATCH] restore the original function and add deprecated --- Cargo.lock | 167 +++++++++++++++++++++++++++++++++++ Cargo.toml | 5 +- src/lib.rs | 44 +++++++++ src/log.rs | 2 + src/log/diagnostic_events.rs | 25 ++++++ 5 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 src/log.rs create mode 100644 src/log/diagnostic_events.rs diff --git a/Cargo.lock b/Cargo.lock index cf9450b0..6933a224 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -722,6 +731,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.153" @@ -734,6 +749,15 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.7.1" @@ -760,6 +784,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -796,6 +830,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -858,6 +898,50 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "ring" version = "0.17.7" @@ -1052,6 +1136,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "slab" version = "0.4.9" @@ -1061,6 +1154,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "socket2" version = "0.5.5" @@ -1099,6 +1198,7 @@ dependencies = [ "thiserror", "tokio", "tracing", + "tracing-test", ] [[package]] @@ -1195,6 +1295,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.36" @@ -1337,6 +1447,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "tracing-test" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "557b891436fe0d5e0e363427fc7f217abf9ccd510d5136549847bdcbcd011d68" +dependencies = [ + "tracing-core", + "tracing-subscriber", + "tracing-test-macro", +] + +[[package]] +name = "tracing-test-macro" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04659ddb06c87d233c566112c1c9c5b9e98256d9af50ec3bc9c8327f873a7568" +dependencies = [ + "quote", + "syn", ] [[package]] @@ -1395,6 +1556,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 8792d6cc..af2bb982 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,4 +38,7 @@ tracing = "0.1.40" # networking jsonrpsee-http-client = "0.20.1" jsonrpsee-core = "0.20.1" -http = "1.0.0" \ No newline at end of file +http = "1.0.0" + +[dev-dependencies] +tracing-test = "0.2" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e5a8e498..decd1669 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ use termcolor::{Color, ColorChoice, StandardStream, WriteColor}; use termcolor_output::colored; use tokio::time::sleep; +mod log; mod txn; pub use txn::Assembled; @@ -824,9 +825,34 @@ impl Client { /// /// # Errors + #[deprecated( + since = "21.5.0", + note = "This method is deprecated. Use `simulate_and_prepare_transaction` instead which returns the full `SimulateTransactionResponse` in addtion the the `Assembled` transaction." + )] pub async fn simulate_and_assemble_transaction( &self, tx: &Transaction, + ) -> Result { + let sim_res = self + .simulate_transaction_envelope(&TransactionEnvelope::Tx(TransactionV1Envelope { + tx: tx.clone(), + signatures: VecM::default(), + })) + .await?; + match sim_res.error { + None => Ok(Assembled::new(tx, sim_res)?), + Some(e) => { + log::diagnostic_events(&sim_res.events, tracing::Level::ERROR); + Err(Error::TransactionSimulationFailed(e)) + } + } + } + + /// + /// # Errors + pub async fn simulate_and_prepare_transaction( + &self, + tx: &Transaction, ) -> Result { let sim_res = self .simulate_transaction_envelope(&TransactionEnvelope::Tx(TransactionV1Envelope { @@ -1153,9 +1179,11 @@ pub(crate) fn parse_cursor(c: &str) -> Result<(u64, i32), Error> { #[cfg(test)] mod tests { use super::*; + use crate::log; use std::env; use std::fs; use std::path::PathBuf; + use tracing_test::traced_test; #[test] fn simulation_transaction_response_parsing() { @@ -1402,4 +1430,20 @@ mod tests { } } } + + #[traced_test] + #[test] + fn test_diagnostic_events_logging() { + let events = vec![ + "AAAAAAAAAAAAAAAAAAAAAgAAAAAAAAADAAAADwAAAAdmbl9jYWxsAAAAAA0AAAAgfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAAPAAAABGRlY3IAAAAB".to_string(), + "AAAAAAAAAAAAAAABfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAACAAAAAAAAAAEAAAAPAAAAA2xvZwAAAAAQAAAAAQAAAAIAAAAOAAAACWNvdW50OiB7fQAAAAAAAAMAAAAA".to_string(), + "AAAAAAAAAAAAAAABfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAACAAAAAAAAAAIAAAAPAAAABWVycm9yAAAAAAAAAgAAAAEAAAAGAAAAEAAAAAEAAAACAAAADgAAACdWTSBjYWxsIHRyYXBwZWQ6IFVucmVhY2hhYmxlQ29kZVJlYWNoZWQAAAAADwAAAARkZWNy".to_string(), + ]; + + log::diagnostic_events(&events, tracing::Level::ERROR); + + assert!(logs_contain("0: \"AAAAAAAAAAAAAAAAAAAAAgAAAAAAAAADAAAADwAAAAdmbl9jYWxsAAAAAA0AAAAgfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAAPAAAABGRlY3IAAAAB\" {\"in_successful_contract_call\":false,\"event\":{\"ext\":\"v0\",\"contract_id\":null,\"type_\":\"diagnostic\",\"body\":{\"v0\":{\"topics\":[{\"symbol\":\"fn_call\"},{\"bytes\":\"7cabc3fe92093e546719ddd129a0594877e8abf9c96c9498c6455349c49b86e6\"},{\"symbol\":\"decr\"}],\"data\":\"void\"}}}}")); + assert!(logs_contain("1: \"AAAAAAAAAAAAAAABfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAACAAAAAAAAAAEAAAAPAAAAA2xvZwAAAAAQAAAAAQAAAAIAAAAOAAAACWNvdW50OiB7fQAAAAAAAAMAAAAA\" {\"in_successful_contract_call\":false,\"event\":{\"ext\":\"v0\",\"contract_id\":\"7cabc3fe92093e546719ddd129a0594877e8abf9c96c9498c6455349c49b86e6\",\"type_\":\"diagnostic\",\"body\":{\"v0\":{\"topics\":[{\"symbol\":\"log\"}],\"data\":{\"vec\":[{\"string\":\"count: {}\"},{\"u32\":0}]}}}}}")); + assert!(logs_contain("2: \"AAAAAAAAAAAAAAABfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAACAAAAAAAAAAIAAAAPAAAABWVycm9yAAAAAAAAAgAAAAEAAAAGAAAAEAAAAAEAAAACAAAADgAAACdWTSBjYWxsIHRyYXBwZWQ6IFVucmVhY2hhYmxlQ29kZVJlYWNoZWQAAAAADwAAAARkZWNy\" {\"in_successful_contract_call\":false,\"event\":{\"ext\":\"v0\",\"contract_id\":\"7cabc3fe92093e546719ddd129a0594877e8abf9c96c9498c6455349c49b86e6\",\"type_\":\"diagnostic\",\"body\":{\"v0\":{\"topics\":[{\"symbol\":\"error\"},{\"error\":{\"wasm_vm\":\"invalid_action\"}}],\"data\":{\"vec\":[{\"string\":\"VM call trapped: UnreachableCodeReached\"},{\"symbol\":\"decr\"}]}}}}}")); + } } diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 00000000..36126814 --- /dev/null +++ b/src/log.rs @@ -0,0 +1,2 @@ +pub mod diagnostic_events; +pub use diagnostic_events::*; diff --git a/src/log/diagnostic_events.rs b/src/log/diagnostic_events.rs new file mode 100644 index 00000000..15addb61 --- /dev/null +++ b/src/log/diagnostic_events.rs @@ -0,0 +1,25 @@ +use serde_json; +use stellar_xdr::curr::{DiagnosticEvent, Limits, ReadXdr}; + +pub fn diagnostic_events(events: &[String], level: tracing::Level) { + for (i, event_xdr) in events.iter().enumerate() { + let event_result = DiagnosticEvent::from_xdr_base64(event_xdr, Limits::none()); + let json_result = event_result + .as_ref() + .ok() + .and_then(|event| serde_json::to_string(event).ok()); + + let log_message = match (event_result, json_result) { + (Ok(_), Some(json)) => format!("{i}: {event_xdr:#?} {json}"), + (Err(e), _) => format!("{i}: {event_xdr:#?} Failed to decode DiagnosticEvent XDR: {e}"), + (Ok(_), None) => format!("{i}: {event_xdr:#?} JSON encoding of DiagnosticEvent failed"), + }; + + match level { + tracing::Level::TRACE => tracing::trace!("{}", log_message), + tracing::Level::INFO => tracing::info!("{}", log_message), + tracing::Level::ERROR => tracing::error!("{}", log_message), + _ => tracing::debug!("{}", log_message), // Default to debug for other levels + } + } +}