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

Commit

Permalink
Change for interpretting min and max peers
Browse files Browse the repository at this point in the history
* Only min specified -> Set min to that value and max to default
* Only max specified -> Set min and max to that value
* Both specified -> Set min the smallest value and max to the largest value
  • Loading branch information
niklasad1 committed Mar 12, 2018
1 parent 58a1671 commit 7b68b15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
8 changes: 4 additions & 4 deletions parity/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,11 @@ usage! {
"--port=[PORT]",
"Override the port on which the node should listen.",

ARG arg_min_peers: (u16) = 25u16, or |c: &Config| c.network.as_ref()?.min_peers.clone(),
ARG arg_min_peers: (Option<u16>) = None, or |c: &Config| c.network.as_ref()?.min_peers.clone(),
"--min-peers=[NUM]",
"Try to maintain at least NUM peers.",

ARG arg_max_peers: (u16) = 50u16, or |c: &Config| c.network.as_ref()?.max_peers.clone(),
ARG arg_max_peers: (Option<u16>) = None, or |c: &Config| c.network.as_ref()?.max_peers.clone(),
"--max-peers=[NUM]",
"Allow up to NUM peers.",

Expand Down Expand Up @@ -1470,8 +1470,8 @@ mod tests {
// -- Networking Options
flag_no_warp: false,
arg_port: 30303u16,
arg_min_peers: 25u16,
arg_max_peers: 50u16,
arg_min_peers: Some(25u16),
arg_max_peers: Some(50u16),
arg_max_pending_peers: 64u16,
arg_snapshot_peers: 0u16,
arg_allow_ips: "all".into(),
Expand Down
23 changes: 19 additions & 4 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::io::Read;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::collections::BTreeMap;
use std::cmp::max;
use std::cmp::{min, max};
use std::str::FromStr;
use cli::{Args, ArgsError};
use hash::keccak;
Expand Down Expand Up @@ -54,6 +54,9 @@ use account::{AccountCmd, NewAccount, ListAccounts, ImportAccounts, ImportFromGe
use snapshot::{self, SnapshotCommand};
use network::{IpFilter};

const DEFAULT_MAX_PEERS: u16 = 50;
const DEFAULT_MIN_PEERS: u16 = 25;

#[derive(Debug, PartialEq)]
pub enum Cmd {
Run(RunCmd),
Expand Down Expand Up @@ -455,8 +458,8 @@ impl Configuration {
}

fn max_peers(&self) -> u32 {
let peers = self.args.arg_max_peers as u32;
max(self.min_peers(), peers)
max(self.args.arg_max_peers.unwrap_or(DEFAULT_MAX_PEERS) as u32,
self.args.arg_min_peers.unwrap_or(DEFAULT_MIN_PEERS) as u32)
}

fn ip_filter(&self) -> Result<IpFilter, String> {
Expand All @@ -467,7 +470,19 @@ impl Configuration {
}

fn min_peers(&self) -> u32 {
self.args.arg_peers.unwrap_or(self.args.arg_min_peers) as u32
match (self.args.arg_max_peers, self.args.arg_min_peers) {
// Only `max peers``specified and respect that by configuring `min peers` that value
(Some(max), None) => max as u32,

// Only `min_peers` specified pick that value
(None, Some(min)) => min as u32,

// Both or none specified, pick the smallest value
(_, _) => {
min(self.args.arg_max_peers.unwrap_or(DEFAULT_MAX_PEERS) as u32,
self.args.arg_min_peers.unwrap_or(DEFAULT_MIN_PEERS) as u32)
}
}
}

fn max_pending_peers(&self) -> u32 {
Expand Down

0 comments on commit 7b68b15

Please sign in to comment.