Skip to content

Commit b497690

Browse files
authored
add different foliage coverage to TL new peak (#19634)
* add diffrent foliage covarage * assert different header hash
1 parent 2b7a453 commit b497690

File tree

1 file changed

+72
-48
lines changed

1 file changed

+72
-48
lines changed

chia/_tests/timelord/test_new_peak.py

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from chia_rs import BlockRecord, FullBlock, SubEpochSummary, UnfinishedBlock
77
from chia_rs.sized_bytes import bytes32
8-
from chia_rs.sized_ints import uint128
8+
from chia_rs.sized_ints import uint64, uint128
99

1010
from chia._tests.blockchain.blockchain_test_utils import _validate_and_add_block
1111
from chia._tests.util.blockchain import create_blockchain
@@ -18,6 +18,7 @@
1818
from chia.server.server import ChiaServer
1919
from chia.simulator.block_tools import BlockTools
2020
from chia.simulator.full_node_simulator import FullNodeSimulator
21+
from chia.simulator.wallet_tools import WalletTool
2122
from chia.timelord.timelord_api import TimelordAPI
2223

2324

@@ -115,14 +116,11 @@ async def test_timelord_new_peak_unfinished_not_orphaned(
115116
await _validate_and_add_block(b1, block_2)
116117

117118
block_record = b1.block_record(block_2.header_hash)
118-
sub_slot_iters, difficulty = get_next_sub_slot_iters_and_difficulty(
119-
bt.constants, len(block_1.finished_sub_slots) > 0, b1.block_record(block_1.prev_header_hash), b1
120-
)
121119

122120
timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
123121
block_1.reward_chain_block.get_unfinished(),
124-
difficulty,
125-
sub_slot_iters,
122+
uint64(block_record.weight - default_1000_blocks[-1].weight),
123+
block_record.sub_slot_iters,
126124
block_1.foliage,
127125
next_sub_epoch_summary(bt.constants, b1, block_record.required_iters, block_1, True),
128126
await get_rc_prev(b1, block_1),
@@ -185,17 +183,11 @@ async def test_timelord_new_peak_unfinished_orphaned(
185183
await _validate_and_add_block(b2, block_2)
186184

187185
block_record_1 = b1.block_record(block_1.header_hash)
188-
sub_slot_iters, difficulty = get_next_sub_slot_iters_and_difficulty(
189-
bt.constants,
190-
len(block_1.finished_sub_slots) > 0,
191-
b1.block_record(block_1.prev_header_hash),
192-
b1,
193-
)
194186

195187
timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
196188
block_1.reward_chain_block.get_unfinished(),
197-
difficulty,
198-
sub_slot_iters,
189+
uint64(block_record_1.weight - default_1000_blocks[-1].weight),
190+
block_record_1.sub_slot_iters,
199191
block_1.foliage,
200192
next_sub_epoch_summary(bt.constants, b1, block_record_1.required_iters, block_1, True),
201193
await get_rc_prev(b1, block_1),
@@ -367,17 +359,10 @@ async def test_timelord_new_peak_unfinished_eos(
367359
assert fn_peak is not None and fn_peak.header_hash == block_2.header_hash
368360

369361
block_record = b2.block_record(block_2.header_hash)
370-
sub_slot_iters, difficulty = get_next_sub_slot_iters_and_difficulty(
371-
bt.constants,
372-
len(block_2.finished_sub_slots) > 0,
373-
b1.block_record(block_2.prev_header_hash),
374-
b1,
375-
)
376-
377362
timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
378363
block_2.reward_chain_block.get_unfinished(),
379-
difficulty,
380-
sub_slot_iters,
364+
uint64(block_record.weight - default_1000_blocks[-1].weight),
365+
block_record.sub_slot_iters,
381366
block_2.foliage,
382367
next_sub_epoch_summary(bt.constants, b1, block_record.required_iters, block_2, True),
383368
await get_rc_prev(b2, block_2),
@@ -461,59 +446,98 @@ async def test_timelord_new_peak_node_sync(
461446
assert peak_tl.reward_chain_block.get_hash() == peak.reward_chain_block.get_hash()
462447

463448
@pytest.mark.anyio
449+
@pytest.mark.parametrize("different_foliage", [False, True])
464450
async def test_timelord_new_peak_is_in_unfinished_cache(
465451
self,
466452
one_node: tuple[list[FullNodeService], list[FullNodeSimulator], BlockTools],
467453
timelord: tuple[TimelordAPI, ChiaServer],
468454
default_1000_blocks: list[FullBlock],
455+
different_foliage: bool,
469456
) -> None:
470457
_, _, bt = one_node
458+
wallet = WalletTool(bt.constants)
459+
coinbase_puzzlehash = wallet.get_new_puzzlehash()
460+
timelord_api, _ = timelord
461+
blocks = bt.get_consecutive_blocks(
462+
num_blocks=10,
463+
block_list_input=default_1000_blocks,
464+
skip_overflow=True,
465+
force_overflow=False,
466+
farmer_reward_puzzle_hash=coinbase_puzzlehash,
467+
guarantee_transaction_block=True,
468+
)
471469
async with create_blockchain(bt.constants, 2) as (b1, _):
472-
timelord_api, _ = timelord
473-
for block in default_1000_blocks:
470+
for block in blocks:
474471
await _validate_and_add_block(b1, block)
475472

476-
peak = timelord_peak_from_block(b1, default_1000_blocks[-1])
477-
assert peak is not None
478-
assert timelord_api.timelord.new_peak is None
473+
peak = timelord_peak_from_block(b1, blocks[-1])
474+
assert peak is not None and timelord_api.timelord.new_peak is None
479475
await timelord_api.new_peak_timelord(peak)
480476
assert timelord_api.timelord.new_peak is not None
481477
await time_out_assert(60, tl_new_peak_is_none, True, timelord_api)
482-
assert timelord_api.timelord.last_state.peak is not None
483478
assert (
484-
timelord_api.timelord.last_state.peak.reward_chain_block.get_hash()
479+
timelord_api.timelord.last_state.peak is not None
480+
and timelord_api.timelord.last_state.peak.reward_chain_block.get_hash()
485481
== peak.reward_chain_block.get_hash()
486482
)
487483

488-
# make two new blocks on tip, block_2 has higher total iterations
489-
block_1 = bt.get_consecutive_blocks(1, default_1000_blocks)[-1]
484+
block_1 = bt.get_consecutive_blocks(
485+
block_list_input=blocks,
486+
num_blocks=1,
487+
farmer_reward_puzzle_hash=coinbase_puzzlehash,
488+
guarantee_transaction_block=True,
489+
skip_overflow=True,
490+
)[-1]
490491

491492
await _validate_and_add_block(b1, block_1)
492-
493493
block_record_1 = b1.block_record(block_1.header_hash)
494-
sub_slot_iters, difficulty = get_next_sub_slot_iters_and_difficulty(
495-
bt.constants,
496-
len(block_1.finished_sub_slots) > 0,
497-
b1.block_record(block_1.prev_header_hash),
498-
b1,
499-
)
494+
if len(block_1.finished_sub_slots) > 0:
495+
timelord_api.timelord.last_state.set_state(block_1.finished_sub_slots[-1])
496+
497+
block_1_diffrent_foliage = None
498+
if not different_foliage:
499+
timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
500+
block_1.reward_chain_block.get_unfinished(),
501+
uint64(block_record_1.weight - blocks[-1].weight),
502+
block_record_1.sub_slot_iters,
503+
block_1.foliage,
504+
next_sub_epoch_summary(bt.constants, b1, block_record_1.required_iters, block_1, True),
505+
await get_rc_prev(b1, block_1),
506+
)
507+
else:
508+
spend_coin = None
509+
for coin in blocks[-8].get_included_reward_coins():
510+
if coin.puzzle_hash == coinbase_puzzlehash:
511+
spend_coin = coin
512+
513+
assert spend_coin is not None
514+
sb = wallet.generate_signed_transaction(uint64(1000), bytes32(b"0" * 32), spend_coin)
515+
block_1_diffrent_foliage = bt.get_consecutive_blocks(
516+
block_list_input=blocks,
517+
num_blocks=1,
518+
farmer_reward_puzzle_hash=coinbase_puzzlehash,
519+
guarantee_transaction_block=True,
520+
transaction_data=sb,
521+
skip_overflow=True,
522+
)[-1]
523+
assert block_1_diffrent_foliage.header_hash != block_1.header_hash
524+
timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
525+
block_1_diffrent_foliage.reward_chain_block.get_unfinished(),
526+
uint64(block_1_diffrent_foliage.weight - blocks[-1].weight),
527+
block_record_1.sub_slot_iters,
528+
block_1_diffrent_foliage.foliage,
529+
next_sub_epoch_summary(bt.constants, b1, block_record_1.required_iters, block_1, True),
530+
await get_rc_prev(b1, block_1),
531+
)
500532

501-
timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
502-
block_1.reward_chain_block.get_unfinished(),
503-
difficulty,
504-
sub_slot_iters,
505-
block_1.foliage,
506-
next_sub_epoch_summary(bt.constants, b1, block_record_1.required_iters, block_1, True),
507-
await get_rc_prev(b1, block_1),
508-
)
509533
await timelord_api.new_unfinished_block_timelord(timelord_unf_block)
510534
assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()
511535
assert (
512536
timelord_api.timelord.unfinished_blocks[-1].reward_chain_block.get_hash()
513537
== timelord_unf_block.reward_chain_block.get_hash()
514538
)
515-
new_peak = timelord_peak_from_block(b1, block_1)
516539

540+
new_peak = timelord_peak_from_block(b1, block_1)
517541
assert timelord_unf_block.reward_chain_block.total_iters == new_peak.reward_chain_block.total_iters
518542
await timelord_api.new_peak_timelord(new_peak)
519543
await time_out_assert(60, tl_new_peak_is_none, True, timelord_api)

0 commit comments

Comments
 (0)