Skip to content

Commit

Permalink
restore the original function and add deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron committed Jul 30, 2024
1 parent 0ea004b commit 5888f0a
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 1 deletion.
167 changes: 167 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ tracing = "0.1.40"
# networking
jsonrpsee-http-client = "0.20.1"
jsonrpsee-core = "0.20.1"
http = "1.0.0"
http = "1.0.0"

[dev-dependencies]
tracing-test = "0.2"
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Assembled, Error> {
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<AssembledOutcome, Error> {
let sim_res = self
.simulate_transaction_envelope(&TransactionEnvelope::Tx(TransactionV1Envelope {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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\"}]}}}}}"));
}
}
2 changes: 2 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod diagnostic_events;
pub use diagnostic_events::*;
25 changes: 25 additions & 0 deletions src/log/diagnostic_events.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 5888f0a

Please sign in to comment.