Skip to content

staticaddr/withdraw: add retry and reorg handling for withdrawals#1129

Open
kaldun-tech wants to merge 2 commits intolightninglabs:masterfrom
kaldun-tech:master
Open

staticaddr/withdraw: add retry and reorg handling for withdrawals#1129
kaldun-tech wants to merge 2 commits intolightninglabs:masterfrom
kaldun-tech:master

Conversation

@kaldun-tech
Copy link
Copy Markdown

@kaldun-tech kaldun-tech commented Apr 24, 2026

Summary

Fixes #1087

Addresses three related issues in the static address withdrawal manager:

  1. No retry on registration failure - RegisterSpendNtfn/RegisterConfirmationsNtfn failures silently stopped monitoring
  2. No reorg handling - Reorged transactions weren't re-registered
  3. State persistence race - handleWithdrawal failure prevented state persistence, breaking recovery

Changes

  • Reorder state persistence to happen BEFORE handleWithdrawal so recovery works even if monitoring setup fails
  • Add retry-on-next-block for registration failures
  • Add reorg detection via lndclient.WithReOrgChan() - on reorg after spend detection, returns to spend
    watching (not just confirmation re-registration) since the spend tx itself may be invalidated
  • On reorg after spend detection, return to spend watching (not just re-register for confirmations) since the spend tx may be invalidated
  • Make handleWithdrawal async/best-effort since tx is already published

Testing

go test -v ./staticaddr/withdraw/...

🤖 Generated with Claude Code

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the reliability of the static address withdrawal manager by addressing critical gaps in error handling and state management. By introducing retry mechanisms, reorg awareness, and ensuring atomic state persistence, the changes ensure that withdrawal monitoring is resilient against network instability and node restarts.

Highlights

  • Robust Withdrawal Monitoring: Implemented automatic retry logic for registration failures, ensuring that monitoring does not silently stop if notification registration fails.
  • Reorg Handling: Integrated reorg detection using lndclient.WithReOrgChan() to properly handle chain reorganizations, including re-registering for spend notifications if a transaction is invalidated.
  • State Persistence Ordering: Reordered state persistence to occur before monitoring setup, ensuring that recovery processes can correctly identify and resume pending withdrawals after a restart.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements retry logic and reorg handling for static address withdrawal confirmations to prevent silent failures. It reorders state persistence to occur before monitoring setup, ensuring recovery on restart, and refactors withdrawal monitoring into an asynchronous process with dedicated loops for spend and confirmation notifications. Feedback identifies critical resource leaks in the notification registration loops, where subscriptions are not canceled during retries, and recommends using sub-contexts to manage these resources properly.

Comment thread staticaddr/withdraw/manager.go
Comment thread staticaddr/withdraw/manager.go
This commit fixes three related issues in the withdrawal manager:

1. No retry when RegisterSpendNtfn/RegisterConfirmationsNtfn fails
2. No handling for reorgs (tx confirmed then reorged out)
3. State not persisted before handleWithdrawal, causing recovery failures

Changes:
- Reorder state persistence to happen BEFORE handleWithdrawal so
  recoverWithdrawals can find deposits in Withdrawing state on restart
- Add retry-on-next-block for registration failures using block
  notifications
- Add reorg detection via lndclient.WithReOrgChan for both spend and
  confirmation notifications
- On reorg after spend detection, return to spend watching (not just
  re-register for confirmations) since the spend tx may be invalidated
- Make handleWithdrawal async/best-effort since tx is already published

Fixes issue lightninglabs#1087

Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
@kaldun-tech kaldun-tech marked this pull request as ready for review April 26, 2026 17:29
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.

withdraw: retry confirmation watching on failure and handle reorgs

1 participant