Skip to content

refactor: Return std::optional from GetNameProxy/GetProxy#34741

Open
maflcko wants to merge 2 commits intobitcoin:masterfrom
maflcko:2603-opt-proxy
Open

refactor: Return std::optional from GetNameProxy/GetProxy#34741
maflcko wants to merge 2 commits intobitcoin:masterfrom
maflcko:2603-opt-proxy

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Mar 5, 2026

Currently the getters have a mutable reference as inout param and return a bool to indicate success. This is confusing, because the success bool is redundant with the IsValid() state on the proxy object.

So in theory, the functions could reset the mutable proxy object to an invalid state and return void.

However, this would also be confusing, because devs can forget to check IsValid().

Fix all issues by using std::optional<Proxy>, where devs no longer have to check IsValid() manually, or a separate bool. Note that new code in the repo is already using std::optional<Proxy>, see git grep 'std::optional<Proxy>' bitcoin-core/master. Also, std::optional<Proxy> will enforce checking at compile time, whereas calling Proxy::IsValid is not enforced.

@DrahtBot DrahtBot changed the title refactor: Return std::optional from GetNameProxy/GetProxy refactor: Return std::optional from GetNameProxy/GetProxy Mar 5, 2026
@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 5, 2026

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 ViniciusCestarii

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34520 (refactor: Add [[nodiscard]] to functions returning bool+mutable ref by maflcko)
  • #34486 (net: Reduce local network activity when networkactive=0 by willcl-ark)
  • #32065 (i2p: make a time gap between creating transient sessions and using them by vasild)
  • #30951 (net: option to disallow v1 connection on ipv4 and ipv6 peers by stratospher)

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.

@luke-jr
Copy link
Member

luke-jr commented Mar 6, 2026

Wouldn't util::Result be a better fit? But since IsValid is already there, why introduce a second way to indicate none?

@maflcko
Copy link
Member Author

maflcko commented Mar 10, 2026

Wouldn't util::Result be a better fit?

Why? Result is primarily intended for high-level errors that should be passed to the user, possibly even translated. I think this is low-level enough to not need translated error strings, or error messages at all. Also, adding error strings won't be possible in a refactor-only pull anyway.

But since IsValid is already there, why introduce a second way to indicate none?

It is what other new code is using. You can use git grep 'std::optional<Proxy>' bitcoin-core/master to find those places. Also, std::optional is easier to read and is enforced at compile-time, whereas for Proxy, checking IsValid is not enforced.

Comment on lines +290 to +291
const auto ipv4 = m_node.getProxy((Network)1);
const auto ipv6 = m_node.getProxy((Network)2);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since this section is already being refactored, it might be a good opportunity to replace the magic numbers with Network::NET_IPV4 / Network::NET_IPV6 for clarity.

Suggested change
const auto ipv4 = m_node.getProxy((Network)1);
const auto ipv6 = m_node.getProxy((Network)2);
const auto ipv4 = m_node.getProxy(Network::NET_IPV4);
const auto ipv6 = m_node.getProxy(Network::NET_IPV6);

Copy link
Member Author

Choose a reason for hiding this comment

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

thx, done

Copy link
Contributor

@ViniciusCestarii ViniciusCestarii left a comment

Choose a reason for hiding this comment

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

ACK fa270fd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants