// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// 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 .
//! Parity-specific rpc implementation.
use std::sync::Arc;
use std::str::FromStr;
use std::collections::{BTreeMap, HashSet};
use futures::{future, Future, BoxFuture};
use util::Address;
use util::misc::version_data;
use crypto::{DEFAULT_MAC, ecies};
use ethkey::{Brain, Generator};
use ethstore::random_phrase;
use ethsync::{SyncProvider, ManageNetwork};
use ethcore::account_provider::AccountProvider;
use ethcore::client::{MiningBlockChainClient};
use ethcore::ids::BlockId;
use ethcore::miner::MinerService;
use ethcore::mode::Mode;
use ethcore::transaction::SignedTransaction;
use ethcore_logger::RotatingLogger;
use node_health::{NodeHealth, Health};
use updater::{Service as UpdateService};
use jsonrpc_core::Error;
use jsonrpc_macros::Trailing;
use v1::helpers::{self, errors, fake_sign, ipfs, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::accounts::unwrap_provider;
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
OperationsInfo, DappId, ChainStatus,
AccountInfo, HwAccountInfo, RichHeader
};
/// Parity implementation.
pub struct ParityClient {
client: Arc,
miner: Arc,
updater: Arc,
sync: Arc,
net: Arc,
health: NodeHealth,
accounts: Option>,
logger: Arc,
settings: Arc,
signer: Option>,
dapps_address: Option<(String, u16)>,
ws_address: Option<(String, u16)>,
eip86_transition: u64,
}
impl ParityClient where
C: MiningBlockChainClient,
{
/// Creates new `ParityClient`.
pub fn new(
client: Arc,
miner: Arc,
sync: Arc,
updater: Arc,
net: Arc,
health: NodeHealth,
accounts: Option>,
logger: Arc,
settings: Arc,
signer: Option>,
dapps_address: Option<(String, u16)>,
ws_address: Option<(String, u16)>,
) -> Self {
let eip86_transition = client.eip86_transition();
ParityClient {
client,
miner,
sync,
updater,
net,
health,
accounts,
logger,
settings,
signer,
dapps_address,
ws_address,
eip86_transition,
}
}
/// Attempt to get the `Arc`, errors if provider was not
/// set.
fn account_provider(&self) -> Result, Error> {
unwrap_provider(&self.accounts)
}
}
impl Parity for ParityClient where
C: MiningBlockChainClient + 'static,
M: MinerService + 'static,
U: UpdateService + 'static,
{
type Metadata = Metadata;
fn accounts_info(&self, dapp: Trailing) -> Result, Error> {
let dapp = dapp.unwrap_or_default();
let store = self.account_provider()?;
let dapp_accounts = store
.note_dapp_used(dapp.clone().into())
.and_then(|_| store.dapp_addresses(dapp.into()))
.map_err(|e| errors::account("Could not fetch accounts.", e))?
.into_iter().collect::>();
let info = store.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
let other = store.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, Error> {
let store = self.account_provider()?;
let info = store.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 default_account(&self, meta: Self::Metadata) -> BoxFuture {
let dapp_id = meta.dapp_id();
future::ok(
try_bf!(self.account_provider())
.dapp_default_address(dapp_id.into())
.map(Into::into)
.ok()
.unwrap_or_default()
).boxed()
}
fn transactions_limit(&self) -> Result {
Ok(self.miner.transactions_limit())
}
fn min_gas_price(&self) -> Result {
Ok(U256::from(self.miner.minimal_gas_price()))
}
fn extra_data(&self) -> Result {
Ok(Bytes::new(self.miner.extra_data()))
}
fn gas_floor_target(&self) -> Result {
Ok(U256::from(self.miner.gas_floor_target()))
}
fn gas_ceil_target(&self) -> Result {
Ok(U256::from(self.miner.gas_ceil_target()))
}
fn dev_logs(&self) -> Result, Error> {
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 net_config = self.net.network_config();
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(net_config.min_peers, net_config.max_peers),
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