perf(rpc): isolate block template construction#292
Conversation
Run synchronous coinbase and template construction on Tokio's blocking pool so expensive proving work does not stall async RPC workers.
Analyzed one file, diff |
|
Blocked on next tag |
evan-forbes
left a comment
There was a problem hiding this comment.
depending on if the rpc is exposed for non-miners, then we might want to go ahead and fix the exhaustion issue brought up by v12
will defer to author on when, if, and where we do that. could also be a flup
Problem
getblocktemplateis an async RPC, but its final response was built synchronously on a Tokio async worker by callingBlockTemplateResponse::new_internaldirectly.new_internalconstructs a coinbase transaction whenever it is not given a usable precomputed coinbase. For shielded miner addresses, that work includes Sapling proof construction and can take seconds. With #291, the first construction in a process can also initialize the shared prover by parsing the bundled Sapling parameters. While this synchronous work runs, the Tokio worker cannot poll unrelated RPC futures, and concurrent template requests can increase async-runtime latency.The long-poll implementation already uses
spawn_blockingwhen precomputing a provisional coinbase, but final response construction was still synchronous in two cases:Noneand builds its coinbase inline;new_internalto build a replacement inline.Solution
Run
BlockTemplateResponse::new_internalthroughtokio::task::spawn_blockingin both final response paths. This moves any coinbase proof generation and first-use prover initialization onto Tokio's blocking pool while the RPC future yields its async worker.NetworkandMinerParamsare cloned into owned values because a blocking task must ownSend + 'staticinputs. The remaining response inputs are already owned and are moved into the task. The existing precomputed-coinbase fast path and all response semantics remain unchanged.This is the separately scoped follow-up to the blocking-RPC concern raised on #277 and is independent of the shared-prover optimization in #291.
Test plan
cargo fmt --all -- --checkcargo test -p zakura-rpc --lib methods::types::get_block_templatecargo clippy -p zakura-rpc --all-targets -- -D warnings