// 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 .
//! Ethcore-specific rpc implementation.
use std::{fs, io};
use std::sync::{mpsc, Arc, Weak};
use std::str::FromStr;
use util::{RotatingLogger, Address, Mutex, sha3};
use util::misc::version_data;
use crypto::ecies;
use fetch::{Client as FetchClient, Fetch};
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 jsonrpc_core::Error;
use v1::traits::Ethcore;
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings, Histogram};
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::dispatch::DEFAULT_MAC;
use v1::helpers::auto_args::Ready;
/// Ethcore implementation.
pub struct EthcoreClient where
C: MiningBlockChainClient,
M: MinerService,
S: SyncProvider,
F: Fetch {
client: Weak,
miner: Weak,
sync: Weak,
net: Weak,
logger: Arc,
settings: Arc,
signer: Option>,
fetch: Mutex,
dapps_port: Option,
}
impl EthcoreClient where
C: MiningBlockChainClient,
M: MinerService,
S: SyncProvider, {
/// Creates new `EthcoreClient` with default `Fetch`.
pub fn new(
client: &Arc,
miner: &Arc,
sync: &Arc,
net: &Arc,
logger: Arc,
settings: Arc,
signer: Option>,
dapps_port: Option,
) -> Self {
Self::with_fetch(client, miner, sync, net, logger, settings, signer, dapps_port)
}
}
impl EthcoreClient where
C: MiningBlockChainClient,
M: MinerService,
S: SyncProvider,
F: Fetch, {
/// Creates new `EthcoreClient` with customizable `Fetch`.
pub fn with_fetch(
client: &Arc,
miner: &Arc,
sync: &Arc,
net: &Arc,
logger: Arc,
settings: Arc,
signer: Option>,
dapps_port: Option,
) -> Self {
EthcoreClient {
client: Arc::downgrade(client),
miner: Arc::downgrade(miner),
sync: Arc::downgrade(sync),
net: Arc::downgrade(net),
logger: logger,
settings: settings,
signer: signer,
fetch: Mutex::new(F::default()),
dapps_port: dapps_port,
}
}
fn active(&self) -> Result<(), Error> {
// TODO: only call every 30s at most.
take_weak!(self.client).keep_alive();
Ok(())
}
}
impl Ethcore for EthcoreClient where
M: MinerService + 'static,
C: MiningBlockChainClient + 'static,
S: SyncProvider + 'static,
F: Fetch + '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