Skip to content

Persist the peer before initiating a channel - #1149

Open
cryptokvc wants to merge 2 commits into
lightningdevkit:mainfrom
cryptokvc:fix/open-channel-persist-peer-first
Open

cryptokvc wants to merge 2 commits into
lightningdevkit:mainfrom
cryptokvc:fix/open-channel-persist-peer-first

Conversation

@cryptokvc

Copy link
Copy Markdown

Fixes #1143.

open_channel_inner created the channel and only then wrote the peer to the peer store. If that write failed, the API returned PersistenceFailed for a channel that was already live, and a caller retrying would open a second channel (and push the amount again).

This takes the first option from the issue: the peer is persisted before create_channel, so a persistence failure is returned before anything is initiated. If channel creation then fails and the peer was only stored for this attempt, it is removed again so we don't keep reconnecting to it.

While testing the retry path I hit the cache problem the issue mentions: PeerStore::add_peer inserted the peer into memory before writing, so after a failed write the next add_peer found it and returned Ok without persisting. add_peer now updates the in-memory set only once the write succeeds, as remove_peer already does. Without this, the retry below would open the channel but leave the peer unpersisted.

Tests

  • peer_store::tests::add_peer_does_not_mutate_memory_if_persist_fails (unit): fails on main, passes with this change.
  • channel_open_fails_cleanly_when_peer_persistence_fails (integration): fails only the peer-store write on node A and checks that open_channel returns PersistenceFailed with no channel and the peer not persisted; then, with the store recovered, a retry opens exactly one channel and persists the peer.

cargo test --lib passes (202 tests) and cargo fmt is clean. I could not run the integration test locally (bitcoind/electrs can't be downloaded in my environment), so it is only compile-checked (cargo test --test integration_tests_rust --no-run); CI is authoritative for it.

This change was prepared with the help of an AI assistant (Claude); I reviewed the code and ran the tests above.

cryptokvc and others added 2 commits October 11, 2026 11:15
open_channel_inner created the channel and only then wrote the peer to the
peer store. If that write failed, the call returned PersistenceFailed for a
channel that already existed, and a caller retrying would open a second one.

Persist the peer first and only then create the channel, removing the peer
again if channel creation fails and it was only stored for this attempt.

PeerStore::add_peer also inserted the peer into memory before persisting it,
so after a failed write the next add_peer saw the peer and skipped the write.
Update the in-memory set only once the write succeeds, as remove_peer already
does.

Fixes lightningdevkit#1143.

This change was prepared with the help of an AI assistant (Claude) and
reviewed and tested before submission.

Co-authored-by: Claude <noreply@anthropic.com>
Inject a failure for the peer-store write on node A and check that
open_channel returns PersistenceFailed with no channel created and the peer
not persisted. Once the store recovers, a retry opens exactly one channel
and persists the peer.

This change was prepared with the help of an AI assistant (Claude) and
reviewed before submission.

Co-authored-by: Claude <noreply@anthropic.com>
@ldk-reviews-bot

ldk-reviews-bot commented Oct 11, 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 11, 2026 09:26
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.

Avoid returning failure after initiating a channel

2 participants