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

Honor --max-peers if --min-peers is not specified #8087

Merged
merged 5 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions parity/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,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 @@ -1482,8 +1482,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 Expand Up @@ -1868,4 +1868,18 @@ mod tests {
stratum: None,
});
}

#[test]
fn should_not_accept_min_peers_bigger_than_max_peers() {
match Args::parse(&["parity", "--max-peers=39", "--min-peers=40"]) {
Err(ArgsError::PeerConfiguration) => (),
_ => assert_eq!(false, true),
}
}

#[test]
fn should_accept_max_peers_equal_or_bigger_than_min_peers() {
Args::parse(&["parity", "--max-peers=40", "--min-peers=40"]).unwrap();
Args::parse(&["parity", "--max-peers=100", "--min-peers=40"]).unwrap();
}
}
12 changes: 12 additions & 0 deletions parity/cli/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ macro_rules! usage {
Clap(ClapError),
Decode(toml::de::Error),
Config(String, io::Error),
PeerConfiguration,
}

impl ArgsError {
Expand All @@ -177,6 +178,10 @@ macro_rules! usage {
println_stderr!("{}", e);
process::exit(2)
},
ArgsError::PeerConfiguration => {
println_stderr!("You have supplied `min_peers` > `max_peers`");
process::exit(2)
}
}
}
}
Expand Down Expand Up @@ -312,6 +317,13 @@ macro_rules! usage {
pub fn parse<S: AsRef<str>>(command: &[S]) -> Result<Self, ArgsError> {
let raw_args = RawArgs::parse(command)?;

if let (Some(max_peers), Some(min_peers)) = (raw_args.arg_max_peers, raw_args.arg_min_peers) {
// Invalid configuration pattern `mix_peers` > `max_peers`
if min_peers > max_peers {
return Err(ArgsError::PeerConfiguration);
}
}

// Skip loading config file if no_config flag is specified
if raw_args.flag_no_config {
return Ok(raw_args.into_args(Config::default()));
Expand Down
40 changes: 36 additions & 4 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::cmp::{max, min};
use std::time::Duration;
use std::io::Read;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::collections::BTreeMap;
use std::cmp::max;
use std::str::FromStr;
use cli::{Args, ArgsError};
use hash::keccak;
Expand Down Expand Up @@ -55,6 +55,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 @@ -468,8 +471,9 @@ impl Configuration {
}

fn max_peers(&self) -> u32 {
let peers = self.args.arg_max_peers as u32;
max(self.min_peers(), peers)
self.args.arg_max_peers
.or(max(self.args.arg_min_peers, Some(DEFAULT_MAX_PEERS)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always return T as long at least one of the arguments is Some but I kept unwrap_or if somebody reacts if we use raw unwrapping

.unwrap_or(DEFAULT_MAX_PEERS) as u32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case max_peers isn't defined can we set it to max(min_peers, DEFAULT_MAX_PEERS)? With the current code if you start parity with just --min-peers 5, max peers will also be set to 5 (I'd expect it to use the default value).

Copy link
Collaborator Author

@niklasad1 niklasad1 Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yepp, I'll buy that

}

fn ip_filter(&self) -> Result<IpFilter, String> {
Expand All @@ -480,7 +484,9 @@ impl Configuration {
}

fn min_peers(&self) -> u32 {
self.args.arg_peers.unwrap_or(self.args.arg_min_peers) as u32
self.args.arg_min_peers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above but with min(max_peers, DEFAULT_MIN_PEERS).

.or(min(self.args.arg_max_peers, Some(DEFAULT_MIN_PEERS)))
.unwrap_or(DEFAULT_MIN_PEERS) as u32
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here!


fn max_pending_peers(&self) -> u32 {
Expand Down Expand Up @@ -1945,4 +1951,30 @@ mod tests {
assert_eq!(std.directories().cache, dir::helpers::replace_home_and_local(&base_path, &local_path, ::dir::CACHE_PATH));
assert_eq!(base.directories().cache, "/test/cache");
}

#[test]
fn should_respect_only_max_peers() {
let args = vec!["parity", "--max-peers=5"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert!(c.net_conf.min_peers <= 5);
assert_eq!(c.net_conf.max_peers, 5);
},
_ => panic!("Should be Cmd::Run"),
}
}

#[test]
fn should_respect_only_min_peers() {
let args = vec!["parity", "--min-peers=500"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 500);
assert!(c.net_conf.max_peers >= 500);
},
_ => panic!("Should be Cmd::Run"),
}
}
}