Integrated VM tracing into JSONRPC.

This commit is contained in:
Gav Wood 2016-05-29 00:58:52 +02:00
parent 86fdcabd0e
commit cd16828fef
6 changed files with 44 additions and 8 deletions

View File

@ -124,6 +124,7 @@ pub struct InstructionInfo {
pub side_effects: bool,
pub tier: GasPriceTier
}
impl InstructionInfo {
pub fn new(name: &'static str, additional: usize, args: usize, ret: usize, side_effects: bool, tier: GasPriceTier) -> InstructionInfo {
InstructionInfo {
@ -139,7 +140,7 @@ impl InstructionInfo {
#[cfg_attr(rustfmt, rustfmt_skip)]
/// Return details about specific instruction
pub fn get_info (instruction: Instruction) -> InstructionInfo {
pub fn get_info(instruction: Instruction) -> InstructionInfo {
match instruction {
STOP => InstructionInfo::new("STOP", 0, 0, 0, true, GasPriceTier::Zero),
ADD => InstructionInfo::new("ADD", 0, 2, 1, false, GasPriceTier::VeryLow),

View File

@ -33,3 +33,4 @@ pub use self::evm::{Evm, Error, Result};
pub use self::ext::{Ext, ContractCreateResult, MessageCallResult};
pub use self::factory::{Factory, VMType};
pub use self::schedule::Schedule;
pub use self::instructions::get_info;

View File

@ -196,7 +196,7 @@ impl<'a> Executive<'a> {
};
// finalize here!
Ok(try!(self.finalize(t, substate, gas_left, output, tracer.traces().pop(), vm_tracer.drain())))
Ok(try!(self.finalize(t, substate, gas_left, output, tracer.traces().pop(), vm_tracer.drain().and_then(|mut i| i.subs.pop()))))
}
fn exec_vm<T, V>(

View File

@ -139,3 +139,4 @@ mod tests;
mod json_tests;
pub use types::*;
pub use evm::get_info;

View File

@ -106,7 +106,7 @@ impl Tracer for ExecutiveTracer {
}
}
/// Simple VM tracer. Traces all operations. Ignores delegatecalls.
/// Simple VM tracer. Traces all operations.
#[derive(Default)]
pub struct ExecutiveVMTracer {
data: VMTrace,

View File

@ -24,7 +24,9 @@ use std::collections::BTreeMap;
use jsonrpc_core::*;
use ethminer::{MinerService};
use ethcore::client::{BlockChainClient};
use ethcore::trace::VMTrace;
use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action};
use ethcore::get_info;
use v1::traits::Ethcore;
use v1::types::{Bytes, CallRequest};
@ -66,6 +68,34 @@ impl<C, M> EthcoreClient<C, M> where C: BlockChainClient, M: MinerService {
}
}
fn vm_trace_to_object(t: &VMTrace) -> Value {
let mut ret = BTreeMap::new();
ret.insert("code".to_owned(), to_value(&t.code).unwrap());
let mut subs = t.subs.iter();
let mut next_sub = subs.next();
let ops = t.operations
.iter()
.enumerate()
.map(|(i, op)| {
let mut m = map![
"pc".to_owned() => to_value(&op.pc).unwrap(),
"inst".to_owned() => to_value(&get_info(op.instruction).name).unwrap(),
"gas_cost".to_owned() => to_value(&op.gas_cost).unwrap(),
"stack".to_owned() => to_value(&op.stack).unwrap()
];
if next_sub.is_some() && next_sub.unwrap().parent_step == i {
m.insert("sub".to_owned(), vm_trace_to_object(next_sub.unwrap()));
next_sub = subs.next();
}
Value::Object(m)
})
.collect::<Vec<_>>();
ret.insert("ops".to_owned(), Value::Array(ops));
Value::Object(ret)
}
impl<C, M> Ethcore for EthcoreClient<C, M> where C: BlockChainClient + 'static, M: MinerService + 'static {
fn set_min_gas_price(&self, params: Params) -> Result<Value, Error> {
@ -162,11 +192,14 @@ impl<C, M> Ethcore for EthcoreClient<C, M> where C: BlockChainClient + 'static,
from_params(params)
.and_then(|(request,)| {
let signed = try!(self.sign_call(request));
let _ = take_weak!(self.client).call(&signed, true);
// TODO: construct JSON trace from _.vm_trace.
let mut ret = Vec::new();
ret.push(Value::Object(map!["foo".to_owned() => Value::String("var".to_owned())]));
Ok(Value::Array(ret))
let r = take_weak!(self.client).call(&signed, true);
trace!(target: "jsonrpc", "returned {:?}", r);
if let Ok(executed) = r {
if let Some(vm_trace) = executed.vm_trace {
return Ok(vm_trace_to_object(&vm_trace));
}
}
Ok(Value::Null)
})
}
}