Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHIA-1945 Accept a tx_filter param in get_block_header and exclude reward coins when the filter is not requested #18969

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions chia/_tests/blockchain/test_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async def test_long_chain(self, empty_blockchain: Blockchain, default_1000_block
block_bad = recursive_replace(
block, "finished_sub_slots", [new_finished_ss] + block.finished_sub_slots[1:]
)
header_block_bad = get_block_header(block_bad, [], [])
header_block_bad = get_block_header(block_bad, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = block.finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand All @@ -192,7 +192,7 @@ async def test_long_chain(self, empty_blockchain: Blockchain, default_1000_block
block, "finished_sub_slots", [new_finished_ss_2] + block.finished_sub_slots[1:]
)

header_block_bad_2 = get_block_header(block_bad_2, [], [])
header_block_bad_2 = get_block_header(block_bad_2, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = block.finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand Down Expand Up @@ -222,7 +222,7 @@ async def test_long_chain(self, empty_blockchain: Blockchain, default_1000_block
log.warning(f"Number of slots: {len(block.finished_sub_slots)}")
block_bad_3 = recursive_replace(block, "finished_sub_slots", [new_finished_ss_3])

header_block_bad_3 = get_block_header(block_bad_3, [], [])
header_block_bad_3 = get_block_header(block_bad_3, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = block.finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand Down Expand Up @@ -251,7 +251,7 @@ async def test_long_chain(self, empty_blockchain: Blockchain, default_1000_block
)
block_bad_4 = recursive_replace(block, "finished_sub_slots", [new_finished_ss_4])

header_block_bad_4 = get_block_header(block_bad_4, [], [])
header_block_bad_4 = get_block_header(block_bad_4, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = block.finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand Down Expand Up @@ -507,7 +507,7 @@ async def test_invalid_sub_slot_challenge_hash_genesis(self, empty_blockchain: B
blocks[0], "finished_sub_slots", [new_finished_ss] + blocks[0].finished_sub_slots[1:]
)

header_block_bad = get_block_header(block_0_bad, [], [])
header_block_bad = get_block_header(block_0_bad, [], [], tx_filter=False)
expected_vs = ValidationState(
empty_blockchain.constants.SUB_SLOT_ITERS_STARTING, empty_blockchain.constants.DIFFICULTY_STARTING, None
)
Expand Down Expand Up @@ -536,7 +536,7 @@ async def test_invalid_sub_slot_challenge_hash_non_genesis(
)

await _validate_and_add_block(empty_blockchain, blocks[0])
header_block_bad = get_block_header(block_1_bad, [], [])
header_block_bad = get_block_header(block_1_bad, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = blocks[1].finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = blocks[1].finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand All @@ -563,7 +563,7 @@ async def test_invalid_sub_slot_challenge_hash_empty_ss(self, empty_blockchain:
)
await _validate_and_add_block(empty_blockchain, blocks[0])

header_block_bad = get_block_header(block_1_bad, [], [])
header_block_bad = get_block_header(block_1_bad, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = blocks[1].finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = blocks[1].finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand Down Expand Up @@ -718,7 +718,7 @@ async def test_invalid_icc_into_cc(self, empty_blockchain: Blockchain, bt: Block
block, "finished_sub_slots", block.finished_sub_slots[:-1] + [new_finished_ss]
)

header_block_bad = get_block_header(block_bad, [], [])
header_block_bad = get_block_header(block_bad, [], [], tx_filter=False)
# TODO: Inspect these block values as they are currently None
expected_difficulty = block.finished_sub_slots[0].challenge_chain.new_difficulty or uint64(0)
expected_sub_slot_iters = block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters or uint64(0)
Expand Down Expand Up @@ -789,7 +789,7 @@ async def test_empty_slot_no_ses(self, empty_blockchain: Blockchain, bt: BlockTo
blocks[-1], "finished_sub_slots", blocks[-1].finished_sub_slots[:-1] + [new_finished_ss]
)

header_block_bad = get_block_header(block_bad, [], [])
header_block_bad = get_block_header(block_bad, [], [], tx_filter=False)
expected_vs = ValidationState(
empty_blockchain.constants.SUB_SLOT_ITERS_STARTING, empty_blockchain.constants.DIFFICULTY_STARTING, None
)
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/util/test_full_block_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,6 @@ async def test_parser():
@pytest.mark.skip("This test is expensive and has already convinced us the parser works")
async def test_header_block():
for block in get_full_blocks():
hb: HeaderBlock = get_block_header(block, [], [])
hb: HeaderBlock = get_block_header(block, [], [], tx_filter=True)
hb_bytes = header_block_from_block(memoryview(bytes(block)))
assert HeaderBlock.from_bytes(hb_bytes) == hb
2 changes: 1 addition & 1 deletion chia/_tests/wallet/test_wallet_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def test_wallet_blockchain(

header_blocks: list[HeaderBlock] = []
for block in default_1000_blocks:
header_block = get_block_header(block, [], [])
header_block = get_block_header(block, [], [], tx_filter=False)
header_blocks.append(header_block)

res, err = await chain.add_block(header_blocks[50])
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/weight_proof/test_weight_proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def load_blocks_dont_validate(
)
sub_blocks[block.header_hash] = sub_block
height_to_hash[block.height] = block.header_hash
header_cache[block.header_hash] = get_block_header(block, [], [])
header_cache[block.header_hash] = get_block_header(block, [], [], tx_filter=False)
if sub_block.sub_epoch_summary_included is not None:
sub_epoch_summaries[block.height] = sub_block.sub_epoch_summary_included
prev_block = block
Expand Down
6 changes: 3 additions & 3 deletions chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,19 +838,19 @@ async def get_header_blocks_in_range(
if self.height_to_hash(block.height) != block.header_hash:
raise ValueError(f"Block at {block.header_hash} is no longer in the blockchain (it's in a fork)")
if tx_filter is False:
header = get_block_header(block, [], [])
header = get_block_header(block, [], [], tx_filter)
elif block.transactions_generator is None:
# There is no point in getting additions and removals for
# blocks that do not have transactions.
header = get_block_header(block, [], [])
header = get_block_header(block, [], [], tx_filter)
else:
added_coins_records, removed_coins_records = await asyncio.gather(
self.coin_store.get_coins_added_at_height(block.height),
self.coin_store.get_coins_removed_at_height(block.height),
)
tx_additions = [cr.coin for cr in added_coins_records if not cr.coinbase]
removed = [cr.coin.name() for cr in removed_coins_records]
header = get_block_header(block, tx_additions, removed)
header = get_block_header(block, tx_additions, removed, tx_filter)
header_blocks[header.header_hash] = header

return header_blocks
Expand Down
2 changes: 1 addition & 1 deletion chia/consensus/multiprocess_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _pre_validate_block(
removals, tx_additions = tx_removals_and_additions(conds)

assert conds is None or conds.validated_signature is True
header_block = get_block_header(block, tx_additions, removals)
header_block = get_block_header(block, tx_additions, removals, tx_filter=True)
required_iters, error = validate_finished_header_block(
constants,
blockchain,
Expand Down
4 changes: 2 additions & 2 deletions chia/full_node/full_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ async def request_block_header(self, request: wallet_protocol.RequestBlockHeader
tx_additions = [add[0] for add in additions]
tx_removals = [rem.name() for rem in removals]

header_block = get_block_header(block, tx_additions, tx_removals)
header_block = get_block_header(block, tx_additions, tx_removals, tx_filter=True)
msg = make_msg(
ProtocolMessageTypes.respond_block_header,
wallet_protocol.RespondBlockHeader(header_block),
Expand Down Expand Up @@ -1499,7 +1499,7 @@ async def request_header_blocks(self, request: wallet_protocol.RequestHeaderBloc
)
added_coins = [record.coin for record in added_coins_records if not record.coinbase]
removal_names = [record.coin.name() for record in removed_coins_records]
header_block = get_block_header(block, added_coins, removal_names)
header_block = get_block_header(block, added_coins, removal_names, tx_filter=True)
header_blocks.append(header_block)

msg = make_msg(
Expand Down
4 changes: 2 additions & 2 deletions chia/util/generator_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@


def get_block_header(
block: FullBlock, tx_addition_coins: Collection[Coin], removals_names: Collection[bytes32]
block: FullBlock, tx_addition_coins: Collection[Coin], removals_names: Collection[bytes32], tx_filter: bool
) -> HeaderBlock:
# Create filter
byte_array_tx: list[bytearray] = []
if block.is_transaction_block():
if tx_filter and block.is_transaction_block():
for coin in tx_addition_coins:
byte_array_tx.append(bytearray(coin.puzzle_hash))
for coin in block.get_included_reward_coins():
Expand Down
Loading