Skip to content

Conversation

@hieblmi
Copy link
Collaborator

@hieblmi hieblmi commented Jan 15, 2026

Before reconciling our in-mem view of deposits with lnd we give the wallet extra time to index transactions of interest in the new block. This mitigates a race between wallet indexing and reconciling deposits.

@gemini-code-assist
Copy link

Summary of Changes

Hello @hieblmi, 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 addresses a timing-sensitive issue where the system's in-memory view of deposits might be reconciled with lnd before lnd has fully indexed new transactions. By adding a brief pause, the change ensures that lnd's wallet has completed its indexing process, leading to more accurate and reliable deposit reconciliation.

Highlights

  • Race Condition Mitigation: Introduced a 500ms delay before reconciling in-memory deposits with lnd to allow the wallet sufficient time to index new transactions, thereby preventing a race condition.

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

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@hieblmi hieblmi requested review from bhandras and starius January 15, 2026 14:11
Copy link

@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 introduces a time.Sleep to mitigate a race condition between receiving a new block notification and the lnd wallet finishing indexing of that block. While this is a pragmatic approach to address the race condition, the implementation can be improved by extracting the hardcoded sleep duration into a named constant for better readability and maintainability. I've also raised a question about the fragility of using a fixed sleep and whether a more deterministic synchronization mechanism with lnd might be available.

// transactions of interest in the new block. This
// mitigates a race between wallet indexing and
// reconciling deposits.
time.Sleep(500 * time.Millisecond)

Choose a reason for hiding this comment

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

medium

Using a hardcoded 'magic number' like 500 for the sleep duration can make the code harder to understand and maintain. It's better to define this value as a named constant at the package level (e.g., walletIndexDelay = 500 * time.Millisecond). This improves readability by giving the value a clear meaning and makes it easier to adjust in the future.

Additionally, relying on a fixed sleep duration to resolve a race condition is inherently fragile, as the required delay might vary depending on system load. Have you investigated if lnd provides a more deterministic signal for when wallet indexing is complete for a new block? If not, this workaround is understandable, but its potential flakiness is worth noting.

Copy link
Collaborator

@starius starius Jan 16, 2026

Choose a reason for hiding this comment

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

+1

Can we wait for some observable event instead of sleeping?

IIUC, the root cause is that the output of ListUnspent WalletKit API can be outdated and reconcileDeposits uses ListUnspent to detect deposits. There is no way to detect this condition from ListUnspentResponse.

Maybe we should fix this in LND, adding the current height and block hash to ListUnspentResponse? Then we can retry ListUnspent until we get up-to-date results.

We are notified about new block in ChainNotifier component of LND, but track deposits using its WalletKit component which is also a root cause of the mismatch. Future idea: can ChainNotifier have some API to track deposits? Something similar to RegisterSpendNtfn but where you provide a pkScript and it notifies you when funds arrive to that pkScript. We can use RegisterConfirmationsNtfn without specifying txid to track any deposit to specific pkScript (e.g. static address in Loop). Limitation: repeated deposits to the same script won't trigger additional notifications because the notifier caches the first confirmation and treats subsequent reuse as duplicate. I remember this limitation was addressed somewhere, but I can't find where.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

+1

Can we wait for some observable event instead of sleeping?

IIUC, the root cause is that the output of ListUnspent WalletKit API can be outdated and reconcileDeposits uses ListUnspent to detect deposits. There is no way to detect this condition from ListUnspentResponse.

Yes!

Maybe we should fix this in LND, adding the current height and block hash to ListUnspentResponse? Then we can retry ListUnspent until we get up-to-date results.

The wallet controller has a method IsSynced which compares the wallet tip to the chain backend. I think we can use the result in the ListUnspent call to see if indexing is finished.

We are notified about new block in ChainNotifier component of LND, but track deposits using its WalletKit component which is also a root cause of the mismatch. Future idea: can ChainNotifier have some API to track deposits? Something similar to RegisterSpendNtfn but where you provide a pkScript and it notifies you when funds arrive to that pkScript. We can use RegisterConfirmationsNtfn without specifying txid to track any deposit to specific pkScript (e.g. static address in Loop). Limitation: repeated deposits to the same script won't trigger additional notifications because the notifier caches the first confirmation and treats subsequent reuse as duplicate. I remember this limitation was addressed somewhere, but I can't find where.

We use ImportTaprootScript to add the pkscript to the wallet, then poll ListUnspent for changes. This hacks around the RegisterConfirmationsNtfn limitation.

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.

2 participants