Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Transaction tracing for eth_call #1210

Merged
merged 7 commits into from Jun 7, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethcore/src/client/client.rs
Expand Up @@ -461,7 +461,7 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
// give the sender a sufficient balance
state.add_balance(&sender, &(needed_balance - balance));
}
let options = TransactOptions { tracing: false, vm_tracing: analytics.vm_tracing, check_nonce: false };
let options = TransactOptions { tracing: analytics.transaction_tracing, vm_tracing: analytics.vm_tracing, check_nonce: false };
let mut ret = Executive::new(&mut state, &env_info, self.engine.deref().deref(), &self.vm_factory).transact(t, options);

// TODO gav move this into Executive.
Expand Down
2 changes: 2 additions & 0 deletions ethcore/src/client/mod.rs
Expand Up @@ -52,6 +52,8 @@ use error::Error as EthError;
/// Options concerning what analytics we run on the call.
#[derive(Eq, PartialEq, Default, Clone, Copy, Debug)]
pub struct CallAnalytics {
/// Make a transaction trace.
pub transaction_tracing: bool,
/// Make a VM trace.
pub vm_tracing: bool,
/// Make a diff.
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/miner/miner.rs
Expand Up @@ -280,7 +280,7 @@ impl MinerService for Miner {
// give the sender a sufficient balance
state.add_balance(&sender, &(needed_balance - balance));
}
let options = TransactOptions { tracing: false, vm_tracing: analytics.vm_tracing, check_nonce: false };
let options = TransactOptions { tracing: analytics.transaction_tracing, vm_tracing: analytics.vm_tracing, check_nonce: false };
let mut ret = Executive::new(&mut state, &env_info, self.engine(), chain.vm_factory()).transact(t, options);

// TODO gav move this into Executive.
Expand Down
46 changes: 23 additions & 23 deletions rpc/src/v1/impls/traces.rs
Expand Up @@ -28,7 +28,7 @@ use ethcore::state_diff::StateDiff;
use ethcore::account_diff::{Diff, Existance};
use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action};
use v1::traits::Traces;
use v1::types::{TraceFilter, Trace, BlockNumber, Index, CallRequest};
use v1::types::{TraceFilter, LocalizedTrace, Trace, BlockNumber, Index, CallRequest, Bytes};

/// Traces api implementation.
pub struct TracesClient<C, M> where C: BlockChainClient, M: MinerService {
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<C, M> Traces for TracesClient<C, M> where C: BlockChainClient + 'static, M:
.and_then(|(filter, )| {
let client = take_weak!(self.client);
let traces = client.filter_traces(filter.into());
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(Trace::from).collect());
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(LocalizedTrace::from).collect());
to_value(&traces)
})
}
Expand All @@ -166,7 +166,7 @@ impl<C, M> Traces for TracesClient<C, M> where C: BlockChainClient + 'static, M:
.and_then(|(block_number,)| {
let client = take_weak!(self.client);
let traces = client.block_traces(block_number.into());
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(Trace::from).collect());
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(LocalizedTrace::from).collect());
to_value(&traces)
})
}
Expand All @@ -176,7 +176,7 @@ impl<C, M> Traces for TracesClient<C, M> where C: BlockChainClient + 'static, M:
.and_then(|(transaction_hash,)| {
let client = take_weak!(self.client);
let traces = client.transaction_traces(TransactionID::Hash(transaction_hash));
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(Trace::from).collect());
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(LocalizedTrace::from).collect());
to_value(&traces)
})
}
Expand All @@ -190,36 +190,36 @@ impl<C, M> Traces for TracesClient<C, M> where C: BlockChainClient + 'static, M:
address: address.into_iter().map(|i| i.value()).collect()
};
let trace = client.trace(id);
let trace = trace.map(Trace::from);
let trace = trace.map(LocalizedTrace::from);
to_value(&trace)
})
}

fn vm_trace_call(&self, params: Params) -> Result<Value, Error> {
trace!(target: "jsonrpc", "vm_trace_call: {:?}", params);
fn call(&self, params: Params) -> Result<Value, Error> {
trace!(target: "jsonrpc", "call: {:?}", params);
from_params(params)
.and_then(|(request,)| {
.and_then(|(request, flags)| {
let flags: Vec<String> = flags;
let analytics = CallAnalytics {
transaction_tracing: flags.contains(&("trace".to_owned())),
vm_tracing: flags.contains(&("vmTrace".to_owned())),
state_diffing: flags.contains(&("stateDiff".to_owned())),
};
let signed = try!(self.sign_call(request));
let r = take_weak!(self.client).call(&signed, CallAnalytics{ vm_tracing: true, state_diffing: false });
let r = take_weak!(self.client).call(&signed, analytics);
if let Ok(executed) = r {
// TODO maybe add other stuff to this?
let mut ret = map!["output".to_owned() => to_value(&Bytes(executed.output)).unwrap()];
if let Some(trace) = executed.trace {
ret.insert("trace".to_owned(), to_value(&Trace::from(trace)).unwrap());
}
if let Some(vm_trace) = executed.vm_trace {
return Ok(vm_trace_to_object(&vm_trace));
ret.insert("vmTrace".to_owned(), vm_trace_to_object(&vm_trace));
}
}
Ok(Value::Null)
})
}

fn state_diff_call(&self, params: Params) -> Result<Value, Error> {
trace!(target: "jsonrpc", "state_diff_call: {:?}", params);
from_params(params)
.and_then(|(request,)| {
let signed = try!(self.sign_call(request));
let r = take_weak!(self.client).call(&signed, CallAnalytics{ vm_tracing: false, state_diffing: true });
if let Ok(executed) = r {
if let Some(state_diff) = executed.state_diff {
return Ok(state_diff_to_object(&state_diff));
ret.insert("stateDiff".to_owned(), state_diff_to_object(&state_diff));
}
return Ok(Value::Object(ret))
}
Ok(Value::Null)
})
Expand Down
11 changes: 3 additions & 8 deletions rpc/src/v1/traits/traces.rs
Expand Up @@ -32,11 +32,8 @@ pub trait Traces: Sized + Send + Sync + 'static {
/// Returns all traces produced at given block.
fn block_traces(&self, _: Params) -> Result<Value, Error>;

/// Executes the given call and returns the VM trace for it.
fn vm_trace_call(&self, _: Params) -> Result<Value, Error>;

/// Executes the given call and returns the diff for it.
fn state_diff_call(&self, params: Params) -> Result<Value, Error>;
/// Executes the given call and returns a number of possible traces for it.
fn call(&self, _: Params) -> Result<Value, Error>;

/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
Expand All @@ -45,9 +42,7 @@ pub trait Traces: Sized + Send + Sync + 'static {
delegate.add_method("trace_get", Traces::trace);
delegate.add_method("trace_transaction", Traces::transaction_traces);
delegate.add_method("trace_block", Traces::block_traces);

delegate.add_method("trace_vmTraceCall", Traces::vm_trace_call);
delegate.add_method("trace_stateDiffCall", Traces::state_diff_call);
delegate.add_method("trace_call", Traces::call);

delegate
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/v1/types/mod.rs.in
Expand Up @@ -41,5 +41,5 @@ pub use self::transaction::Transaction;
pub use self::transaction_request::{TransactionRequest, TransactionConfirmation, TransactionModification};
pub use self::call_request::CallRequest;
pub use self::receipt::Receipt;
pub use self::trace::Trace;
pub use self::trace::{Trace, LocalizedTrace};
pub use self::trace_filter::TraceFilter;
36 changes: 30 additions & 6 deletions rpc/src/v1/types/trace.rs
Expand Up @@ -16,7 +16,7 @@

use util::{Address, U256, H256};
use ethcore::trace::trace;
use ethcore::trace::LocalizedTrace;
use ethcore::trace::{Trace as EthTrace, LocalizedTrace as EthLocalizedTrace};
use v1::types::Bytes;

/// Create response
Expand Down Expand Up @@ -161,7 +161,7 @@ impl From<trace::Res> for Res {

/// Trace
#[derive(Debug, Serialize)]
pub struct Trace {
pub struct LocalizedTrace {
/// Action
action: Action,
/// Result
Expand All @@ -185,9 +185,9 @@ pub struct Trace {
block_hash: H256,
}

impl From<LocalizedTrace> for Trace {
fn from(t: LocalizedTrace) -> Self {
Trace {
impl From<EthLocalizedTrace> for LocalizedTrace {
fn from(t: EthLocalizedTrace) -> Self {
LocalizedTrace {
action: From::from(t.action),
result: From::from(t.result),
trace_address: t.trace_address.into_iter().map(From::from).collect(),
Expand All @@ -200,6 +200,30 @@ impl From<LocalizedTrace> for Trace {
}
}

/// Trace
#[derive(Debug, Serialize)]
pub struct Trace {
/// Depth within the call trace tree.
depth: usize,
/// Action
action: Action,
/// Result
result: Res,
/// Subtraces
subtraces: Vec<Trace>,
}

impl From<EthTrace> for Trace {
fn from(t: EthTrace) -> Self {
Trace {
depth: t.depth.into(),
action: t.action.into(),
result: t.result.into(),
subtraces: t.subs.into_iter().map(From::from).collect(),
}
}
}

#[cfg(test)]
mod tests {
use serde_json;
Expand All @@ -209,7 +233,7 @@ mod tests {

#[test]
fn test_trace_serialize() {
let t = Trace {
let t = LocalizedTrace {
action: Action::Call(Call {
from: Address::from(4),
to: Address::from(5),
Expand Down