// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
// Parity Ethereum 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 Ethereum 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 Ethereum. If not, see .
//! Parity-specific rpc implementation.
use std::sync::Arc;
use std::str::FromStr;
use std::collections::{BTreeMap, HashSet};
use ethereum_types::Address;
use version::version_data;
use crypto::DEFAULT_MAC;
use ethcore::account_provider::AccountProvider;
use ethcore::client::{BlockChainClient, StateClient, Call};
use ethcore::miner::{self, MinerService};
use ethcore::snapshot::{SnapshotService, RestorationStatus};
use ethcore::state::StateInfo;
use ethcore_logger::RotatingLogger;
use ethkey::{crypto::ecies, Brain, Generator};
use ethstore::random_phrase;
use jsonrpc_core::futures::future;
use jsonrpc_core::{BoxFuture, Result};
use jsonrpc_macros::Trailing;
use sync::{SyncProvider, ManageNetwork};
use types::ids::BlockId;
use updater::{Service as UpdateService};
use v1::helpers::block_import::is_major_importing;
use v1::helpers::{self, errors, fake_sign, ipfs, SigningQueue, SignerService, NetworkSettings, verify_signature};
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, H64, U64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
OperationsInfo, ChainStatus, Log, Filter,
AccountInfo, HwAccountInfo, RichHeader, Receipt, RecoveredAccount,
block_number_to_id
};
use Host;
/// Parity implementation.
pub struct ParityClient {
client: Arc,
miner: Arc,
updater: Arc,
sync: Arc,
net: Arc,
accounts: Arc,
logger: Arc,
settings: Arc,
signer: Option>,
ws_address: Option,
snapshot: Option>,
}
impl ParityClient where
C: BlockChainClient,
{
/// Creates new `ParityClient`.
pub fn new(
client: Arc,
miner: Arc,
sync: Arc,
updater: Arc,
net: Arc,
accounts: Arc,
logger: Arc,
settings: Arc,
signer: Option>,
ws_address: Option,
snapshot: Option>,
) -> Self {
ParityClient {
client,
miner,
sync,
updater,
net,
accounts,
logger,
settings,
signer,
ws_address,
snapshot,
}
}
}
impl Parity for ParityClient where
S: StateInfo + 'static,
C: miner::BlockChainClient + BlockChainClient + StateClient + Call + 'static,
M: MinerService + 'static,
U: UpdateService + 'static,
{
type Metadata = Metadata;
fn accounts_info(&self) -> Result> {
let dapp_accounts = self.accounts.accounts()
.map_err(|e| errors::account("Could not fetch accounts.", e))?
.into_iter().collect::>();
let info = self.accounts.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
let other = self.accounts.addresses_info();
Ok(info
.into_iter()
.chain(other.into_iter())
.filter(|&(ref a, _)| dapp_accounts.contains(a))
.map(|(a, v)| (H160::from(a), AccountInfo { name: v.name }))
.collect()
)
}
fn hardware_accounts_info(&self) -> Result> {
let info = self.accounts.hardware_accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
Ok(info
.into_iter()
.map(|(a, v)| (H160::from(a), HwAccountInfo { name: v.name, manufacturer: v.meta }))
.collect()
)
}
fn locked_hardware_accounts_info(&self) -> Result> {
self.accounts.locked_hardware_accounts().map_err(|e| errors::account("Error communicating with hardware wallet.", e))
}
fn default_account(&self) -> Result {
Ok(self.accounts.default_account()
.map(Into::into)
.ok()
.unwrap_or_default())
}
fn transactions_limit(&self) -> Result {
Ok(self.miner.queue_status().limits.max_count)
}
fn min_gas_price(&self) -> Result {
Ok(self.miner.queue_status().options.minimal_gas_price.into())
}
fn extra_data(&self) -> Result {
Ok(Bytes::new(self.miner.authoring_params().extra_data))
}
fn gas_floor_target(&self) -> Result {
Ok(U256::from(self.miner.authoring_params().gas_range_target.0))
}
fn gas_ceil_target(&self) -> Result {
Ok(U256::from(self.miner.authoring_params().gas_range_target.1))
}
fn dev_logs(&self) -> Result> {
warn!("This method is deprecated and will be removed in future. See PR #10102");
let logs = self.logger.logs();
Ok(logs.as_slice().to_owned())
}
fn dev_logs_levels(&self) -> Result {
Ok(self.logger.levels().to_owned())
}
fn net_chain(&self) -> Result {
Ok(self.settings.chain.clone())
}
fn chain(&self) -> Result {
Ok(self.client.spec_name())
}
fn net_peers(&self) -> Result {
let sync_status = self.sync.status();
let num_peers_range = self.net.num_peers_range();
debug_assert!(num_peers_range.end > num_peers_range.start);
let peers = self.sync.peers().into_iter().map(Into::into).collect();
Ok(Peers {
active: sync_status.num_active_peers,
connected: sync_status.num_peers,
max: sync_status.current_max_peers(num_peers_range.start, num_peers_range.end - 1),
peers: peers
})
}
fn net_port(&self) -> Result {
Ok(self.settings.network_port)
}
fn node_name(&self) -> Result {
Ok(self.settings.name.clone())
}
fn registry_address(&self) -> Result