Skip to content

fix(rpc): cache the getblocktemplate coinbase per block - #10847

Merged
mergify[bot] merged 4 commits into
mainfrom
gbt-coinbase-cache
Jul 1, 2026
Merged

fix(rpc): cache the getblocktemplate coinbase per block#10847
mergify[bot] merged 4 commits into
mainfrom
gbt-coinbase-cache

Conversation

@upbqdn

@upbqdn upbqdn commented Jun 28, 2026

Copy link
Copy Markdown
Member

Motivation

Closes #10846.

Solution

Cache the built coinbase per (height, fees) so short-poll getblocktemplate requests within a block reuse it instead of re-running the Sapling/Orchard proof. The ZIP-317 fee-calculation fake coinbase is routed through the same cache (it re-proved too). randomize_coinbase_data clears it (Regtest generate only).

Tests

Unit tests assert a cache hit reuses identical bytes while two fresh shielded builds differ. Verified on a live testnet pool: shielded getblocktemplate dropped from ~6 core-sec/call to ~0 (cached) — only the first call per block proves.

AI Disclosure

Claude Code was used for the ZIP-317-path fix and the end-to-end validation.

Copilot AI review requested due to automatic review settings June 28, 2026 14:09
@upbqdn upbqdn self-assigned this Jun 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

jvff
jvff previously approved these changes Jun 30, 2026
Comment thread zebra-rpc/src/methods/types/get_block_template.rs Outdated
@gustavovalverde

Copy link
Copy Markdown
Member

/changelog

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

Proposed changelog entries for this PR. Review and paste into the relevant
CHANGELOG.md [Unreleased] sections:

## Top-level `CHANGELOG.md``[Unreleased]`

### Fixed

- `getblocktemplate` now caches the built coinbase transaction per block height and total fees, so repeated short-poll requests within the same block no longer rebuild it. This prevents CPU saturation and multi-second template latency when mining to a shielded (Sapling or Orchard) address.

---

## `zebra-rpc/CHANGELOG.md``[Unreleased]`

### Fixed

- `getblocktemplate` now caches the built coinbase transaction per `(height, fees)`, so repeated short-poll requests within a block no longer rebuild it. This avoids re-running the Sapling/Orchard proof on every request when mining to a shielded address, which otherwise pegged the CPU and made each template take seconds.

Copilot AI review requested due to automatic review settings July 1, 2026 12:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

upbqdn added 4 commits July 1, 2026 15:02
`getblocktemplate` rebuilt the coinbase on every request, re-running the
Sapling/Orchard proof for shielded miner addresses. The existing precompute
only feeds long-poll clients, so short-polling pools (which re-request without
long polling) re-proved the coinbase on every poll — pegging the CPU and making
each template take seconds.

Cache the built coinbase keyed on `(height, fees)` so repeated requests within a
block reuse it, and clear the cache when the coinbase data is randomized.
Transaction selection builds a fake coinbase to size the block, which re-proved
the shielded coinbase on every getblocktemplate call, outside the per-block
cache. Route it through the same cache: only the first call per block proves.
The `Option`-returning `CoinbaseCache::get` tripped
`clippy::unwrap_in_result` (denied via `-D warnings` in the test-crates
MSRV build). Recover the guard on poison at all three lock sites; a
coinbase cache need not crash the node over an unrelated thread's panic.

Claude-Session: https://claude.ai/code/session_01VUPwpe81Ws8c5MeXWWgZb7
@mergify mergify Bot added the queued label Jul 1, 2026
@mergify

mergify Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 50 minutes 34 seconds in the queue, including 40 minutes 9 seconds running CI.

Required conditions to merge

mergify Bot added a commit that referenced this pull request Jul 1, 2026
@mergify
mergify Bot merged commit 3b6f695 into main Jul 1, 2026
174 checks passed
@mergify
mergify Bot deleted the gbt-coinbase-cache branch July 1, 2026 14:42
@mergify mergify Bot removed the queued label Jul 1, 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.

getblocktemplate re-proves the shielded coinbase on every call

5 participants