Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add withLog parameter to debug_traceCall #2897

Merged
merged 18 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions client/evm-tracing/src/formatters/blockscout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ pub struct BlockscoutCall {
pub gas_used: U256,
#[serde(flatten)]
pub inner: BlockscoutCallInner,

pub logs: Vec<crate::types::single::Log>,
}
11 changes: 9 additions & 2 deletions client/evm-tracing/src/formatters/call_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use super::blockscout::BlockscoutCallInner;
use crate::types::{
single::{Call, TransactionTrace},
single::{Call, Log, TransactionTrace},
CallResult, CallType, CreateResult,
};

Expand Down Expand Up @@ -70,8 +70,12 @@ impl super::ResponseFormatter for Formatter {
},
to,
input,
res,
res: res.clone(),
value: Some(value),
logs: match res {
CallResult::Output { .. } => it.logs.clone(),
CallResult::Error { .. } => Vec::new(),
},
},
BlockscoutCallInner::Create { init, res } => CallTracerInner::Create {
input: init,
Expand Down Expand Up @@ -285,6 +289,9 @@ pub enum CallTracerInner {

#[serde(skip_serializing_if = "Option::is_none")]
value: Option<U256>,

#[serde(skip_serializing_if = "Vec::is_empty")]
logs: Vec<Log>,
},
Create {
#[serde(rename = "type", serialize_with = "opcode_serialize")]
Expand Down
94 changes: 93 additions & 1 deletion client/evm-tracing/src/listeners/call_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::formatters::blockscout::BlockscoutCall as Call;
use crate::formatters::blockscout::BlockscoutCallInner as CallInner;
use crate::types::{CallResult, CallType, ContextType, CreateResult};
use crate::types::{single::Log, CallResult, CallType, ContextType, CreateResult};
use ethereum_types::{H160, U256};
use evm_tracing_events::{
runtime::{Capture, ExitError, ExitReason, ExitSucceed},
Expand Down Expand Up @@ -79,6 +79,9 @@ pub struct Listener {
/// True if only the `GasometerEvent::RecordTransaction` event has been received.
/// Allow to correctly handle transactions that cannot pay for the tx data in Legacy mode.
record_transaction_event_only: bool,

/// If true the listener will collect EvmEvent::Log events.
pub with_log: bool,
}

struct Context {
Expand Down Expand Up @@ -116,6 +119,7 @@ impl Default for Listener {
skip_next_context: false,
call_list_first_transaction: true,
record_transaction_event_only: false,
with_log: false,
}
}
}
Expand Down Expand Up @@ -160,6 +164,7 @@ impl Listener {
input: context.data,
res,
},
logs: Vec::<Log>::new(),
}
}
ContextType::Create => {
Expand All @@ -178,6 +183,7 @@ impl Listener {
init: context.data,
res,
},
logs: Vec::<Log>::new(),
}
}
};
Expand Down Expand Up @@ -208,6 +214,7 @@ impl Listener {
input: vec![],
res,
},
logs: Vec::<Log>::new(),
};

self.insert_entry(self.entries_next_index, entry);
Expand Down Expand Up @@ -492,6 +499,7 @@ impl Listener {
to: target,
balance,
},
logs: Vec::<Log>::new(),
},
);
self.entries_next_index += 1;
Expand Down Expand Up @@ -519,6 +527,21 @@ impl Listener {
// behavior (like batch precompile does) thus we simply consider this a call.
self.call_type = Some(CallType::Call);
}
EvmEvent::Log {
address,
topics,
data,
} => {
let log = Log {
address,
topics,
data,
};
let key = self.entries_next_index;
if self.with_log {
self.insert_log_entry(key, log);
}
}

// We ignore other kinds of message if any (new ones may be added in the future).
#[allow(unreachable_patterns)]
Expand All @@ -536,6 +559,16 @@ impl Listener {
}
}

fn insert_log_entry(&mut self, key: u32, log_entry: Log) {
if let Some(ref mut last) = self.entries.last_mut() {
if let Some(ref mut last_call) = last.get(&key) {
let mut call = (*last_call).clone();
call.logs.push(log_entry);
last.insert(key, call);
}
}
}

fn pop_context_to_entry(
&mut self,
reason: ExitReason,
Expand Down Expand Up @@ -577,6 +610,7 @@ impl Listener {
input: context.data,
res,
},
logs: Vec::<Log>::new(),
}
}
ContextType::Create => {
Expand Down Expand Up @@ -605,6 +639,7 @@ impl Listener {
init: context.data,
res,
},
logs: Vec::<Log>::new(),
}
}
},
Expand Down Expand Up @@ -681,6 +716,7 @@ mod tests {
TransactCall,
TransactCreate,
TransactCreate2,
Log,
}

enum TestRuntimeEvent {
Expand Down Expand Up @@ -782,6 +818,11 @@ mod tests {
gas_limit: 0u64,
address: H160::default(),
},
TestEvmEvent::Log => EvmEvent::Log {
address: H160::default(),
topics: Vec::new(),
data: Vec::new(),
},
}
}

Expand Down Expand Up @@ -876,6 +917,10 @@ mod tests {
listener.evm_event(test_emit_evm_event(TestEvmEvent::Suicide, false, None));
}

fn do_evm_log_event(listener: &mut Listener) {
listener.evm_event(test_emit_evm_event(TestEvmEvent::Log, false, None));
}

fn do_runtime_step_event(listener: &mut Listener) {
listener.runtime_event(test_emit_runtime_event(TestRuntimeEvent::Step));
}
Expand Down Expand Up @@ -1127,4 +1172,51 @@ mod tests {
// There are 5 main nested calls for a total of 56 elements in the callstack: 1 main + 55 nested.
assert_eq!(listener.entries[0].len(), (depth * (subdepth + 1)) + 1);
}

#[test]
fn call_log_event() {
let mut listener = Listener::default();
listener.with_log = true;
do_transact_create_event(&mut listener);
do_gasometer_event(&mut listener);
do_evm_create_event(&mut listener);
do_evm_call_event(&mut listener);
do_evm_log_event(&mut listener);
do_exit_event(&mut listener);
listener.finish_transaction();
assert_eq!(listener.entries.len(), 1);
assert_eq!(listener.entries[0].len(), 2);
assert_eq!(listener.entries[0].get(&1).unwrap().logs.len(), 1);
}

#[test]
fn call_multiple_logs_event() {
let mut listener = Listener::default();
listener.with_log = true;
do_transact_create_event(&mut listener);
do_gasometer_event(&mut listener);
do_evm_create_event(&mut listener);
do_evm_call_event(&mut listener);
do_evm_log_event(&mut listener);
do_evm_log_event(&mut listener);
do_exit_event(&mut listener);
listener.finish_transaction();
assert_eq!(listener.entries.len(), 1);
assert_eq!(listener.entries[0].len(), 2);
assert_eq!(listener.entries[0].get(&1).unwrap().logs.len(), 2);
}

#[test]
fn call_log_event_not_recorder_when_with_log_is_false() {
let mut listener = Listener::default();
listener.with_log = false;
do_transact_create_event(&mut listener);
do_gasometer_event(&mut listener);
do_evm_create_event(&mut listener);
do_evm_call_event(&mut listener);
do_evm_log_event(&mut listener);
do_exit_event(&mut listener);
assert_eq!(listener.entries.len(), 1);
assert_eq!(listener.entries[0].get(&1).unwrap().logs.len(), 0);
}
}
26 changes: 24 additions & 2 deletions client/evm-tracing/src/types/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
//! the whole block tracing output.

use super::serialization::*;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use ethereum_types::{H256, U256};
use ethereum_types::{H160, H256, U256};
use parity_scale_codec::{Decode, Encode};
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};

Expand Down Expand Up @@ -100,3 +100,25 @@ pub struct RawStepLog {
#[serde(skip_serializing_if = "Option::is_none")]
pub storage: Option<BTreeMap<H256, H256>>,
}

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceCallConfig {
pub with_log: bool,
}

impl Default for TraceCallConfig {
fn default() -> Self {
Self { with_log: true }
}
}

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, Serialize)]
pub struct Log {
/// Event address.
pub address: H160,
/// Event topics
pub topics: Vec<H256>,
/// Event data
pub data: Vec<u8>,
}
1 change: 1 addition & 0 deletions client/rpc-core/debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct TraceParams {
pub disable_stack: Option<bool>,
/// Javascript tracer (we just check if it's Blockscout tracer string)
pub tracer: Option<String>,
pub tracer_config: Option<single::TraceCallConfig>,
librelois marked this conversation as resolved.
Show resolved Hide resolved
pub timeout: Option<String>,
}

Expand Down
Loading
Loading