Skip to content

Commit

Permalink
remove block_height_list from BlockGenerator (#18302)
Browse files Browse the repository at this point in the history
* remove block_height_list from BlockGenerator. This field is only used when farming blocks, and we don't use it in production right now. We only use it in the tests, BlockTools. It has been updated with a cleaner and more direct way of setting the block reference list in new blocks

* test
  • Loading branch information
arvidn authored Aug 1, 2024
1 parent a1be3a9 commit ba9420b
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 167 deletions.
70 changes: 12 additions & 58 deletions chia/_tests/blockchain/test_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2533,56 +2533,12 @@ async def test_invalid_transactions_ref_list(
block_list_input=blocks,
guarantee_transaction_block=True,
transaction_data=tx,
previous_generator=[blocks[-1].height],
block_refs=[blocks[-1].height],
)
block = blocks[-1]
if consensus_mode >= ConsensusMode.HARD_FORK_2_0:
# once the hard for activates, we don't use this form of block
# compression anymore
assert len(block.transactions_generator_ref_list) == 0
# the remaining tests assume a reflist
return
else:
assert len(block.transactions_generator_ref_list) > 0

block_2 = recursive_replace(block, "transactions_info.generator_refs_root", bytes([1] * 32))
block_2 = recursive_replace(
block_2, "foliage_transaction_block.transactions_info_hash", block_2.transactions_info.get_hash()
)
block_2 = recursive_replace(
block_2, "foliage.foliage_transaction_block_hash", block_2.foliage_transaction_block.get_hash()
)
new_m = block_2.foliage.foliage_transaction_block_hash
assert new_m is not None
new_fsb_sig = bt.get_plot_signature(new_m, block.reward_chain_block.proof_of_space.plot_public_key)
block_2 = recursive_replace(block_2, "foliage.foliage_transaction_block_signature", new_fsb_sig)

await _validate_and_add_block(
b, block_2, expected_error=Err.INVALID_TRANSACTIONS_GENERATOR_REFS_ROOT, skip_prevalidation=True
)

# Too many heights
block_2 = recursive_replace(block, "transactions_generator_ref_list", [block.height - 2, block.height - 1])
# Fails preval
await _validate_and_add_block(b, block_2, expected_error=Err.FAILED_GETTING_GENERATOR_MULTIPROCESSING)
# Fails add_block
await _validate_and_add_block_multi_error(
b,
block_2,
[Err.GENERATOR_REF_HAS_NO_GENERATOR, Err.INVALID_TRANSACTIONS_GENERATOR_REFS_ROOT],
skip_prevalidation=True,
)

# Not tx block
for h in range(0, block.height - 1):
block_2 = recursive_replace(block, "transactions_generator_ref_list", [h])
await _validate_and_add_block(b, block_2, expected_error=Err.FAILED_GETTING_GENERATOR_MULTIPROCESSING)
await _validate_and_add_block_multi_error(
b,
block_2,
[Err.GENERATOR_REF_HAS_NO_GENERATOR, Err.INVALID_TRANSACTIONS_GENERATOR_REFS_ROOT],
skip_prevalidation=True,
)
# once the hard fork activated, we no longer use this form of block
# compression anymore
assert len(block.transactions_generator_ref_list) == 0

@pytest.mark.anyio
async def test_cost_exceeds_max(
Expand Down Expand Up @@ -2616,7 +2572,7 @@ async def test_cost_exceeds_max(
)

assert blocks[-1].transactions_generator is not None
block_generator = BlockGenerator(blocks[-1].transactions_generator, [], [])
block_generator = BlockGenerator(blocks[-1].transactions_generator, [])
npc_result = get_name_puzzle_conditions(
block_generator,
b.constants.MAX_BLOCK_COST_CLVM * 1000,
Expand Down Expand Up @@ -2682,7 +2638,7 @@ async def test_invalid_cost_in_block(
new_fsb_sig = bt.get_plot_signature(new_m, block.reward_chain_block.proof_of_space.plot_public_key)
block_2 = recursive_replace(block_2, "foliage.foliage_transaction_block_signature", new_fsb_sig)
assert block_2.transactions_generator is not None
block_generator = BlockGenerator(block_2.transactions_generator, [], [])
block_generator = BlockGenerator(block_2.transactions_generator, [])
assert block.transactions_info is not None
npc_result = get_name_puzzle_conditions(
block_generator,
Expand All @@ -2709,7 +2665,7 @@ async def test_invalid_cost_in_block(
new_fsb_sig = bt.get_plot_signature(new_m, block.reward_chain_block.proof_of_space.plot_public_key)
block_2 = recursive_replace(block_2, "foliage.foliage_transaction_block_signature", new_fsb_sig)
assert block_2.transactions_generator is not None
block_generator = BlockGenerator(block_2.transactions_generator, [], [])
block_generator = BlockGenerator(block_2.transactions_generator, [])
assert block.transactions_info is not None
npc_result = get_name_puzzle_conditions(
block_generator,
Expand Down Expand Up @@ -2737,7 +2693,7 @@ async def test_invalid_cost_in_block(
block_2 = recursive_replace(block_2, "foliage.foliage_transaction_block_signature", new_fsb_sig)

assert block_2.transactions_generator is not None
block_generator = BlockGenerator(block_2.transactions_generator, [], [])
block_generator = BlockGenerator(block_2.transactions_generator, [])
max_cost = (
min(b.constants.MAX_BLOCK_COST_CLVM * 1000, block.transactions_info.cost)
if block.transactions_info is not None
Expand Down Expand Up @@ -3619,7 +3575,7 @@ async def test_reorg_new_ref(empty_blockchain: Blockchain, bt: BlockTools) -> No

spend_bundle2 = wallet_a.generate_signed_transaction(uint64(1_000), receiver_puzzlehash, all_coins.pop())
blocks_reorg_chain = bt.get_consecutive_blocks(
4, blocks_reorg_chain, seed=b"2", previous_generator=[uint32(5), uint32(11)], transaction_data=spend_bundle2
4, blocks_reorg_chain, seed=b"2", block_refs=[uint32(5), uint32(11)], transaction_data=spend_bundle2
)
blocks_reorg_chain = bt.get_consecutive_blocks(4, blocks_reorg_chain, seed=b"2")

Expand Down Expand Up @@ -3685,7 +3641,7 @@ async def test_reorg_stale_fork_height(empty_blockchain: Blockchain, bt: BlockTo

# this block (height 10) refers back to the generator in block 5
spend_bundle2 = wallet_a.generate_signed_transaction(uint64(1_000), receiver_puzzlehash, all_coins.pop())
blocks = bt.get_consecutive_blocks(4, blocks, previous_generator=[uint32(5)], transaction_data=spend_bundle2)
blocks = bt.get_consecutive_blocks(4, blocks, block_refs=[uint32(5)], transaction_data=spend_bundle2)

for block in blocks[:5]:
await _validate_and_add_block(b, block, expected_result=AddBlockResult.NEW_PEAK)
Expand Down Expand Up @@ -3795,7 +3751,7 @@ async def test_reorg_flip_flop(empty_blockchain: Blockchain, bt: BlockTools) ->
chain_a = bt.get_consecutive_blocks(
5,
chain_a,
previous_generator=[uint32(10)],
block_refs=[uint32(10)],
transaction_data=spend_bundle,
guarantee_transaction_block=True,
)
Expand Down Expand Up @@ -3835,9 +3791,7 @@ async def test_reorg_flip_flop(empty_blockchain: Blockchain, bt: BlockTools) ->
)

spend_bundle = wallet_a.generate_signed_transaction(uint64(1_000), receiver_puzzlehash, all_coins.pop())
chain_b = bt.get_consecutive_blocks(
10, chain_b, seed=b"2", previous_generator=[uint32(15)], transaction_data=spend_bundle
)
chain_b = bt.get_consecutive_blocks(10, chain_b, seed=b"2", block_refs=[uint32(15)], transaction_data=spend_bundle)

assert len(chain_a) == len(chain_b)

Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/core/full_node/stores/test_coin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def test_basic_coin_store(db_version: int, softfork_height: uint32, bt: Bl
should_be_included.add(pool_coin)
if block.is_transaction_block():
if block.transactions_generator is not None:
block_gen: BlockGenerator = BlockGenerator(block.transactions_generator, [], [])
block_gen: BlockGenerator = BlockGenerator(block.transactions_generator, [])
npc_result = get_name_puzzle_conditions(
block_gen,
bt.constants.MAX_BLOCK_COST_CLVM,
Expand Down
8 changes: 3 additions & 5 deletions chia/_tests/core/mempool/test_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ def generator_condition_tester(
prg = f"(q ((0x0101010101010101010101010101010101010101010101010101010101010101 {'(q ' if quote else ''} {conditions} {')' if quote else ''} {coin_amount} (() (q . ())))))" # noqa
print(f"program: {prg}")
program = SerializedProgram.from_bytes(binutils.assemble(prg).as_bin())
generator = BlockGenerator(program, [], [])
generator = BlockGenerator(program, [])
print(f"len: {len(bytes(program))}")
npc_result: NPCResult = get_name_puzzle_conditions(
generator, max_cost, mempool_mode=mempool_mode, height=height, constants=test_constants
Expand Down Expand Up @@ -2437,7 +2437,7 @@ def test_create_coin_different_parent(self, softfork_height: uint32) -> None:
f'(q ((0x0101010101010101010101010101010101010101010101010101010101010101 (q (51 "{puzzle_hash}" 10)) 123 (() (q . ())))(0x0101010101010101010101010101010101010101010101010101010101010102 (q (51 "{puzzle_hash}" 10)) 123 (() (q . ()))) ))' # noqa
).as_bin()
)
generator = BlockGenerator(program, [], [])
generator = BlockGenerator(program, [])
npc_result: NPCResult = get_name_puzzle_conditions(
generator, MAX_BLOCK_COST_CLVM, mempool_mode=False, height=softfork_height, constants=test_constants
)
Expand Down Expand Up @@ -3184,6 +3184,4 @@ def test_get_puzzle_and_solution_for_coin_failure() -> None:
with pytest.raises(
ValueError, match=f"Failed to get puzzle and solution for coin {TEST_COIN}, error: \\('coin not found', '80'\\)"
):
get_puzzle_and_solution_for_coin(
BlockGenerator(SerializedProgram.to(None), [], []), TEST_COIN, 0, test_constants
)
get_puzzle_and_solution_for_coin(BlockGenerator(SerializedProgram.to(None), []), TEST_COIN, 0, test_constants)
10 changes: 5 additions & 5 deletions chia/_tests/core/test_cost_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async def test_mempool_mode(softfork_height: int, bt: BlockTools) -> None:
f" (() (q . (({unknown_opcode} '00000000000000000000000000000000' 0x0cbba106e000))) ()))))"
).as_bin()
)
generator = BlockGenerator(program, [], [])
generator = BlockGenerator(program, [])
npc_result: NPCResult = get_name_puzzle_conditions(
generator,
bt.constants.MAX_BLOCK_COST_CLVM,
Expand Down Expand Up @@ -184,7 +184,7 @@ async def test_clvm_mempool_mode(softfork_height: int) -> None:
# ("0xfe"). In mempool mode, this should fail, but in non-mempool
# mode, the unknown operator should be treated as if it returns ().
program = SerializedProgram.from_bytes(binutils.assemble(f"(i (0xfe (q . 0)) (q . ()) {disassembly})").as_bin())
generator = BlockGenerator(program, [], [])
generator = BlockGenerator(program, [])
npc_result: NPCResult = get_name_puzzle_conditions(
generator,
test_constants.MAX_BLOCK_COST_CLVM,
Expand All @@ -210,7 +210,7 @@ async def test_tx_generator_speed(softfork_height: int, benchmark_runner: Benchm
program = SerializedProgram.from_bytes(generator_bytes)

with benchmark_runner.assert_runtime(seconds=1.25):
generator = BlockGenerator(program, [], [])
generator = BlockGenerator(program, [])
npc_result = get_name_puzzle_conditions(
generator,
test_constants.MAX_BLOCK_COST_CLVM,
Expand Down Expand Up @@ -238,7 +238,7 @@ async def test_clvm_max_cost(softfork_height: int) -> None:
)

# ensure we fail if the program exceeds the cost
generator = BlockGenerator(program, [], [])
generator = BlockGenerator(program, [])
npc_result = get_name_puzzle_conditions(
generator, 10000000, mempool_mode=False, height=uint32(softfork_height), constants=test_constants
)
Expand Down Expand Up @@ -306,7 +306,7 @@ async def test_get_puzzle_and_solution_for_coin_performance(benchmark_runner: Be

# benchmark the function to pick out the puzzle and solution for a specific
# coin
generator = BlockGenerator(LARGE_BLOCK.transactions_generator, [], [])
generator = BlockGenerator(LARGE_BLOCK.transactions_generator, [])
with benchmark_runner.assert_runtime(seconds=8.5):
for _ in range(3):
for c in spent_coins:
Expand Down
6 changes: 0 additions & 6 deletions chia/_tests/generator/test_generator_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Dict
from unittest import TestCase

from chia.full_node.generator import create_block_generator
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.serialized_program import SerializedProgram
from chia.types.generator_types import GeneratorBlockCacheInterface
Expand Down Expand Up @@ -37,11 +36,6 @@ def get_generator_for_block_height(self, index: uint32) -> SerializedProgram:


class TestGeneratorTypes(TestCase):
def test_make_generator(self) -> None:
block_dict = BlockDict({uint32(1): gen1})
gen = create_block_generator(gen2, [uint32(1)], block_dict)
print(gen)

def test_make_generator_args(self) -> None:
gen_args = Program.to([[bytes(gen1)]])

Expand Down
3 changes: 1 addition & 2 deletions chia/_tests/generator/test_rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def to_sp(sexp: bytes) -> SerializedProgram:

def block_generator() -> BlockGenerator:
generator_list = [to_sp(FIRST_GENERATOR), to_sp(SECOND_GENERATOR)]
generator_heights = [uint32(0), uint32(1)]
return BlockGenerator(to_sp(COMPILED_GENERATOR_CODE), generator_list, generator_heights)
return BlockGenerator(to_sp(COMPILED_GENERATOR_CODE), generator_list)


EXPECTED_ABBREVIATED_COST = 108379
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/util/generator_tools_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run_and_get_removals_and_additions(

if block.transactions_generator is not None:
npc_result = get_name_puzzle_conditions(
BlockGenerator(block.transactions_generator, [], []),
BlockGenerator(block.transactions_generator, []),
max_cost,
mempool_mode=mempool_mode,
height=height,
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/util/run_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def run_generator_with_args(
if not generator_program_hex:
return []
generator_program = SerializedProgram.fromhex(generator_program_hex)
block_generator = BlockGenerator(generator_program, generator_args, [])
block_generator = BlockGenerator(generator_program, generator_args)
return run_generator(block_generator, constants, min(constants.MAX_BLOCK_COST_CLVM, cost))


Expand Down
9 changes: 1 addition & 8 deletions chia/consensus/block_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,13 @@ def create_foliage(
assert prev_block is not None
prev_block_hash = prev_block.header_hash

generator_block_heights_list: List[uint32] = []

foliage_transaction_block_hash: Optional[bytes32]

if is_transaction_block:
cost = uint64(0)

# Calculate the cost of transactions
if block_generator is not None:
generator_block_heights_list = block_generator.block_height_list
cost = compute_cost(block_generator, constants, height)

spend_bundle_fees = compute_fees(additions, removals)
Expand Down Expand Up @@ -229,10 +226,6 @@ def create_foliage(
generator_hash = std_hash(block_generator.program)

generator_refs_hash = bytes32([1] * 32)
if generator_block_heights_list not in (None, []):
generator_ref_list_bytes = b"".join([i.stream_to_bytes() for i in generator_block_heights_list])
generator_refs_hash = std_hash(generator_ref_list_bytes)

filter_hash: bytes32 = std_hash(encoded)

transactions_info: Optional[TransactionsInfo] = TransactionsInfo(
Expand Down Expand Up @@ -423,7 +416,7 @@ def create_unfinished_block(
foliage_transaction_block,
transactions_info,
block_generator.program if block_generator else None,
block_generator.block_height_list if block_generator else [],
[], # generator_refs
)


Expand Down
4 changes: 2 additions & 2 deletions chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ async def get_block_generator(
assert len(ref_list) == 0
return None
if len(ref_list) == 0:
return BlockGenerator(block.transactions_generator, [], [])
return BlockGenerator(block.transactions_generator, [])

result: List[SerializedProgram] = []
previous_br = await self.get_block_record_from_db(block.prev_header_hash)
Expand Down Expand Up @@ -1120,4 +1120,4 @@ async def get_block_generator(
[gen] = await self.block_store.get_generators_at([ref_height])
result.append(gen)
assert len(result) == len(ref_list)
return BlockGenerator(block.transactions_generator, result, [])
return BlockGenerator(block.transactions_generator, result)
4 changes: 2 additions & 2 deletions chia/full_node/bundle_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
def simple_solution_generator(bundle: SpendBundle) -> BlockGenerator:
spends = [(cs.coin, bytes(cs.puzzle_reveal), bytes(cs.solution)) for cs in bundle.coin_spends]
block_program = solution_generator(spends)
return BlockGenerator(SerializedProgram.from_bytes(block_program), [], [])
return BlockGenerator(SerializedProgram.from_bytes(block_program), [])


def simple_solution_generator_backrefs(bundle: SpendBundle) -> BlockGenerator:
spends = [(cs.coin, bytes(cs.puzzle_reveal), bytes(cs.solution)) for cs in bundle.coin_spends]
block_program = solution_generator_backrefs(spends)
return BlockGenerator(SerializedProgram.from_bytes(block_program), [], [])
return BlockGenerator(SerializedProgram.from_bytes(block_program), [])
37 changes: 0 additions & 37 deletions chia/full_node/generator.py

This file was deleted.

Loading

0 comments on commit ba9420b

Please sign in to comment.