Traces to AutoArgs

This commit is contained in:
Tomasz Drwięga 2016-12-14 20:09:14 +01:00
parent 6e51b23e30
commit d2b9bc5bd8
2 changed files with 82 additions and 115 deletions

View File

@ -17,14 +17,14 @@
//! Traces api implementation.
use std::sync::{Weak, Arc};
use jsonrpc_core::*;
use serde;
use rlp::{UntrustedRlp, View};
use ethcore::client::{BlockChainClient, CallAnalytics, TransactionId, TraceId};
use ethcore::miner::MinerService;
use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action};
use jsonrpc_core::Error;
use jsonrpc_macros::Trailing;
use v1::traits::Traces;
use v1::helpers::{errors, CallRequest as CRequest};
use v1::types::{TraceFilter, LocalizedTrace, BlockNumber, Index, CallRequest, Bytes, TraceResults, H256};
@ -37,22 +37,6 @@ fn to_call_analytics(flags: Vec<String>) -> CallAnalytics {
}
}
/// Returns number of different parameters in given `Params` object.
fn params_len(params: &Params) -> usize {
match params {
&Params::Array(ref vec) => vec.len(),
_ => 0,
}
}
/// Deserialize request parameters with optional third parameter `BlockNumber` defaulting to `BlockNumber::Latest`.
fn from_params_default_third<F1, F2>(params: Params) -> Result<(F1, F2, BlockNumber, ), Error> where F1: serde::de::Deserialize, F2: serde::de::Deserialize {
match params_len(&params) {
2 => from_params::<(F1, F2, )>(params).map(|(f1, f2)| (f1, f2, BlockNumber::Latest)),
_ => from_params::<(F1, F2, BlockNumber)>(params)
}
}
/// Traces api implementation.
pub struct TracesClient<C, M> where C: BlockChainClient, M: MinerService {
client: Weak<C>,
@ -91,90 +75,77 @@ impl<C, M> TracesClient<C, M> where C: BlockChainClient, M: MinerService {
}
impl<C, M> Traces for TracesClient<C, M> where C: BlockChainClient + 'static, M: MinerService + 'static {
fn filter(&self, params: Params) -> Result<Value, Error> {
fn filter(&self, filter: TraceFilter) -> Result<Vec<LocalizedTrace>, Error> {
try!(self.active());
from_params::<(TraceFilter,)>(params)
.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(LocalizedTrace::from).collect());
Ok(to_value(&traces))
})
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(LocalizedTrace::from).collect());
Ok(traces)
}
fn block_traces(&self, params: Params) -> Result<Value, Error> {
fn block_traces(&self, block_number: BlockNumber) -> Result<Vec<LocalizedTrace>, Error> {
try!(self.active());
from_params::<(BlockNumber,)>(params)
.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(LocalizedTrace::from).collect());
Ok(to_value(&traces))
})
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(LocalizedTrace::from).collect());
Ok(traces)
}
fn transaction_traces(&self, params: Params) -> Result<Value, Error> {
fn transaction_traces(&self, transaction_hash: H256) -> Result<Vec<LocalizedTrace>, Error> {
try!(self.active());
from_params::<(H256,)>(params)
.and_then(|(transaction_hash,)| {
let client = take_weak!(self.client);
let traces = client.transaction_traces(TransactionId::Hash(transaction_hash.into()));
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(LocalizedTrace::from).collect());
Ok(to_value(&traces))
})
let client = take_weak!(self.client);
let traces = client.transaction_traces(TransactionId::Hash(transaction_hash.into()));
let traces = traces.map_or_else(Vec::new, |traces| traces.into_iter().map(LocalizedTrace::from).collect());
Ok(traces)
}
fn trace(&self, params: Params) -> Result<Value, Error> {
fn trace(&self, transaction_hash: H256, address: Vec<Index>) -> Result<Option<LocalizedTrace>, Error> {
try!(self.active());
from_params::<(H256, Vec<Index>)>(params)
.and_then(|(transaction_hash, address)| {
let client = take_weak!(self.client);
let id = TraceId {
transaction: TransactionId::Hash(transaction_hash.into()),
address: address.into_iter().map(|i| i.value()).collect()
};
let trace = client.trace(id);
let trace = trace.map(LocalizedTrace::from);
Ok(to_value(&trace))
})
let client = take_weak!(self.client);
let id = TraceId {
transaction: TransactionId::Hash(transaction_hash.into()),
address: address.into_iter().map(|i| i.value()).collect()
};
let trace = client.trace(id);
let trace = trace.map(LocalizedTrace::from);
Ok(trace)
}
fn call(&self, params: Params) -> Result<Value, Error> {
fn call(&self, request: CallRequest, flags: Vec<String>, block: Trailing<BlockNumber>) -> Result<Option<TraceResults>, Error> {
try!(self.active());
from_params_default_third(params)
.and_then(|(request, flags, block)| {
let request = CallRequest::into(request);
let signed = try!(self.sign_call(request));
match take_weak!(self.client).call(&signed, block.into(), to_call_analytics(flags)) {
Ok(e) => Ok(to_value(&TraceResults::from(e))),
_ => Ok(Value::Null),
}
})
let block = block.0;
let request = CallRequest::into(request);
let signed = try!(self.sign_call(request));
Ok(match take_weak!(self.client).call(&signed, block.into(), to_call_analytics(flags)) {
Ok(e) => Some(TraceResults::from(e)),
_ => None,
})
}
fn raw_transaction(&self, params: Params) -> Result<Value, Error> {
fn raw_transaction(&self, raw_transaction: Bytes, flags: Vec<String>, block: Trailing<BlockNumber>) -> Result<Option<TraceResults>, Error> {
try!(self.active());
from_params_default_third(params)
.and_then(|(raw_transaction, flags, block)| {
let raw_transaction = Bytes::to_vec(raw_transaction);
match UntrustedRlp::new(&raw_transaction).as_val() {
Ok(signed) => match take_weak!(self.client).call(&signed, block.into(), to_call_analytics(flags)) {
Ok(e) => Ok(to_value(&TraceResults::from(e))),
_ => Ok(Value::Null),
},
Err(e) => Err(errors::invalid_params("Transaction is not valid RLP", e)),
}
})
let block = block.0;
let raw_transaction = Bytes::to_vec(raw_transaction);
match UntrustedRlp::new(&raw_transaction).as_val() {
Ok(signed) => Ok(match take_weak!(self.client).call(&signed, block.into(), to_call_analytics(flags)) {
Ok(e) => Some(TraceResults::from(e)),
_ => None,
}),
Err(e) => Err(errors::invalid_params("Transaction is not valid RLP", e)),
}
}
fn replay_transaction(&self, params: Params) -> Result<Value, Error> {
fn replay_transaction(&self, transaction_hash: H256, flags: Vec<String>) -> Result<Option<TraceResults>, Error> {
try!(self.active());
from_params::<(H256, _)>(params)
.and_then(|(transaction_hash, flags)| {
match take_weak!(self.client).replay(TransactionId::Hash(transaction_hash.into()), to_call_analytics(flags)) {
Ok(e) => Ok(to_value(&TraceResults::from(e))),
_ => Ok(Value::Null),
}
})
Ok(match take_weak!(self.client).replay(TransactionId::Hash(transaction_hash.into()), to_call_analytics(flags)) {
Ok(e) => Some(TraceResults::from(e)),
_ => None,
})
}
}

View File

@ -16,43 +16,39 @@
//! Traces specific rpc interface.
use std::sync::Arc;
use jsonrpc_core::{Params, Value, Error, IoDelegate};
use jsonrpc_core::Error;
use jsonrpc_macros::Trailing;
use v1::types::{TraceFilter, LocalizedTrace, BlockNumber, Index, CallRequest, Bytes, TraceResults, H256};
/// Traces specific rpc interface.
pub trait Traces: Sized + Send + Sync + 'static {
/// Returns traces matching given filter.
fn filter(&self, _: Params) -> Result<Value, Error>;
build_rpc_trait! {
/// Traces specific rpc interface.
pub trait Traces {
/// Returns traces matching given filter.
#[rpc(name = "trace_filter")]
fn filter(&self, TraceFilter) -> Result<Vec<LocalizedTrace>, Error>;
/// Returns transaction trace at given index.
fn trace(&self, _: Params) -> Result<Value, Error>;
/// Returns transaction trace at given index.
#[rpc(name = "trace_get")]
fn trace(&self, H256, Vec<Index>) -> Result<Option<LocalizedTrace>, Error>;
/// Returns all traces of given transaction.
fn transaction_traces(&self, _: Params) -> Result<Value, Error>;
/// Returns all traces of given transaction.
#[rpc(name = "trace_transaction")]
fn transaction_traces(&self, H256) -> Result<Vec<LocalizedTrace>, Error>;
/// Returns all traces produced at given block.
fn block_traces(&self, _: Params) -> Result<Value, Error>;
/// Returns all traces produced at given block.
#[rpc(name = "trace_block")]
fn block_traces(&self, BlockNumber) -> Result<Vec<LocalizedTrace>, Error>;
/// Executes the given call and returns a number of possible traces for it.
fn call(&self, _: Params) -> Result<Value, Error>;
/// Executes the given call and returns a number of possible traces for it.
#[rpc(name = "trace_call")]
fn call(&self, CallRequest, Vec<String>, Trailing<BlockNumber>) -> Result<Option<TraceResults>, Error>;
/// Executes the given raw transaction and returns a number of possible traces for it.
fn raw_transaction(&self, _: Params) -> Result<Value, Error>;
/// Executes the given raw transaction and returns a number of possible traces for it.
#[rpc(name = "trace_rawTransaction")]
fn raw_transaction(&self, Bytes, Vec<String>, Trailing<BlockNumber>) -> Result<Option<TraceResults>, Error>;
/// Executes the transaction with the given hash and returns a number of possible traces for it.
fn replay_transaction(&self, _: Params) -> Result<Value, Error>;
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("trace_filter", Traces::filter);
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_call", Traces::call);
delegate.add_method("trace_rawTransaction", Traces::raw_transaction);
delegate.add_method("trace_replayTransaction", Traces::replay_transaction);
delegate
/// Executes the transaction with the given hash and returns a number of possible traces for it.
#[rpc(name = "trace_replayTransaction")]
fn replay_transaction(&self, H256, Vec<String>) -> Result<Option<TraceResults>, Error>;
}
}