Skip to content

fix(peer_store): upsert peer address when re-adding known peer - #1175

Open
vincenzopalazzo wants to merge 2 commits into
lightningdevkit:mainfrom
vincenzopalazzo:fix/peer-store-upsert-address
Open

vincenzopalazzo wants to merge 2 commits into
lightningdevkit:mainfrom
vincenzopalazzo:fix/peer-store-upsert-address

Conversation

@vincenzopalazzo

@vincenzopalazzo vincenzopalazzo commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Supersedes #1002 and #1115. Addresses the peer-store side of #700; the
liquidity-source side is tracked in #1147.

Rebased onto the persist-first peer store path from #1234.

upsert_peer replaces a stored address through update_peers.
Node::connect and open_channel call it only when this call establishes
a new outbound connection to the given address (ConnectPeerResult::Dialed).
Joining another attempt, or finding the peer already connected (including
an inbound connection), keeps add_peer, so an address we did not dial
cannot overwrite a working one.

A connect that joins a failed attempt to a different address logs that
error and retries its own address, so a reconnect stuck on a stale address
cannot swallow the new one (#700). Joining a failed attempt to the same
address returns that error, and only one outbound dial is in flight per peer.

The LSPS2 receive path stays add_peer.

Assisted-by: Cursor Grok (AI)

@ldk-reviews-bot

ldk-reviews-bot commented Oct 16, 2026 •

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@ldk-reviews-bot
ldk-reviews-bot requested a review from tnull October 16, 2026 21:19

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

🤖 Potential bug: an unverified address can overwrite a working peer address

connect_peer_if_necessary returns Ok(()) without dialing when the peer is already connected, so the subsequent upsert_peer can persist an address we never connected to. For example, if a peer is connected at good:9735, calling node.connect(X, "typo:9735", true) succeeds and replaces the working address. After a restart, the reconnect loop uses the typo and fails to reconnect. The same issue applies to open_channel_inner.

Could we only upsert when we actually dialed the requested address? Comparing with PeerDetails::socket_address won't work because, for inbound peers, it's the ephemeral source address rather than the listening address. A test covering an already-connected peer with a different requested address would help prevent this regression.

Comment thread src/peer_store.rs Resolved
@tnull

tnull commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Please rebase this onto #1234.

Comment thread src/peer_store.rs Resolved
@vincenzopalazzo
vincenzopalazzo force-pushed the fix/peer-store-upsert-address branch from 1b28e63 to 358673c Compare November 19, 2026 09:58
@vincenzopalazzo

Copy link
Copy Markdown
Member Author

Sorry for delay, this should be ready for another pass!

@tnull tnull left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Claude:

  1. !already_connected plus Ok(()) doesn't prove this call dialed address: connect_peer_if_necessary also succeeds after joining a pending attempt (e.g. the reconnect loop dialing the stored address) or after an inbound connection. ConnectionManager should return whether its own
    dial succeeded, and only then should we upsert.
  2. A connect that joins a failing reconnect attempt never tries the new address, which is the #700 case. It should fall through to its own dial.
  3. The new integration test also passes on main. Needs one where the stored address actually changes: connect via the first listening address, restart node_b, connect via the second.

@vincenzopalazzo

vincenzopalazzo commented Dec 2, 2026 •

Copy link
Copy Markdown
Member Author

Ok, I pushed the fix that was commented on by Claude and made a round of review with Neon; it should be fine for another round now.

thanks

@vincenzopalazzo
vincenzopalazzo force-pushed the fix/peer-store-upsert-address branch from f440eca to 8e3bc29 Compare December 2, 2026 20:32
vincenzopalazzo and others added 2 commits December 3, 2026 12:42
Replaces the stored address through the persist-first update_peers path
from lightningdevkit#1234. No-ops when the peer is already stored at the same address.

Co-authored-by: Bartok <259807879+Bartok9@users.noreply.github.com>
Assisted-by: Cursor Grok (AI)
Co-authored-by: Cursor <cursoragent@cursor.com>
connect_peer_if_necessary reports Dialed only for its own outbound dial.
Joining another attempt or an inbound connection is NotDialed, so an
unverified address cannot overwrite a working one.

Pending attempts store their address. A connect that joins a failed
attempt to the same address returns that error; to a different address
it logs and retries, so a reconnect stuck on a stale address cannot
swallow a new one (lightningdevkit#700).

Assisted-by: Cursor Grok (AI)
Co-authored-by: Cursor <cursoragent@cursor.com>
@vincenzopalazzo
vincenzopalazzo force-pushed the fix/peer-store-upsert-address branch from cc27889 to 98e0d6c Compare December 3, 2026 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants