Skip to content

Conversation

brunoerg
Copy link
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).

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`).
@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 12, 2023

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

@DrahtBot DrahtBot added the P2P label Jun 12, 2023
@brunoerg
Copy link
Contributor Author

cc: @mzumsande

@amitiuttarwar
Copy link
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

@brunoerg
Copy link
Contributor Author

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.

Copy link
Contributor

@mzumsande mzumsande left a comment

Choose a reason for hiding this comment

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

utACK 5fa4055

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.

@amitiuttarwar
Copy link
Contributor

utACK 5fa4055

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;
}

@brunoerg
Copy link
Contributor Author

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.

@amitiuttarwar
Copy link
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.

@achow101
Copy link
Member

ACK 5fa4055

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

@achow101 achow101 merged commit 54ba330 into bitcoin:master Jun 29, 2023
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 30, 2023
…stinct network group

5fa4055 net: do not `break` when `addr` is not from a distinct network group (brunoerg)

Pull request description:

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

ACKs for top commit:
  amitiuttarwar:
    utACK 5fa4055
  achow101:
    ACK 5fa4055
  mzumsande:
    utACK 5fa4055

Tree-SHA512: 225bb6df450b46960db934983c583e862d1a17bacfc46d3657a0eb25a0204e106e8cd18de36764e210e0a92489ab4b5773437e4a641c9b455bde74ff8a041787
luke-jr pushed a commit to luke-jr/bitcoin that referenced this pull request Aug 16, 2023
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`).

Github-Pull: bitcoin#27863
Rebased-From: 5fa4055
@bitcoin bitcoin locked and limited conversation to collaborators Jun 28, 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.

5 participants