net: do not `break` when `addr` is not from a distinct network group #27863

pull brunoerg wants to merge 1 commits into bitcoin:master from brunoerg:2023-06-net-netgroup-continue changing 1 files +1 −1
  1. brunoerg commented at 5:00 PM on June 12, 2023: contributor

    When the address is from a network group we already caught, do a continue and try to find another address until conditions are met or we reach the limit (nTries).

  2. net: do not `break` when `addr` is not from a distinct network group
    When the address is from a network group we already caught,
    do a `continue` and try to find another address until conditions
    are met or we reach the limit (`nTries`).
    5fa4055452
  3. DrahtBot commented at 5:00 PM on June 12, 2023: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK mzumsande, amitiuttarwar, achow101

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

  4. DrahtBot added the label P2P on Jun 12, 2023
  5. brunoerg commented at 2:38 PM on June 14, 2023: contributor
  6. amitiuttarwar commented at 12:51 AM on June 15, 2023: contributor

    can you contextualize this change? generally it's helpful when the OP & commit message explain why the change is meaningful vs what the change is doing. that'd be valuable for me to evaluate this patch

  7. brunoerg commented at 1:09 AM on June 15, 2023: contributor

    can you contextualize this change? generally it's helpful when the OP & commit message explain why the change is meaningful vs what the change is doing. that'd be valuable for me to evaluate this patch @amitiuttarwar, sure! We require outbound IPv4/IPv6 connections to be to distinct network groups. E.g. in case asmap has been used, we expect to have our outbound connections from different Autonomous System (AS). In the current implementation, we basically select an address (from addrman) and if it's not from a distinct network group, we do a break. This means that we're going to leave the loop, sleep again, add seed nodes, recalculates already-connected network ranges and so on. However, we could instead do a continue and try to find another address that can meet our requirement (distinct network group). That is, we could try it 100x (nTried) before really leaving this loop.

  8. mzumsande commented at 4:01 PM on June 15, 2023: contributor

    utACK 5fa4055452861ca1700008e1761815e88b29fae7

    I once wrote up my understanding of the continue/break question here on a related question. In short, think it's better to continue if we just picked an unlucky address from addrman, but nothing went wrong fundamentally, so it seems better to try another address right away instead of leaving the loop and having a sleep.

    I think the upside is that we would find new connection faster (less sleeping for half a second), a possible downside might be that if for some reason we wouldn't have a suitable peer in addrman (because we might have run out of netgroups) we would spend more resources in the net thread. But after #27374, that shouldn't be possible.

  9. amitiuttarwar commented at 11:10 PM on June 15, 2023: contributor

    utACK 5fa4055452861ca1700008e1761815e88b29fae7

    thanks for the context, I was initially unclear if it was a performance optimization or causing another issue.

    makes sense to me for this case to continue to the next iteration of the inner loop to try again with another address, rather than break to jump back to the outer loop, sleeping for 1/2 second then iterating through all the nodes to aggregate stats again.

    it seems like the same logic could also be applied to the next conditional, which is also focused on the address that was randomly selected:

    if (!addr.IsValid() || IsLocal(addr)) {
        break;
    }
    
  10. brunoerg commented at 2:21 PM on June 16, 2023: contributor

    it seems like the same logic could also be applied to the next conditional, which is also focused on the address that was randomly selected

    I think we can leave that one with break. It's an extreme conditional, because we don't expect to have any invalid or local addresses in addrman.

  11. amitiuttarwar commented at 11:24 PM on June 16, 2023: contributor

    I think we can leave that one with break. It's an extreme conditional, because we don't expect to have any invalid or local addresses in addrman.

    ok sure. I agree it should be unlikely / unexpected to hit, but I don't see why it would be preferable to jump back to the outer loop & sleep, etc. vs have a tighter iteration (esp since ThreadOpenConnections is a significant influencer to addrman info). but no worries, I don't think it matters too much either way & not necessary for this PR.

  12. achow101 commented at 11:35 PM on June 29, 2023: member

    ACK 5fa4055452861ca1700008e1761815e88b29fae7

    Agree that continue makes sense if we're simply unlucky.

  13. achow101 merged this on Jun 29, 2023
  14. achow101 closed this on Jun 29, 2023

  15. sidhujag referenced this in commit 1447a2db20 on Jun 30, 2023
  16. luke-jr referenced this in commit bd6b7ccdd4 on Aug 16, 2023
  17. bitcoin locked this on Jun 28, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-02 12:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me