Chia-Network / chia_rs

Rust crate & wheel with consensus code
Apache License 2.0
21 stars 26 forks source link

[CHIA-1563] add function to run a *trusted* block and return additions and removals. #748

Closed arvidn closed 1 month ago

arvidn commented 1 month ago

This adds a function called additions_and_removals(), which is similar to run_block_generator() and run_block_generator2() but with some important differences.

additions_and_removals():

motivation

The two main reasons to introduce this function are:

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 an UnfinishedBlock, 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:

git grep output, for non-tests and non-imports:

git grep -B 1 -A 1 get_name_puzzle_conditions

chia/clvm/spend_sim.py-        program: BlockGenerator = simple_solution_generator(spend_bundle)
chia/clvm/spend_sim.py:        npc_result: NPCResult = get_name_puzzle_conditions(
chia/clvm/spend_sim.py-            program,
--
chia/consensus/block_creation.py-def compute_block_cost(generator: BlockGenerator, constants: ConsensusConstants, height: uint32) -> uint64:
chia/consensus/block_creation.py:    result: NPCResult = get_name_puzzle_conditions(
chia/consensus/block_creation.py-        generator, constants.MAX_BLOCK_COST_CLVM, mempool_mode=True, height=height, constants=constants
--
chia/consensus/blockchain.py-            assert block.foliage_transaction_block is not None
chia/consensus/blockchain.py:            npc = get_name_puzzle_conditions(
chia/consensus/blockchain.py-                block_generator,
--
chia/consensus/multiprocess_validation.py-                assert block_generator.program == block.transactions_generator
chia/consensus/multiprocess_validation.py:                npc_result = get_name_puzzle_conditions(
chia/consensus/multiprocess_validation.py-                    block_generator,
--
chia/consensus/multiprocess_validation.py-        assert block_generator.program == unfinished_block.transactions_generator
chia/consensus/multiprocess_validation.py:        npc_result: NPCResult = get_name_puzzle_conditions(
chia/consensus/multiprocess_validation.py-            block_generator,
--
chia/full_node/full_node_api.py-                functools.partial(
chia/full_node/full_node_api.py:                    get_name_puzzle_conditions,
chia/full_node/full_node_api.py-                    block_generator,

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 compares run_block_generator() (slowest), run_block_generator2() (middle) and additions_and_removals() (fastest).

image

coveralls-official[bot] commented 1 month ago

Pull Request Test Coverage Report for Build 11263059066

Details


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 Coverage Status
Change from base Build 11262793680: 0.1%
Covered Lines: 12804
Relevant Lines: 15291

💛 - Coveralls