Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Reserved peers rpc API (#1360)
Browse files Browse the repository at this point in the history
* reserved peers: lock them, use a hashset, and provide to networkcontext

* adding and removing reserved peer service API

* add NonReservedPeerMode, and setters in host

* setting non reserved mode, restriction accepted connections

* implement RPC apis

* fix deadlock

* fix rpc tests
  • Loading branch information
rphmeier authored and gavofyork committed Jun 21, 2016
1 parent e0b4eab commit 951512f
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 65 deletions.
5 changes: 4 additions & 1 deletion parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ impl Configuration {
net_path.push("network");
ret.config_path = Some(net_path.to_str().unwrap().to_owned());
ret.reserved_nodes = self.init_reserved_nodes();
ret.reserved_only = self.args.flag_reserved_only;

if self.args.flag_reserved_only {
ret.non_reserved_mode = ::util::network::NonReservedPeerMode::Deny;
}
ret
}

Expand Down
5 changes: 3 additions & 2 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
logger: logger.clone(),
settings: network_settings.clone(),
allow_pending_receipt_query: !conf.args.flag_geth,
net_service: service.network(),
});

let dependencies = rpc::Dependencies {
Expand Down Expand Up @@ -315,11 +316,11 @@ fn execute_export(conf: Configuration) {
udp_port: None,
nat_enabled: false,
discovery_enabled: false,
reserved_only: true,
boot_nodes: Vec::new(),
use_secret: None,
ideal_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: ::util::network::NonReservedPeerMode::Accept,
};
let client_config = conf.client_config(&spec);

Expand Down Expand Up @@ -387,11 +388,11 @@ fn execute_import(conf: Configuration) {
udp_port: None,
nat_enabled: false,
discovery_enabled: false,
reserved_only: true,
boot_nodes: Vec::new(),
use_secret: None,
ideal_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: ::util::network::NonReservedPeerMode::Accept,
};
let client_config = conf.client_config(&spec);

Expand Down
4 changes: 3 additions & 1 deletion parity/rpc_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use ethcore::client::Client;
use util::RotatingLogger;
use ethcore::account_provider::AccountProvider;
use util::network_settings::NetworkSettings;
use util::network::NetworkService;

#[cfg(feature="rpc")]
pub use ethcore_rpc::ConfirmationsQueue;
Expand Down Expand Up @@ -89,6 +90,7 @@ pub struct Dependencies {
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
pub allow_pending_receipt_query: bool,
pub net_service: Arc<NetworkService<::ethcore::service::SyncMessage>>,
}

fn to_modules(apis: &[Api]) -> BTreeMap<String, String> {
Expand Down Expand Up @@ -163,7 +165,7 @@ pub fn setup_rpc<T: Extendable>(server: T, deps: Arc<Dependencies>, apis: ApiSet
server.add_delegate(EthcoreClient::new(&deps.client, &deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate())
},
Api::EthcoreSet => {
server.add_delegate(EthcoreSetClient::new(&deps.miner).to_delegate())
server.add_delegate(EthcoreSetClient::new(&deps.miner, &deps.net_service).to_delegate())
},
Api::Traces => {
server.add_delegate(TracesClient::new(&deps.client, &deps.miner).to_delegate())
Expand Down
34 changes: 33 additions & 1 deletion rpc/src/v1/impls/ethcore_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

/// Ethcore-specific rpc interface for operations altering the settings.
use util::{U256, Address};
use util::network::{NetworkService, NonReservedPeerMode};
use std::sync::{Arc, Weak};
use jsonrpc_core::*;
use ethcore::miner::MinerService;
use ethcore::service::SyncMessage;
use v1::traits::EthcoreSet;
use v1::types::{Bytes};

Expand All @@ -27,13 +29,15 @@ pub struct EthcoreSetClient<M> where
M: MinerService {

miner: Weak<M>,
net: Weak<NetworkService<SyncMessage>>,
}

impl<M> EthcoreSetClient<M> where M: MinerService {
/// Creates new `EthcoreSetClient`.
pub fn new(miner: &Arc<M>) -> Self {
pub fn new(miner: &Arc<M>, net: &Arc<NetworkService<SyncMessage>>) -> Self {
EthcoreSetClient {
miner: Arc::downgrade(miner),
net: Arc::downgrade(net),
}
}
}
Expand Down Expand Up @@ -74,4 +78,32 @@ impl<M> EthcoreSet for EthcoreSetClient<M> where M: MinerService + 'static {
to_value(&true)
})
}

fn add_reserved_peer(&self, params: Params) -> Result<Value, Error> {
from_params::<(String,)>(params).and_then(|(peer,)| {
match take_weak!(self.net).add_reserved_peer(&peer) {
Ok(()) => to_value(&true),
Err(_) => Err(Error::invalid_params()),
}
})
}

fn remove_reserved_peer(&self, params: Params) -> Result<Value, Error> {
from_params::<(String,)>(params).and_then(|(peer,)| {
match take_weak!(self.net).remove_reserved_peer(&peer) {
Ok(()) => to_value(&true),
Err(_) => Err(Error::invalid_params()),
}
})
}

fn drop_non_reserved_peers(&self, _: Params) -> Result<Value, Error> {
take_weak!(self.net).set_non_reserved_mode(NonReservedPeerMode::Deny);
to_value(&true)
}

fn accept_non_reserved_peers(&self, _: Params) -> Result<Value, Error> {
take_weak!(self.net).set_non_reserved_mode(NonReservedPeerMode::Accept);
to_value(&true)
}
}
61 changes: 42 additions & 19 deletions rpc/src/v1/tests/mocked/ethcore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ use std::str::FromStr;
use jsonrpc_core::IoHandler;
use v1::{Ethcore, EthcoreClient, EthcoreSet, EthcoreSetClient};
use ethcore::miner::MinerService;
use ethcore::service::SyncMessage;
use v1::tests::helpers::TestMinerService;
use ethcore::client::{TestBlockChainClient};
use util::numbers::*;
use rustc_serialize::hex::FromHex;
use util::log::RotatingLogger;
use util::network::{NetworkConfiguration, NetworkService};
use util::network_settings::NetworkSettings;

fn miner_service() -> Arc<TestMinerService> {
Expand All @@ -50,21 +52,26 @@ fn settings() -> Arc<NetworkSettings> {
})
}

fn network_service() -> Arc<NetworkService<SyncMessage>> {
Arc::new(NetworkService::new(NetworkConfiguration::new()).unwrap())
}

fn ethcore_client(client: &Arc<TestBlockChainClient>, miner: &Arc<TestMinerService>) -> EthcoreClient<TestBlockChainClient, TestMinerService> {
EthcoreClient::new(client, miner, logger(), settings())
}

fn ethcore_set_client(miner: &Arc<TestMinerService>) -> EthcoreSetClient<TestMinerService> {
EthcoreSetClient::new(miner)
fn ethcore_set_client(miner: &Arc<TestMinerService>, net: &Arc<NetworkService<SyncMessage>>) -> EthcoreSetClient<TestMinerService> {
EthcoreSetClient::new(miner, net)
}

#[test]
fn rpc_ethcore_extra_data() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_extraData", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x01020304","id":1}"#;
Expand All @@ -79,9 +86,10 @@ fn rpc_ethcore_default_extra_data() {

let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_defaultExtraData", "params": [], "id": 1}"#;
let response = format!(r#"{{"jsonrpc":"2.0","result":"0x{}","id":1}}"#, misc::version_data().to_hex());
Expand All @@ -93,9 +101,10 @@ fn rpc_ethcore_default_extra_data() {
fn rpc_ethcore_gas_floor_target() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_gasFloorTarget", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x3039","id":1}"#;
Expand All @@ -107,9 +116,10 @@ fn rpc_ethcore_gas_floor_target() {
fn rpc_ethcore_min_gas_price() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_minGasPrice", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x01312d00","id":1}"#;
Expand All @@ -121,9 +131,10 @@ fn rpc_ethcore_min_gas_price() {
fn rpc_ethcore_set_min_gas_price() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_setMinGasPrice", "params":["0xcd1722f3947def4cf144679da39c4c32bdc35681"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
Expand All @@ -136,9 +147,10 @@ fn rpc_ethcore_set_min_gas_price() {
fn rpc_ethcore_set_gas_floor_target() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_setGasFloorTarget", "params":["0xcd1722f3947def4cf144679da39c4c32bdc35681"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
Expand All @@ -151,9 +163,10 @@ fn rpc_ethcore_set_gas_floor_target() {
fn rpc_ethcore_set_extra_data() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_setExtraData", "params":["0xcd1722f3947def4cf144679da39c4c32bdc35681"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
Expand All @@ -166,9 +179,10 @@ fn rpc_ethcore_set_extra_data() {
fn rpc_ethcore_set_author() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_setAuthor", "params":["0xcd1722f3947def4cf144679da39c4c32bdc35681"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
Expand All @@ -181,13 +195,14 @@ fn rpc_ethcore_set_author() {
fn rpc_ethcore_dev_logs() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let logger = logger();
logger.append("a".to_owned());
logger.append("b".to_owned());
let ethcore = EthcoreClient::new(&client, &miner, logger.clone(), settings()).to_delegate();
let io = IoHandler::new();
io.add_delegate(ethcore);
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_devLogs", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":["b","a"],"id":1}"#;
Expand All @@ -199,9 +214,10 @@ fn rpc_ethcore_dev_logs() {
fn rpc_ethcore_dev_logs_levels() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_devLogsLevels", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"rpc=trace","id":1}"#;
Expand All @@ -212,9 +228,10 @@ fn rpc_ethcore_dev_logs_levels() {
fn rpc_ethcore_set_transactions_limit() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_setTransactionsLimit", "params":[10240240], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
Expand All @@ -227,9 +244,10 @@ fn rpc_ethcore_set_transactions_limit() {
fn rpc_ethcore_transactions_limit() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_transactionsLimit", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":1024,"id":1}"#;
Expand All @@ -241,9 +259,10 @@ fn rpc_ethcore_transactions_limit() {
fn rpc_ethcore_net_chain() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netChain", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"testchain","id":1}"#;
Expand All @@ -255,9 +274,10 @@ fn rpc_ethcore_net_chain() {
fn rpc_ethcore_net_max_peers() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netMaxPeers", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":25,"id":1}"#;
Expand All @@ -269,9 +289,10 @@ fn rpc_ethcore_net_max_peers() {
fn rpc_ethcore_net_port() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netPort", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":30303,"id":1}"#;
Expand All @@ -283,9 +304,10 @@ fn rpc_ethcore_net_port() {
fn rpc_ethcore_rpc_settings() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_rpcSettings", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"enabled":true,"interface":"all","port":8545},"id":1}"#;
Expand All @@ -297,9 +319,10 @@ fn rpc_ethcore_rpc_settings() {
fn rpc_ethcore_node_name() {
let miner = miner_service();
let client = client_service();
let network = network_service();
let io = IoHandler::new();
io.add_delegate(ethcore_client(&client, &miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner).to_delegate());
io.add_delegate(ethcore_set_client(&miner, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "ethcore_nodeName", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"mynode","id":1}"#;
Expand Down
Loading

0 comments on commit 951512f

Please sign in to comment.