bitcoin icon indicating copy to clipboard operation
bitcoin copied to clipboard

p2p: return `CSubNet` in `LookupSubNet`

Open brunoerg opened this issue 2 years ago • 8 comments

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: https://github.com/bitcoin/bitcoin/blob/29d540b7ada890dd588c4825d40c27c5e6f20061/src/httpserver.cpp#L172-L174 https://github.com/bitcoin/bitcoin/blob/29d540b7ada890dd588c4825d40c27c5e6f20061/src/net_permissions.cpp#L114-L116

It makes sense to return CSubNet instead of bool.

brunoerg avatar Sep 13 '22 21:09 brunoerg

Approach ACK

w0xlt avatar Sep 13 '22 23:09 w0xlt

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.

jonatack avatar Sep 14 '22 11:09 jonatack

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.

DrahtBot avatar Sep 14 '22 22:09 DrahtBot

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

brunoerg avatar Sep 19 '22 17:09 brunoerg

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

luke-jr avatar Sep 19 '22 23:09 luke-jr

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.

brunoerg avatar Sep 20 '22 13:09 brunoerg

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.

luke-jr avatar Sep 20 '22 14:09 luke-jr

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.

w0xlt avatar Sep 21 '22 12:09 w0xlt

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()).

luke-jr avatar Sep 22 '22 03:09 luke-jr

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 avatar Sep 22 '22 12:09 brunoerg

Force-pushed addressing @vasild's review.

brunoerg avatar Feb 17 '23 18:02 brunoerg

Force-pushed addressing lastest @vasild's review

brunoerg avatar Feb 20 '23 22:02 brunoerg

Rebased

brunoerg avatar Mar 09 '23 14:03 brunoerg

ACK fb3e812277041f239b97b88689a5076796d75b9b

achow101 avatar Oct 26 '23 18:10 achow101