2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-30 17:41:24 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Simple executive tracer.
|
|
|
|
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
2017-09-06 20:47:45 +02:00
|
|
|
use util::Address;
|
|
|
|
use bytes::Bytes;
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::ActionParams;
|
2017-07-18 12:14:06 +02:00
|
|
|
use trace::trace::{Call, Create, Action, Res, CreateResult, CallResult, VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, Suicide, Reward, RewardType};
|
2016-09-05 11:56:44 +02:00
|
|
|
use trace::{Tracer, VMTracer, FlatTrace, TraceError};
|
2016-04-30 17:41:24 +02:00
|
|
|
|
|
|
|
/// Simple executive tracer. Traces all calls and creates. Ignores delegatecalls.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ExecutiveTracer {
|
2016-07-28 20:31:29 +02:00
|
|
|
traces: Vec<FlatTrace>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn top_level_subtraces(traces: &[FlatTrace]) -> usize {
|
|
|
|
traces.iter().filter(|t| t.trace_address.is_empty()).count()
|
|
|
|
}
|
|
|
|
|
2016-09-28 17:24:26 +02:00
|
|
|
fn prefix_subtrace_addresses(mut traces: Vec<FlatTrace>) -> Vec<FlatTrace> {
|
2016-07-28 20:31:29 +02:00
|
|
|
// input traces are expected to be ordered like
|
|
|
|
// []
|
|
|
|
// [0]
|
|
|
|
// [0, 0]
|
|
|
|
// [0, 1]
|
|
|
|
// []
|
|
|
|
// [0]
|
|
|
|
//
|
|
|
|
// so they can be transformed to
|
|
|
|
//
|
|
|
|
// [0]
|
|
|
|
// [0, 0]
|
|
|
|
// [0, 0, 0]
|
|
|
|
// [0, 0, 1]
|
|
|
|
// [1]
|
|
|
|
// [1, 0]
|
2016-09-28 17:24:26 +02:00
|
|
|
let mut current_subtrace_index = 0;
|
|
|
|
let mut first = true;
|
2016-10-27 08:28:12 +02:00
|
|
|
for trace in &mut traces {
|
2016-09-28 17:24:26 +02:00
|
|
|
match (first, trace.trace_address.is_empty()) {
|
|
|
|
(true, _) => first = false,
|
|
|
|
(_, true) => current_subtrace_index += 1,
|
|
|
|
_ => {}
|
2016-10-27 08:28:12 +02:00
|
|
|
}
|
2016-09-28 17:24:26 +02:00
|
|
|
trace.trace_address.push_front(current_subtrace_index);
|
|
|
|
}
|
|
|
|
traces
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 17:24:26 +02:00
|
|
|
#[test]
|
|
|
|
fn should_prefix_address_properly() {
|
|
|
|
use super::trace::{Action, Res, Suicide};
|
|
|
|
|
|
|
|
let f = |v: Vec<usize>| FlatTrace {
|
|
|
|
action: Action::Suicide(Suicide {
|
|
|
|
address: Default::default(),
|
|
|
|
balance: Default::default(),
|
|
|
|
refund_address: Default::default(),
|
|
|
|
}),
|
|
|
|
result: Res::None,
|
|
|
|
subtraces: 0,
|
|
|
|
trace_address: v.into_iter().collect(),
|
|
|
|
};
|
|
|
|
let t = vec![vec![], vec![0], vec![0, 0], vec![0], vec![], vec![], vec![0], vec![]].into_iter().map(&f).collect();
|
|
|
|
let t = prefix_subtrace_addresses(t);
|
|
|
|
assert_eq!(t, vec![vec![0], vec![0, 0], vec![0, 0, 0], vec![0, 0], vec![1], vec![2], vec![2, 0], vec![3]].into_iter().map(&f).collect::<Vec<_>>());
|
2016-10-27 08:28:12 +02:00
|
|
|
}
|
2016-09-28 17:24:26 +02:00
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
impl Tracer for ExecutiveTracer {
|
|
|
|
fn prepare_trace_call(&self, params: &ActionParams) -> Option<Call> {
|
|
|
|
Some(Call::from(params.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_trace_create(&self, params: &ActionParams) -> Option<Create> {
|
|
|
|
Some(Create::from(params.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_trace_output(&self) -> Option<Bytes> {
|
|
|
|
Some(vec![])
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:31:29 +02:00
|
|
|
fn trace_call(&mut self, call: Option<Call>, gas_used: U256, output: Option<Bytes>, subs: Vec<FlatTrace>) {
|
|
|
|
let trace = FlatTrace {
|
|
|
|
trace_address: Default::default(),
|
|
|
|
subtraces: top_level_subtraces(&subs),
|
2016-04-30 17:41:24 +02:00
|
|
|
action: Action::Call(call.expect("self.prepare_trace_call().is_some(): so we must be tracing: qed")),
|
|
|
|
result: Res::Call(CallResult {
|
|
|
|
gas_used: gas_used,
|
|
|
|
output: output.expect("self.prepare_trace_output().is_some(): so we must be tracing: qed")
|
2016-07-28 20:31:29 +02:00
|
|
|
}),
|
2016-04-30 17:41:24 +02:00
|
|
|
};
|
2016-07-27 21:34:32 +02:00
|
|
|
debug!(target: "trace", "Traced call {:?}", trace);
|
2016-04-30 17:41:24 +02:00
|
|
|
self.traces.push(trace);
|
2016-09-28 17:24:26 +02:00
|
|
|
self.traces.extend(prefix_subtrace_addresses(subs));
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:31:29 +02:00
|
|
|
fn trace_create(&mut self, create: Option<Create>, gas_used: U256, code: Option<Bytes>, address: Address, subs: Vec<FlatTrace>) {
|
|
|
|
let trace = FlatTrace {
|
|
|
|
subtraces: top_level_subtraces(&subs),
|
2016-04-30 17:41:24 +02:00
|
|
|
action: Action::Create(create.expect("self.prepare_trace_create().is_some(): so we must be tracing: qed")),
|
|
|
|
result: Res::Create(CreateResult {
|
|
|
|
gas_used: gas_used,
|
|
|
|
code: code.expect("self.prepare_trace_output.is_some(): so we must be tracing: qed"),
|
|
|
|
address: address
|
2016-07-28 20:31:29 +02:00
|
|
|
}),
|
|
|
|
trace_address: Default::default(),
|
2016-04-30 17:41:24 +02:00
|
|
|
};
|
2016-07-27 21:34:32 +02:00
|
|
|
debug!(target: "trace", "Traced create {:?}", trace);
|
2016-04-30 17:41:24 +02:00
|
|
|
self.traces.push(trace);
|
2016-09-28 17:24:26 +02:00
|
|
|
self.traces.extend(prefix_subtrace_addresses(subs));
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
fn trace_failed_call(&mut self, call: Option<Call>, subs: Vec<FlatTrace>, error: TraceError) {
|
2016-07-28 20:31:29 +02:00
|
|
|
let trace = FlatTrace {
|
|
|
|
trace_address: Default::default(),
|
|
|
|
subtraces: top_level_subtraces(&subs),
|
2016-04-30 17:41:24 +02:00
|
|
|
action: Action::Call(call.expect("self.prepare_trace_call().is_some(): so we must be tracing: qed")),
|
2016-09-05 11:56:44 +02:00
|
|
|
result: Res::FailedCall(error),
|
2016-04-30 17:41:24 +02:00
|
|
|
};
|
2016-07-27 21:34:32 +02:00
|
|
|
debug!(target: "trace", "Traced failed call {:?}", trace);
|
2016-04-30 17:41:24 +02:00
|
|
|
self.traces.push(trace);
|
2016-09-28 17:24:26 +02:00
|
|
|
self.traces.extend(prefix_subtrace_addresses(subs));
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
fn trace_failed_create(&mut self, create: Option<Create>, subs: Vec<FlatTrace>, error: TraceError) {
|
2016-07-28 20:31:29 +02:00
|
|
|
let trace = FlatTrace {
|
|
|
|
subtraces: top_level_subtraces(&subs),
|
2016-04-30 17:41:24 +02:00
|
|
|
action: Action::Create(create.expect("self.prepare_trace_create().is_some(): so we must be tracing: qed")),
|
2016-09-05 11:56:44 +02:00
|
|
|
result: Res::FailedCreate(error),
|
2016-07-28 20:31:29 +02:00
|
|
|
trace_address: Default::default(),
|
2016-04-30 17:41:24 +02:00
|
|
|
};
|
2016-07-27 21:34:32 +02:00
|
|
|
debug!(target: "trace", "Traced failed create {:?}", trace);
|
2016-04-30 17:41:24 +02:00
|
|
|
self.traces.push(trace);
|
2016-09-28 17:24:26 +02:00
|
|
|
self.traces.extend(prefix_subtrace_addresses(subs));
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:31:29 +02:00
|
|
|
fn trace_suicide(&mut self, address: Address, balance: U256, refund_address: Address) {
|
|
|
|
let trace = FlatTrace {
|
|
|
|
subtraces: 0,
|
2017-08-03 15:55:58 +02:00
|
|
|
action: Action::Suicide(Suicide { address, refund_address, balance } ),
|
2016-07-22 14:47:23 +02:00
|
|
|
result: Res::None,
|
2016-07-28 20:31:29 +02:00
|
|
|
trace_address: Default::default(),
|
2016-07-22 14:47:23 +02:00
|
|
|
};
|
2017-08-02 17:10:06 +02:00
|
|
|
debug!(target: "trace", "Traced suicide {:?}", trace);
|
2016-07-22 14:47:23 +02:00
|
|
|
self.traces.push(trace);
|
|
|
|
}
|
2017-09-04 16:36:49 +02:00
|
|
|
|
2017-07-31 12:06:38 +02:00
|
|
|
fn trace_reward(&mut self, author: Address, value: U256, reward_type: RewardType) {
|
2017-07-18 12:14:06 +02:00
|
|
|
let trace = FlatTrace {
|
|
|
|
subtraces: 0,
|
2017-08-03 15:55:58 +02:00
|
|
|
action: Action::Reward(Reward { author, value, reward_type } ),
|
2017-07-18 12:14:06 +02:00
|
|
|
result: Res::None,
|
|
|
|
trace_address: Default::default(),
|
|
|
|
};
|
2017-08-02 17:10:06 +02:00
|
|
|
debug!(target: "trace", "Traced reward {:?}", trace);
|
2017-07-18 12:14:06 +02:00
|
|
|
self.traces.push(trace);
|
|
|
|
}
|
2016-07-22 14:47:23 +02:00
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
fn subtracer(&self) -> Self {
|
|
|
|
ExecutiveTracer::default()
|
|
|
|
}
|
|
|
|
|
2017-08-28 14:25:16 +02:00
|
|
|
fn drain(self) -> Vec<FlatTrace> {
|
2016-04-30 17:41:24 +02:00
|
|
|
self.traces
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 12:40:31 +02:00
|
|
|
|
|
|
|
/// Simple VM tracer. Traces all operations.
|
|
|
|
pub struct ExecutiveVMTracer {
|
|
|
|
data: VMTrace,
|
|
|
|
}
|
|
|
|
|
2016-08-03 20:07:30 +02:00
|
|
|
impl ExecutiveVMTracer {
|
|
|
|
/// Create a new top-level instance.
|
|
|
|
pub fn toplevel() -> Self {
|
|
|
|
ExecutiveVMTracer {
|
|
|
|
data: VMTrace {
|
|
|
|
parent_step: 0,
|
|
|
|
code: vec![],
|
|
|
|
operations: vec![Default::default()], // prefill with a single entry so that prepare_subtrace can get the parent_step
|
|
|
|
subs: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 12:40:31 +02:00
|
|
|
impl VMTracer for ExecutiveVMTracer {
|
2017-07-10 13:23:40 +02:00
|
|
|
fn trace_next_instruction(&mut self, _pc: usize, _instruction: u8) -> bool { true }
|
|
|
|
|
|
|
|
fn trace_prepare_execute(&mut self, pc: usize, instruction: u8, gas_cost: U256) {
|
2016-06-02 12:40:31 +02:00
|
|
|
self.data.operations.push(VMOperation {
|
|
|
|
pc: pc,
|
|
|
|
instruction: instruction,
|
2017-07-10 13:23:40 +02:00
|
|
|
gas_cost: gas_cost,
|
2016-06-02 12:40:31 +02:00
|
|
|
executed: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem_diff: Option<(usize, &[u8])>, store_diff: Option<(U256, U256)>) {
|
|
|
|
let ex = VMExecutedOperation {
|
|
|
|
gas_used: gas_used,
|
|
|
|
stack_push: stack_push.iter().cloned().collect(),
|
|
|
|
mem_diff: mem_diff.map(|(s, r)| MemoryDiff{ offset: s, data: r.iter().cloned().collect() }),
|
|
|
|
store_diff: store_diff.map(|(l, v)| StorageDiff{ location: l, value: v }),
|
|
|
|
};
|
|
|
|
self.data.operations.last_mut().expect("trace_executed is always called after a trace_prepare_execute").executed = Some(ex);
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:36:30 +02:00
|
|
|
fn prepare_subtrace(&self, code: &[u8]) -> Self {
|
2016-06-02 12:40:31 +02:00
|
|
|
ExecutiveVMTracer { data: VMTrace {
|
2016-08-03 20:07:30 +02:00
|
|
|
parent_step: self.data.operations.len() - 1, // won't overflow since we must already have pushed an operation in trace_prepare_execute.
|
2016-06-03 11:36:30 +02:00
|
|
|
code: code.to_vec(),
|
2016-06-02 12:40:31 +02:00
|
|
|
operations: vec![],
|
|
|
|
subs: vec![],
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:23:40 +02:00
|
|
|
fn done_subtrace(&mut self, sub: Self) {
|
2016-06-02 12:40:31 +02:00
|
|
|
self.data.subs.push(sub.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drain(mut self) -> Option<VMTrace> { self.data.subs.pop() }
|
|
|
|
}
|