8000 p2p: return `CSubNet` in `LookupSubNet` by brunoerg · Pull Request #26078 · bitcoin/bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

p2p: return CSubNet in LookupSubNet #26078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

brunoerg
Copy link
Contributor

Analyzing the usage of LookupSubNet, noticed that most cases uses check if the subnet is valid by calling subnet.IsValid(), and the boolean returned by LookupSubNet hasn't been used so much, see:

bitcoin/src/httpserver.cpp

Lines 172 to 174 in 29d540b

CSubNet subnet;
LookupSubNet(strAllow, subnet);
if (!subnet.IsValid()) {

CSubNet subnet;
LookupSubNet(net, subnet);
if (!subnet.IsValid()) {

It makes sense to return CSubNet instead of bool.

@DrahtBot DrahtBot 8000 added the P2P label Sep 13, 2022
@w0xlt
Copy link
Contributor
w0xlt commented Sep 13, 2022

Approach ACK

@jonatack
Copy link
Member
jonatack commented Sep 14, 2022

Interesting simplification and looks reasonable at first glance. A possible alternative would be to retain the checks and make LookupSubNet return std::optional<CSubNet> instead.

rpc/net.cpp:717:9: error: no matching function for call to 'LookupSubNet'
        LookupSubNet(request.params[0].get_str(), subNet);
        ^~~~~~~~~~~~
./netbase.h:177:9: note: candidate function not viable: requires single argument 'subnet_str', but 2 arguments were provided
CSubNet LookupSubNet(const std::string& subnet_str);
        ^
1 error generated.

@brunoerg brunoerg force-pushed the 2022-09-csubnet-lookup branch from 3007397 to 0b558d3 Compare September 14, 2022 13:49
@brunoerg brunoerg marked this pull request as ready for review September 14, 2022 14:15
@brunoerg brunoerg force-pushed the 2022-09-csubnet-lookup branch from 0b558d3 to 60a0283 Compare September 14, 2022 14:21
@brunoerg brunoerg marked this pull request as draft September 14, 2022 17:42
@DrahtBot
Copy link
Contributor
DrahtBot commented Sep 14, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK vasild, theStack, achow101
Concept ACK stickies-v
Stale ACK w0xlt

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

@brunoerg brunoerg force-pushed the 2022-09-csubnet-lookup branch 3 times, most recently from 210cb2d to 0b3db2f Compare September 19, 2022 16:30
@brunoerg brunoerg marked this pull request as ready for review September 19, 2022 17:42
@brunoerg
Copy link
Contributor Author

Addressed @jonatack's review and now it returns std::optional<CSubNet>.

@luke-jr
Copy link
Member
luke-jr commented Sep 19, 2022

std::optional seems redundant here. I think it's better to just return the CSubNet?

@brunoerg brunoerg force-pushed the 2022-09-csubnet-lookup branch from 0b3db2f to 07c6053 Compare September 20, 2022 13:40
@brunoerg
Copy link
Contributor Author

Force-pushed to make LookupSubnet not return an invalid but existing CSubNet, now it returns a valid CSubNet or std::nullopt. Thanks, @luke-jr and @mzumsande.

Copy link
Contributor
@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

reACK 07c6053

@luke-jr
Copy link
Member
luke-jr commented Sep 20, 2022

I still think adding a std::optional here is asking for bugs. We're already seeing this kind of thing with the new util::Result stuff. It's important to only have one "no value" state.

@brunoerg brunoerg force-pushed the 2022-09-csubnet-lookup branch from 07c6053 to b7e197c Compare September 20, 2022 18:40
@w0xlt
Copy link
Contributor
w0xlt commented Sep 21, 2022

I don´t think std::optional is a problem per se, but CSubNet has the bool CSubNet::valid which can be used instead of std::optional in this case.

The problem in util::Result has more to do with the complexity of the class than in the use of std::optional.

@luke-jr
Copy link
Member
luke-jr commented Sep 22, 2022

The problem is having two ways to represent an invalid state, especially when they can be at odds with each other (in that case, .has_value() && !->IsValid()).

@brunoerg
Copy link
Contributor Author

The problem is having two ways to represent an invalid state, especially when they can be at odds with each other (in that case, .has_value() && !->IsValid()).

Fair enough, it's better to return CSubNet instead. I'm going to change it.

@brunoerg brunoerg force-pushed the 2022-09-csubnet-lookup branch from b7e197c to fc60ced Compare September 22, 2022 13:59
Copy link
Contributor
@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

reACK fc60ced

nit: Maybe an explicit initialization could be added?

class CSubNet
{
protected:
    // ...
    /// Is this value valid? (only used to signal parse errors)
    bool valid{false};

achow101 added a commit that referenced this pull request May 30, 2023
5c832c3 p2p, refactor: return `std::optional<CNetAddr>` in `LookupHost` (brunoerg)
34bcdfc p2p, refactor: return vector/optional<CService> in `Lookup` (brunoerg)
7799eb1 p2p, refactor: return `std::vector<CNetAddr>` in `LookupHost` (brunoerg)
5c1774a p2p, refactor: return `std::vector<CNetAddr>` in `LookupIntern` (brunoerg)

Pull request description:

  Continuation of #26078.

  To improve readability instead of returning a bool and passing stuff by reference, this PR changes:

  - `LookupHost` to return `std::vector<CNetAddr>`
  - `LookupHost` to return `std::optional<CNetAddr>`
  - `Lookup` to return `std::vector<CService>`
  - `Lookup` to return `std::optional<CService>`.
  - `LookupIntern` to return `std::vector<CNetAddr>`

  As discussed in #26078, it would be better to avoid using `optional` in some cases, but for specific `Lookup` and `LookupHost` functions it's necessary to use `optional` to verify if they were able to catch some data from their overloaded function.

ACKs for top commit:
  achow101:
    ACK 5c832c3
  stickies-v:
    re-ACK 5c832c3 - just addressing two nits, no other changes
  theStack:
    re-ACK 5c832c3

Tree-SHA512: ea346fdc54463999646269bd600cd4a1590ef958001d2f0fc2be608ca51e1b4365efccca76dd4972b023e12fcc6e67d226608b0df7beb901bdeadd19948df840
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 30, 2023
…pHost`

5c832c3 p2p, refactor: return `std::optional<CNetAddr>` in `LookupHost` (brunoerg)
34bcdfc p2p, refactor: return vector/optional<CService> in `Lookup` (brunoerg)
7799eb1 p2p, refactor: return `std::vector<CNetAddr>` in `LookupHost` (brunoerg)
5c1774a p2p, refactor: return `std::vector<CNetAddr>` in `LookupIntern` (brunoerg)

Pull request description:

  Continuation of bitcoin#26078.

  To improve readability instead of returning a bool and passing stuff by reference, this PR changes:

  - `LookupHost` to return `std::vector<CNetAddr>`
  - `LookupHost` to return `std::optional<CNetAddr>`
  - `Lookup` to return `std::vector<CService>`
  - `Lookup` to return `std::optional<CService>`.
  - `LookupIntern` to return `std::vector<CNetAddr>`

  As discussed in bitcoin#26078, it would be better to avoid using `optional` in some cases, but for specific `Lookup` and `LookupHost` functions it's necessary to use `optional` to verify if they were able to catch some data from their overloaded function.

ACKs for top commit:
  achow101:
    ACK 5c832c3
  stickies-v:
    re-ACK 5c832c3 - just addressing two nits, no other changes
  theStack:
    re-ACK 5c832c3

Tree-SHA512: ea346fdc54463999646269bd600cd4a1590ef958001d2f0fc2be608ca51e1b4365efccca76dd4972b023e12fcc6e67d226608b0df7beb901bdeadd19948df840
Copy link
Contributor
@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK fb3e812

@DrahtBot DrahtBot requested review from w0xlt and removed request for w0xlt June 8, 2023 07:36
@achow101 achow101 requested review from theStack and stickies-v and removed request for w0xlt September 20, 2023 16:46
Copy link
Contributor
@theStack theStack left a comment

Choose a reason for hiding this comment

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

Code-review ACK fb3e812

@DrahtBot DrahtBot requested a review from w0xlt October 3, 2023 00:30
Copy link
Contributor
@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

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

Concept ACK, but Approach ~0 (for now). Reviewed the code (fb3e812) and it all looks good to me.

Generally supportive of reducing out-parameters in our function signatures, and this PR offers a modest improvement in readability and uniformizes how we check the return value of LookupSubNet, which is nice.

I think luke-jr raised a good point that having the return value be falsy in two different ways (through std::optional wrapper and .IsValid() is dangerous and should be avoided, and I agree. I'm approach ~0 as I think this PR improves things, but I think (but did not yet fully implement/verify) the real improvement would be to enforce CSubNet validity in the constructor (wrapped with std::optional factory methods) so we can improve the interface by handling bad input data early and guarantee that all CSubNet instances are valid, and wrap them in a std::optional when validity is not guaranteed. This requires a more substantial refactoring, though, so I'm not opposed to merging this first (and my suggested approach may turn out infeasible/not worth it in practice) - but I think it would make for a much cleaner interface.

Comment on lines +243 to 244
subnet = LookupSubNet("1.2.3.4/255.255.255.255");
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would make sense to make these one-liners, I think, ideally w/ scripted diff? Or a fn/lambda CheckSubnetEquals to make it even more readable?

@DrahtBot DrahtBot requested review from w0xlt and removed request for w0xlt October 9, 2023 16:14
@achow101
Copy link
Member

ACK fb3e812

@DrahtBot DrahtBot requested review from w0xlt and stickies-v and removed request for w0xlt October 26, 2023 18:21
@achow101 achow101 merged commit 7be62df into bitcoin:master Oct 26, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Oct 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0