Skip to content

Commit

Permalink
slightly simplify call to pre_validate_blocks_multiprocessing. batch …
Browse files Browse the repository at this point in the history
…size is always 4, check_filter is always True. Remove the member function wrapper around it in Blockchain, to prepare for being able to pass wrapped blockchains into it
  • Loading branch information
arvidn committed Sep 12, 2024
1 parent 9ecba6d commit b20def2
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 116 deletions.
14 changes: 11 additions & 3 deletions chia/_tests/blockchain/blockchain_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.blockchain import AddBlockResult, Blockchain
from chia.consensus.difficulty_adjustment import get_next_sub_slot_iters_and_difficulty
from chia.consensus.multiprocess_validation import PreValidationResult
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_blocks_multiprocessing
from chia.types.full_block import FullBlock
from chia.util.errors import Err
from chia.util.ints import uint32, uint64
Expand Down Expand Up @@ -75,8 +75,16 @@ async def _validate_and_add_block(
else:
# validate_signatures must be False in order to trigger add_block() to
# validate the signature.
pre_validation_results: List[PreValidationResult] = await blockchain.pre_validate_blocks_multiprocessing(
[block], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=prev_ses_block, validate_signatures=False
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
blockchain.constants,
blockchain,
[block],
blockchain.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=prev_ses_block,
validate_signatures=False,
)
assert pre_validation_results is not None
results = pre_validation_results[0]
Expand Down
132 changes: 109 additions & 23 deletions chia/_tests/blockchain/test_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from chia.consensus.constants import ConsensusConstants
from chia.consensus.full_block_to_block_record import block_to_block_record
from chia.consensus.get_block_generator import get_block_generator
from chia.consensus.multiprocess_validation import PreValidationResult
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_blocks_multiprocessing
from chia.consensus.pot_iterations import is_overflow_block
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions
from chia.simulator.block_tools import BlockTools, create_block_tools_async
Expand Down Expand Up @@ -1819,8 +1819,11 @@ async def test_pre_validation_fails_bad_blocks(self, empty_blockchain: Blockchai
block_bad = recursive_replace(
blocks[-1], "reward_chain_block.total_iters", blocks[-1].reward_chain_block.total_iters + 1
)
res = await empty_blockchain.pre_validate_blocks_multiprocessing(
res = await pre_validate_blocks_multiprocessing(
bt.constants,
empty_blockchain,
[blocks[0], block_bad],
empty_blockchain.pool,
{},
sub_slot_iters=ssi,
difficulty=difficulty,
Expand All @@ -1845,8 +1848,11 @@ async def test_pre_validation(
end_i = min(i + n_at_a_time, len(blocks))
blocks_to_validate = blocks[i:end_i]
start_pv = time.time()
res = await empty_blockchain.pre_validate_blocks_multiprocessing(
res = await pre_validate_blocks_multiprocessing(
bt.constants,
empty_blockchain,
blocks_to_validate,
empty_blockchain.pool,
{},
sub_slot_iters=ssi,
difficulty=difficulty,
Expand Down Expand Up @@ -1950,8 +1956,16 @@ async def test_conditions(
)
ssi = b.constants.SUB_SLOT_ITERS_STARTING
diff = b.constants.DIFFICULTY_STARTING
pre_validation_results: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
[blocks[-1]], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[blocks[-1]],
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
# Ignore errors from pre-validation, we are testing block_body_validation
repl_preval_results = replace(pre_validation_results[0], error=None, required_iters=uint64(1))
Expand Down Expand Up @@ -2066,8 +2080,16 @@ async def test_timelock_conditions(
)
ssi = b.constants.SUB_SLOT_ITERS_STARTING
diff = b.constants.DIFFICULTY_STARTING
pre_validation_results: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
[blocks[-1]], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=True
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[blocks[-1]],
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=True,
)
assert pre_validation_results is not None
assert (await b.add_block(blocks[-1], pre_validation_results[0], None, sub_slot_iters=ssi))[0] == expected
Expand Down Expand Up @@ -2139,8 +2161,16 @@ async def test_aggsig_garbage(
)
ssi = b.constants.SUB_SLOT_ITERS_STARTING
diff = b.constants.DIFFICULTY_STARTING
pre_validation_results: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
[blocks[-1]], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[blocks[-1]],
empty_blockchain.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
# Ignore errors from pre-validation, we are testing block_body_validation
repl_preval_results = replace(pre_validation_results[0], error=None, required_iters=uint64(1))
Expand Down Expand Up @@ -2257,8 +2287,16 @@ async def test_ephemeral_timelock(
)
ssi = b.constants.SUB_SLOT_ITERS_STARTING
diff = b.constants.DIFFICULTY_STARTING
pre_validation_results: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
[blocks[-1]], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=True
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[blocks[-1]],
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=True,
)
assert pre_validation_results is not None
assert (await b.add_block(blocks[-1], pre_validation_results[0], None, sub_slot_iters=ssi))[0] == expected
Expand Down Expand Up @@ -2607,8 +2645,16 @@ async def test_cost_exceeds_max(
)
)[1]
assert err in [Err.BLOCK_COST_EXCEEDS_MAX]
results: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
[blocks[-1]], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[blocks[-1]],
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
assert results is not None
assert Err(results[0].error) == Err.BLOCK_COST_EXCEEDS_MAX
Expand Down Expand Up @@ -3178,8 +3224,16 @@ async def test_invalid_agg_sig(self, empty_blockchain: Blockchain, bt: BlockTool
# Bad signature also fails in prevalidation
ssi = b.constants.SUB_SLOT_ITERS_STARTING
diff = b.constants.DIFFICULTY_STARTING
preval_results = await b.pre_validate_blocks_multiprocessing(
[last_block], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=True
preval_results = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[last_block],
empty_blockchain.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=True,
)
assert preval_results is not None
assert preval_results[0].error == Err.BAD_AGGREGATE_SIGNATURE.value
Expand Down Expand Up @@ -3288,8 +3342,16 @@ async def test_long_reorg(
print(f"pre-validating {len(blocks)} blocks")
ssi = b.constants.SUB_SLOT_ITERS_STARTING
diff = b.constants.DIFFICULTY_STARTING
pre_validation_results: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
blocks, {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
b.constants,
b,
blocks,
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
for i, block in enumerate(blocks):
if block.height != 0 and len(block.finished_sub_slots) > 0:
Expand Down Expand Up @@ -3841,13 +3903,29 @@ async def test_reorg_flip_flop(empty_blockchain: Blockchain, bt: BlockTools) ->
block1, block2 = b1, b2
counter += 1

preval: List[PreValidationResult] = await b.pre_validate_blocks_multiprocessing(
[block1], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
preval: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[block1],
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
_, err, _ = await b.add_block(block1, preval[0], None, sub_slot_iters=ssi)
assert err is None
preval = await b.pre_validate_blocks_multiprocessing(
[block2], {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
preval = await pre_validate_blocks_multiprocessing(
bt.constants,
b,
[block2],
b.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
_, err, _ = await b.add_block(block2, preval[0], None, sub_slot_iters=ssi)
assert err is None
Expand All @@ -3873,8 +3951,16 @@ async def test_get_tx_peak(default_400_blocks: List[FullBlock], empty_blockchain
test_blocks = default_400_blocks[:100]
ssi = empty_blockchain.constants.SUB_SLOT_ITERS_STARTING
diff = empty_blockchain.constants.DIFFICULTY_STARTING
res = await bc.pre_validate_blocks_multiprocessing(
test_blocks, {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=None, validate_signatures=False
res = await pre_validate_blocks_multiprocessing(
bc.constants,
bc,
test_blocks,
bc.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)

last_tx_block_record = None
Expand Down
55 changes: 29 additions & 26 deletions chia/_tests/core/full_node/test_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from chia._tests.util.setup_nodes import SimulatorsAndWalletsServices
from chia._tests.util.time_out_assert import time_out_assert, time_out_assert_custom_interval, time_out_messages
from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.multiprocess_validation import pre_validate_blocks_multiprocessing
from chia.consensus.pot_iterations import is_overflow_block
from chia.full_node.full_node import WalletUpdate
from chia.full_node.full_node_api import FullNodeAPI
Expand Down Expand Up @@ -425,37 +426,39 @@ async def check_transaction_confirmed(transaction) -> bool:
for reorg_block in reog_blocks[:r]:
await _validate_and_add_block_no_error(blockchain, reorg_block)
for i in range(1, height):
for batch_size in range(1, height, 3):
results = await blockchain.pre_validate_blocks_multiprocessing(
all_blocks[:i],
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
batch_size=batch_size,
validate_signatures=False,
)
assert results is not None
for result in results:
assert result.error is None
results = await pre_validate_blocks_multiprocessing(
bt.constants,
blockchain,
all_blocks[:i],
blockchain.pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
assert results is not None
for result in results:
assert result.error is None

for r in range(0, len(all_blocks), 3):
for block in all_blocks[:r]:
await _validate_and_add_block_no_error(blockchain, block)
for i in range(1, height):
for batch_size in range(1, height, 3):
results = await blockchain.pre_validate_blocks_multiprocessing(
all_blocks[:i],
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
batch_size=batch_size,
validate_signatures=False,
)
assert results is not None
for result in results:
assert result.error is None
results = await blockchain.pre_validate_blocks_multiprocessing(
bt.constants,
blockchain,
all_blocks[:i],
{},
blockchain.pool,
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=None,
validate_signatures=False,
)
assert results is not None
for result in results:
assert result.error is None


class TestFullNodeProtocol:
Expand Down
17 changes: 14 additions & 3 deletions chia/_tests/farmer_harvester/test_third_party_harvesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from chia._tests.util.time_out_assert import time_out_assert
from chia.consensus.blockchain import AddBlockResult
from chia.consensus.difficulty_adjustment import get_next_sub_slot_iters_and_difficulty
from chia.consensus.multiprocess_validation import PreValidationResult
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_blocks_multiprocessing
from chia.farmer.farmer import Farmer, calculate_harvester_fee_quality
from chia.farmer.farmer_api import FarmerAPI
from chia.full_node.full_node import FullNode
Expand Down Expand Up @@ -435,8 +435,19 @@ async def add_test_blocks_into_full_node(blocks: List[FullBlock], full_node: Ful
prev_ses_block = curr
new_slot = len(block.finished_sub_slots) > 0
ssi, diff = get_next_sub_slot_iters_and_difficulty(full_node.constants, new_slot, prev_b, full_node.blockchain)
pre_validation_results: List[PreValidationResult] = await full_node.blockchain.pre_validate_blocks_multiprocessing(
blocks, {}, sub_slot_iters=ssi, difficulty=diff, prev_ses_block=prev_ses_block, validate_signatures=True

constants = full_node.blockchain.constants
pool = full_node.blockchain.pool
pre_validation_results: List[PreValidationResult] = await pre_validate_blocks_multiprocessing(
constants,
full_node.blockchain,
blocks,
pool,
{},
sub_slot_iters=ssi,
difficulty=diff,
prev_ses_block=prev_ses_block,
validate_signatures=True,
)
assert pre_validation_results is not None and len(pre_validation_results) == len(blocks)
for i in range(len(blocks)):
Expand Down
Loading

0 comments on commit b20def2

Please sign in to comment.