Skip to content

Commit 5cac4e1

Browse files
sanityclaude
andcommitted
fix: resolve address mismatch in handle_connect_peer for transient promotion
The peer's advertised address (from PeerKeyLocation) may differ from the actual TCP connection's remote address stored in self.connections: - PeerKeyLocation typically contains the peer's listening port - self.connections is keyed by the actual TCP source port (ephemeral) Previously, pub_key lookup was only tried when the address was unspecified. Now we always try pub_key lookup first, ensuring we find the correct connection entry regardless of address mismatch. This fixes the CI-only failure in test_three_node_network_connectivity where QueryConnections returned insufficient connections because pub_key wasn't being set on connections with mismatched addresses. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 71fba53 commit 5cac4e1

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

crates/core/src/node/network_bridge/p2p_protoc.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,24 +1446,32 @@ impl P2pConnManager {
14461446
.socket_addr()
14471447
.unwrap_or_else(|| SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0));
14481448

1449-
if peer_addr.ip().is_unspecified() {
1450-
if let Some((existing_addr, _)) = self.connection_entry_by_pub_key(peer.pub_key()) {
1451-
peer_addr = existing_addr;
1452-
peer = PeerKeyLocation::new(peer.pub_key().clone(), existing_addr);
1449+
// IMPORTANT: Always try pub_key lookup first, not just for unspecified addresses.
1450+
// The peer's advertised address (from PeerKeyLocation) may differ from the actual
1451+
// TCP connection's remote address stored in self.connections. For example:
1452+
// - PeerKeyLocation may have the peer's listening port (e.g., 37791)
1453+
// - self.connections is keyed by the actual TCP source port (ephemeral port)
1454+
// By looking up via pub_key (populated when we receive the first message from this
1455+
// peer), we can find the correct connection entry regardless of address mismatch.
1456+
if let Some((existing_addr, _)) = self.connection_entry_by_pub_key(peer.pub_key()) {
1457+
if existing_addr != peer_addr {
14531458
tracing::info!(
14541459
tx = %tx,
14551460
remote = %peer,
1456-
fallback_addr = %peer_addr,
1461+
advertised_addr = %peer_addr,
1462+
actual_addr = %existing_addr,
14571463
transient,
1458-
"ConnectPeer provided unspecified address; using existing connection address"
1459-
);
1460-
} else {
1461-
tracing::debug!(
1462-
tx = %tx,
1463-
transient,
1464-
"ConnectPeer received unspecified address without existing connection reference"
1464+
"ConnectPeer: using existing connection address (differs from advertised)"
14651465
);
14661466
}
1467+
peer_addr = existing_addr;
1468+
peer = PeerKeyLocation::new(peer.pub_key().clone(), existing_addr);
1469+
} else if peer_addr.ip().is_unspecified() {
1470+
tracing::debug!(
1471+
tx = %tx,
1472+
transient,
1473+
"ConnectPeer received unspecified address without existing connection reference"
1474+
);
14671475
}
14681476

14691477
tracing::info!(

0 commit comments

Comments
 (0)