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

Conversation

niklasad1
Copy link
Collaborator

@niklasad1 niklasad1 commented Mar 12, 2018

Attempt to solve #7576

This PR changes the following:

  • min and max in cli/mod.rs to Option<u16>
  • none provided => use defaults
  • min-peers only => max-peers = max(x, DEFAULT_MAX_PEERS)
  • max-peers only => min-peers = min(x, DEFAULT_MIN_PEERS)
  • min-peers > max-peers => ERROR
  • Added tests for this

I'm not sure if this is the right approach but seemed most straight-forward to me

@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. M2-config 📂 Chain specifications and node configurations. labels Mar 12, 2018
@parity-cla-bot
Copy link

It looks like @niklasad1 signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@niklasad1 niklasad1 changed the title Honor --max-peers not if --min-peers is not specified Honor --max-peers if not --min-peers is not specified Mar 12, 2018
@niklasad1 niklasad1 changed the title Honor --max-peers if not --min-peers is not specified Honor --max-peers if --min-peers is not specified Mar 12, 2018
@twittner twittner requested a review from axelchalon March 19, 2018 15:29
@twittner
Copy link
Contributor

Sorry for adding a reviewer, mouse click error.

@twittner twittner removed the request for review from axelchalon March 19, 2018 15:32
@niklasad1 niklasad1 force-pushed the max-min-peers branch 2 times, most recently from 57cdbd9 to b9de9f3 Compare March 19, 2018 20:17
@5chdn 5chdn added this to the 1.11 milestone Mar 20, 2018
Copy link
Collaborator

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

Looks good, left a suggestion to simplify the code.

@@ -467,7 +474,13 @@ 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not:

self.args.arg_min_peers
   .or(self.args.arg_max_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.

yepp, that is way cleaner

@@ -455,8 +457,13 @@ impl Configuration {
}

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

Choose a reason for hiding this comment

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

self.args.arg_max_peers
   .or(self.args.arg_min_peers)
   .unwrap_or(DEFAULT_MAX_PEERS) as u32

@tomusdrw tomusdrw added A6-mustntgrumble 💦 Pull request has areas for improvement. The author need not address them before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels Mar 23, 2018
// Skip loading config file if no_config flag is specified
if raw_args.flag_no_config {
return Ok(raw_args.into_args(Config::default()));
else if raw_args.flag_no_config {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This else is fishy, why do we ignore no-config flag if max_peers or min_peers are not provided?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I also think this else is a mistake.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yepp, you’re right it should be s/else if/if.

I made a faulty assumption that first if statement catches only errors!

// Skip loading config file if no_config flag is specified
if raw_args.flag_no_config {
return Ok(raw_args.into_args(Config::default()));
else if raw_args.flag_no_config {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I also think this else is a mistake.

max(self.min_peers(), peers)
self.args.arg_max_peers
.or(self.args.arg_min_peers)
.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

@@ -467,7 +470,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).

* 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
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

self.args.arg_peers.unwrap_or(self.args.arg_min_peers) as u32
self.args.arg_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!

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

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

LGTM!

@andresilva andresilva added A8-looksgood 🦄 Pull request is reviewed well. and removed A6-mustntgrumble 💦 Pull request has areas for improvement. The author need not address them before merging. labels Apr 2, 2018
@5chdn 5chdn merged commit 0a535bf into master Apr 3, 2018
@5chdn 5chdn deleted the max-min-peers branch April 3, 2018 14:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. M2-config 📂 Chain specifications and node configurations. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants