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

improve tracing #2731

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ test-eth: githooks test-evm

.PHONY: test-evm
test-evm: githooks
SKIP_WASM_BUILD= ${cargo_test} -p module-evm --features tracing
SKIP_WASM_BUILD= ${cargo_test} -p module-evm -p module-evm-bridge --features tracing
SKIP_WASM_BUILD= ${cargo_test} --release -p evm-jsontests --features evm-tests

.PHONY: test-runtimes
Expand Down
1 change: 1 addition & 0 deletions modules/evm-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ try-runtime = [
"frame-system/try-runtime",
"module-evm/try-runtime",
]
tracing = ["module-evm/tracing"]
78 changes: 78 additions & 0 deletions modules/evm-bridge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,81 @@ fn liquidation_err_fails_as_expected() {
);
});
}

#[cfg(feature = "tracing")]
#[test]
fn tracing_should_work() {
use module_evm::runner::tracing;
use primitives::evm::tracing::TracerConfig;

ExtBuilder::default()
.balances(vec![(alice(), 1_000_000_000_000), (bob(), 1_000_000_000_000)])
.build()
.execute_with(|| {
deploy_contracts();
let mut tracer = tracing::Tracer::new(TracerConfig::CallTracer);
tracing::using(&mut tracer, || {
assert_err!(
EVMBridge::<Runtime>::transfer(
InvokeContext {
contract: erc20_address(),
sender: bob_evm_addr(),
origin: bob_evm_addr(),
},
alice_evm_addr(),
10
),
Error::<Runtime>::ExecutionRevert
);
});
let expected = r#"[
{
"type": "CALL",
"from": "0x1000000000000000000000000000000000000002",
"to": "0x5dddfce53ee040d9eb21afbc0ae1bb4dbb0ba643",
"input": "0xa9059cbb0000000000000000000000001000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a",
"value": "0x0",
"gas": 200000,
"gasUsed": 200000,
"output": null,
"error": null,
"revertReason": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002645524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000",
"depth": 0,
"calls": []
}
]"#;
let expected = serde_json::from_str::<Vec<tracing::CallTrace>>(expected).unwrap();
assert_eq!(tracer.finalize(), tracing::TraceOutcome::Calls(expected));

tracing::using(&mut tracer, || {
assert_ok!(EVMBridge::<Runtime>::transfer(
InvokeContext {
contract: erc20_address(),
sender: alice_evm_addr(),
origin: alice_evm_addr(),
},
bob_evm_addr(),
100
));
});

let expected = r#"[
{
"type": "CALL",
"from": "0x1000000000000000000000000000000000000001",
"to": "0x5dddfce53ee040d9eb21afbc0ae1bb4dbb0ba643",
"input": "0xa9059cbb00000000000000000000000010000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000064",
"value": "0x0",
"gas": 200000,
"gasUsed": 51929,
"output": "0x0000000000000000000000000000000000000000000000000000000000000001",
"error": null,
"revertReason": null,
"depth": 0,
"calls": []
}
]"#;
let expected = serde_json::from_str::<Vec<tracing::CallTrace>>(expected).unwrap();
assert_eq!(tracer.finalize(), tracing::TraceOutcome::Calls(expected));
});
}
27 changes: 5 additions & 22 deletions modules/evm/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,10 @@ sp_api::decl_runtime_apis! {

#[cfg(feature = "tracing")]
sp_api::decl_runtime_apis! {
pub trait EVMTraceApi<Balance> where
Balance: Codec + MaybeDisplay + MaybeFromStr,
{
fn trace_call(
from: H160,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<primitives::evm::tracing::CallTrace>, sp_runtime::DispatchError>;

fn trace_vm(
from: H160,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<primitives::evm::tracing::VMTrace, sp_runtime::DispatchError>;
pub trait EVMTraceApi {
fn trace_extrinsic(
extrinsic: Block::Extrinsic,
tracer_config: primitives::evm::tracing::TracerConfig,
) -> Result<primitives::evm::tracing::TraceOutcome, sp_runtime::transaction_validity::TransactionValidityError>;
}
}
42 changes: 20 additions & 22 deletions modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> StackExecu
}

if let Err(e) = self.record_external_operation(crate::ExternalOperation::AccountBasicRead) {
return (e.into(), Vec::new());
return emit_exit!(e.into(), Vec::new());
}

let context = Context {
Expand Down Expand Up @@ -873,15 +873,6 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> StackExecu
self.state.metadata_mut().access_address(caller);
self.state.metadata_mut().access_address(address);

event!(Create {
caller,
address,
scheme,
value,
init_code: &init_code,
target_gas
});

ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
if let Some(depth) = self.state.metadata().depth {
if depth >= self.config.call_stack_limit {
return Capture::Exit((ExitError::CallTooDeep.into(), None, Vec::new()));
Expand Down Expand Up @@ -1011,15 +1002,6 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> StackExecu
// Set target address
*self.state.metadata_mut().target_mut() = Some(code_address);

event!(Call {
code_address,
transfer: &transfer,
input: &input,
target_gas,
is_static,
context: &context,
});

let after_gas = if take_l64 && self.config.call_l64_after_gas {
if self.config.estimate {
let initial_after_gas = self.state.metadata().gasometer.gas();
Expand Down Expand Up @@ -1386,7 +1368,6 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
) -> Capture<(ExitReason, Option<H160>, Vec<u8>), Self::CreateInterrupt> {
if let Err(e) = self.maybe_record_init_code_cost(&init_code) {
let reason: ExitReason = e.into();
emit_exit!(reason.clone());
return Capture::Exit((reason, None, Vec::new()));
}

Expand All @@ -1402,6 +1383,14 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
init_code: Vec<u8>,
target_gas: Option<u64>,
) -> Capture<(ExitReason, Option<H160>, Vec<u8>), Self::CreateInterrupt> {
event!(Create {
caller,
address: self.create_address(scheme).unwrap_or_default(),
value,
init_code: &init_code,
target_gas
});

if let Err(e) = self.maybe_record_init_code_cost(&init_code) {
let reason: ExitReason = e.into();
emit_exit!(reason.clone());
Expand Down Expand Up @@ -1449,6 +1438,15 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
is_static: bool,
context: Context,
) -> Capture<(ExitReason, Vec<u8>), Self::CallInterrupt> {
event!(Call {
code_address,
transfer: &transfer,
input: &input,
target_gas,
is_static,
context: &context,
});

let capture = self.call_inner(
code_address,
transfer,
Expand Down Expand Up @@ -1558,7 +1556,7 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
}

event!(PrecompileSubcall {
code_address: code_address.clone(),
code_address,
transfer: &transfer,
input: &input,
target_gas: gas_limit,
Expand All @@ -1576,7 +1574,7 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
is_static,
context.clone(),
) {
Capture::Exit((s, v)) => (s, v),
Capture::Exit((s, v)) => emit_exit!(s, v),
Capture::Trap(rt) => {
// Ideally this would pass the interrupt back to the executor so it could be
// handled like any other call, however the type signature of this function does
Expand Down
Loading
Loading