Fix load share (#9321)
* fix(light_sync): calculate `load_share` properly * refactor(api.rs): extract `light_params` fn, add test * style(api.rs): add trailing commas
This commit is contained in:
parent
a6df452841
commit
98dbd1fdc7
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -839,6 +839,7 @@ dependencies = [
|
|||||||
name = "ethcore-sync"
|
name = "ethcore-sync"
|
||||||
version = "1.12.0"
|
version = "1.12.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"common-types 0.1.0",
|
||||||
"env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
"env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ethcore 1.12.0",
|
"ethcore 1.12.0",
|
||||||
"ethcore-io 1.12.0",
|
"ethcore-io 1.12.0",
|
||||||
|
@ -8,6 +8,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
|||||||
[lib]
|
[lib]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
common-types = { path = "../types" }
|
||||||
parity-bytes = { git = "https://github.com/paritytech/parity-common" }
|
parity-bytes = { git = "https://github.com/paritytech/parity-common" }
|
||||||
ethcore-network = { path = "../../util/network" }
|
ethcore-network = { path = "../../util/network" }
|
||||||
ethcore-network-devp2p = { path = "../../util/network-devp2p" }
|
ethcore-network-devp2p = { path = "../../util/network-devp2p" }
|
||||||
|
@ -24,6 +24,8 @@ use devp2p::NetworkService;
|
|||||||
use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId,
|
use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId,
|
||||||
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ErrorKind,
|
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ErrorKind,
|
||||||
ConnectionFilter};
|
ConnectionFilter};
|
||||||
|
|
||||||
|
use types::pruning_info::PruningInfo;
|
||||||
use ethereum_types::{H256, H512, U256};
|
use ethereum_types::{H256, H512, U256};
|
||||||
use io::{TimerToken};
|
use io::{TimerToken};
|
||||||
use ethcore::ethstore::ethkey::Secret;
|
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};
|
PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3};
|
||||||
use light::client::AsLightClient;
|
use light::client::AsLightClient;
|
||||||
use light::Provider;
|
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 network::IpFilter;
|
||||||
use private_tx::PrivateTxHandler;
|
use private_tx::PrivateTxHandler;
|
||||||
use transaction::UnverifiedTransaction;
|
use transaction::UnverifiedTransaction;
|
||||||
@ -256,23 +261,16 @@ pub struct EthSync {
|
|||||||
light_subprotocol_name: [u8; 3],
|
light_subprotocol_name: [u8; 3],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EthSync {
|
fn light_params(
|
||||||
/// Creates and register protocol with the network service
|
network_id: u64,
|
||||||
pub fn new(params: Params, connection_filter: Option<Arc<ConnectionFilter>>) -> Result<Arc<EthSync>, Error> {
|
max_peers: u32,
|
||||||
|
pruning_info: PruningInfo,
|
||||||
|
sample_store: Option<Box<SampleStore>>,
|
||||||
|
) -> LightParams {
|
||||||
const MAX_LIGHTSERV_LOAD: f64 = 0.5;
|
const MAX_LIGHTSERV_LOAD: f64 = 0.5;
|
||||||
|
|
||||||
let pruning_info = params.chain.pruning_info();
|
|
||||||
let light_proto = match params.config.serve_light {
|
|
||||||
false => None,
|
|
||||||
true => Some({
|
|
||||||
let sample_store = params.network_config.net_config_path
|
|
||||||
.clone()
|
|
||||||
.map(::std::path::PathBuf::from)
|
|
||||||
.map(|mut p| { p.push("request_timings"); light_net::FileStore(p) })
|
|
||||||
.map(|store| Box::new(store) as Box<_>);
|
|
||||||
|
|
||||||
let mut light_params = LightParams {
|
let mut light_params = LightParams {
|
||||||
network_id: params.config.network_id,
|
network_id: network_id,
|
||||||
config: Default::default(),
|
config: Default::default(),
|
||||||
capabilities: Capabilities {
|
capabilities: Capabilities {
|
||||||
serve_headers: true,
|
serve_headers: true,
|
||||||
@ -283,9 +281,32 @@ impl EthSync {
|
|||||||
sample_store: sample_store,
|
sample_store: sample_store,
|
||||||
};
|
};
|
||||||
|
|
||||||
let max_peers = ::std::cmp::min(params.network_config.max_peers, 1);
|
let max_peers = ::std::cmp::max(max_peers, 1);
|
||||||
light_params.config.load_share = MAX_LIGHTSERV_LOAD / max_peers as f64;
|
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<Arc<ConnectionFilter>>) -> Result<Arc<EthSync>, Error> {
|
||||||
|
let pruning_info = params.chain.pruning_info();
|
||||||
|
let light_proto = match params.config.serve_light {
|
||||||
|
false => None,
|
||||||
|
true => Some({
|
||||||
|
let sample_store = params.network_config.net_config_path
|
||||||
|
.clone()
|
||||||
|
.map(::std::path::PathBuf::from)
|
||||||
|
.map(|mut p| { p.push("request_timings"); light_net::FileStore(p) })
|
||||||
|
.map(|store| Box::new(store) as Box<_>);
|
||||||
|
|
||||||
|
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);
|
let mut light_proto = LightProtocol::new(params.provider, light_params);
|
||||||
light_proto.add_handler(Arc::new(TxRelay(params.chain.clone())));
|
light_proto.add_handler(Arc::new(TxRelay(params.chain.clone())));
|
||||||
|
|
||||||
@ -916,3 +937,19 @@ impl LightSyncProvider for LightSync {
|
|||||||
Default::default() // TODO
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
//! https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol
|
//! 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 as network;
|
||||||
extern crate ethcore_network_devp2p as devp2p;
|
extern crate ethcore_network_devp2p as devp2p;
|
||||||
extern crate parity_bytes as bytes;
|
extern crate parity_bytes as bytes;
|
||||||
|
Loading…
Reference in New Issue
Block a user