Skip to content

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

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

vincenzopalazzo wants to merge 1 commit 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.

Problem

PeerStore::add_peer returned early when the pubkey was already
stored, so Node::connect(node_id, new_address, persist = true) silently
kept the stale address for reconnection.

PeerStore::add_peer returned early when the pubkey was already stored,
so Node::connect(..., persist=true) with a new address silently kept
the stale address used for reconnection (see lightningdevkit#700).

Split add_peer into two methods sharing a persist-then-apply helper,
following the maintainer review on lightningdevkit#1002:

- upsert_peer updates the stored address on re-add. Used where the
  address is explicitly supplied: Node::connect with persist set,
  open_channel, and the LSPS2 payment path.
- add_peer_if_missing only stores unknown peers. Used for the
  gossip-derived address of inbound channel counterparties, which
  must not override an explicitly stored address.

In-memory state is only mutated after a successful write, matching
remove_peer. Following the review suggestion on lightningdevkit#1002, avoid cloning
the entire peer map: apply the update temporarily under the write lock
for serialization, restore it, persist, then apply it permanently.
Readers cannot observe the temporary value as they need the same lock.
Re-adding an unchanged peer is a no-op. remove_peer is switched to
the same pattern for consistency.

Addresses the peer-store side of lightningdevkit#700. The liquidity-source side
(NodeBuilder/Liquidity add_liquidity_source) is deliberately left
out here; it is tracked in lightningdevkit#1147.

Supersedes lightningdevkit#1002 and lightningdevkit#1115.

Co-authored-by: Bartok <259807879+Bartok9@users.noreply.github.com>
Assisted-by: goose (AI)
@ldk-reviews-bot

ldk-reviews-bot commented Oct 16, 2026 •

Copy link
Copy Markdown

I've assigned @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
}
}

self.persist_and_apply(peer_info).await

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.

This overlaps with #1150, which already makes add_peer persist before publishing. Whichever lands second will need a rebase. Maybe land #1150 first and keep this PR to the upsert semantics?

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.

3 participants