Skip to content

Conversation

ffranr
Copy link
Contributor

@ffranr ffranr commented Oct 13, 2025

Closes: #1840


What

  • Introduce reusable full block header cache (by height and hash).
  • Integrate cache in LndRpcChainBridge.
  • Refactor GetBlockTimestamp to read via GetBlockHeaderByHeight and drop the old timestamp cache.

Why

  • Avoid stale timestamps from reorged headers.
  • Reduce repeated header RPCs.
  • Provide a shared, configurable cache for future callers.

Highlights

  • Reorg detection with conflicting height handling and orphan invalidation.
  • Unsettled vs settled semantics via confirmation depth.
  • Configurable capacity and random purge fraction.

Tests

  • Basic get and put, capacity and purge, settled semantics, reorg path.

@ffranr ffranr added this to the v0.8 milestone Oct 13, 2025
@ffranr ffranr self-assigned this Oct 13, 2025
@ffranr ffranr added the chain label Oct 13, 2025
@ffranr ffranr moved this from 🆕 New to 👀 In review in Taproot-Assets Project Board Oct 13, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @ffranr, 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 introduces a robust, reorg-aware block header cache to the lndservices package. The primary goal is to enhance the reliability of block header lookups by preventing the use of stale data during blockchain reorganizations and to optimize performance by reducing redundant RPC calls to the underlying LND node. The new cache is integrated into the LndRpcChainBridge, and the GetBlockTimestamp function has been refactored to leverage this improved caching mechanism and provide more comprehensive error handling.

Highlights

  • Reorg Detection: The new cache includes reorg detection with conflicting height handling and orphan invalidation, ensuring data consistency during blockchain reorganizations.
  • Settled Semantics: The cache distinguishes between 'unsettled' and 'settled' block headers based on a configurable confirmation depth, preventing the use of potentially unstable data.
  • Configurable Cache: The cache offers configurable capacity and a random purge fraction, allowing for flexible resource management and performance tuning.
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.

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 reorg-aware block header cache, which is a great improvement for performance and correctness by avoiding stale data and reducing RPC calls. The implementation is clean and well-tested. The refactoring of GetBlockTimestamp to propagate errors is also a significant enhancement. I have a couple of minor suggestions for improvement, mainly around a small inconsistency in a comment and an opportunity for optimization in the cache purging logic. Overall, this is a solid contribution.

Refactored GetBlockTimestamp to call GetBlockHeaderByHeight and return
an optional error type. Removed the timestamp-to-block-height cache, as
it did not handle re-orgs correctly. This prepares the codebase for a
more comprehensive caching mechanism to be added in a follow-up commit.
@ffranr ffranr force-pushed the wip/add-block-header-cache branch 2 times, most recently from 4b98e2a to ed9b47a Compare October 13, 2025 16:32
@ffranr ffranr requested review from darioAnongba and jtobin October 13, 2025 21:16
Copy link
Contributor

@darioAnongba darioAnongba left a comment

Choose a reason for hiding this comment

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

Code looks good to me. Only nit comment is that we should slow down on the comments that add no value, example:

// Calculate the height at which this block would be considered settled.
settledHeight := height + c.cfg.NumConfSettled

Here the code has actually more information in it than the comment IMO.

@ffranr ffranr force-pushed the wip/add-block-header-cache branch from ed9b47a to 2813377 Compare October 16, 2025 13:21
Copy link
Member

@jtobin jtobin left a comment

Choose a reason for hiding this comment

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

Really nice. 👌 Just a couple of questions/comments.

Introduce a reusable cache that stores full headers keyed by height and
hash. Tracks confirmation depth and treats shallow entries as unsettled
(return miss). Detects conflicts at a height and invalidates shallower
headers on reorg. Size and random purge fraction are configurable
(default 100k entries, 10 percent).

Not yet used by LndRpcChainBridge.
Adds a block header cache to LndRpcChainBridge, which indirectly
improves performance of methods like GetBlockTimestamp by avoiding
repeated block header fetches.
@ffranr ffranr force-pushed the wip/add-block-header-cache branch from 2813377 to 129d128 Compare October 16, 2025 14:35
@ffranr ffranr requested a review from jtobin October 16, 2025 14:38
Copy link
Member

@jtobin jtobin left a comment

Choose a reason for hiding this comment

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

LGTM. 👍 👍

@ffranr ffranr merged commit 965998d into 0-8-0-staging Oct 16, 2025
18 of 19 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Taproot-Assets Project Board Oct 16, 2025
GeorgeTsagk pushed a commit that referenced this pull request Oct 20, 2025
lndservices: add reorg aware block header cache; use in ChainBridge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants