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;
|
|
|
|
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};
|
2017-02-04 22:18:19 +01:00
|
|
|
use futures::{BoxFuture, Future};
|
2016-12-22 18:26:39 +01:00
|
|
|
use util::sha3;
|
2016-12-12 02:57:19 +01:00
|
|
|
use updater::{Service as UpdateService};
|
2016-11-06 12:51:53 +01:00
|
|
|
|
|
|
|
use jsonrpc_core::Error;
|
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-03-19 08:46:51 +01:00
|
|
|
use v1::types::{Bytes, H160, H256, U256, ReleaseInfo, Transaction};
|
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> {
|
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,
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 08:46:51 +01:00
|
|
|
impl<C, M, U, F> ParitySetClient<C, M, U, F> {
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Creates new `ParitySetClient` with given `Fetch`.
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn new(client: &Arc<C>, miner: &Arc<M>, updater: &Arc<U>, net: &Arc<ManageNetwork>, fetch: F) -> 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,
|
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> {
|
|
|
|
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> {
|
|
|
|
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> {
|
|
|
|
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> {
|
2017-01-13 09:51:36 +01:00
|
|
|
take_weak!(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
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn set_author(&self, author: H160) -> Result<bool, Error> {
|
|
|
|
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
|
|
|
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> {
|
|
|
|
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> {
|
|
|
|
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> {
|
|
|
|
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> {
|
|
|
|
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-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-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
|
|
|
|
2017-03-13 12:10:53 +01:00
|
|
|
fn set_spec_name(&self, spec_name: String) -> Result<bool, Error> {
|
|
|
|
take_weak!(self.client).set_spec_name(spec_name);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
fn hash_content(&self, url: String) -> BoxFuture<H256, Error> {
|
|
|
|
self.fetch.process(self.fetch.fetch(&url).then(move |result| {
|
|
|
|
result
|
|
|
|
.map_err(errors::from_fetch_error)
|
|
|
|
.and_then(|response| {
|
|
|
|
sha3(&mut io::BufReader::new(response)).map_err(errors::from_fetch_error)
|
|
|
|
})
|
|
|
|
.map(Into::into)
|
|
|
|
}))
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
2016-12-12 02:57:19 +01:00
|
|
|
|
|
|
|
fn upgrade_ready(&self) -> Result<Option<ReleaseInfo>, Error> {
|
|
|
|
let updater = take_weak!(self.updater);
|
|
|
|
Ok(updater.upgrade_ready().map(Into::into))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute_upgrade(&self) -> Result<bool, Error> {
|
|
|
|
let updater = take_weak!(self.updater);
|
|
|
|
Ok(updater.execute_upgrade())
|
|
|
|
}
|
2017-03-19 08:46:51 +01:00
|
|
|
|
|
|
|
fn remove_transaction(&self, hash: H256) -> Result<Option<Transaction>, Error> {
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let hash = hash.into();
|
|
|
|
|
|
|
|
Ok(miner.remove_pending_transaction(&*client, &hash).map(Into::into))
|
|
|
|
}
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|