Skip to content

fix(core): keep blocksHashCache entries unique per height - #2548

Open
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-blocks-hash-cache-dedup
Open

fix(core): keep blocksHashCache entries unique per height#2548
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-blocks-hash-cache-dedup

Conversation

@gzliudan

@gzliudan gzliudan commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

UpdateBlocksHashCache appended unconditionally when a per-height entry already existed, so refreshing the cache for a block that was already cached produced duplicate hash entries in the per-height slice. The cache tracks the hashes of all competing blocks at a height, and its LRU limit (blocksHashCacheLimit = 900) bounds the number of height entries, not the length of a single slice, so duplicates grew without bound.

Reachability

Before the known-block work this defect was latent: SetHead purges the cache on rollback, but slow reorgs into the pruned region re-import old blocks via recoverAncestors, and the follow-up writeBlockWithState refreshes the cache again, appending a duplicate. The upcoming known-block promotion PR refreshes the cache on every re-delivery of a known segment, which a remote peer can trigger cheaply and repeatedly, so this fix must land before (or together with) that PR.

Change

Skip the append when the hash is already tracked. Distinct fork hashes at the same height are still kept, preserving the multi-fork behavior covered by the existing fork subtest of TestBlocksHashCacheUpdate.

Testing

  • New subtest in TestBlocksHashCacheUpdate: repeated UpdateBlocksHashCache calls for the same block keep exactly one entry per hash.
  • go test ./core -run 'TestBlocksHashCacheUpdate|TestAreTwoBlocksSamePath' -count=1 passes; gofmt, go vet, go build ./core/... clean.

Compatibility

No schema, migration, or wire-protocol impact; cache is in-memory only. Labels core/db suggested.

UpdateBlocksHashCache appended unconditionally, so refreshing the
cache for a block that was already cached — which the known-block
path now does when it promotes a stored block — grew the per-height
slice with duplicate hashes. Skip the append when the hash is already
tracked; distinct fork hashes at the same height are still kept.
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: c13cb46e-708e-45e2-bf75-e89c6da74e9f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused fix correctly prevents duplicate entries and includes regression coverage.

Pull request overview

Prevents unbounded duplicate hashes in the per-height block hash cache.

Changes:

  • Skip insertion when a block hash is already cached at its height.
  • Test repeated updates while preserving fork hashes.
File summaries
File Description
core/blockchain.go Adds duplicate detection before cache insertion.
core/blockchain_test.go Verifies repeated updates retain one hash entry.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

4 participants