From d2b9bc5bd8fca75bfcdbad8640ddd47e654b291c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 14 Dec 2016 20:09:14 +0100 Subject: [PATCH] Traces to AutoArgs --- rpc/src/v1/impls/traces.rs | 139 ++++++++++++++---------------------- rpc/src/v1/traits/traces.rs | 58 +++++++-------- 2 files changed, 82 insertions(+), 115 deletions(-) diff --git a/rpc/src/v1/impls/traces.rs b/rpc/src/v1/impls/traces.rs index 6b37e40cc..5f42c0cf3 100644 --- a/rpc/src/v1/impls/traces.rs +++ b/rpc/src/v1/impls/traces.rs @@ -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) -> 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(params: Params) -> Result<(F1, F2, BlockNumber, ), Error> where F1: serde::de::Deserialize, F2: serde::de::Deserialize { - match params_len(¶ms) { - 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 where C: BlockChainClient, M: MinerService { client: Weak, @@ -91,90 +75,77 @@ impl TracesClient where C: BlockChainClient, M: MinerService { } impl Traces for TracesClient where C: BlockChainClient + 'static, M: MinerService + 'static { - fn filter(&self, params: Params) -> Result { + fn filter(&self, filter: TraceFilter) -> Result, 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 { + fn block_traces(&self, block_number: BlockNumber) -> Result, 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 { + fn transaction_traces(&self, transaction_hash: H256) -> Result, 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 { + fn trace(&self, transaction_hash: H256, address: Vec) -> Result, Error> { try!(self.active()); - from_params::<(H256, Vec)>(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 { + fn call(&self, request: CallRequest, flags: Vec, block: Trailing) -> Result, 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 { + fn raw_transaction(&self, raw_transaction: Bytes, flags: Vec, block: Trailing) -> Result, 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 { + fn replay_transaction(&self, transaction_hash: H256, flags: Vec) -> Result, 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, + }) } } diff --git a/rpc/src/v1/traits/traces.rs b/rpc/src/v1/traits/traces.rs index 36561216f..1d5fef5ec 100644 --- a/rpc/src/v1/traits/traces.rs +++ b/rpc/src/v1/traits/traces.rs @@ -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; +build_rpc_trait! { + /// Traces specific rpc interface. + pub trait Traces { + /// Returns traces matching given filter. + #[rpc(name = "trace_filter")] + fn filter(&self, TraceFilter) -> Result, Error>; - /// Returns transaction trace at given index. - fn trace(&self, _: Params) -> Result; + /// Returns transaction trace at given index. + #[rpc(name = "trace_get")] + fn trace(&self, H256, Vec) -> Result, Error>; - /// Returns all traces of given transaction. - fn transaction_traces(&self, _: Params) -> Result; + /// Returns all traces of given transaction. + #[rpc(name = "trace_transaction")] + fn transaction_traces(&self, H256) -> Result, Error>; - /// Returns all traces produced at given block. - fn block_traces(&self, _: Params) -> Result; + /// Returns all traces produced at given block. + #[rpc(name = "trace_block")] + fn block_traces(&self, BlockNumber) -> Result, Error>; - /// Executes the given call and returns a number of possible traces for it. - fn call(&self, _: Params) -> Result; + /// Executes the given call and returns a number of possible traces for it. + #[rpc(name = "trace_call")] + fn call(&self, CallRequest, Vec, Trailing) -> Result, Error>; - /// Executes the given raw transaction and returns a number of possible traces for it. - fn raw_transaction(&self, _: Params) -> Result; + /// Executes the given raw transaction and returns a number of possible traces for it. + #[rpc(name = "trace_rawTransaction")] + fn raw_transaction(&self, Bytes, Vec, Trailing) -> Result, Error>; - /// Executes the transaction with the given hash and returns a number of possible traces for it. - fn replay_transaction(&self, _: Params) -> Result; - - /// Should be used to convert object to io delegate. - fn to_delegate(self) -> IoDelegate { - 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) -> Result, Error>; } }