From 4b11d79829d028e091b19af077767051574cd837 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 22 Jan 2019 09:51:40 +0100 Subject: [PATCH] fix(ManageNetwork): replace Range with RangeInclusive (#10209) * fix(ManageNetwork): replace Range -> RangeIncls Fixes `TODO: Range should be changed to RangeInclusive once stable (https://github.com/rust-lang/rust/pull/50758)` * fix(tests) * fix(grumbles): off-by-one error in debug_asserts * RangeInclusive::end() is inclusive which means that if start and end is equal the `debug_assert(range.end() > range.start()` will fail which is shouldn't --- ethcore/sync/src/api.rs | 16 +++++++--------- parity/informant.rs | 4 ++-- rpc/src/v1/impls/parity.rs | 4 ++-- rpc/src/v1/tests/mocked/manage_network.rs | 4 ++-- util/network-devp2p/src/service.rs | 10 +++------- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/ethcore/sync/src/api.rs b/ethcore/sync/src/api.rs index 253f7fe83..28565db39 100644 --- a/ethcore/sync/src/api.rs +++ b/ethcore/sync/src/api.rs @@ -17,7 +17,7 @@ use std::sync::{Arc, mpsc, atomic}; use std::collections::{HashMap, BTreeMap}; use std::io; -use std::ops::Range; +use std::ops::RangeInclusive; use std::time::Duration; use bytes::Bytes; use devp2p::NetworkService; @@ -615,9 +615,7 @@ pub trait ManageNetwork : Send + Sync { /// Stop network fn stop_network(&self); /// Returns the minimum and maximum peers. - /// Note that `range.end` is *exclusive*. - // TODO: Range should be changed to RangeInclusive once stable (https://github.com/rust-lang/rust/pull/50758) - fn num_peers_range(&self) -> Range; + fn num_peers_range(&self) -> RangeInclusive; /// Get network context for protocol. fn with_proto_context(&self, proto: ProtocolId, f: &mut FnMut(&NetworkContext)); } @@ -656,7 +654,7 @@ impl ManageNetwork for EthSync { self.stop(); } - fn num_peers_range(&self) -> Range { + fn num_peers_range(&self) -> RangeInclusive { self.network.num_peers_range() } @@ -935,7 +933,7 @@ impl ManageNetwork for LightSync { self.network.stop(); } - fn num_peers_range(&self) -> Range { + fn num_peers_range(&self) -> RangeInclusive { self.network.num_peers_range() } @@ -948,12 +946,12 @@ impl LightSyncProvider for LightSync { fn peer_numbers(&self) -> PeerNumbers { let (connected, active) = self.proto.peer_count(); let peers_range = self.num_peers_range(); - debug_assert!(peers_range.end > peers_range.start); + debug_assert!(peers_range.end() >= peers_range.start()); PeerNumbers { connected: connected, active: active, - max: peers_range.end as usize - 1, - min: peers_range.start as usize, + max: *peers_range.end() as usize, + min: *peers_range.start() as usize, } } diff --git a/parity/informant.rs b/parity/informant.rs index 0299f524f..78d055686 100644 --- a/parity/informant.rs +++ b/parity/informant.rs @@ -146,7 +146,7 @@ impl InformantData for FullNodeInformantData { (Some(sync), Some(net)) => { let status = sync.status(); let num_peers_range = net.num_peers_range(); - debug_assert!(num_peers_range.end > num_peers_range.start); + debug_assert!(num_peers_range.end() >= num_peers_range.start()); cache_sizes.insert("sync", status.mem_used); @@ -154,7 +154,7 @@ impl InformantData for FullNodeInformantData { last_imported_block_number: status.last_imported_block_number.unwrap_or(chain_info.best_block_number), last_imported_old_block_number: status.last_imported_old_block_number, num_peers: status.num_peers, - max_peers: status.current_max_peers(num_peers_range.start, num_peers_range.end - 1), + max_peers: status.current_max_peers(*num_peers_range.start(), *num_peers_range.end()), snapshot_sync: status.is_snapshot_syncing(), }) } diff --git a/rpc/src/v1/impls/parity.rs b/rpc/src/v1/impls/parity.rs index 39948ce1a..46e93bc73 100644 --- a/rpc/src/v1/impls/parity.rs +++ b/rpc/src/v1/impls/parity.rs @@ -187,13 +187,13 @@ impl Parity for ParityClient where fn net_peers(&self) -> Result { let sync_status = self.sync.status(); let num_peers_range = self.net.num_peers_range(); - debug_assert!(num_peers_range.end > num_peers_range.start); + debug_assert!(num_peers_range.end() >= num_peers_range.start()); let peers = self.sync.peers().into_iter().map(Into::into).collect(); Ok(Peers { active: sync_status.num_active_peers, connected: sync_status.num_peers, - max: sync_status.current_max_peers(num_peers_range.start, num_peers_range.end - 1), + max: sync_status.current_max_peers(*num_peers_range.start(), *num_peers_range.end()), peers: peers }) } diff --git a/rpc/src/v1/tests/mocked/manage_network.rs b/rpc/src/v1/tests/mocked/manage_network.rs index 560ed89ff..d327a8743 100644 --- a/rpc/src/v1/tests/mocked/manage_network.rs +++ b/rpc/src/v1/tests/mocked/manage_network.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use std::ops::Range; +use std::ops::RangeInclusive; use sync::ManageNetwork; use self::ethcore_network::{ProtocolId, NetworkContext}; @@ -30,6 +30,6 @@ impl ManageNetwork for TestManageNetwork { fn add_reserved_peer(&self, _peer: String) -> Result<(), String> { Ok(()) } fn start_network(&self) {} fn stop_network(&self) {} - fn num_peers_range(&self) -> Range { 25 .. 51 } + fn num_peers_range(&self) -> RangeInclusive { 25..=50 } fn with_proto_context(&self, _: ProtocolId, _: &mut FnMut(&NetworkContext)) { } } diff --git a/util/network-devp2p/src/service.rs b/util/network-devp2p/src/service.rs index e57f01066..dfacec4be 100644 --- a/util/network-devp2p/src/service.rs +++ b/util/network-devp2p/src/service.rs @@ -20,7 +20,7 @@ use host::Host; use io::*; use parking_lot::RwLock; use std::net::SocketAddr; -use std::ops::Range; +use std::ops::RangeInclusive; use std::sync::Arc; use ansi_term::Colour; use network::ConnectionFilter; @@ -95,12 +95,8 @@ impl NetworkService { } /// Returns the number of peers allowed. - /// - /// Keep in mind that `range.end` is *exclusive*. - pub fn num_peers_range(&self) -> Range { - let start = self.config.min_peers; - let end = self.config.max_peers + 1; - start .. end + pub fn num_peers_range(&self) -> RangeInclusive { + self.config.min_peers..=self.config.max_peers } /// Returns external url if available.