Honor --max-peers if --min-peers is not specified (#8087)
* Change interpretation min and max peers * 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 * simplify logic, new ParseError & add tests * simplify code according to the review comments * address review comments * more fine-grained tests
This commit is contained in:
@@ -409,11 +409,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.",
|
||||
|
||||
@@ -1504,8 +1504,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(),
|
||||
@@ -1896,4 +1896,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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +161,7 @@ macro_rules! usage {
|
||||
Clap(ClapError),
|
||||
Decode(toml::de::Error),
|
||||
Config(String, io::Error),
|
||||
PeerConfiguration,
|
||||
}
|
||||
|
||||
impl ArgsError {
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()));
|
||||
|
||||
Reference in New Issue
Block a user