From 7a1753946053130023151c71c79d07d33da3ad6f Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Wed, 8 Aug 2018 18:17:59 +0200 Subject: [PATCH 1/3] fix(light_sync): calculate `load_share` properly --- ethcore/sync/src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethcore/sync/src/api.rs b/ethcore/sync/src/api.rs index ef54a4802d9..f841750cbf7 100644 --- a/ethcore/sync/src/api.rs +++ b/ethcore/sync/src/api.rs @@ -283,7 +283,7 @@ impl EthSync { sample_store: sample_store, }; - let max_peers = ::std::cmp::min(params.network_config.max_peers, 1); + let max_peers = ::std::cmp::max(params.network_config.max_peers, 1); light_params.config.load_share = MAX_LIGHTSERV_LOAD / max_peers as f64; let mut light_proto = LightProtocol::new(params.provider, light_params); From 388ca6714cde5d3770e1c699f17b50bafbfc1d2b Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Wed, 8 Aug 2018 19:01:41 +0200 Subject: [PATCH 2/3] refactor(api.rs): extract `light_params` fn, add test --- Cargo.lock | 1 + ethcore/sync/Cargo.toml | 1 + ethcore/sync/src/api.rs | 71 +++++++++++++++++++++++++++++++---------- ethcore/sync/src/lib.rs | 1 + 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a933343a729..f6f99660690 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -838,6 +838,7 @@ dependencies = [ name = "ethcore-sync" version = "1.12.0" dependencies = [ + "common-types 0.1.0", "env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore 1.12.0", "ethcore-io 1.12.0", diff --git a/ethcore/sync/Cargo.toml b/ethcore/sync/Cargo.toml index 6cc87df31f2..7935eeca4b2 100644 --- a/ethcore/sync/Cargo.toml +++ b/ethcore/sync/Cargo.toml @@ -8,6 +8,7 @@ authors = ["Parity Technologies "] [lib] [dependencies] +common-types = { path = "../types" } parity-bytes = { git = "https://github.com/paritytech/parity-common" } ethcore-network = { path = "../../util/network" } ethcore-network-devp2p = { path = "../../util/network-devp2p" } diff --git a/ethcore/sync/src/api.rs b/ethcore/sync/src/api.rs index f841750cbf7..448e2fbf0b1 100644 --- a/ethcore/sync/src/api.rs +++ b/ethcore/sync/src/api.rs @@ -24,6 +24,8 @@ use devp2p::NetworkService; use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId, NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ErrorKind, ConnectionFilter}; + +use types::pruning_info::PruningInfo; use ethereum_types::{H256, H512, U256}; use io::{TimerToken}; use ethcore::ethstore::ethkey::Secret; @@ -39,7 +41,10 @@ use chain::{ETH_PROTOCOL_VERSION_63, ETH_PROTOCOL_VERSION_62, PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3}; use light::client::AsLightClient; use light::Provider; -use light::net::{self as light_net, LightProtocol, Params as LightParams, Capabilities, Handler as LightHandler, EventContext}; +use light::net::{ + self as light_net, LightProtocol, Params as LightParams, + Capabilities, Handler as LightHandler, EventContext, SampleStore, +}; use network::IpFilter; use private_tx::PrivateTxHandler; use transaction::UnverifiedTransaction; @@ -256,11 +261,35 @@ pub struct EthSync { light_subprotocol_name: [u8; 3], } +fn light_params( + network_id: u64, + max_peers: u32, + pruning_info: PruningInfo, + sample_store: Option> +) -> LightParams { + const MAX_LIGHTSERV_LOAD: f64 = 0.5; + + let mut light_params = LightParams { + network_id: network_id, + config: Default::default(), + capabilities: Capabilities { + serve_headers: true, + serve_chain_since: Some(pruning_info.earliest_chain), + serve_state_since: Some(pruning_info.earliest_state), + tx_relay: true, + }, + sample_store: sample_store, + }; + + let max_peers = ::std::cmp::max(max_peers, 1); + light_params.config.load_share = MAX_LIGHTSERV_LOAD / max_peers as f64; + + light_params +} + impl EthSync { /// Creates and register protocol with the network service pub fn new(params: Params, connection_filter: Option>) -> Result, Error> { - const MAX_LIGHTSERV_LOAD: f64 = 0.5; - let pruning_info = params.chain.pruning_info(); let light_proto = match params.config.serve_light { false => None, @@ -271,20 +300,12 @@ impl EthSync { .map(|mut p| { p.push("request_timings"); light_net::FileStore(p) }) .map(|store| Box::new(store) as Box<_>); - let mut light_params = LightParams { - network_id: params.config.network_id, - config: Default::default(), - capabilities: Capabilities { - serve_headers: true, - serve_chain_since: Some(pruning_info.earliest_chain), - serve_state_since: Some(pruning_info.earliest_state), - tx_relay: true, - }, - sample_store: sample_store, - }; - - let max_peers = ::std::cmp::max(params.network_config.max_peers, 1); - light_params.config.load_share = MAX_LIGHTSERV_LOAD / max_peers as f64; + let light_params = light_params( + params.config.network_id, + params.network_config.max_peers, + pruning_info, + sample_store + ); let mut light_proto = LightProtocol::new(params.provider, light_params); light_proto.add_handler(Arc::new(TxRelay(params.chain.clone()))); @@ -916,3 +937,19 @@ impl LightSyncProvider for LightSync { Default::default() // TODO } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn light_params_load_share_depends_on_max_peers() { + let pruning_info = PruningInfo { + earliest_chain: 0, + earliest_state: 0, + }; + let params1 = light_params(0, 10, pruning_info.clone(), None); + let params2 = light_params(0, 20, pruning_info, None); + assert!(params1.config.load_share > params2.config.load_share) + } +} diff --git a/ethcore/sync/src/lib.rs b/ethcore/sync/src/lib.rs index eb38d09d9d5..121da2374d0 100644 --- a/ethcore/sync/src/lib.rs +++ b/ethcore/sync/src/lib.rs @@ -21,6 +21,7 @@ //! https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol //! +extern crate common_types as types; extern crate ethcore_network as network; extern crate ethcore_network_devp2p as devp2p; extern crate parity_bytes as bytes; From 3de1f896a00325c06a35be057ec9c557febf796d Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Wed, 8 Aug 2018 22:00:48 +0200 Subject: [PATCH 3/3] style(api.rs): add trailing commas --- ethcore/sync/src/api.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethcore/sync/src/api.rs b/ethcore/sync/src/api.rs index 448e2fbf0b1..606aa39b31f 100644 --- a/ethcore/sync/src/api.rs +++ b/ethcore/sync/src/api.rs @@ -265,7 +265,7 @@ fn light_params( network_id: u64, max_peers: u32, pruning_info: PruningInfo, - sample_store: Option> + sample_store: Option>, ) -> LightParams { const MAX_LIGHTSERV_LOAD: f64 = 0.5; @@ -304,7 +304,7 @@ impl EthSync { params.config.network_id, params.network_config.max_peers, pruning_info, - sample_store + sample_store, ); let mut light_proto = LightProtocol::new(params.provider, light_params);