2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-07 22:52:48 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Parity-specific rpc interface for operations altering the settings.
|
2016-12-22 18:26:39 +01:00
|
|
|
use std::io;
|
2017-05-28 14:40:36 +02:00
|
|
|
use std::sync::Arc;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
use ethcore::client::BlockChainClient;
|
2016-06-07 22:52:48 +02:00
|
|
|
use ethcore::miner::MinerService;
|
2016-10-31 16:58:35 +01:00
|
|
|
use ethcore::mode::Mode;
|
2018-04-10 12:13:49 +02:00
|
|
|
use sync::ManageNetwork;
|
2016-12-22 18:26:39 +01:00
|
|
|
use fetch::{self, Fetch};
|
2018-03-14 13:40:54 +01:00
|
|
|
use futures_cpupool::CpuPool;
|
2017-08-31 11:35:41 +02:00
|
|
|
use hash::keccak_buffer;
|
2016-12-12 02:57:19 +01:00
|
|
|
use updater::{Service as UpdateService};
|
2016-11-06 12:51:53 +01:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
use jsonrpc_core::{BoxFuture, Result};
|
2017-10-05 12:35:01 +02:00
|
|
|
use jsonrpc_core::futures::Future;
|
2017-05-24 12:24:07 +02:00
|
|
|
use v1::helpers::dapps::DappsService;
|
2016-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::errors;
|
2016-11-06 12:51:53 +01:00
|
|
|
use v1::traits::ParitySet;
|
2017-05-24 12:24:07 +02:00
|
|
|
use v1::types::{Bytes, H160, H256, U256, ReleaseInfo, Transaction, LocalDapp};
|
2016-06-07 22:52:48 +02:00
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Parity-specific rpc interface for operations altering the settings.
|
2017-03-19 08:46:51 +01:00
|
|
|
pub struct ParitySetClient<C, M, U, F = fetch::Client> {
|
2017-05-28 14:40:36 +02:00
|
|
|
client: Arc<C>,
|
|
|
|
miner: Arc<M>,
|
|
|
|
updater: Arc<U>,
|
|
|
|
net: Arc<ManageNetwork>,
|
2017-05-24 12:24:07 +02:00
|
|
|
dapps: Option<Arc<DappsService>>,
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: F,
|
2018-03-14 13:40:54 +01:00
|
|
|
pool: CpuPool,
|
2017-04-19 14:30:00 +02:00
|
|
|
eip86_transition: u64,
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
impl<C, M, U, F> ParitySetClient<C, M, U, F>
|
2018-04-13 17:34:27 +02:00
|
|
|
where C: BlockChainClient + 'static,
|
2017-04-19 14:30:00 +02:00
|
|
|
{
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Creates new `ParitySetClient` with given `Fetch`.
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn new(
|
|
|
|
client: &Arc<C>,
|
|
|
|
miner: &Arc<M>,
|
|
|
|
updater: &Arc<U>,
|
|
|
|
net: &Arc<ManageNetwork>,
|
|
|
|
dapps: Option<Arc<DappsService>>,
|
|
|
|
fetch: F,
|
2018-03-14 13:40:54 +01:00
|
|
|
pool: CpuPool,
|
2017-05-24 12:24:07 +02:00
|
|
|
) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
ParitySetClient {
|
2017-05-28 14:40:36 +02:00
|
|
|
client: client.clone(),
|
|
|
|
miner: miner.clone(),
|
|
|
|
updater: updater.clone(),
|
|
|
|
net: net.clone(),
|
2017-05-24 12:24:07 +02:00
|
|
|
dapps: dapps,
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: fetch,
|
2018-03-14 13:40:54 +01:00
|
|
|
pool: pool,
|
2017-04-19 14:30:00 +02:00
|
|
|
eip86_transition: client.eip86_transition(),
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 02:57:19 +01:00
|
|
|
impl<C, M, U, F> ParitySet for ParitySetClient<C, M, U, F> where
|
2018-04-13 17:34:27 +02:00
|
|
|
C: BlockChainClient + 'static,
|
2016-11-06 12:51:53 +01:00
|
|
|
M: MinerService + 'static,
|
2016-12-12 02:57:19 +01:00
|
|
|
U: UpdateService + 'static,
|
2016-11-06 12:51:53 +01:00
|
|
|
F: Fetch + 'static,
|
|
|
|
{
|
2016-06-07 22:52:48 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
fn set_min_gas_price(&self, _gas_price: U256) -> Result<bool> {
|
|
|
|
warn!("setMinGasPrice is deprecated. Ignoring request.");
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_transactions_limit(&self, _limit: usize) -> Result<bool> {
|
|
|
|
warn!("setTransactionsLimit is deprecated. Ignoring request.");
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_tx_gas_limit(&self, _limit: U256) -> Result<bool> {
|
|
|
|
warn!("setTxGasLimit is deprecated. Ignoring request.");
|
|
|
|
Ok(false)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn set_gas_floor_target(&self, target: U256) -> Result<bool> {
|
2018-04-13 17:34:27 +02:00
|
|
|
let mut range = self.miner.authoring_params().gas_range_target.clone();
|
|
|
|
range.0 = target.into();
|
|
|
|
self.miner.set_gas_range_target(range);
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-23 14:29:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn set_gas_ceil_target(&self, target: U256) -> Result<bool> {
|
2018-04-13 17:34:27 +02:00
|
|
|
let mut range = self.miner.authoring_params().gas_range_target.clone();
|
|
|
|
range.1 = target.into();
|
|
|
|
self.miner.set_gas_range_target(range);
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn set_extra_data(&self, extra_data: Bytes) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.miner.set_extra_data(extra_data.into_vec());
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
fn set_author(&self, address: H160) -> Result<bool> {
|
|
|
|
self.miner.set_author(address.into(), None).map_err(Into::into).map_err(errors::password)?;
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn set_engine_signer(&self, address: H160, password: String) -> Result<bool> {
|
2018-04-13 17:34:27 +02:00
|
|
|
self.miner.set_author(address.into(), Some(password)).map_err(Into::into).map_err(errors::password)?;
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-27 20:19:01 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn add_reserved_peer(&self, peer: String) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
match self.net.add_reserved_peer(peer) {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(()) => Ok(true),
|
|
|
|
Err(e) => Err(errors::invalid_params("Peer address", e)),
|
|
|
|
}
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn remove_reserved_peer(&self, peer: String) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
match self.net.remove_reserved_peer(peer) {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(()) => Ok(true),
|
|
|
|
Err(e) => Err(errors::invalid_params("Peer address", e)),
|
|
|
|
}
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn drop_non_reserved_peers(&self) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.net.deny_unreserved_peers();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn accept_non_reserved_peers(&self) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.net.accept_unreserved_peers();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
2016-07-11 17:02:42 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn start_network(&self) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.net.start_network();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-07-11 17:02:42 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn stop_network(&self) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.net.stop_network();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-07-11 17:02:42 +02:00
|
|
|
}
|
2016-10-31 16:58:35 +01:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn set_mode(&self, mode: String) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.client.set_mode(match mode.as_str() {
|
2016-11-05 10:38:00 +01:00
|
|
|
"offline" => Mode::Off,
|
2016-10-31 16:58:35 +01:00
|
|
|
"dark" => Mode::Dark(300),
|
|
|
|
"passive" => Mode::Passive(300, 3600),
|
|
|
|
"active" => Mode::Active,
|
|
|
|
e => { return Err(errors::invalid_params("mode", e.to_owned())); },
|
|
|
|
});
|
|
|
|
Ok(true)
|
|
|
|
}
|
2016-11-06 12:51:53 +01:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn set_spec_name(&self, spec_name: String) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
self.client.set_spec_name(spec_name);
|
2017-03-13 12:10:53 +01:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn hash_content(&self, url: String) -> BoxFuture<H256> {
|
2018-04-09 16:14:33 +02:00
|
|
|
let future = self.fetch.get(&url, Default::default()).then(move |result| {
|
2017-01-11 20:02:27 +01:00
|
|
|
result
|
2017-07-04 17:01:06 +02:00
|
|
|
.map_err(errors::fetch)
|
2018-03-14 13:40:54 +01:00
|
|
|
.and_then(move |response| {
|
|
|
|
let mut reader = io::BufReader::new(fetch::BodyReader::new(response));
|
|
|
|
keccak_buffer(&mut reader).map_err(errors::fetch)
|
2017-01-11 20:02:27 +01:00
|
|
|
})
|
|
|
|
.map(Into::into)
|
2018-03-14 13:40:54 +01:00
|
|
|
});
|
|
|
|
Box::new(self.pool.spawn(future))
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
2016-12-12 02:57:19 +01:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn dapps_refresh(&self) -> Result<bool> {
|
2017-08-09 19:06:40 +02:00
|
|
|
self.dapps.as_ref().map(|dapps| dapps.refresh_local_dapps()).ok_or_else(errors::dapps_disabled)
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn dapps_list(&self) -> Result<Vec<LocalDapp>> {
|
2017-05-24 12:24:07 +02:00
|
|
|
self.dapps.as_ref().map(|dapps| dapps.list_dapps()).ok_or_else(errors::dapps_disabled)
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn upgrade_ready(&self) -> Result<Option<ReleaseInfo>> {
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self.updater.upgrade_ready().map(Into::into))
|
2016-12-12 02:57:19 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn execute_upgrade(&self) -> Result<bool> {
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self.updater.execute_upgrade())
|
2016-12-12 02:57:19 +01:00
|
|
|
}
|
2017-03-19 08:46:51 +01:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn remove_transaction(&self, hash: H256) -> Result<Option<Transaction>> {
|
2017-05-28 14:40:36 +02:00
|
|
|
let block_number = self.client.chain_info().best_block_number;
|
2017-03-19 08:46:51 +01:00
|
|
|
let hash = hash.into();
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
Ok(self.miner.remove_transaction(&hash)
|
|
|
|
.map(|t| Transaction::from_pending(t.pending().clone(), block_number + 1, self.eip86_transition))
|
|
|
|
)
|
2017-03-19 08:46:51 +01:00
|
|
|
}
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|