2016-12-11 19:14:42 +01:00
|
|
|
// Copyright 2015, 2016 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;
|
|
|
|
use std::sync::{Arc, Weak};
|
2016-11-06 12:51:53 +01:00
|
|
|
|
2016-06-07 22:52:48 +02:00
|
|
|
use ethcore::miner::MinerService;
|
2016-07-05 17:50:46 +02:00
|
|
|
use ethcore::client::MiningBlockChainClient;
|
2016-10-31 16:58:35 +01:00
|
|
|
use ethcore::mode::Mode;
|
2016-07-11 17:02:42 +02:00
|
|
|
use ethsync::ManageNetwork;
|
2016-12-22 18:26:39 +01:00
|
|
|
use fetch::{self, Fetch};
|
|
|
|
use futures::Future;
|
|
|
|
use util::sha3;
|
2016-12-12 02:57:19 +01:00
|
|
|
use updater::{Service as UpdateService};
|
2016-12-22 18:26:39 +01:00
|
|
|
use parity_reactor::Remote;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
|
|
|
use jsonrpc_core::Error;
|
2016-12-13 14:27:27 +01:00
|
|
|
use jsonrpc_macros::Ready;
|
2016-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::errors;
|
2016-11-06 12:51:53 +01:00
|
|
|
use v1::traits::ParitySet;
|
2016-12-12 02:57:19 +01:00
|
|
|
use v1::types::{Bytes, H160, H256, U256, ReleaseInfo};
|
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.
|
2016-12-22 18:26:39 +01:00
|
|
|
pub struct ParitySetClient<C, M, U, F=fetch::Client> where
|
2016-07-05 17:50:46 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-11-06 12:51:53 +01:00
|
|
|
M: MinerService,
|
2016-12-12 02:57:19 +01:00
|
|
|
U: UpdateService,
|
2016-11-06 12:51:53 +01:00
|
|
|
F: Fetch,
|
2016-07-11 17:02:42 +02:00
|
|
|
{
|
2016-07-05 17:50:46 +02:00
|
|
|
client: Weak<C>,
|
2016-06-07 22:52:48 +02:00
|
|
|
miner: Weak<M>,
|
2016-12-12 02:57:19 +01:00
|
|
|
updater: Weak<U>,
|
2016-07-11 17:02:42 +02:00
|
|
|
net: Weak<ManageNetwork>,
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: F,
|
|
|
|
remote: Remote,
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 02:57:19 +01:00
|
|
|
impl<C, M, U, F> ParitySetClient<C, M, U, F> where
|
2016-11-06 12:51:53 +01:00
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
2016-12-12 02:57:19 +01:00
|
|
|
U: UpdateService,
|
2016-11-06 12:51:53 +01:00
|
|
|
F: Fetch,
|
|
|
|
{
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Creates new `ParitySetClient` with given `Fetch`.
|
|
|
|
pub fn new(client: &Arc<C>, miner: &Arc<M>, updater: &Arc<U>, net: &Arc<ManageNetwork>, fetch: F, remote: Remote) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
ParitySetClient {
|
2016-07-05 17:50:46 +02:00
|
|
|
client: Arc::downgrade(client),
|
2016-06-07 22:52:48 +02:00
|
|
|
miner: Arc::downgrade(miner),
|
2016-12-12 02:57:19 +01:00
|
|
|
updater: Arc::downgrade(updater),
|
2016-06-21 13:56:33 +02:00
|
|
|
net: Arc::downgrade(net),
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: fetch,
|
|
|
|
remote: remote,
|
2016-06-07 22:52:48 +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-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
|
2016-07-05 17:50:46 +02:00
|
|
|
C: MiningBlockChainClient + '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
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_min_gas_price(&self, gas_price: U256) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_minimal_gas_price(gas_price.into());
|
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_gas_floor_target(&self, target: U256) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_gas_floor_target(target.into());
|
|
|
|
Ok(true)
|
2016-06-23 14:29:16 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_gas_ceil_target(&self, target: U256) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_gas_ceil_target(target.into());
|
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_extra_data(&self, extra_data: Bytes) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_extra_data(extra_data.to_vec());
|
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_author(&self, author: H160) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_author(author.into());
|
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 22:31:38 +01:00
|
|
|
fn set_engine_signer(&self, address: H160, password: String) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
|
|
|
take_weak!(self.miner).set_engine_signer(address.into(), password).map_err(Into::into).map_err(errors::from_password_error)?;
|
2016-12-05 18:08:16 +01:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_transactions_limit(&self, limit: usize) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_transactions_limit(limit);
|
|
|
|
Ok(true)
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
2016-06-21 13:56:33 +02:00
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_tx_gas_limit(&self, limit: U256) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
take_weak!(self.miner).set_tx_gas_limit(limit.into());
|
|
|
|
Ok(true)
|
2016-06-27 20:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn add_reserved_peer(&self, peer: String) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
match take_weak!(self.net).add_reserved_peer(peer) {
|
|
|
|
Ok(()) => Ok(true),
|
|
|
|
Err(e) => Err(errors::invalid_params("Peer address", e)),
|
|
|
|
}
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn remove_reserved_peer(&self, peer: String) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
|
|
|
match take_weak!(self.net).remove_reserved_peer(peer) {
|
|
|
|
Ok(()) => Ok(true),
|
|
|
|
Err(e) => Err(errors::invalid_params("Peer address", e)),
|
|
|
|
}
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn drop_non_reserved_peers(&self) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
2016-07-14 12:07:33 +02:00
|
|
|
take_weak!(self.net).deny_unreserved_peers();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-06-21 13:56:33 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn accept_non_reserved_peers(&self) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-10-04 19:05:46 +02:00
|
|
|
|
2016-07-14 12:07:33 +02:00
|
|
|
take_weak!(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
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn start_network(&self) -> Result<bool, Error> {
|
2016-07-11 17:02:42 +02:00
|
|
|
take_weak!(self.net).start_network();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(true)
|
2016-07-11 17:02:42 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn stop_network(&self) -> Result<bool, Error> {
|
2016-07-11 17:02:42 +02:00
|
|
|
take_weak!(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
|
|
|
|
|
|
|
fn set_mode(&self, mode: String) -> Result<bool, Error> {
|
|
|
|
take_weak!(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
|
|
|
|
|
|
|
fn hash_content(&self, ready: Ready<H256>, url: String) {
|
|
|
|
let res = self.active();
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(e) => ready.ready(Err(e)),
|
|
|
|
Ok(()) => {
|
2016-12-22 18:26:39 +01:00
|
|
|
let task = self.fetch.fetch(&url).then(move |result| {
|
|
|
|
let result = result
|
2016-11-06 12:51:53 +01:00
|
|
|
.map_err(errors::from_fetch_error)
|
2016-12-22 18:26:39 +01:00
|
|
|
.and_then(|response| {
|
|
|
|
sha3(&mut io::BufReader::new(response)).map_err(errors::from_fetch_error)
|
|
|
|
})
|
2016-11-06 12:51:53 +01:00
|
|
|
.map(Into::into);
|
|
|
|
|
|
|
|
// Receive ready and invoke with result.
|
|
|
|
ready.ready(result);
|
2016-12-22 18:26:39 +01:00
|
|
|
Ok(()) as Result<(), ()>
|
|
|
|
});
|
|
|
|
|
|
|
|
self.remote.spawn(task);
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 02:57:19 +01:00
|
|
|
|
|
|
|
fn upgrade_ready(&self) -> Result<Option<ReleaseInfo>, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-12-12 02:57:19 +01:00
|
|
|
let updater = take_weak!(self.updater);
|
|
|
|
Ok(updater.upgrade_ready().map(Into::into))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute_upgrade(&self) -> Result<bool, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-12-12 02:57:19 +01:00
|
|
|
let updater = take_weak!(self.updater);
|
|
|
|
Ok(updater.execute_upgrade())
|
|
|
|
}
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|