Skip to content

Persist the peer before initiating a channel - #1252

Open
cryptokvc wants to merge 2 commits into
lightningdevkit:mainfrom
cryptokvc:persist-peer-before-channel
Open

cryptokvc wants to merge 2 commits into
lightningdevkit:mainfrom
cryptokvc:persist-peer-before-channel

Conversation

@cryptokvc

Copy link
Copy Markdown
Contributor

Fixes #1143. Supersedes #1149, rebased onto current main as two additive commits, as requested there.

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, and before the fee-rate override is registered. 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, unless we have a channel with it: a concurrent open_channel to the same peer may have stored it too and succeeded in the meantime. That check mirrors remove_peer_after_reconnect (from @Camillarhi's review on #1149).

The PeerStore::add_peer caching issue the issue mentions is left to #1150, as agreed on #1149.

Commits

  1. Persist the peer before initiating a channel: src/lib.rs (+25/−1).
  2. Test that a failed peer write does not leave a live channel: integration test with a KVStore wrapper that fails writes of the peer list on demand. With the failure on, open_channel returns PersistenceFailed and no channel exists; once the store recovers, a retry opens exactly one channel and the peer is persisted.

Testing on main 0fc5565: cargo test --lib 483 passed, and cargo clippy --all-targets compiles the integration tests. The only new clippy notes are in the new test and match constructs already used by the existing tests in that file (EsploraSyncConfig::default() then a field assignment, node_entropy.into()). rustfmt is clean. I couldn't run the bitcoind/electrs integration tests in my environment.

🤖 Generated with Claude Code

https://claude.ai/code/session_014cRzwuyJe8BGwXowsQ3kpw

cryptokvc and others added 2 commits November 20, 2026 04:09
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.

Persist the peer first, so a persistence failure is returned before
anything is initiated. If channel creation then fails and the peer was
only stored for this attempt, remove it again so we don't keep
reconnecting to it, unless we have a channel with it (a concurrent
open_channel to the same peer may have succeeded in the meantime), as
remove_peer_after_reconnect does.

Co-authored-by: Claude <noreply@anthropic.com>
PeerWriteFailingStore wraps an InMemoryStore and fails writes of the
persisted peer list while a flag is set. With the flag on, open_channel
returns PersistenceFailed and no channel exists. Once the store recovers,
a retry opens exactly one channel and the peer is persisted.

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

ldk-reviews-bot commented Nov 20, 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.

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