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

Commit c6dcf62

Browse files
committed
Remove NetworkService::config()
1 parent d193436 commit c6dcf62

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

ethcore/sync/src/api.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use std::sync::Arc;
1818
use std::collections::{HashMap, BTreeMap};
1919
use std::io;
20+
use std::ops::Range;
2021
use std::time::Duration;
2122
use bytes::Bytes;
2223
use devp2p::{NetworkService, ConnectionFilter};
@@ -451,11 +452,18 @@ impl ChainNotify for EthSync {
451452
}
452453

453454
fn start(&self) {
454-
match self.network.start().map_err(Into::into) {
455-
Err(ErrorKind::Io(ref e)) if e.kind() == io::ErrorKind::AddrInUse => warn!("Network port {:?} is already in use, make sure that another instance of an Ethereum client is not running or change the port using the --port option.", self.network.config().listen_address.expect("Listen address is not set.")),
456-
Err(err) => warn!("Error starting network: {}", err),
455+
match self.network.start() {
456+
Err((err, listen_address)) => {
457+
match err.into() {
458+
ErrorKind::Io(ref e) if e.kind() == io::ErrorKind::AddrInUse => {
459+
warn!("Network port {:?} is already in use, make sure that another instance of an Ethereum client is not running or change the port using the --port option.", listen_address.expect("Listen address is not set."))
460+
},
461+
err => warn!("Error starting network: {}", err),
462+
}
463+
},
457464
_ => {},
458465
}
466+
459467
self.network.register_protocol(self.eth_handler.clone(), self.subprotocol_name, &[ETH_PROTOCOL_VERSION_62, ETH_PROTOCOL_VERSION_63])
460468
.unwrap_or_else(|e| warn!("Error registering ethereum protocol: {:?}", e));
461469
// register the warp sync subprotocol
@@ -519,8 +527,10 @@ pub trait ManageNetwork : Send + Sync {
519527
fn start_network(&self);
520528
/// Stop network
521529
fn stop_network(&self);
522-
/// Query the current configuration of the network
523-
fn network_config(&self) -> NetworkConfiguration;
530+
/// Returns the minimum and maximum peers.
531+
/// Note that `range.end` is *exclusive*.
532+
// TODO: Range should be changed to RangeInclusive once stable (https://github.com/rust-lang/rust/pull/50758)
533+
fn num_peers_range(&self) -> Range<u32>;
524534
/// Get network context for protocol.
525535
fn with_proto_context(&self, proto: ProtocolId, f: &mut FnMut(&NetworkContext));
526536
}
@@ -560,8 +570,8 @@ impl ManageNetwork for EthSync {
560570
self.stop();
561571
}
562572

563-
fn network_config(&self) -> NetworkConfiguration {
564-
NetworkConfiguration::from(self.network.config().clone())
573+
fn num_peers_range(&self) -> Range<u32> {
574+
self.network.num_peers_range()
565575
}
566576

567577
fn with_proto_context(&self, proto: ProtocolId, f: &mut FnMut(&NetworkContext)) {
@@ -814,11 +824,15 @@ impl ManageNetwork for LightSync {
814824
}
815825

816826
fn start_network(&self) {
817-
match self.network.start().map_err(Into::into) {
818-
Err(ErrorKind::Io(ref e)) if e.kind() == io::ErrorKind::AddrInUse => {
819-
warn!("Network port {:?} is already in use, make sure that another instance of an Ethereum client is not running or change the port using the --port option.", self.network.config().listen_address.expect("Listen address is not set."))
820-
}
821-
Err(err) => warn!("Error starting network: {}", err),
827+
match self.network.start() {
828+
Err((err, listen_address)) => {
829+
match err.into() {
830+
ErrorKind::Io(ref e) if e.kind() == io::ErrorKind::AddrInUse => {
831+
warn!("Network port {:?} is already in use, make sure that another instance of an Ethereum client is not running or change the port using the --port option.", listen_address.expect("Listen address is not set."))
832+
},
833+
err => warn!("Error starting network: {}", err),
834+
}
835+
},
822836
_ => {},
823837
}
824838

@@ -837,8 +851,8 @@ impl ManageNetwork for LightSync {
837851
}
838852
}
839853

840-
fn network_config(&self) -> NetworkConfiguration {
841-
NetworkConfiguration::from(self.network.config().clone())
854+
fn num_peers_range(&self) -> Range<u32> {
855+
self.network.num_peers_range()
842856
}
843857

844858
fn with_proto_context(&self, proto: ProtocolId, f: &mut FnMut(&NetworkContext)) {
@@ -849,12 +863,13 @@ impl ManageNetwork for LightSync {
849863
impl LightSyncProvider for LightSync {
850864
fn peer_numbers(&self) -> PeerNumbers {
851865
let (connected, active) = self.proto.peer_count();
852-
let config = self.network_config();
866+
let peers_range = self.num_peers_range();
867+
debug_assert!(peers_range.end > peers_range.start);
853868
PeerNumbers {
854869
connected: connected,
855870
active: active,
856-
max: config.max_peers as usize,
857-
min: config.min_peers as usize,
871+
max: peers_range.end as usize - 1,
872+
min: peers_range.start as usize,
858873
}
859874
}
860875

parity/informant.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ impl InformantData for FullNodeInformantData {
145145
let (importing, sync_info) = match (self.sync.as_ref(), self.net.as_ref()) {
146146
(Some(sync), Some(net)) => {
147147
let status = sync.status();
148-
let net_config = net.network_config();
148+
let num_peers_range = net.num_peers_range();
149+
debug_assert!(num_peers_range.end > num_peers_range.start);
149150

150151
cache_sizes.insert("sync", status.mem_used);
151152

@@ -154,7 +155,7 @@ impl InformantData for FullNodeInformantData {
154155
last_imported_block_number: status.last_imported_block_number.unwrap_or(chain_info.best_block_number),
155156
last_imported_old_block_number: status.last_imported_old_block_number,
156157
num_peers: status.num_peers,
157-
max_peers: status.current_max_peers(net_config.min_peers, net_config.max_peers),
158+
max_peers: status.current_max_peers(num_peers_range.start, num_peers_range.end - 1),
158159
snapshot_sync: status.is_snapshot_syncing(),
159160
}))
160161
}

rpc/src/v1/impls/parity.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ impl<C, M, U, S> Parity for ParityClient<C, M, U> where
212212

213213
fn net_peers(&self) -> Result<Peers> {
214214
let sync_status = self.sync.status();
215-
let net_config = self.net.network_config();
215+
let num_peers_range = self.net.num_peers_range();
216+
debug_assert!(num_peers_range.end > num_peers_range.start);
216217
let peers = self.sync.peers().into_iter().map(Into::into).collect();
217218

218219
Ok(Peers {
219220
active: sync_status.num_active_peers,
220221
connected: sync_status.num_peers,
221-
max: sync_status.current_max_peers(net_config.min_peers, net_config.max_peers),
222+
max: sync_status.current_max_peers(num_peers_range.start, num_peers_range.end - 1),
222223
peers: peers
223224
})
224225
}

rpc/src/v1/tests/mocked/manage_network.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use sync::{ManageNetwork, NetworkConfiguration};
17+
use std::ops::Range;
18+
use sync::ManageNetwork;
1819
use self::ethcore_network::{ProtocolId, NetworkContext};
1920

2021
extern crate ethcore_network;
@@ -29,6 +30,6 @@ impl ManageNetwork for TestManageNetwork {
2930
fn add_reserved_peer(&self, _peer: String) -> Result<(), String> { Ok(()) }
3031
fn start_network(&self) {}
3132
fn stop_network(&self) {}
32-
fn network_config(&self) -> NetworkConfiguration { NetworkConfiguration::new_local() }
33+
fn num_peers_range(&self) -> Range<u32> { 25 .. 51 }
3334
fn with_proto_context(&self, _: ProtocolId, _: &mut FnMut(&NetworkContext)) { }
3435
}

util/network-devp2p/src/service.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use network::{NetworkContext, PeerId, ProtocolId, NetworkIoMessage};
1919
use host::Host;
2020
use io::*;
2121
use parking_lot::RwLock;
22+
use std::net::SocketAddr;
23+
use std::ops::Range;
2224
use std::sync::Arc;
2325
use ansi_term::Colour;
2426
use connection_filter::ConnectionFilter;
@@ -92,9 +94,13 @@ impl NetworkService {
9294
&self.io_service
9395
}
9496

95-
/// Returns network configuration.
96-
pub fn config(&self) -> &NetworkConfiguration {
97-
&self.config
97+
/// Returns the number of peers allowed.
98+
///
99+
/// Keep in mind that `range.end` is *exclusive*.
100+
pub fn num_peers_range(&self) -> Range<u32> {
101+
let start = self.config.min_peers;
102+
let end = self.config.max_peers;
103+
start .. end
98104
}
99105

100106
/// Returns external url if available.
@@ -109,17 +115,23 @@ impl NetworkService {
109115
host.as_ref().map(|h| h.local_url())
110116
}
111117

112-
/// Start network IO
113-
pub fn start(&self) -> Result<(), Error> {
118+
/// Start network IO.
119+
///
120+
/// In case of error, also returns the listening address for better error reporting.
121+
pub fn start(&self) -> Result<(), (Error, Option<SocketAddr>)> {
114122
let mut host = self.host.write();
123+
let listen_addr = self.config.listen_address.clone();
115124
if host.is_none() {
116-
let h = Arc::new(Host::new(self.config.clone(), self.filter.clone())?);
117-
self.io_service.register_handler(h.clone())?;
125+
let h = Arc::new(Host::new(self.config.clone(), self.filter.clone())
126+
.map_err(|err| (err.into(), listen_addr))?);
127+
self.io_service.register_handler(h.clone())
128+
.map_err(|err| (err.into(), listen_addr))?;
118129
*host = Some(h);
119130
}
120131

121132
if self.host_handler.public_url.read().is_none() {
122-
self.io_service.register_handler(self.host_handler.clone())?;
133+
self.io_service.register_handler(self.host_handler.clone())
134+
.map_err(|err| (err.into(), listen_addr))?;
123135
}
124136

125137
Ok(())

whisper/cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>,
215215
let network = devp2p::NetworkService::new(net::NetworkConfiguration::new_local(), None)?;
216216

217217
// Start network service
218-
network.start()?;
218+
network.start().map_err(|(err, _)| err)?;
219219

220220
// Attach whisper protocol to the network service
221221
network.register_protocol(whisper_network_handler.clone(), whisper::net::PROTOCOL_ID,

0 commit comments

Comments
 (0)