Skip to content
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

p2p: cleanup LookupIntern, Lookup and LookupHost #26261

Merged
merged 4 commits into from
May 30, 2023

Conversation

brunoerg
Copy link
Contributor

@brunoerg brunoerg commented Oct 5, 2022

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.

@DrahtBot DrahtBot added the P2P label Oct 5, 2022
@stickies-v
Copy link
Contributor

Wouldn't a std::optional approach make more sense here? And I think it'd be appropriate to update all Lookup functions at once, instead of just LookupInternal?

@brunoerg
Copy link
Contributor Author

brunoerg commented Oct 6, 2022

@stickies-v

Wouldn't a std::optional approach make more sense here?

Given my experience in #26078, I'm not sure about using std::optional here. At some point, I'd have to check if it has_value, if so, I'd have to check the content of the vector anyway. Not sure about the benefits and code quality here.

And I think it'd be appropriate to update all Lookup functions at once, instead of just LookupInternal?

I agree, I'd planned to do it in a follow-up (tried to keep this PR simple/easy to review) but could do it here, no problem.

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.

Not sure about the benefits and code quality here.

I think you're right. At first sight it looked like in the current implementation there was the need to distinguish between invalid parameters (!ContainsNoNUL(name)) and a name that just wouldn't resolve to any addresses (empty vector), but upon further inspection that seems to not be true.

but could do it here, no problem.

I think the main benefit of doing them all together is to ensure they all adhere to the same consistent interface. From a review point of view, that's most efficient too, and it shouldn't be a huge change anyway.

src/netbase.cpp Outdated Show resolved Hide resolved
@brunoerg
Copy link
Contributor Author

brunoerg commented Oct 7, 2022

I think the main benefit of doing them all together is to ensure they all adhere to the same consistent interface. From a review point of view, that's most efficient too, and it shouldn't be a huge change anyway.

You're right, I am gonna turn this PR to draft to work on it.

@brunoerg brunoerg marked this pull request as draft October 7, 2022 12:57
@brunoerg brunoerg changed the title p2p: return std::vector<CNetAddr> in LookupIntern p2p: cleanup LookupIntern, Lookup and LookupHost Oct 10, 2022
@brunoerg brunoerg force-pushed the 2022-10-cleanup-netbase branch 2 times, most recently from 55829ab to 2e9a2f7 Compare October 10, 2022 20:50
@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 11, 2022

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

Reviews

See the guideline for information on the review process.

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

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #27679 (ZMQ: Support UNIX domain sockets by pinheadmz)
  • #27375 (net: support unix domain sockets for -proxy and -onion by pinheadmz)
  • #27071 (Handle CJDNS from LookupSubNet() by vasild)
  • #26078 (p2p: return CSubNet in LookupSubNet by brunoerg)
  • #25284 (net: Use serialization parameters for CAddress serialization by MarcoFalke)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

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.

Approach ACK. Left a few comments and nits - a lot of the std::optional comments recur in many different places in this PR, I didn't necessarily always point them all out.

src/netbase.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/netbase.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/netbase.h Outdated Show resolved Hide resolved
src/netbase.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/init.cpp Outdated Show resolved Hide resolved
src/test/netbase_tests.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
@brunoerg brunoerg marked this pull request as draft October 20, 2022 13:36
@brunoerg brunoerg marked this pull request as ready for review October 20, 2022 15:58
@brunoerg
Copy link
Contributor Author

Thanks @stickies-v for your valuable review, just addressed your suggestions!

Ready for review!

@brunoerg
Copy link
Contributor Author

Thanks, @stickies-v for your review. Will address your latest suggestions in a follow-up PR.

@brunoerg
Copy link
Contributor Author

I had to touch bench code to fix an error CI pointed out, so I took the moment to address some suggestions from @stickies-v.

Addressed:
#26261 (comment)
#26261 (comment)
#26261 (comment)

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.

re-ACK 4eee95e

Verified that the only changes are a refactoring to bench, removing now unnecessary helper functions ResolveIP and ResolveService, and incorporating my previous outstanding review comments.

src/bench/addrman.cpp Outdated Show resolved Hide resolved
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 4eee95e

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.

re-ACK 4eee95e

Left two comments below, one nit and one "thinking out loud what could happen in the worst case", both no stoppers.

src/rpc/net.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Show resolved Hide resolved
@stickies-v
Copy link
Contributor

@brunoerg are you going to address nits or leave as is? I'd really like to get this merged asap to prevent further rebase conflicts. Happy to quickly re-ack nits too, though.

@brunoerg
Copy link
Contributor Author

brunoerg commented May 26, 2023

@stickies-v I'm addressing them at this moment, will push soon.

@brunoerg
Copy link
Contributor Author

Force-pushed addressing:

Maybe done with nits, and we can have it merged?

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.

re-ACK 5c832c3 - just addressing two nits, no other changes

git range-diff 8b59231641845f71df37e163bf5b8157fb197d05 4eee95e57bb6f773bcaeb405bca949f158a62134 5c832c3820253affc65c0ed490e26e5b0a4d5c9b
1:  c0cf3d70e = 1:  5c1774a56 p2p, refactor: return `std::vector<CNetAddr>` in `LookupIntern`
2:  94b5edbf6 = 2:  7799eb125 p2p, refactor: return `std::vector<CNetAddr>` in `LookupHost`
3:  617bc481e ! 3:  34bcdfc6a p2p, refactor: return vector/optional<CService> in `Lookup`
    @@ src/bench/addrman.cpp: static void AddrManSelectFromAlmostEmpty(benchmark::Bench
     -    CService addr = ResolveService("250.3.1.1", 8333);
     -    addrman.Add({CAddress(addr, NODE_NONE)}, ResolveService("250.3.1.1", 8333));
     +    CService addr = Lookup("250.3.1.1", 8333, false).value();
    -+    addrman.Add({CAddress(addr, NODE_NONE)}, Lookup("250.3.1.1", 8333, false).value());
    ++    addrman.Add({CAddress(addr, NODE_NONE)}, addr);

          bench.run([&] {
              (void)addrman.Select();
4:  4eee95e57 ! 4:  5c832c382 p2p, refactor: return `std::optional<CNetAddr>` in `LookupHost`
    @@ src/rpc/net.cpp: static RPCHelpMan setban()
     -        CNetAddr resolved;
     -        LookupHost(request.params[0].get_str(), resolved, false);
     -        netAddr = resolved;
    -+        const std::optional<CNetAddr> addr = LookupHost(request.params[0].get_str(), false);
    ++        const std::optional<CNetAddr> addr{LookupHost(request.params[0].get_str(), false)};
     +        if (addr.has_value()) {
     +            netAddr = addr.value();
     +        }

@DrahtBot DrahtBot requested review from theStack and vasild May 26, 2023 17:12
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.

re-ACK 5c832c3

Maybe done with nits, and we can have it merged?

Agree!

@achow101
Copy link
Member

ACK 5c832c3

@achow101 achow101 merged commit 7130048 into bitcoin:master May 30, 2023
16 checks passed
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
@bitcoin bitcoin locked and limited conversation to collaborators May 29, 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.

None yet

6 participants