CHIA-1668 Move validate_block_merkle_roots next to validate_block_body (#18757)

Move validate_block_merkle_roots next to validate_block_body.
This commit is contained in:
Amine Khaldi
2024-10-28 10:31:24 -07:00
committed by GitHub
parent 6084112d48
commit 03a41ac486
2 changed files with 34 additions and 44 deletions
+34 -3
View File
@@ -6,17 +6,16 @@ from collections.abc import Awaitable, Collection
from dataclasses import dataclass, field
from typing import Callable, Optional, Union
from chia_rs import AugSchemeMPL, BLSCache, G1Element, SpendBundleConditions
from chia_rs import AugSchemeMPL, BLSCache, G1Element, SpendBundleConditions, compute_merkle_set_root
from chiabip158 import PyBIP158
from chia.consensus.block_record import BlockRecord
from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward
from chia.consensus.block_root_validation import validate_block_merkle_roots
from chia.consensus.blockchain_interface import BlockRecordsProtocol
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.constants import ConsensusConstants
from chia.full_node.mempool_check_conditions import mempool_check_time_locks
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.coin import Coin, hash_coin_ids
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_record import CoinRecord
from chia.types.full_block import FullBlock
@@ -149,6 +148,38 @@ class ForkInfo:
self.removals_since_fork = {k: v for k, v in self.removals_since_fork.items() if v.height <= height}
def validate_block_merkle_roots(
block_additions_root: bytes32,
block_removals_root: bytes32,
tx_additions: list[tuple[Coin, bytes32]],
tx_removals: list[bytes32],
) -> Optional[Err]:
# Create addition Merkle set
puzzlehash_coins_map: dict[bytes32, list[bytes32]] = {}
for coin, coin_name in tx_additions:
if coin.puzzle_hash in puzzlehash_coins_map:
puzzlehash_coins_map[coin.puzzle_hash].append(coin_name)
else:
puzzlehash_coins_map[coin.puzzle_hash] = [coin_name]
# Addition Merkle set contains puzzlehash and hash of all coins with that puzzlehash
additions_merkle_items: list[bytes32] = []
for puzzle, coin_ids in puzzlehash_coins_map.items():
additions_merkle_items.append(puzzle)
additions_merkle_items.append(hash_coin_ids(coin_ids))
additions_root = bytes32(compute_merkle_set_root(additions_merkle_items))
removals_root = bytes32(compute_merkle_set_root(tx_removals))
if block_additions_root != additions_root:
return Err.BAD_ADDITION_ROOT
if block_removals_root != removals_root:
return Err.BAD_REMOVAL_ROOT
return None
async def validate_block_body(
constants: ConsensusConstants,
records: BlockRecordsProtocol,
-41
View File
@@ -1,41 +0,0 @@
from __future__ import annotations
from typing import Optional
from chia_rs import compute_merkle_set_root
from chia.types.blockchain_format.coin import Coin, hash_coin_ids
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.errors import Err
def validate_block_merkle_roots(
block_additions_root: bytes32,
block_removals_root: bytes32,
tx_additions: list[tuple[Coin, bytes32]],
tx_removals: list[bytes32],
) -> Optional[Err]:
# Create addition Merkle set
puzzlehash_coins_map: dict[bytes32, list[bytes32]] = {}
for coin, coin_name in tx_additions:
if coin.puzzle_hash in puzzlehash_coins_map:
puzzlehash_coins_map[coin.puzzle_hash].append(coin_name)
else:
puzzlehash_coins_map[coin.puzzle_hash] = [coin_name]
# Addition Merkle set contains puzzlehash and hash of all coins with that puzzlehash
additions_merkle_items: list[bytes32] = []
for puzzle, coin_ids in puzzlehash_coins_map.items():
additions_merkle_items.append(puzzle)
additions_merkle_items.append(hash_coin_ids(coin_ids))
additions_root = bytes32(compute_merkle_set_root(additions_merkle_items))
removals_root = bytes32(compute_merkle_set_root(tx_removals))
if block_additions_root != additions_root:
return Err.BAD_ADDITION_ROOT
if block_removals_root != removals_root:
return Err.BAD_REMOVAL_ROOT
return None