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,11 +261,35 @@ pub struct EthSync {
|
|||||||
light_subprotocol_name: [u8; 3],
|
light_subprotocol_name: [u8; 3],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn light_params(
|
||||||
|
network_id: u64,
|
||||||
|
max_peers: u32,
|
||||||
|
pruning_info: PruningInfo,
|
||||||
|
sample_store: Option<Box<SampleStore>>,
|
||||||
|
) -> 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 {
|
impl EthSync {
|
||||||
/// Creates and register protocol with the network service
|
/// Creates and register protocol with the network service
|
||||||
pub fn new(params: Params, connection_filter: Option<Arc<ConnectionFilter>>) -> Result<Arc<EthSync>, Error> {
|
pub fn new(params: Params, connection_filter: Option<Arc<ConnectionFilter>>) -> Result<Arc<EthSync>, Error> {
|
||||||
const MAX_LIGHTSERV_LOAD: f64 = 0.5;
|
|
||||||
|
|
||||||
let pruning_info = params.chain.pruning_info();
|
let pruning_info = params.chain.pruning_info();
|
||||||
let light_proto = match params.config.serve_light {
|
let light_proto = match params.config.serve_light {
|
||||||
false => None,
|
false => None,
|
||||||
@ -271,20 +300,12 @@ impl EthSync {
|
|||||||
.map(|mut p| { p.push("request_timings"); light_net::FileStore(p) })
|
.map(|mut p| { p.push("request_timings"); light_net::FileStore(p) })
|
||||||
.map(|store| Box::new(store) as Box<_>);
|
.map(|store| Box::new(store) as Box<_>);
|
||||||
|
|
||||||
let mut light_params = LightParams {
|
let light_params = light_params(
|
||||||
network_id: params.config.network_id,
|
params.config.network_id,
|
||||||
config: Default::default(),
|
params.network_config.max_peers,
|
||||||
capabilities: Capabilities {
|
pruning_info,
|
||||||
serve_headers: true,
|
sample_store,
|
||||||
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::min(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);
|
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