fix: add secondary pub_key check to prevent self-connections during early init #2145
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
test_three_node_network_connectivitytest started failing after both PR #2140 (self-connection guard) and PR #2142 (ConnectResponse address fix) were merged. Neither PR caused failures individually, but combined they exposed a race condition.Root cause: When a gateway has no other peers during early initialization, connect requests can be forwarded back to itself. The existing
peer_keycheck inshould_accept()fails to catch these self-connections becauseget_peer_key()returnsNoneduring early initialization—the node doesn't know its ownpeer_keyyet.The
PeerIdequality check is address-based (not pub_key-based), so comparingpeer_keyonly works when the address matches exactly. But during early init, the node may not have recorded its own address yet.This Solution
Add a secondary check that compares
pub_keydirectly:This works because:
pub_keyis always set inConnectionManager(unlikepeer_keywhich isOption<PeerId>)pub_keydefinitively means same node, regardless of addresspeer_keyisNoneTesting
rejects_self_connection_by_pubkey_when_peer_key_not_settest that:ConnectionManagerwithpeer_key = None(simulating early init)should_accept()rejects aPeerIdwith matchingpub_keyFixes
Closes #2139
[AI-assisted - Claude]