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/>.
|
|
|
|
|
|
|
|
/// Ethcore-specific rpc interface for operations altering the settings.
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
use jsonrpc_core::*;
|
|
|
|
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-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::errors;
|
2016-06-07 22:52:48 +02:00
|
|
|
use v1::traits::EthcoreSet;
|
2016-07-06 11:23:29 +02:00
|
|
|
use v1::types::{Bytes, H160, U256};
|
2016-06-07 22:52:48 +02:00
|
|
|
|
|
|
|
/// Ethcore-specific rpc interface for operations altering the settings.
|
2016-07-05 17:50:46 +02:00
|
|
|
pub struct EthcoreSetClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
2016-07-11 17:02:42 +02:00
|
|
|
M: MinerService
|
|
|
|
{
|
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-06-07 22:52:48 +02:00
|
|
|
}
|
|
|
|
|
2016-07-05 17:50:46 +02:00
|
|
|
impl<C, M> EthcoreSetClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService {
|
2016-06-07 22:52:48 +02:00
|
|
|
/// Creates new `EthcoreSetClient`.
|
2016-07-11 17:02:42 +02:00
|
|
|
pub fn new(client: &Arc<C>, miner: &Arc<M>, net: &Arc<ManageNetwork>) -> Self {
|
2016-06-07 22:52:48 +02:00
|
|
|
EthcoreSetClient {
|
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-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-07-05 17:50:46 +02:00
|
|
|
impl<C, M> EthcoreSet for EthcoreSetClient<C, M> where
|
|
|
|
C: MiningBlockChainClient + 'static,
|
|
|
|
M: MinerService + '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-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() {
|
|
|
|
"off" => Mode::Off,
|
|
|
|
"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-06-07 22:52:48 +02:00
|
|
|
}
|