Skip to content

Protect wallet birthdays from reorg - #1274

Open
tankyleo wants to merge 10 commits into
lightningdevkit:mainfrom
tankyleo:2026-12-wallet-birthday-reorg
Open

tankyleo wants to merge 10 commits into
lightningdevkit:mainfrom
tankyleo:2026-12-wallet-birthday-reorg

Conversation

@tankyleo

@tankyleo tankyleo commented Dec 4, 2026

Copy link
Copy Markdown
Contributor
    Protect new wallets from reorgs near their birthday

    For wallets using Bitcoin Core, a reorg that removes the birthday
    checkpoint can leave BDK unable to connect replacement blocks to its saved
    checkpoints. The wallet then stops tracking the active chain: incoming
    payments can go undiscovered and balances can remain stale. Restarting
    reloads the same orphaned checkpoint, leaving the user needing wallet
    recovery.

    Using the current tip as the birthday exposes a newly created wallet to
    this failure after even a one-block reorg, including before its first
    sync. Avoiding historical scanning should not make ordinary chain
    reorganizations require user intervention.

    We budget for reorgs of up to 10 blocks. Accepting up to 10 additional
    blocks of initial scanning gives the wallet a checkpoint outside that
    window. This bounded startup cost is preferable to leaving a fresh wallet
    unable to synchronize until recovered.

    Co-Authored-By: Codex

For wallets using Bitcoin Core, a reorg that removes the birthday
checkpoint can leave BDK unable to connect replacement blocks to its saved
checkpoints. The wallet then stops tracking the active chain: incoming
payments can go undiscovered and balances can remain stale. Restarting
reloads the same orphaned checkpoint, leaving the user needing wallet
recovery.

Using the current tip as the birthday exposes a newly created wallet to
this failure after even a one-block reorg, including before its first
sync. Avoiding historical scanning should not make ordinary chain
reorganizations require user intervention.

We budget for reorgs of up to 10 blocks. Accepting up to 10 additional
blocks of initial scanning gives the wallet a checkpoint outside that
window. This bounded startup cost is preferable to leaving a fresh wallet
unable to synchronize until recovered.

Co-Authored-By: Codex
Requesting a recent rescan height should not leave a new Bitcoin Core
wallet more vulnerable to reorgs than using the default. If its birthday
checkpoint is removed, the wallet can stop following the active chain.
Restarting reloads the same checkpoint, leaving the user needing wallet
recovery.

Treat the requested height as history we must include, while retaining
at least the same 10-block margin as the default. Scanning a little
extra history is preferable to requiring wallet recovery after an
ordinary reorg, and earlier requests must never lose any requested
history. A fresh wallet therefore needs a known chain tip even when
an explicit rescan height is supplied.

Co-Authored-By: Codex
@ldk-reviews-bot

ldk-reviews-bot commented Dec 4, 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.

@tankyleo
tankyleo requested a review from tnull December 4, 2026 01:20

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

No blockers from my side, a few comments:

  1. tests/integration_tests_rust.rs:2885-2887: The comment says finding the payments "must now depend on the explicit, inclusive rescan height" after mining 10 more blocks, but nothing asserts that. Could we build a default-config node at that point and check that it sees a zero balance?

  2. src/builder.rs:125: Rather than a bare 10, could we derive WALLET_BIRTHDAY_BLOCK_BUFFER from ANTI_REORG_DELAY * 2? That is the depth of BlockLocator::previous_blocks, i.e., how deep a reorg LDK's own listeners can lose their tip and still find a known ancestor, so the wallet
    would get the same tolerance for a stated reason. It would mean updating the "10 blocks" wording in the four builder doc comments and the changelog, plus the block counts the tests are built around.

  3. src/builder.rs:1924-1951: A fresh wallet without a rescan height now needs a get_block_hash_by_height call after the tip fetch. If that fails we return WalletSetupFailed rather than ChainTipFetchFailed, although the cause is the same kind of backend problem. Should we map it to
    the same error, or at least document it?

  4. src/builder.rs:1903: chain_tip_opt.ok_or(BuildError::ChainTipFetchFailed)? can never fire: the guard at :1876 already returns for bitcoind without a tip, and a rescan height can only be set with bitcoind. Harmless, but it is an unlogged duplicate of the logged path above.

  5. tests/integration_tests_rust.rs:2889-2890: "both transactions are found 11 blocks after the configured rescan height" is now about 21 blocks given the extra 10 mined just above. "Retaining the previous test's case" also describes history rather than what the test does.

  6. Docs/changelog: A reorg that goes deeper than the buffer before the wallet has synced past its checkpoint still leaves apply_block_events failing in Wallet::block_connected (src/wallet/mod.rs:4655-4665), with only an error log per block. That is pre-existing and this PR narrows the
    window considerably, but could we mention that the protection is bounded?

Also, did you confirm that wallet_birthday_margin_survives_reorg fails on main?

@tankyleo

tankyleo commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

All comments should be addressed in these fixups thank you.

Also, did you confirm that wallet_birthday_margin_survives_reorg fails on main?

Yes confirmed here.

@tankyleo
tankyleo requested a review from tnull December 4, 2026 19:41
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