Skip to content

Route peer store mutations through a single persist-first path - #1234

Merged
tankyleo merged 2 commits into
lightningdevkit:mainfrom
tnull:2026-11-peer-store-single-mutation-path
Nov 17, 2026
Merged

tankyleo merged 2 commits into
lightningdevkit:mainfrom
tnull:2026-11-peer-store-single-mutation-path

Conversation

@tnull

@tnull tnull commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Several recent PRs each fixed one symptom of the peer store's persistence
handling. This addresses them at the store level instead:

  • add_peer and remove_peer now share one mutation path that persists
    the updated peer set before publishing it to readers, so the in-memory
    set can no longer get ahead of the persisted one and a retry after a
    failed write persists again.
  • Removing a peer that isn't stored no longer writes.
  • The persisted peer count uses CollectionLength, as Use CollectionLength for the persisted event queue length #1230 did for the
    event queue, and we no longer preallocate based on it.
  • After a write whose outcome is unknown (failed or cancelled, but possibly
    applied by the store), the next mutation rewrites the peer set even if it
    changes nothing.

Supersedes #1150 and the peer-store half of #1215. It also replaces the PeerStore rewrite in #1175, which can be rebased to only add upsert_peer and its call-site changes on top. #1149 needed the #1150 fix and can now be rebased onto this.

Closes #1142.

@tnull tnull added this to the 0.8 milestone Nov 12, 2026
@tnull
tnull requested a review from jkczyz November 12, 2026 12:51
@ldk-reviews-bot

ldk-reviews-bot commented Nov 12, 2026 •

Copy link
Copy Markdown

👋 Thanks for assigning @tankyleo 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.

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

LGTM!

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

LGTM, one nit can we squash all the commits for the peer store mutations into a single one ?

Adding a peer inserted it into the in-memory set before the write was
awaited, so a failed write followed by a retry reported success while
the peer was gone after a restart. Removing a peer went through
separate hand-rolled logic, rewrote the peer set even when the peer
was unknown, and thereby could fail for a removal with nothing to do.

Route both mutations through a single helper that applies the change
to a copy of the peer set, persists it, and only then publishes it to
readers, skipping the write if nothing changed. As a write that fails
or is cancelled may still have reached the store, the next mutation
rewrites the peer set even if it changes nothing, so that, e.g., a
removed peer is not reconnected to after a restart.

Co-Authored-By: HAL 9000
The number of persisted peers was written as a plain `u16`, so a peer
set of more than 65535 entries had its count truncated and most of its
peers silently dropped on the next restart. The event queue had the
same issue, which was fixed by switching to `CollectionLength`.

Do the same for the peer store. As `CollectionLength` encodes counts
below `u16::MAX` exactly as before, previously persisted peer sets
are still read and smaller ones are still written unchanged. As the
count read from disk is thereby no longer bounded to `u16::MAX`, we
also stop preallocating for it, so that a corrupted count fails to
read rather than exhausting memory.

Co-Authored-By: HAL 9000
@tnull
tnull force-pushed the 2026-11-peer-store-single-mutation-path branch from 1497cf4 to d968165 Compare November 17, 2026 08:57
@tnull

tnull commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

LGTM, one nit can we squash all the commits for the peer store mutations into a single one ?

Not quite sure why it's preferable, but done.

@tnull
tnull requested a review from tankyleo November 17, 2026 08:57
@tankyleo

Copy link
Copy Markdown
Contributor

Not quite sure why it's preferable, but done.

For context this is the reason I asked: 8544da8 introduced a regression by skipping writes when removing a peer absent from memory, potentially leaving it in storage after an earlier failed or cancelled add had actually persisted it. 1497cf4 fixed this by tracking unconfirmed writes and forcing the next mutation to persist even when the in-memory map was unchanged.

@tankyleo
tankyleo merged commit 2a752ec into lightningdevkit:main Nov 17, 2026
14 of 27 checks passed
vincenzopalazzo added a commit to vincenzopalazzo/ldk-node that referenced this pull request Nov 19, 2026
Node::connect(..., persist=true) and open_channel kept the first stored
address, so a later working address was never used for reconnect (lightningdevkit#700).

upsert_peer replaces the address through the persist-first update_peers
path from lightningdevkit#1234. connect and open_channel call it only when this call
dialed the requested address. An already-connected peer keeps add_peer,
so an unverified address cannot overwrite a working one.

The LSPS2 receive path stays add_peer (lightningdevkit#1147).

Co-authored-by: Bartok <259807879+Bartok9@users.noreply.github.com>
Assisted-by: goose (AI)
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.

Do not report unpersisted peers as successfully stored

4 participants