Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into quex.key_management_t…
Browse files Browse the repository at this point in the history
…o_marshal
  • Loading branch information
Quexington committed Sep 24, 2024
2 parents d2e894d + c6630f9 commit 57dca66
Show file tree
Hide file tree
Showing 21 changed files with 1,015 additions and 446 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
136 changes: 111 additions & 25 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(
empty_blockchain.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(
empty_blockchain.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(
b.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(
b.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(
b.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 @@ -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(
b.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(
b.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(
b.constants,
b,
[last_block],
b.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(
b.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(
b.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 @@ -3871,10 +3949,18 @@ async def test_reorg_flip_flop(empty_blockchain: Blockchain, bt: BlockTools) ->
async def test_get_tx_peak(default_400_blocks: List[FullBlock], empty_blockchain: Blockchain) -> None:
bc = 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
ssi = bc.constants.SUB_SLOT_ITERS_STARTING
diff = bc.constants.DIFFICULTY_STARTING
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(
blockchain.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 pre_validate_blocks_multiprocessing(
blockchain.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
10 changes: 6 additions & 4 deletions chia/_tests/core/mempool/test_mempool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions, mempool_check_time_locks
from chia.full_node.mempool_manager import (
MEMPOOL_MIN_FEE_INCREASE,
QUOTE_BYTES,
QUOTE_EXECUTION_COST,
MempoolManager,
TimelockConditions,
can_replace,
Expand Down Expand Up @@ -1943,8 +1945,8 @@ async def get_coin_records(coin_ids: Collection[bytes32]) -> List[CoinRecord]:


TEST_FILL_RATE_ITEM_COST = 144_720_020
QUOTE_BYTE_COST = 2 * DEFAULT_CONSTANTS.COST_PER_BYTE
QUOTE_EXECUTION_COST = 20
TEST_COST_PER_BYTE = 12_000
TEST_BLOCK_OVERHEAD = QUOTE_BYTES * TEST_COST_PER_BYTE + QUOTE_EXECUTION_COST


@pytest.mark.anyio
Expand All @@ -1962,8 +1964,8 @@ async def get_coin_records(coin_ids: Collection[bytes32]) -> List[CoinRecord]:
# Here we set the block cost limit to twice the test items' cost - 1,
# so we expect only one of the two test items to get included in the block.
# NOTE: The cost difference here is because get_conditions_from_spendbundle
# does not include the overhead to make a block (quote byte cost + quote runtime cost).
(TEST_FILL_RATE_ITEM_COST * 2 - 1, 1, TEST_FILL_RATE_ITEM_COST + QUOTE_BYTE_COST + QUOTE_EXECUTION_COST),
# does not include the block overhead.
(TEST_FILL_RATE_ITEM_COST * 2 - 1, 1, TEST_FILL_RATE_ITEM_COST + TEST_BLOCK_OVERHEAD),
],
)
async def test_fill_rate_block_validation(
Expand Down
13 changes: 10 additions & 3 deletions chia/_tests/environments/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ class WalletTestFramework:
environments: List[WalletEnvironment]
tx_config: TXConfig = DEFAULT_TX_CONFIG

async def process_pending_states(self, state_transitions: List[WalletStateTransition]) -> None:
async def process_pending_states(
self, state_transitions: List[WalletStateTransition], invalid_transactions: List[bytes32] = []
) -> None:
"""
This is the main entry point for processing state in wallet tests. It does the following things:
Expand Down Expand Up @@ -302,7 +304,11 @@ async def process_pending_states(self, state_transitions: List[WalletStateTransi
for i, env in enumerate(self.environments):
await self.full_node.wait_for_wallet_synced(wallet_node=env.node, timeout=20)
try:
pending_txs.append(await env.wait_for_transactions_to_settle(self.full_node))
pending_txs.append(
await env.wait_for_transactions_to_settle(
self.full_node, _exclude_from_mempool_check=invalid_transactions
)
)
except TimeoutError: # pragma: no cover
raise TimeoutError(f"All TXs for env-{i} were not found in mempool or marked as in mempool")
for i, (env, transition) in enumerate(zip(self.environments, state_transitions)):
Expand All @@ -328,7 +334,8 @@ async def process_pending_states(self, state_transitions: List[WalletStateTransi
)
try:
await env.wait_for_transactions_to_settle(
self.full_node, _exclude_from_mempool_check=[tx.name for tx in local_pending_txs]
self.full_node,
_exclude_from_mempool_check=invalid_transactions + [tx.name for tx in local_pending_txs],
)
except TimeoutError: # pragma: no cover
raise TimeoutError(f"All TXs for env-{i} were not found in mempool or marked as in mempool")
Expand Down
Loading

0 comments on commit 57dca66

Please sign in to comment.