Skip to content

[feature]: Add reorg-aware block header cache to LndRpcChainBridge #1840

@ffranr

Description

@ffranr

LndRpcChainBridge (lndservices/chain_bridge.go) currently caches only block timestamps by height via blockTimestampCache *lru.Cache[uint32, cacheableTimestamp]. That cache can return timestamps for headers that were reorged out since it is unaware of chain reorgs and confirmation depth. We should replace this with a reorg-aware cache of full block headers.

Goals

  • Cache full headers, not just timestamps. Header hash and timestamp are derivable from the header.
  • Make GetBlockTimestamp read from the new header cache. Remove or retire blockTimestampCache.
  • Implement as a reusable, package-level component with clean interfaces, suitable for use beyond LndRpcChainBridge.

Header cache requirements

  • Size is configurable, default target 100k entries.

  • On reaching capacity, purge a random fraction (default 10 percent). Fraction is configurable.

  • Track confirmation depth. Parameter numberOfConfirmations with typical value 6.

  • Store headers even when depth < numberOfConfirmations, but treat them as unsettled:

    • Queries for unsettled heights or hashes return a cache miss to force an external lookup.
    • Once a later settled height is observed that implies sufficient depth, mark prior entries as settled.
  • Support lookup by height and by header hash.

  • Be resilient to reorgs:

    • Detect conflicting headers at the same height.
    • On detection, invalidate the conflicting branch entries that are now orphaned.

Testing

  • Unit tests that cover:

    • Basic put and get by height and by hash.
    • Capacity and random purge behavior.
    • Unsettled vs settled semantics with numberOfConfirmations.
    • Reorg scenario: insert headers for H..H+N, then replace H+k with an alternate header and ensure orphaned entries are invalidated and queries miss until settled.
    • GetBlockTimestamp integration against the new cache.

Metadata

Metadata

Assignees

Projects

Status

✅ Done

Relationships

None yet

Development

No branches or pull requests

Issue actions