Skip to content

net: Avoid undershooting in GetAddressesUnsafe - #34162

Closed
fjahr wants to merge 2 commits into
bitcoin:masterfrom
fjahr:2025-12-33663-alt
Closed

fjahr wants to merge 2 commits into
bitcoin:masterfrom
fjahr:2025-12-33663-alt

Conversation

@fjahr

@fjahr fjahr commented Dec 28, 2025

Copy link
Copy Markdown
Contributor

This is demonstrating an alternative approach to #33663. The author didn't seem to like by suggestion so I am putting it here to be considered by reviewers. I personally prefer this approach due to it's simplicity.

GetAddressesUnsafe filters banned and discouraged addresses if those exist. If addresses are filtered the returned list of addresses can be shorter than originally requested because the list isn't backfilled with more valid addresses. These results end up in GETADDR responses as well as the getnodeaddresses RPC.

Fix this by requesting all available addresses and filter them, then only trim them to the requested max_addresses afterwards. Also adds a functional test which checks the new behavior via getnodeaddresses RPC.

@DrahtBot DrahtBot added the P2P label Dec 28, 2025
@DrahtBot

DrahtBot commented Dec 28, 2025 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34162.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK danielabrozzoni, naiyoma
Stale ACK Bicaru20, bensig

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:

  • #33663 (net: Filter addrman during address selection via AddrPolicy to avoid underfill by waketraindev)

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.

@Bicaru20

Copy link
Copy Markdown
Contributor

Code review ACK b4394c8. I prefer this approach instead of the proposed in #33663 as this one is much simpler. I tested the code an it works as expected.

My only concern is the impact on efficiency, since now we are passing the filters to all available addresses instead of limiting them to max_addresses. However, I believe this will probably not be an issue, as the number should never be large enough to cause problems,

Comment on lines +606 to +607
for addr in addresses:
assert addr["address"] not in banned

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why make this verification? I can't think why getnodeaddresses would return some addresses that are banned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's just a protection against a regression if some refactoring of the banned filtering causes it to not work properly anymore. That may seem a bit far fetched but it also doesn't cost much.

@danielabrozzoni danielabrozzoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concept ACK

I prefer this solution over #33663, as it’s much simpler, and I share the concern that the other one seemed a bit overengineered. (I agree that if we decided to have a AddrPolicy instead, it should also include network and filtered).

I ran test_getnodeaddresses_with_banned on master and it fails, as expected.

My only concern is the impact on efficiency, since now we are passing the filters to all available addresses instead of limiting them to max_addresses.

I think this is a valid concern, and something I wonder too. However, it seems to me that GetAddressesUnsafe is never called in places where performance is critical. These are the call sites:

  • GetAddresses: to recreate the cache when it's expired (happens about once a day)
  • ASMapHealthCheck: health check for asmap, it’s run on startup and once a day
  • When responding to a GETADDR request from a node with NetPermissionFlags::Addr permission
  • getnodeaddresses RPC

I think it would be interesting to try and see the performance difference… I will try to look into it, and report back if I succeed :)

@bensig

bensig commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

ACK b4394c8
tested

fjahr added 2 commits January 21, 2026 09:45
When addresses are filtered by banman, the final return value could be lower than requested. Fix this by requesting and filtering all addresses before returning a trimmed list.
@fjahr
fjahr force-pushed the 2025-12-33663-alt branch from b4394c8 to 3f8fe85 Compare January 21, 2026 08:54
@fjahr

fjahr commented Jan 21, 2026

Copy link
Copy Markdown
Contributor Author

Rebased

@naiyoma naiyoma left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK, I do agree that this approach is simpler, but could be further optimized to improve performance.

For GETADDR, this is my understanding of how we filter addresses. During address selection, and then twice again after selection.

  • Filter for banned and discouraged addresses in GetAddressesUnsafe.
  • Filter for IsValid, IsAddrCompatible, and m_addr_known in PushAddress.

If GetAddr returns 1000 addresses and a user has 100 banned and 100 invalid addresses:

GetAddr() returns:           1000 addresses
After GetAddressesUnsafe():  900 addresses (100 banned removed)
After PushAddress():         800 addresses (100 invalid/incompatible removed)
Final sent to peer:          800 addresses

By fetching all addresses upfront if banman is true and filtering out banned/discouraged addresses and then resizing to max_addresses, we increase the chances of returning 1000 addresses.

I tested 54896f3 by sending a getaddr to my node

And then logged timestamps: start time right before getting all addresses, after Getaddr(), and end time before returning all addresses.

Before change

2026-01-25T15:45:12Z [net] GetAddressesUnsafe Performance:
2026-01-25T15:45:12Z [net]   max_addresses=1000, max_pct=23
2026-01-25T15:45:12Z [net]   GetAddr() returned: 1000 addresses
2026-01-25T15:45:12Z [net]   Actually banned/discouraged: 0
2026-01-25T15:45:12Z [net]   Trimmed by resize: 0
2026-01-25T15:45:12Z [net]   Final count: 1000
2026-01-25T15:45:12Z [net]   Time - GetAddr=1583μs, ban_filter=682μs, total=2265μs
2026-01-25T15:45:12Z [net] GETADDR: GetAddressesUnsafe returned 1000 addresses in 2573μs

After change

2026-01-25T15:13:32Z [net] received: getaddr (0 bytes) peer=5
2026-01-25T15:13:32Z [net] GetAddressesUnsafe Perfomance:
2026-01-25T15:13:32Z [net] GetAddressesUnsafe: Trimmed by resize: 13540
2026-01-25T15:13:32Z [net] GetAddressesUnsafe: Actually banned/discouraged: 0
2026-01-25T15:13:32Z [net] GetAddressesUnsafe: max_addresses=1000, max_pct=23
2026-01-25T15:13:32Z [net] GetAddressesUnsafe: GetAddr() returned 14540 addresses
2026-01-25T15:13:32Z [net] GetAddressesUnsafe: Final count: 1000
2026-01-25T15:13:32Z [net] GetAddressesUnsafe: Time - GetAddr=17960μs, ban_filter=8781μs, total=26741μs
2026-01-25T15:13:32Z [net] GETADDR: GetAddressesUnsafe returned 1000 addresses in 27012μs

This difference isn’t significant to me (0.024439 seconds), since it’s just a few seconds. But I only have a few addresses in my AddrMan. I’d expect this to be slower if I had more banned addresses and more addresses (40k+).
Though performance issue doesn’t bother me as much, since I think it can be improved by not fetching all the addresses, instead max_address * 2 or even max_address * 4 is still sufficient.

@sedited
sedited requested review from naiyoma and removed request for naiyoma March 16, 2026 08:44
@sedited
sedited requested a review from ajtowns March 19, 2026 16:54
Comment thread src/net.cpp
{
std::vector<CAddress> addresses = addrman.get().GetAddr(max_addresses, max_pct, network, filtered);
// Get all addresses if filtering for banned addresses to avoid undershooting max_addresses in the returned vec
std::vector<CAddress> addresses = addrman.get().GetAddr(m_banman ? 0 : max_addresses, max_pct, network, filtered);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just dumping the entire db in a vector doesn't really seem at all sensible; if undershooting is a problem, should be passing the ban filter through to GetAddr somehow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See #33663 and the approach discussion there.

@sedited
sedited requested a review from naiyoma May 23, 2026 08:39
@sedited
sedited requested a review from Bicaru20 August 10, 2026 19:35
@sedited

sedited commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

@ajtowns do you want to take another look here?

@fjahr

fjahr commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing as this has done its job of showing an alternative, simpler approach to #33663 and it wasn't my intention to maintain a competing PR long-term. Some reviewers also seem undecided if the juice is worth the squeeze which is probably why this has stalled. But if someone wants to take this effort over, I would be happy to review. So, can be marked as up for grabs.

@fjahr fjahr closed this Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants