Skip to content

perf: fill the credit pool's block data cache at connect time - #8262

Merged
PastaPastaPasta merged 1 commit into
dashpay:developfrom
knst:perf-creditpool-cache
Nov 30, 2026
Merged

PastaPastaPasta merged 1 commit into
dashpay:developfrom
knst:perf-creditpool-cache

Conversation

@knst

@knst knst commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

ConstructCreditPool reads the previous block's CbTx balance and asset unlock amounts through a per-block cache keyed by block hash, but that cache was only filled by the disk read itself. During IBD the previous block is never in it, so every connected block re-read its predecessor from disk (one extra "Load block from disk" per block) and parsed its coinbase payload again.

What was done?

Record the data of the block being connected from the in-memory block, after its unlock transactions and coinbase have been checked, so the next block's pool construction finds it in the cache.

How Has This Been Tested?

invalidate + reconsider 9k blocks on mainnet close to tip

stage develop PR change
Loop (special txs) 2.38 s 1.78 s -25.2%
CheckCreditPoolDiffForBlock 0.04 s 0.09 s +125%
m_qblockman.ProcessBlock 7.86 s 7.45 s -5.2%
m_dmnman.ProcessBlock 4.81 s 4.55 s -5.4%
CalcCbTxMerkleRootQuorums 3.22 s 3.19 s -0.9%
CheckCbTxBestChainlock 5.55 s 5.46 s -1.6%
ProcessSpecialTxsInBlock 25.02 s 23.69 s -5.3%
Connect total 29.51 s 28.27 s -4.2%
Connect block 31.56 s 30.36 s -3.8%

CheckCreditPoolDiffForBlock expected to get slower due to extra ops there, but grow is tiny compare to overall win.

Total speedup for block validation is 4% for post-v20 blocks [credit pool introduction].

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (if you have permission to set one)

@knst knst added this to the 24.1 milestone Nov 27, 2026
@thepastaclaw

thepastaclaw commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Final review complete — no blockers (commit a66dea7) · triage: critical

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Credit-pool block-data extraction now uses a shared, mutex-protected LRU cache. The code caches only successfully extracted data. The credit-pool manager adds CacheBlockData for connected blocks at or after DIP0003 activation. Block processing calls it after credit-pool-diff validation when the run is not check-only and V20 is active.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SpecialTxProcessor
  participant CCreditPoolManager
  participant CreditPoolCache
  SpecialTxProcessor->>CCreditPoolManager: CacheBlockData(block, block_index) when conditions pass
  CCreditPoolManager->>CreditPoolCache: Store successfully extracted data
Loading

Suggested reviewers: panleone

Merge Risk: Minimal · up to a66de

The change fills the credit-pool cache while a block is connected, so the next block does not reread the predecessor from disk during IBD. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the credit pool's block data cache is now filled when a block is connected. It uses the "perf:" prefix, which signals a performance intent, and it is specific enough f…
Description check Passed The description explains the problem. During IBD, ConstructCreditPool rereads the previous block from disk and reparses its coinbase payload because the cache is never filled for that block. It then d…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the credit pool's block data cache is now filled when a block is connected. It uses the "perf:" prefix, which signals a performance intent, and it is specific enough for a teammate scanning history to understand the primary change.

Full details: Description check

Explanation

The description explains the problem. During IBD, ConstructCreditPool rereads the previous block from disk and reparses its coinbase payload because the cache is never filled for that block. It then describes the fix, which records the connected block's data from memory, and includes benchmark results. It matches the changeset in src/evo/creditpool.cpp and specialtxman.cpp.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: eace0261-dd87-41cf-a879-e50212238536
Commits

Reviewing files that changed from the base of the PR and between 6497db2 and a66dea7.

Files selected for processing: 2

  • src/evo/creditpool.cpp
  • src/evo/specialtxman.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Final validation — Phase 1 + Phase 2

Changes requested: 1 blocker

  • Clang builds fail when acquiring the new file-scope cache mutex — src/evo/creditpool.cpp:61 (new)
    Impact: Developers and CI building with Clang thread-safety warnings treated as errors cannot compile the node, so the affected jobs cannot run their tests.

This change caches credit-pool data from the in-memory block as it connects, so the next block can reuse its predecessor’s data without rereading it from disk. The standard Linux and no-wallet tests passed; the macOS, Linux ASan, TSan, fuzz, and multiprocess builds failed in Clang’s thread-safety analysis.

🔴 1 blocking | 🟡 2 suggestion(s)

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: dash-core-commit-history); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.2-sol (effort low) — The diff is a contained cache refactor that reuses the existing credit-pool data parser to populate the cache at block connect time without changing credit-pool rules.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort high); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (zai below 15% reserve: 5h 100% left, weekly 0% left)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — dash-core-commit-history (completed, effort medium); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `src/evo/creditpool.cpp`:
- [BLOCKING] src/evo/creditpool.cpp:61: Clang builds fail when acquiring the new file-scope cache mutex
  Impact: Developers and CI building with Clang thread-safety warnings treated as errors cannot compile the node, so the affected jobs cannot run their tests.
  The macOS, Linux ASan, TSan, fuzz, and multiprocess builds in CI run `46026761978` all stop at `creditpool.cpp:112` and `:135` with `calling function 'MaybeCheckNotHeld' requires negative capability '!cache_mutex' [-Werror,-Wthread-safety-analysis]`. Moving the mutex from function scope to file scope while leaving it a plain `Mutex` makes the first `LOCK` in each function require proof that the file-scope mutex is not held. The diagnostic is on lock acquisition, not on the cache accessor or the `if` initializer. Use `GlobalMutex`, which `sync.h` documents for this case: it selects the `LOCKS_EXCLUDED` overload without changing the mutex’s non-recursive behavior or the accessor’s held-lock requirement.
- [SUGGESTION] src/evo/creditpool.cpp:134-136: Reconnecting a block reparses credit-pool data that is already cached
  Impact: Nodes reconnecting recent post-v20 blocks do another transaction scan and rebuild the unlock-index set if the cached entry survived the disconnect.
  `CacheBlockData` decodes the coinbase and scans the transactions before checking the cache, and `insert` replaces the entry even when the hash is already present. The old lazy lookup returned a hit without doing that work. Skip extraction for hashes already in the cache. On a miss, normal validation can also reuse the checked coinbase balance and the diff’s withdrawal total and new indexes; keep the block extractor for paths such as replay that do not produce a validated diff.

In `src/evo/specialtxman.cpp`:
- [SUGGESTION] src/evo/specialtxman.cpp:992-994: Existing credit-pool tests do not detect a return to predecessor disk reads
  Impact: If a later change stops populating the cache during connection, nodes can resume rereading each predecessor without failing the existing accounting and reorg checks.
  The existing tests check balances and reorg outcomes, which are unchanged when reconstruction takes the disk fallback. Extend an existing asset-lock test to connect a prebuilt successor without first warming the predecessor through block assembly or a pool lookup. A hash-specific credit-pool debug message at the disk fallback would let the test verify that the predecessor is not read on the warm path and that cold reconstruction yields the same balance, withdrawal total, and unlock-index behavior, without asserting on timings.

Comment thread src/evo/creditpool.cpp Resolved
Comment thread src/evo/creditpool.cpp
Comment on lines +134 to +136
if (const std::optional<CreditPoolDataPerBlock> opt_block_data{ReadCreditData(block, block_index->nHeight)}) {
LOCK(::cache_mutex);
BlockDataCache().insert(block_index->GetBlockHash(), *opt_block_data);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Suggestion: Reconnecting a block reparses credit-pool data that is already cached
Impact: Nodes reconnecting recent post-v20 blocks do another transaction scan and rebuild the unlock-index set if the cached entry survived the disconnect.

CacheBlockData decodes the coinbase and scans the transactions before checking the cache, and insert replaces the entry even when the hash is already present. The old lazy lookup returned a hit without doing that work. Skip extraction for hashes already in the cache. On a miss, normal validation can also reuse the checked coinbase balance and the diff’s withdrawal total and new indexes; keep the block extractor for paths such as replay that do not produce a validated diff.

source: gpt-6.2-sol (phase2-reviewer: general, dash-core-commit-history)

Comment thread src/evo/specialtxman.cpp
Comment on lines +992 to +994
if (!fJustCheck && DeploymentActiveAt(*pindex, m_consensus_params, Consensus::DEPLOYMENT_V20)) {
m_cpoolman->CacheBlockData(block, pindex);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Suggestion: Existing credit-pool tests do not detect a return to predecessor disk reads
Impact: If a later change stops populating the cache during connection, nodes can resume rereading each predecessor without failing the existing accounting and reorg checks.

The existing tests check balances and reorg outcomes, which are unchanged when reconstruction takes the disk fallback. Extend an existing asset-lock test to connect a prebuilt successor without first warming the predecessor through block assembly or a pool lookup. A hash-specific credit-pool debug message at the disk fallback would let the test verify that the predecessor is not read on the warm path and that cold reconstruction yields the same balance, withdrawal total, and unlock-index behavior, without asserting on timings.

source: gpt-6.2-sol (phase2-reviewer: general, dash-core-commit-history)

@github-actions

Copy link
Copy Markdown

This pull request has conflicts, please rebase.

thepastaclaw
thepastaclaw previously approved these changes Nov 28, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review — Final validation — Phase 1 + Phase 2

New on this head: The PR populates the credit-pool block-data cache from the in-memory block during connection, avoiding predecessor disk reads during subsequent pool construction. Since the prior review, the rewritten head changes the file-scope cache mutex to GlobalMutex; completed builds, lint, cppcheck, and platform checks pass, while ASAN, TSAN, and multiprocess test jobs remain in progress.

🟡 2 suggestion(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

Still open from earlier rounds (2)

  • 🟡 Suggestion: Existing credit-pool tests do not detect a return to predecessor disk reads — src/evo/specialtxman.cpp:992-994 (thread)
  • 🟡 Suggestion: Reconnecting a block reparses credit-pool data that is already cached — src/evo/creditpool.cpp:134-136 (thread)
Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: dash-core-commit-history); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.2-sol (effort low) — This changes consensus-adjacent block validation and caching logic in creditpool.cpp and specialtxman.cpp, but the contained performance optimization does not itself alter consensus rules, funds movement, cryptography, networking, or storage migrations.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — dash-core-commit-history (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `src/evo/specialtxman.cpp`:
- [SUGGESTION] src/evo/specialtxman.cpp:992-994: Existing credit-pool tests do not detect a return to predecessor disk reads
  (carried; thread: https://github.com/dashpay/dash/pull/8262#discussion_r4721744374)

In `src/evo/creditpool.cpp`:
- [SUGGESTION] src/evo/creditpool.cpp:134-136: Reconnecting a block reparses credit-pool data that is already cached
  (carried; thread: https://github.com/dashpay/dash/pull/8262#discussion_r4721744369)

Comment thread src/evo/creditpool.cpp Resolved
@thepastaclaw thepastaclaw added the pastaclaw:approved thepastaclaw's latest review approved this PR label Nov 28, 2026
ConstructCreditPool reads the previous block's CbTx balance and asset unlock amounts through a per-block cache keyed by block hash, but that cache was only filled by the disk read itself. During IBD the previous block is never in it, so every connected block re-read its predecessor from disk (one extra "Load block from disk" per block) and parsed its coinbase payload again.

Record the data of the block being connected from the in-memory block, after its unlock transactions and coinbase have been checked, so the next block's pool construction finds it in the cache.
@knst
knst force-pushed the perf-creditpool-cache branch from 6497db2 to a66dea7 Compare November 29, 2026 15:27
@thepastaclaw thepastaclaw removed the pastaclaw:approved thepastaclaw's latest review approved this PR label Nov 29, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review — Final validation — Phase 1 + Phase 2

New on this head: This head is a rebase of the existing performance change that populates the credit-pool block-data cache from the connected in-memory block; the substantive cache behavior is unchanged. No code checks have failed, while the source builds, lint, and several platform cache checks remain queued or in progress; Cppcheck and ClangFormat have passed.

🟡 2 suggestion(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

Still open from earlier rounds (2)

  • 🟡 Suggestion: Existing credit-pool tests do not detect a return to predecessor disk reads — src/evo/specialtxman.cpp:986-988 (thread)
  • 🟡 Suggestion: Reconnecting a block reparses credit-pool data that is already cached — src/evo/creditpool.cpp:143-145 (thread)
Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-astra (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-astra (agent: phase2-reviewer, role: dash-core-commit-history); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: critical by gpt-6.2-sol (effort low) — The change modifies consensus validation data flow in src/evo/creditpool.cpp, including the cache used by GetCreditDataFromBlock and the new CCreditPoolManager::CacheBlockData path, with nontrivial parsing and cache-concurrency behavior.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Single stage: Phase 1 and Phase 2 reviewed this head side by side, with no blocker gate between them (triage tier)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-astra — general (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — dash-core-commit-history (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `src/evo/specialtxman.cpp`:
- [SUGGESTION] src/evo/specialtxman.cpp:986-988: Existing credit-pool tests do not detect a return to predecessor disk reads
  (carried; thread: https://github.com/dashpay/dash/pull/8262#discussion_r4721744374)

In `src/evo/creditpool.cpp`:
- [SUGGESTION] src/evo/creditpool.cpp:143-145: Reconnecting a block reparses credit-pool data that is already cached
  (carried; thread: https://github.com/dashpay/dash/pull/8262#discussion_r4721744369)

@PastaPastaPasta PastaPastaPasta left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

utACK

@PastaPastaPasta
PastaPastaPasta merged commit ea87bfb into dashpay:develop Nov 30, 2026
52 checks passed
@thepastaclaw thepastaclaw removed the pastaclaw:approved thepastaclaw's latest review approved this PR label Nov 30, 2026
@knst
knst deleted the perf-creditpool-cache branch November 30, 2026 08:33
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