Skip to content

Commit c6bd0cb

Browse files
committed
Route Soroban TraceHook-based tracing into core trace-level tx logging
1 parent 4f288e2 commit c6bd0cb

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

Cargo.lock

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cackle.toml

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ include = [
2222
"std::sys::unix::rand::hashmap_random_keys",
2323
"hashbrown::raw",
2424
]
25+
no_auto_detect = [
26+
"soroban-env-common",
27+
]
2528

2629
[api.rand]
2730
include = [

src/rust/Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rand = "=0.8.5"
2121

2222
petgraph = "=0.6.4"
2323
toml = "=0.7.8"
24+
itertools = "=0.11.0"
2425

2526
# This copy of the soroban host is always enabled, and should always point to a
2627
# version that supports stellar-core's Config::CURRENT_LEDGER_PROTOCOL_VERSION.
@@ -35,7 +36,7 @@ toml = "=0.7.8"
3536
version = "=20.1.0"
3637
git = "https://github.com/stellar/rs-soroban-env"
3738
package = "soroban-env-host"
38-
rev = "c1b238b65bfd13666be4ac14e0e390c31b549caf"
39+
rev = "b6ef8e32d86b008b2ecfdb2d7ca958e751190523"
3940

4041
# This copy of the soroban host is _optional_ and only enabled during protocol
4142
# transitions. When transitioning from protocol N to N+1, the `curr` copy
@@ -64,12 +65,12 @@ rev = "bed170cde09a53c85ae2e02f9278ef0dfa4e0900"
6465
[dependencies.soroban-test-wasms]
6566
version = "=20.1.0"
6667
git = "https://github.com/stellar/rs-soroban-env"
67-
rev = "c1b238b65bfd13666be4ac14e0e390c31b549caf"
68+
rev = "b6ef8e32d86b008b2ecfdb2d7ca958e751190523"
6869

6970
[dependencies.soroban-synth-wasm]
7071
version = "=20.1.0"
7172
git = "https://github.com/stellar/rs-soroban-env"
72-
rev = "c1b238b65bfd13666be4ac14e0e390c31b549caf"
73+
rev = "b6ef8e32d86b008b2ecfdb2d7ca958e751190523"
7374

7475
[dependencies.cargo-lock]
7576
version = "=9.0.0"

src/rust/src/contract.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{
1010
InvokeHostFunctionOutput, RustBuf, XDRFileHash,
1111
},
1212
};
13-
use log::debug;
14-
use std::{fmt::Display, io::Cursor, panic, time::Instant};
13+
use log::{debug, trace};
14+
use std::{fmt::Display, io::Cursor, panic, rc::Rc, time::Instant};
1515

1616
// This module (contract) is bound to _two separate locations_ in the module
1717
// tree: crate::lo::contract and crate::hi::contract, each of which has a (lo or
@@ -286,6 +286,29 @@ pub(crate) fn invoke_host_function(
286286
}
287287
}
288288

289+
fn make_trace_hook_fn<'a>() -> super::soroban_env_host::TraceHook {
290+
let prev_state = std::cell::RefCell::new(String::new());
291+
Rc::new(move |host, traceevent| {
292+
if traceevent.is_begin() || traceevent.is_end() {
293+
prev_state.replace(String::new());
294+
}
295+
match super::soroban_env_host::TraceRecord::new(host, traceevent) {
296+
Ok(tr) => {
297+
let state_str = format!("{}", tr.state);
298+
if prev_state.borrow().is_empty() {
299+
trace!(target: TX, "{}: {}", tr.event, state_str);
300+
} else {
301+
let diff = crate::log::diff_line(&prev_state.borrow(), &state_str);
302+
trace!(target: TX, "{}: {}", tr.event, diff);
303+
}
304+
prev_state.replace(state_str);
305+
}
306+
Err(e) => trace!(target: TX, "{}", e),
307+
}
308+
Ok(())
309+
})
310+
}
311+
289312
fn invoke_host_function_or_maybe_panic(
290313
enable_diagnostics: bool,
291314
instruction_limit: u32,
@@ -314,10 +337,16 @@ fn invoke_host_function_or_maybe_panic(
314337
)?;
315338
let mut diagnostic_events = vec![];
316339
let ledger_seq_num = ledger_info.sequence_number;
340+
let trace_hook: Option<super::soroban_env_host::TraceHook> =
341+
if crate::log::is_tx_tracing_enabled() {
342+
Some(make_trace_hook_fn())
343+
} else {
344+
None
345+
};
317346
let (res, time_nsecs) = {
318347
let _span1 = tracy_span!("e2e_invoke::invoke_function");
319348
let start_time = Instant::now();
320-
let res = e2e_invoke::invoke_host_function(
349+
let res = e2e_invoke::invoke_host_function_with_trace_hook(
321350
&budget,
322351
enable_diagnostics,
323352
hf_buf,
@@ -329,6 +358,7 @@ fn invoke_host_function_or_maybe_panic(
329358
ttl_entries.iter(),
330359
base_prng_seed,
331360
&mut diagnostic_events,
361+
trace_hook,
332362
);
333363
let stop_time = Instant::now();
334364
let time_nsecs = stop_time.duration_since(start_time).as_nanos() as u64;

src/rust/src/host-dep-tree-curr.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
soroban-env-host 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238b65bfd13666be4ac14e0e390c31b549caf#c1b238b65bfd13666be4ac14e0e390c31b549caf
1+
soroban-env-host 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=b6ef8e32d86b008b2ecfdb2d7ca958e751190523#b6ef8e32d86b008b2ecfdb2d7ca958e751190523
22
├── tracy-client 0.15.2 checksum:434ecabbda9f67eeea1eab44d52f4a20538afa3e2c2770f2efc161142b25b608
33
│ ├── tracy-client-sys 0.20.0 checksum:e8cf8aeb20e40d13be65a0b134f8d82d360e72b2793a11de8867d7fbc0f9d6f6
44
│ │ └── cc 1.0.79 checksum:50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f
@@ -97,7 +97,7 @@ soroban-env-host 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238
9797
│ ├── wasmi_arena 0.4.0 git+https://github.com/stellar/wasmi?rev=ab29800224d85ee64d4ac127bac84cdbb0276721#ab29800224d85ee64d4ac127bac84cdbb0276721
9898
│ ├── spin 0.9.8 checksum:6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67
9999
│ └── smallvec 1.10.0 checksum:a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0
100-
├── soroban-env-common 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238b65bfd13666be4ac14e0e390c31b549caf#c1b238b65bfd13666be4ac14e0e390c31b549caf
100+
├── soroban-env-common 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=b6ef8e32d86b008b2ecfdb2d7ca958e751190523#b6ef8e32d86b008b2ecfdb2d7ca958e751190523
101101
│ ├── tracy-client 0.15.2 checksum:434ecabbda9f67eeea1eab44d52f4a20538afa3e2c2770f2efc161142b25b608
102102
│ ├── stellar-xdr 20.0.2 checksum:e9f00a85bd9b1617d4cb7e741733889c9940e6bdeca360db81752b0ef04fe3a5
103103
│ │ ├── stellar-strkey 0.0.8 checksum:12d2bf45e114117ea91d820a846fd1afbe3ba7d717988fee094ce8227a3bf8bd
@@ -107,7 +107,7 @@ soroban-env-host 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238
107107
│ │ └── base64 0.13.1 checksum:9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8
108108
│ ├── static_assertions 1.1.0 checksum:a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f
109109
│ ├── soroban-wasmi 0.31.1-soroban.20.0.0 git+https://github.com/stellar/wasmi?rev=ab29800224d85ee64d4ac127bac84cdbb0276721#ab29800224d85ee64d4ac127bac84cdbb0276721
110-
│ ├── soroban-env-macros 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238b65bfd13666be4ac14e0e390c31b549caf#c1b238b65bfd13666be4ac14e0e390c31b549caf
110+
│ ├── soroban-env-macros 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=b6ef8e32d86b008b2ecfdb2d7ca958e751190523#b6ef8e32d86b008b2ecfdb2d7ca958e751190523
111111
│ │ ├── syn 2.0.39 checksum:23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a
112112
│ │ ├── stellar-xdr 20.0.2 checksum:e9f00a85bd9b1617d4cb7e741733889c9940e6bdeca360db81752b0ef04fe3a5
113113
│ │ ├── serde_json 1.0.108 checksum:3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b
@@ -123,7 +123,7 @@ soroban-env-host 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238
123123
│ │ └── proc-macro2 1.0.69 checksum:134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da
124124
│ ├── ethnum 1.5.0 checksum:b90ca2580b73ab6a1f724b76ca11ab632df820fd6040c336200d2c1df7b3c82c
125125
│ └── crate-git-revision 0.0.6 checksum:c521bf1f43d31ed2f73441775ed31935d77901cb3451e44b38a1c1612fcbaf98
126-
├── soroban-builtin-sdk-macros 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=c1b238b65bfd13666be4ac14e0e390c31b549caf#c1b238b65bfd13666be4ac14e0e390c31b549caf
126+
├── soroban-builtin-sdk-macros 20.1.0 git+https://github.com/stellar/rs-soroban-env?rev=b6ef8e32d86b008b2ecfdb2d7ca958e751190523#b6ef8e32d86b008b2ecfdb2d7ca958e751190523
127127
│ ├── syn 2.0.39 checksum:23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a
128128
│ ├── quote 1.0.33 checksum:5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae
129129
│ ├── proc-macro2 1.0.69 checksum:134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da

src/rust/src/log.rs

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
44

55
use cxx::let_cxx_string;
6+
use itertools::Itertools;
67
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
78
use std::sync::atomic::{AtomicBool, Ordering};
89

@@ -55,6 +56,19 @@ pub fn init_logging(maxLevel: LogLevel) -> Result<(), SetLoggerError> {
5556
Ok(())
5657
}
5758

59+
pub(crate) fn is_tx_tracing_enabled() -> bool {
60+
let_cxx_string!(partition = partition::TX);
61+
shim_isLogLevelAtLeast(&partition, LogLevel::LVL_TRACE)
62+
}
63+
64+
pub(crate) fn diff_line(last: &String, new: &String) -> String {
65+
last.split(',')
66+
.zip(new.split(','))
67+
.filter(|(a, b)| a != b)
68+
.map(|(_, b)| b)
69+
.join(",")
70+
}
71+
5872
fn convertLogLevel(lvl: Level) -> LogLevel {
5973
match lvl {
6074
Level::Error => LogLevel::LVL_ERROR,

0 commit comments

Comments
 (0)