2016-04-11 21:06:32 +02:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Ethcore-specific rpc implementation.
|
2016-09-27 16:27:06 +02:00
|
|
|
use std::{fs, io};
|
|
|
|
use std::sync::{mpsc, Arc, Weak};
|
2016-08-17 19:25:02 +02:00
|
|
|
use std::str::FromStr;
|
2016-06-16 12:44:08 +02:00
|
|
|
use std::collections::{BTreeMap};
|
2016-09-27 16:27:06 +02:00
|
|
|
use util::{RotatingLogger, Address, Mutex, sha3};
|
2016-08-17 19:25:02 +02:00
|
|
|
use util::misc::version_data;
|
2016-08-11 17:19:20 +02:00
|
|
|
|
2016-09-22 14:48:22 +02:00
|
|
|
use crypto::ecies;
|
2016-09-27 16:27:06 +02:00
|
|
|
use fetch::{Client as FetchClient, Fetch};
|
2016-08-24 18:35:21 +02:00
|
|
|
use ethkey::{Brain, Generator};
|
2016-08-10 17:57:40 +02:00
|
|
|
use ethstore::random_phrase;
|
2016-08-11 17:19:20 +02:00
|
|
|
use ethsync::{SyncProvider, ManageNetwork};
|
|
|
|
use ethcore::miner::MinerService;
|
2016-06-16 12:44:08 +02:00
|
|
|
use ethcore::client::{MiningBlockChainClient};
|
2016-10-03 11:13:10 +02:00
|
|
|
use ethcore::ids::BlockID;
|
2016-08-11 17:19:20 +02:00
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
use jsonrpc_core::{from_params, to_value, Value, Error, Params, Ready};
|
2016-04-11 21:06:32 +02:00
|
|
|
use v1::traits::Ethcore;
|
2016-09-27 16:27:06 +02:00
|
|
|
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction};
|
2016-09-21 12:44:49 +02:00
|
|
|
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
|
2016-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::params::expect_no_params;
|
2016-04-11 21:06:32 +02:00
|
|
|
|
|
|
|
/// Ethcore implementation.
|
2016-09-27 16:27:06 +02:00
|
|
|
pub struct EthcoreClient<C, M, S: ?Sized, F=FetchClient> where
|
2016-06-16 12:44:08 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-08-11 17:19:20 +02:00
|
|
|
M: MinerService,
|
2016-09-27 16:27:06 +02:00
|
|
|
S: SyncProvider,
|
|
|
|
F: Fetch {
|
2016-05-28 16:52:33 +02:00
|
|
|
|
2016-06-16 12:44:08 +02:00
|
|
|
client: Weak<C>,
|
2016-04-11 21:06:32 +02:00
|
|
|
miner: Weak<M>,
|
2016-08-11 17:19:20 +02:00
|
|
|
sync: Weak<S>,
|
|
|
|
net: Weak<ManageNetwork>,
|
2016-04-19 09:58:26 +02:00
|
|
|
logger: Arc<RotatingLogger>,
|
2016-04-21 19:19:42 +02:00
|
|
|
settings: Arc<NetworkSettings>,
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Option<Arc<SignerService>>,
|
2016-09-27 16:27:06 +02:00
|
|
|
fetch: Mutex<F>
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
impl<C, M, S: ?Sized> EthcoreClient<C, M, S> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
S: SyncProvider, {
|
|
|
|
/// Creates new `EthcoreClient` with default `Fetch`.
|
2016-08-11 17:19:20 +02:00
|
|
|
pub fn new(
|
|
|
|
client: &Arc<C>,
|
|
|
|
miner: &Arc<M>,
|
|
|
|
sync: &Arc<S>,
|
|
|
|
net: &Arc<ManageNetwork>,
|
|
|
|
logger: Arc<RotatingLogger>,
|
|
|
|
settings: Arc<NetworkSettings>,
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Option<Arc<SignerService>>
|
2016-08-17 19:25:02 +02:00
|
|
|
) -> Self {
|
2016-09-27 16:27:06 +02:00
|
|
|
Self::with_fetch(client, miner, sync, net, logger, settings, signer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, M, S: ?Sized, F> EthcoreClient<C, M, S, F> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
S: SyncProvider,
|
|
|
|
F: Fetch, {
|
|
|
|
|
|
|
|
/// Creates new `EthcoreClient` with customizable `Fetch`.
|
|
|
|
pub fn with_fetch(
|
|
|
|
client: &Arc<C>,
|
|
|
|
miner: &Arc<M>,
|
|
|
|
sync: &Arc<S>,
|
|
|
|
net: &Arc<ManageNetwork>,
|
|
|
|
logger: Arc<RotatingLogger>,
|
|
|
|
settings: Arc<NetworkSettings>,
|
|
|
|
signer: Option<Arc<SignerService>>
|
|
|
|
) -> Self {
|
2016-04-11 21:06:32 +02:00
|
|
|
EthcoreClient {
|
2016-06-16 12:44:08 +02:00
|
|
|
client: Arc::downgrade(client),
|
2016-04-19 09:58:26 +02:00
|
|
|
miner: Arc::downgrade(miner),
|
2016-08-11 17:19:20 +02:00
|
|
|
sync: Arc::downgrade(sync),
|
|
|
|
net: Arc::downgrade(net),
|
2016-04-19 09:58:26 +02:00
|
|
|
logger: logger,
|
2016-04-21 19:19:42 +02:00
|
|
|
settings: settings,
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: signer,
|
2016-09-27 16:27:06 +02:00
|
|
|
fetch: Mutex::new(F::default()),
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:50:46 +02:00
|
|
|
|
|
|
|
fn active(&self) -> Result<(), Error> {
|
|
|
|
// TODO: only call every 30s at most.
|
|
|
|
take_weak!(self.client).keep_alive();
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
|
|
|
|
M: MinerService + 'static,
|
|
|
|
C: MiningBlockChainClient + 'static,
|
|
|
|
S: SyncProvider + 'static,
|
|
|
|
F: Fetch + 'static {
|
2016-04-13 00:04:40 +02:00
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn transactions_limit(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&take_weak!(self.miner).transactions_limit()))
|
2016-04-18 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn min_gas_price(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&U256::from(take_weak!(self.miner).minimal_gas_price())))
|
2016-04-13 00:04:40 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn extra_data(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&Bytes::new(take_weak!(self.miner).extra_data())))
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn gas_floor_target(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&U256::from(take_weak!(self.miner).gas_floor_target())))
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
2016-04-19 09:58:26 +02:00
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn gas_ceil_target(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&U256::from(take_weak!(self.miner).gas_ceil_target())))
|
2016-06-23 14:29:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn dev_logs(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-04-19 09:58:26 +02:00
|
|
|
let logs = self.logger.logs();
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&logs.as_slice()))
|
2016-04-19 09:58:26 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn dev_logs_levels(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&self.logger.levels()))
|
2016-04-19 09:58:26 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn net_chain(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&self.settings.chain))
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 17:19:20 +02:00
|
|
|
fn net_peers(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-08-11 17:19:20 +02:00
|
|
|
|
|
|
|
let sync_status = take_weak!(self.sync).status();
|
|
|
|
let net_config = take_weak!(self.net).network_config();
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&Peers {
|
2016-08-11 17:19:20 +02:00
|
|
|
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),
|
2016-09-01 12:00:00 +02:00
|
|
|
}))
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn net_port(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&self.settings.network_port))
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn node_name(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&self.settings.name))
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 19:25:02 +02:00
|
|
|
fn registry_address(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
try!(expect_no_params(params));
|
|
|
|
let r = take_weak!(self.client)
|
|
|
|
.additional_params()
|
|
|
|
.get("registrar")
|
|
|
|
.and_then(|s| Address::from_str(s).ok())
|
|
|
|
.map(|s| H160::from(s));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&r))
|
2016-08-17 19:25:02 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn rpc_settings(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-04-21 19:19:42 +02:00
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("enabled".to_owned(), Value::Bool(self.settings.rpc_enabled));
|
|
|
|
map.insert("interface".to_owned(), Value::String(self.settings.rpc_interface.clone()));
|
|
|
|
map.insert("port".to_owned(), Value::U64(self.settings.rpc_port as u64));
|
|
|
|
Ok(Value::Object(map))
|
|
|
|
}
|
2016-05-02 16:12:01 +02:00
|
|
|
|
2016-06-16 12:44:08 +02:00
|
|
|
fn default_extra_data(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&Bytes::new(version_data())))
|
2016-06-16 12:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gas_price_statistics(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
|
|
|
|
|
|
|
match take_weak!(self.client).gas_price_statistics(100, 8) {
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(stats) => Ok(to_value(&stats
|
2016-08-08 17:25:15 +02:00
|
|
|
.into_iter()
|
2016-09-01 12:00:00 +02:00
|
|
|
.map(|x| to_value(&U256::from(x)))
|
|
|
|
.collect::<Vec<_>>())),
|
2016-08-08 17:25:15 +02:00
|
|
|
_ => Err(Error::internal_error()),
|
2016-06-16 12:44:08 +02:00
|
|
|
}
|
2016-05-02 16:12:01 +02:00
|
|
|
}
|
2016-06-21 14:55:25 +02:00
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
fn unsigned_transactions_count(&self, params: Params) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
try!(expect_no_params(params));
|
|
|
|
|
2016-09-21 12:44:49 +02:00
|
|
|
match self.signer {
|
2016-08-08 17:25:15 +02:00
|
|
|
None => Err(errors::signer_disabled()),
|
2016-09-21 12:44:49 +02:00
|
|
|
Some(ref signer) => Ok(to_value(&signer.len())),
|
2016-06-21 14:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-10 17:57:40 +02:00
|
|
|
|
|
|
|
fn generate_secret_phrase(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
try!(expect_no_params(params));
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(to_value(&random_phrase(12)))
|
2016-08-10 17:57:40 +02:00
|
|
|
}
|
2016-08-10 21:23:17 +02:00
|
|
|
|
|
|
|
fn phrase_to_address(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
2016-09-01 12:00:00 +02:00
|
|
|
from_params::<(String,)>(params).map(|(phrase,)|
|
2016-08-24 18:35:21 +02:00
|
|
|
to_value(&H160::from(Brain::new(phrase).generate().unwrap().address()))
|
2016-08-10 21:23:17 +02:00
|
|
|
)
|
|
|
|
}
|
2016-09-22 14:48:22 +02:00
|
|
|
|
2016-10-03 11:13:10 +02:00
|
|
|
fn list_accounts(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
try!(expect_no_params(params));
|
|
|
|
|
|
|
|
take_weak!(self.client)
|
|
|
|
.list_accounts(BlockID::Latest)
|
|
|
|
.map(|a| Ok(to_value(&a.into_iter().map(Into::into).collect::<Vec<H160>>())))
|
|
|
|
.unwrap_or(Ok(Value::Null))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list_storage_keys(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
|
|
|
|
from_params::<(H160,)>(params).and_then(|(_addr,)|
|
|
|
|
Ok(Value::Null)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-22 14:48:22 +02:00
|
|
|
fn encrypt_message(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
from_params::<(H512, Bytes)>(params).and_then(|(key, phrase)| {
|
|
|
|
let s = try!(ecies::encrypt(&key.into(), &[0; 0], &phrase.0).map_err(|_| Error::internal_error()));
|
|
|
|
Ok(to_value(&Bytes::from(s)))
|
|
|
|
})
|
|
|
|
}
|
2016-09-23 18:13:03 +02:00
|
|
|
|
|
|
|
fn pending_transactions(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
try!(expect_no_params(params));
|
|
|
|
|
2016-09-26 12:24:52 +02:00
|
|
|
Ok(to_value(&take_weak!(self.miner).all_transactions().into_iter().map(Into::into).collect::<Vec<Transaction>>()))
|
2016-09-23 18:13:03 +02:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
|
|
|
fn hash_content(&self, params: Params, ready: Ready) {
|
|
|
|
let res = self.active().and_then(|_| from_params::<(String,)>(params));
|
|
|
|
|
|
|
|
let hash_content = |result| {
|
|
|
|
let path = try!(result);
|
|
|
|
let mut file = io::BufReader::new(try!(fs::File::open(&path)));
|
|
|
|
// Try to hash
|
|
|
|
let result = sha3(&mut file);
|
|
|
|
// Remove file (always)
|
|
|
|
try!(fs::remove_file(&path));
|
|
|
|
// Return the result
|
|
|
|
Ok(try!(result))
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(e) => ready.ready(Err(e)),
|
|
|
|
Ok((url, )) => {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let res = self.fetch.lock().request_async(&url, Default::default(), Box::new(move |result| {
|
|
|
|
let result = hash_content(result)
|
|
|
|
.map_err(errors::from_fetch_error)
|
|
|
|
.map(|hash| to_value(H256::from(hash)));
|
|
|
|
|
|
|
|
// Receive ready and invoke with result.
|
|
|
|
let ready: Ready = rx.try_recv().expect("When on_done is invoked ready object is always sent.");
|
|
|
|
ready.ready(result);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Either invoke ready right away or transfer it to the closure.
|
|
|
|
if let Err(e) = res {
|
|
|
|
ready.ready(Err(errors::from_fetch_error(e)));
|
|
|
|
} else {
|
|
|
|
tx.send(ready).expect("Rx end is sent to on_done closure.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|