// Copyright 2015, 2016 Ethcore (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, Weak};
use std::str::FromStr;
use std::collections::BTreeMap;
use util::{RotatingLogger, Address};
use util::misc::version_data;
use crypto::ecies;
use ethkey::{Brain, Generator};
use ethstore::random_phrase;
use ethsync::{SyncProvider, ManageNetwork};
use ethcore::miner::MinerService;
use ethcore::client::{MiningBlockChainClient};
use ethcore::ids::BlockID;
use ethcore::mode::Mode;
use ethcore::account_provider::AccountProvider;
use jsonrpc_core::Error;
use v1::traits::Parity;
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings, Histogram, TransactionStats};
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::dispatch::DEFAULT_MAC;
/// Parity implementation.
pub struct ParityClient where
C: MiningBlockChainClient,
M: MinerService,
S: SyncProvider,
{
client: Weak,
miner: Weak,
sync: Weak,
net: Weak,
accounts: Weak,
logger: Arc,
settings: Arc,
signer: Option>,
dapps_interface: Option,
dapps_port: Option,
}
impl ParityClient where
C: MiningBlockChainClient,
M: MinerService,
S: SyncProvider,
{
/// Creates new `ParityClient`.
pub fn new(
client: &Arc,
miner: &Arc,
sync: &Arc,
net: &Arc,
store: &Arc,
logger: Arc,
settings: Arc,
signer: Option>,
dapps_interface: Option,
dapps_port: Option,
) -> Self {
ParityClient {
client: Arc::downgrade(client),
miner: Arc::downgrade(miner),
sync: Arc::downgrade(sync),
net: Arc::downgrade(net),
accounts: Arc::downgrade(store),
logger: logger,
settings: settings,
signer: signer,
dapps_interface: dapps_interface,
dapps_port: dapps_port,
}
}
fn active(&self) -> Result<(), Error> {
// TODO: only call every 30s at most.
take_weak!(self.client).keep_alive();
Ok(())
}
}
impl Parity for ParityClient where
M: MinerService + 'static,
C: MiningBlockChainClient + 'static,
S: SyncProvider + 'static {
fn transactions_limit(&self) -> Result {
try!(self.active());
Ok(take_weak!(self.miner).transactions_limit())
}
fn min_gas_price(&self) -> Result {
try!(self.active());
Ok(U256::from(take_weak!(self.miner).minimal_gas_price()))
}
fn extra_data(&self) -> Result {
try!(self.active());
Ok(Bytes::new(take_weak!(self.miner).extra_data()))
}
fn gas_floor_target(&self) -> Result {
try!(self.active());
Ok(U256::from(take_weak!(self.miner).gas_floor_target()))
}
fn gas_ceil_target(&self) -> Result {
try!(self.active());
Ok(U256::from(take_weak!(self.miner).gas_ceil_target()))
}
fn dev_logs(&self) -> Result, Error> {
try!(self.active());
let logs = self.logger.logs();
Ok(logs.as_slice().to_owned())
}
fn dev_logs_levels(&self) -> Result {
try!(self.active());
Ok(self.logger.levels().to_owned())
}
fn net_chain(&self) -> Result {
try!(self.active());
Ok(self.settings.chain.clone())
}
fn net_peers(&self) -> Result {
try!(self.active());
let sync = take_weak!(self.sync);
let sync_status = sync.status();
let net_config = take_weak!(self.net).network_config();
let peers = 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 {
try!(self.active());
Ok(self.settings.network_port)
}
fn node_name(&self) -> Result {
try!(self.active());
Ok(self.settings.name.clone())
}
fn registry_address(&self) -> Result