2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-05-02 12:17:30 +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/>.
|
|
|
|
|
|
|
|
//! Traces api implementation.
|
|
|
|
|
2017-05-28 14:40:36 +02:00
|
|
|
use std::sync::Arc;
|
2016-12-13 14:27:27 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
use ethcore::client::{BlockChainClient, CallAnalytics, TransactionId, TraceId, StateClient, StateInfo, Call, BlockId};
|
2018-04-16 15:52:12 +02:00
|
|
|
use rlp::Rlp;
|
2018-01-11 17:49:10 +01:00
|
|
|
use transaction::SignedTransaction;
|
2016-12-13 14:27:27 +01:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
use jsonrpc_core::Result;
|
2016-12-14 20:09:14 +01:00
|
|
|
use jsonrpc_macros::Trailing;
|
2017-08-15 10:23:28 +02:00
|
|
|
use v1::Metadata;
|
2016-05-02 12:17:30 +02:00
|
|
|
use v1::traits::Traces;
|
2017-03-14 13:04:32 +01:00
|
|
|
use v1::helpers::{errors, fake_sign};
|
2018-03-03 18:42:13 +01:00
|
|
|
use v1::types::{TraceFilter, LocalizedTrace, BlockNumber, Index, CallRequest, Bytes, TraceResults, TraceOptions, H256, block_number_to_id};
|
2016-05-02 12:17:30 +02:00
|
|
|
|
2017-08-04 15:58:14 +02:00
|
|
|
fn to_call_analytics(flags: TraceOptions) -> CallAnalytics {
|
2016-07-27 21:34:32 +02:00
|
|
|
CallAnalytics {
|
|
|
|
transaction_tracing: flags.contains(&("trace".to_owned())),
|
|
|
|
vm_tracing: flags.contains(&("vmTrace".to_owned())),
|
|
|
|
state_diffing: flags.contains(&("stateDiff".to_owned())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 12:17:30 +02:00
|
|
|
/// Traces api implementation.
|
2017-11-02 12:50:08 +01:00
|
|
|
pub struct TracesClient<C> {
|
2017-05-28 14:40:36 +02:00
|
|
|
client: Arc<C>,
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 12:50:08 +01:00
|
|
|
impl<C> TracesClient<C> {
|
2016-05-02 12:17:30 +02:00
|
|
|
/// Creates new Traces client.
|
2017-11-02 12:50:08 +01:00
|
|
|
pub fn new(client: &Arc<C>) -> Self {
|
2016-05-02 12:17:30 +02:00
|
|
|
TracesClient {
|
2017-05-28 14:40:36 +02:00
|
|
|
client: client.clone(),
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 13:14:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl<C, S> Traces for TracesClient<C> where
|
|
|
|
S: StateInfo + 'static,
|
2018-04-13 17:34:27 +02:00
|
|
|
C: BlockChainClient + StateClient<State=S> + Call<State=S> + 'static
|
2018-03-03 18:42:13 +01:00
|
|
|
{
|
2017-08-15 10:23:28 +02:00
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn filter(&self, filter: TraceFilter) -> Result<Option<Vec<LocalizedTrace>>> {
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self.client.filter_traces(filter.into())
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(|traces| traces.into_iter().map(LocalizedTrace::from).collect()))
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn block_traces(&self, block_number: BlockNumber) -> Result<Option<Vec<LocalizedTrace>>> {
|
2018-03-03 18:42:13 +01:00
|
|
|
let id = match block_number {
|
|
|
|
BlockNumber::Pending => return Ok(None),
|
|
|
|
num => block_number_to_id(num)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(self.client.block_traces(id)
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(|traces| traces.into_iter().map(LocalizedTrace::from).collect()))
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn transaction_traces(&self, transaction_hash: H256) -> Result<Option<Vec<LocalizedTrace>>> {
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self.client.transaction_traces(TransactionId::Hash(transaction_hash.into()))
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(|traces| traces.into_iter().map(LocalizedTrace::from).collect()))
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn trace(&self, transaction_hash: H256, address: Vec<Index>) -> Result<Option<LocalizedTrace>> {
|
2016-12-14 20:09:14 +01:00
|
|
|
let id = TraceId {
|
|
|
|
transaction: TransactionId::Hash(transaction_hash.into()),
|
|
|
|
address: address.into_iter().map(|i| i.value()).collect()
|
|
|
|
};
|
|
|
|
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self.client.trace(id)
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(LocalizedTrace::from))
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|
2016-06-02 13:14:49 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn call(&self, meta: Self::Metadata, request: CallRequest, flags: TraceOptions, block: Trailing<BlockNumber>) -> Result<TraceResults> {
|
2017-06-13 16:29:35 +02:00
|
|
|
let block = block.unwrap_or_default();
|
2016-12-14 20:09:14 +01:00
|
|
|
|
|
|
|
let request = CallRequest::into(request);
|
2017-11-02 12:50:08 +01:00
|
|
|
let signed = fake_sign::sign_call(request, meta.is_dapp())?;
|
2017-03-14 13:04:32 +01:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
let id = match block {
|
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
|
|
|
|
|
|
|
BlockNumber::Pending => return Err(errors::invalid_params("`BlockNumber::Pending` is not supported", ())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut state = self.client.state_at(id).ok_or(errors::state_pruned())?;
|
|
|
|
let header = self.client.block_header(id).ok_or(errors::state_pruned())?;
|
|
|
|
|
2018-05-09 12:05:56 +02:00
|
|
|
self.client.call(&signed, to_call_analytics(flags), &mut state, &header.decode().map_err(errors::decode)?)
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(TraceResults::from)
|
2017-10-05 12:35:01 +02:00
|
|
|
.map_err(errors::call)
|
2016-07-26 16:48:50 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn call_many(&self, meta: Self::Metadata, requests: Vec<(CallRequest, TraceOptions)>, block: Trailing<BlockNumber>) -> Result<Vec<TraceResults>> {
|
2017-08-04 15:58:14 +02:00
|
|
|
let block = block.unwrap_or_default();
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
let requests = requests.into_iter()
|
2017-08-04 15:58:14 +02:00
|
|
|
.map(|(request, flags)| {
|
|
|
|
let request = CallRequest::into(request);
|
2017-11-02 12:50:08 +01:00
|
|
|
let signed = fake_sign::sign_call(request, meta.is_dapp())?;
|
2017-08-04 15:58:14 +02:00
|
|
|
Ok((signed, to_call_analytics(flags)))
|
|
|
|
})
|
2017-11-14 11:38:17 +01:00
|
|
|
.collect::<Result<Vec<_>>>()?;
|
2017-08-04 15:58:14 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
let id = match block {
|
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
|
|
|
|
|
|
|
BlockNumber::Pending => return Err(errors::invalid_params("`BlockNumber::Pending` is not supported", ())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut state = self.client.state_at(id).ok_or(errors::state_pruned())?;
|
|
|
|
let header = self.client.block_header(id).ok_or(errors::state_pruned())?;
|
|
|
|
|
2018-05-09 12:05:56 +02:00
|
|
|
self.client.call_many(&requests, &mut state, &header.decode().map_err(errors::decode)?)
|
2017-08-04 15:58:14 +02:00
|
|
|
.map(|results| results.into_iter().map(TraceResults::from).collect())
|
2017-10-05 12:35:01 +02:00
|
|
|
.map_err(errors::call)
|
2017-08-04 15:58:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn raw_transaction(&self, raw_transaction: Bytes, flags: TraceOptions, block: Trailing<BlockNumber>) -> Result<TraceResults> {
|
2017-06-13 16:29:35 +02:00
|
|
|
let block = block.unwrap_or_default();
|
2016-12-14 20:09:14 +01:00
|
|
|
|
2018-04-16 15:52:12 +02:00
|
|
|
let tx = Rlp::new(&raw_transaction.into_vec()).as_val().map_err(|e| errors::invalid_params("Transaction is not valid RLP", e))?;
|
2017-07-04 17:01:06 +02:00
|
|
|
let signed = SignedTransaction::new(tx).map_err(errors::transaction)?;
|
2017-03-14 13:04:32 +01:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
let id = match block {
|
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
|
|
|
|
|
|
|
BlockNumber::Pending => return Err(errors::invalid_params("`BlockNumber::Pending` is not supported", ())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut state = self.client.state_at(id).ok_or(errors::state_pruned())?;
|
|
|
|
let header = self.client.block_header(id).ok_or(errors::state_pruned())?;
|
|
|
|
|
2018-05-09 12:05:56 +02:00
|
|
|
self.client.call(&signed, to_call_analytics(flags), &mut state, &header.decode().map_err(errors::decode)?)
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(TraceResults::from)
|
2017-07-04 17:01:06 +02:00
|
|
|
.map_err(errors::call)
|
2016-06-02 13:14:49 +02:00
|
|
|
}
|
2016-07-27 21:34:32 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn replay_transaction(&self, transaction_hash: H256, flags: TraceOptions) -> Result<TraceResults> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.client.replay(TransactionId::Hash(transaction_hash.into()), to_call_analytics(flags))
|
2017-03-14 13:04:32 +01:00
|
|
|
.map(TraceResults::from)
|
2017-07-04 17:01:06 +02:00
|
|
|
.map_err(errors::call)
|
2016-07-27 21:34:32 +02:00
|
|
|
}
|
2018-01-10 11:34:34 +01:00
|
|
|
|
|
|
|
fn replay_block_transactions(&self, block_number: BlockNumber, flags: TraceOptions) -> Result<Vec<TraceResults>> {
|
2018-03-03 18:42:13 +01:00
|
|
|
let id = match block_number {
|
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
|
|
|
|
|
|
|
BlockNumber::Pending => return Err(errors::invalid_params("`BlockNumber::Pending` is not supported", ())),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.client.replay_block_transactions(id, to_call_analytics(flags))
|
2018-01-10 11:34:34 +01:00
|
|
|
.map(|results| results.into_iter().map(TraceResults::from).collect())
|
|
|
|
.map_err(errors::call)
|
|
|
|
}
|
2016-05-02 12:17:30 +02:00
|
|
|
}
|