[CHIA-1563] add function to run a *trusted* block and return additions and removals. #748
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).