Skip to content

added rate limiting to address biased trie cache preloading#2029

Open
pratikspatil024 wants to merge 6 commits intodevelopfrom
psp-pos-3425
Open

added rate limiting to address biased trie cache preloading#2029
pratikspatil024 wants to merge 6 commits intodevelopfrom
psp-pos-3425

Conversation

@pratikspatil024
Copy link
Member

Description

Please provide a detailed description of what was done in this PR

Changes

  • Bugfix (non-breaking change that solves an issue)
  • Hotfix (change that solves an urgent issue, and requires immediate attention)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (change that is not backwards-compatible and/or changes current functionality)
  • Changes only for a subset of nodes

Breaking changes

Please complete this section if any breaking changes have been made, otherwise delete it

Nodes audience

In case this PR includes changes that must be applied only to a subset of nodes, please specify how you handled it (e.g. by adding a flag with a default value...)

Checklist

  • I have added at least 2 reviewer or the whole pos-v1 team
  • I have added sufficient documentation in code
  • I will be resolving comments - if any - by pushing each fix in a separate commit and linking the commit hash in the comment reply
  • Created a task in Jira and informed the team for implementation in Erigon client (if applicable)
  • Includes RPC methods changes, and the Notion documentation has been updated

Cross repository changes

  • This PR requires changes to heimdall
    • In case link the PR here:
  • This PR requires changes to matic-cli
    • In case link the PR here:

Testing

  • I have added unit tests
  • I have added tests to CI
  • I have tested this code manually on local environment
  • I have tested this code manually on remote devnet using express-cli
  • I have tested this code manually on amoy
  • I have created new e2e tests into express-cli

Manual tests

Please complete this section with the steps you performed if you ran manual tests for this functionality, otherwise delete it

Additional comments

Please post additional comments in this section if you have them, otherwise delete it

@codecov
Copy link

codecov bot commented Jan 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.61%. Comparing base (a311f8c) to head (07ccebc).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2029      +/-   ##
===========================================
+ Coverage    49.57%   49.61%   +0.04%     
===========================================
  Files          873      873              
  Lines       150507   150615     +108     
===========================================
+ Hits         74615    74730     +115     
+ Misses       70860    70854       -6     
+ Partials      5032     5031       -1     
Files with missing lines Coverage Δ
core/blockchain.go 62.11% <100.00%> (+0.01%) ⬆️
eth/backend.go 52.56% <100.00%> (+0.06%) ⬆️
eth/ethconfig/config.go 37.50% <ø> (ø)
internal/cli/server/config.go 63.18% <100.00%> (+1.49%) ⬆️
internal/cli/server/flags.go 100.00% <100.00%> (ø)
triedb/pathdb/biased_fastcache.go 89.47% <100.00%> (+1.01%) ⬆️
triedb/pathdb/config.go 78.78% <100.00%> (+3.78%) ⬆️
triedb/pathdb/disklayer.go 77.27% <100.00%> (ø)

... and 22 files with indirect coverage changes

Files with missing lines Coverage Δ
core/blockchain.go 62.11% <100.00%> (+0.01%) ⬆️
eth/backend.go 52.56% <100.00%> (+0.06%) ⬆️
eth/ethconfig/config.go 37.50% <ø> (ø)
internal/cli/server/config.go 63.18% <100.00%> (+1.49%) ⬆️
internal/cli/server/flags.go 100.00% <100.00%> (ø)
triedb/pathdb/biased_fastcache.go 89.47% <100.00%> (+1.01%) ⬆️
triedb/pathdb/config.go 78.78% <100.00%> (+3.78%) ⬆️
triedb/pathdb/disklayer.go 77.27% <100.00%> (ø)

... and 22 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pratikspatil024 pratikspatil024 changed the title [WIP] added rate limiting to address biased trie cache preloading added rate limiting to address biased trie cache preloading Feb 2, 2026
@pratikspatil024 pratikspatil024 requested a review from a team February 2, 2026 09:51
Copy link
Contributor

@cffls cffls left a comment

Choose a reason for hiding this comment

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

Looks good overall! What long does it take to preload mainnet with the default throttling setting?

// Apply rate limiting after reading, based on actual bytes read
if limiter != nil {
if err := limiter.WaitN(c.ctx, len(nodeData)); err != nil {
log.Info("Preload interrupted during rate limit wait",
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this line flood the log?

Copy link
Member Author

Choose a reason for hiding this comment

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

This log will be printed only once (per address) during shutdown.

Copy link
Contributor

Choose a reason for hiding this comment

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

From the api doc, looks like it will also return an error if n exceeds the limit size?

"max depth", maxDepthReached,
"size", common.StorageSize(totalBytesLoaded).String(),
"elapsed", time.Since(startTime))
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why the function returns here. If we return here it means the cache won't be warmed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, this is expected as this will be triggered during shutdown.

@pratikspatil024
Copy link
Member Author

Looks good overall! What long does it take to preload mainnet with the default throttling setting?

@cffls - with 500KB/s on our mainnet devnode, for a cache of 4096MB, it takes around 1h30m.

I think this 500KB/s default could be a bit too optimistic for the BPs, so we can bump the default values a bit, I'll check the logs and confirm.

@claude
Copy link

claude bot commented Feb 4, 2026

Code Review

Issue found: Documentation discrepancy in core/blockchain.go line 203 and eth/ethconfig/config.go line 167.

The comments say default = 5MB/s but the actual implementation default is 500KB/s (see defaultPreloadRateLimit = 500 * 1024 in triedb/pathdb/config.go:47-49).

This is a 10x difference that could cause confusion for users configuring the node.

Suggested fix: Update the comments to say default = 500KB/s to match the actual implementation.


Checked for bugs and CLAUDE.md compliance.

@pratikspatil024
Copy link
Member Author

@claude addressed your comments.
Review this PR again.

@claude
Copy link

claude bot commented Feb 5, 2026

Claude finished @pratikspatil024's task in 1m 6s —— View job


Re-reviewing PR

  • Gather context on the changes
  • Review the updated code for the documentation discrepancy fix
  • Check for any other issues
  • Provide final review feedback

Review Summary

The previous documentation discrepancy has been fixed. The PR now consistently uses 1MB/s as the default rate limit across all files:

  • core/blockchain.go:203 - ✅ Updated to default = 1MB/s
  • eth/ethconfig/config.go:167 - ✅ Updated to default = 1MB/s
  • triedb/pathdb/config.go:47-49 - ✅ Updated to defaultPreloadRateLimit = 1024 * 1024 (1MB/s)
  • internal/cli/server/config.go:684 - ✅ Documents Default: 1MB/s
  • internal/cli/server/flags.go:564 - ✅ Documents Default: 1MB
  • Tests in config_test.go - ✅ Verify default of 1024*1024 bytes

Code Quality Assessment

Strengths:

  1. Comprehensive rate limiting implementation - The rate.Limiter from golang.org/x/time/rate is used correctly with appropriate burst size (64KB)
  2. Graceful shutdown handling - Context cancellation properly interrupts rate-limited operations via limiter.WaitN(c.ctx, len(nodeData))
  3. Good test coverage - Tests cover rate limit creation, throttling behavior, interruption scenarios, and edge cases
  4. Thread safety - Proper mutex usage protects concurrent access during async preloading

Implementation Details:

  • Rate limiting is applied per-address during cache preloading
  • The burst size of 64KB allows for smoother throttling
  • Cache preloads up to 66.6% of capacity, leaving room for hot paths
  • BFS traversal prioritizes shallow (frequently accessed) nodes

No issues found in this re-review.


Copy link
Contributor

@lucca30 lucca30 left a comment

Choose a reason for hiding this comment

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

Looks good to me, just proposed a small change if makes sense in comparison to nodeData which I'm not sure what is the size.


// Apply rate limiting after reading, based on actual bytes read
if limiter != nil {
if err := limiter.WaitN(c.ctx, len(nodeData)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding len(nodeData) to measure the rate:

  • The cache is composed by key + nodeData
        key := append(accountHash.Bytes(), item.path...)
		addrCache.Set(key, nodeData)
  • Key is basically accountHash + path

I guess is super low, but hash is 32 bytes size, so if nodeData has something like 100 bytes than would be good account it

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 5, 2026

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.

3 participants