Closed arvidn closed 1 month ago
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % | ||
---|---|---|---|---|---|
crates/chia-consensus/src/gen/additions_and_removals.rs | 65 | 66 | 98.48% | ||
<!-- | Total: | 96 | 97 | 98.97% | --> |
Totals | |
---|---|
Change from base Build 11262793680: | 0.1% |
Covered Lines: | 12804 |
Relevant Lines: | 15291 |
This adds a function called
additions_and_removals()
, which is similar torun_block_generator()
andrun_block_generator2()
but with some important differences.additions_and_removals()
:motivation
The two main reasons to introduce this function are:
run_block_generator()
chia-blockchain
has for running blocks. Lettingrun_block_generator()
be more specialized to always fully validate blocks (e.g. the signature).running trusted blocks
In
chia-blockchain
we sometimes validate blocks completely (1). Ensuring their conditions hold, their signatures are correct, the proof of space is correct etc. This is done as part of syncing a node as well as keeping up with the chain and maintaining the mempool. When we receive a transaction for the mempool, we fully validate it before accepting it. When we receive anUnfinishedBlock
, we fully validated it.The other use case for running block generators is to just get additions and removals (2) to respond to an RPC or to rebuild the coin store during a reorg (or when adding an orphaned block). This, case 2, could be replaced by the cheaper call to `additions_and_removals().
The non-test call sites of
get_name_puzzle_conditions()
are:CostLogger.add_cost()
- part of the spend-sim, to compute the cost of a block in the simulator. This requires the full `run_block_generator() to compute the cost. Case (1).Blockchain.run_single_block()
- this is to catch up aForkInfo
object with all the additions and removals of a fork of the chain. We need to do this when adding orphaned blocks, or reorging. Case (2).batch_pre_validate_blocks()
- this is run when validating full blocks, either because we're syncing or just received a new block extending the blockchain. Case (1)._run_generator()
- this is called when validating anUnfinishedBlock
. Case (1).FullNodeAPI.request_block_header()
- this is an RPC called on existing, validated, blocks in the chain. Case (2).FullBlockApi.request_block_header()
- To construct a block header from a full block, we need the additions and removals, so we recompute them from a trusted block. Case (2).git grep output, for non-tests and non-imports:
Performance
One of the main reasons to justify this function, rather than just calling
run_block_generator()
is the time you can save. Here's the benchmark for block 4671894 on mainnet. The plot below comparesrun_block_generator()
(slowest),run_block_generator2()
(middle) andadditions_and_removals()
(fastest).