2016-06-07 22:52:48 +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/>.
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Parity-specific rpc interface for operations altering the settings.
|
|
|
|
use std::{fs, io};
|
|
|
|
use std::sync::{Arc, Weak, mpsc};
|
|
|
|
|
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-11-06 12:51:53 +01:00
|
|
|
use fetch::{Client as FetchClient, Fetch};
|
|
|
|
use util::{Mutex, sha3};
|
|
|
|
|
|
|
|
use jsonrpc_core::Error;
|
|
|
|
use v1::helpers::auto_args::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;
|
|
|
|
use v1::types::{Bytes, H160, H256, U256};
|
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.
|
|
|
|
pub struct ParitySetClient<C, M, F=FetchClient> where
|
2016-07-05 17:50:46 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-11-06 12:51:53 +01:00
|
|
|
M: MinerService,
|
|
|
|
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-07-11 17:02:42 +02:00
|
|
|
net: Weak<ManageNetwork>,
|
2016-11-06 12:51:53 +01:00
|
|
|
fetch: Mutex<F>,
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
impl<C, M> ParitySetClient<C, M, FetchClient> where
|
2016-07-05 17:50:46 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-11-06 12:51:53 +01:00
|
|
|
M: MinerService
|
|
|
|
{
|
|
|
|
/// Creates new `ParitySetClient` with default `FetchClient`.
|
2016-07-11 17:02:42 +02:00
|
|
|
pub fn new(client: &Arc<C>, miner: &Arc<M>, net: &Arc<ManageNetwork>) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
Self::with_fetch(client, miner, net)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, M, F> ParitySetClient<C, M, F> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
F: Fetch,
|
|
|
|
{
|
|
|
|
/// Creates new `ParitySetClient` with default `FetchClient`.
|
|
|
|
pub fn with_fetch(client: &Arc<C>, miner: &Arc<M>, net: &Arc<ManageNetwork>) -> Self {
|
|
|
|
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-06-21 13:56:33 +02:00
|
|
|
net: Arc::downgrade(net),
|
2016-11-06 12:51:53 +01:00
|
|
|
fetch: Mutex::new(F::default()),
|
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-11-06 12:51:53 +01:00
|
|
|
impl<C, M, F> ParitySet for ParitySetClient<C, M, F> where
|
2016-07-05 17:50:46 +02:00
|
|
|
C: MiningBlockChainClient + 'static,
|
2016-11-06 12:51:53 +01:00
|
|
|
M: MinerService + 'static,
|
|
|
|
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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-05 18:08:16 +01:00
|
|
|
try!(self.active());
|
2016-12-05 22:31:38 +01:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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-07-05 17:50:46 +02:00
|
|
|
try!(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();
|
|
|
|
|
|
|
|
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(()) => {
|
|
|
|
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(Into::into);
|
|
|
|
|
|
|
|
// Receive ready and invoke with result.
|
|
|
|
let ready: Ready<H256> = rx.recv().expect(
|
|
|
|
"recv() fails when `tx` has been dropped, if this closure is invoked `tx` is not dropped (`res == Ok()`); qed"
|
|
|
|
);
|
|
|
|
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(
|
|
|
|
"send() fails when `rx` end is dropped, if `res == Ok()`: `rx` is moved to the closure; qed"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 22:52:48 +02:00
|
|
|
}
|