|
5 | 5 | import pytest
|
6 | 6 | from chia_rs import BlockRecord, FullBlock, SubEpochSummary, UnfinishedBlock
|
7 | 7 | from chia_rs.sized_bytes import bytes32
|
8 |
| -from chia_rs.sized_ints import uint128 |
| 8 | +from chia_rs.sized_ints import uint64, uint128 |
9 | 9 |
|
10 | 10 | from chia._tests.blockchain.blockchain_test_utils import _validate_and_add_block
|
11 | 11 | from chia._tests.util.blockchain import create_blockchain
|
|
18 | 18 | from chia.server.server import ChiaServer
|
19 | 19 | from chia.simulator.block_tools import BlockTools
|
20 | 20 | from chia.simulator.full_node_simulator import FullNodeSimulator
|
| 21 | +from chia.simulator.wallet_tools import WalletTool |
21 | 22 | from chia.timelord.timelord_api import TimelordAPI
|
22 | 23 |
|
23 | 24 |
|
@@ -115,14 +116,11 @@ async def test_timelord_new_peak_unfinished_not_orphaned(
|
115 | 116 | await _validate_and_add_block(b1, block_2)
|
116 | 117 |
|
117 | 118 | 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 |
| - ) |
121 | 119 |
|
122 | 120 | timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
|
123 | 121 | 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, |
126 | 124 | block_1.foliage,
|
127 | 125 | next_sub_epoch_summary(bt.constants, b1, block_record.required_iters, block_1, True),
|
128 | 126 | await get_rc_prev(b1, block_1),
|
@@ -185,17 +183,11 @@ async def test_timelord_new_peak_unfinished_orphaned(
|
185 | 183 | await _validate_and_add_block(b2, block_2)
|
186 | 184 |
|
187 | 185 | 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 |
| - ) |
194 | 186 |
|
195 | 187 | timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
|
196 | 188 | 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, |
199 | 191 | block_1.foliage,
|
200 | 192 | next_sub_epoch_summary(bt.constants, b1, block_record_1.required_iters, block_1, True),
|
201 | 193 | await get_rc_prev(b1, block_1),
|
@@ -367,17 +359,10 @@ async def test_timelord_new_peak_unfinished_eos(
|
367 | 359 | assert fn_peak is not None and fn_peak.header_hash == block_2.header_hash
|
368 | 360 |
|
369 | 361 | 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 |
| - |
377 | 362 | timelord_unf_block = timelord_protocol.NewUnfinishedBlockTimelord(
|
378 | 363 | 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, |
381 | 366 | block_2.foliage,
|
382 | 367 | next_sub_epoch_summary(bt.constants, b1, block_record.required_iters, block_2, True),
|
383 | 368 | await get_rc_prev(b2, block_2),
|
@@ -461,59 +446,98 @@ async def test_timelord_new_peak_node_sync(
|
461 | 446 | assert peak_tl.reward_chain_block.get_hash() == peak.reward_chain_block.get_hash()
|
462 | 447 |
|
463 | 448 | @pytest.mark.anyio
|
| 449 | + @pytest.mark.parametrize("different_foliage", [False, True]) |
464 | 450 | async def test_timelord_new_peak_is_in_unfinished_cache(
|
465 | 451 | self,
|
466 | 452 | one_node: tuple[list[FullNodeService], list[FullNodeSimulator], BlockTools],
|
467 | 453 | timelord: tuple[TimelordAPI, ChiaServer],
|
468 | 454 | default_1000_blocks: list[FullBlock],
|
| 455 | + different_foliage: bool, |
469 | 456 | ) -> None:
|
470 | 457 | _, _, 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 | + ) |
471 | 469 | 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: |
474 | 471 | await _validate_and_add_block(b1, block)
|
475 | 472 |
|
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 |
479 | 475 | await timelord_api.new_peak_timelord(peak)
|
480 | 476 | assert timelord_api.timelord.new_peak is not None
|
481 | 477 | await time_out_assert(60, tl_new_peak_is_none, True, timelord_api)
|
482 |
| - assert timelord_api.timelord.last_state.peak is not None |
483 | 478 | 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() |
485 | 481 | == peak.reward_chain_block.get_hash()
|
486 | 482 | )
|
487 | 483 |
|
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] |
490 | 491 |
|
491 | 492 | await _validate_and_add_block(b1, block_1)
|
492 |
| - |
493 | 493 | 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 | + ) |
500 | 532 |
|
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 |
| - ) |
509 | 533 | await timelord_api.new_unfinished_block_timelord(timelord_unf_block)
|
510 | 534 | assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()
|
511 | 535 | assert (
|
512 | 536 | timelord_api.timelord.unfinished_blocks[-1].reward_chain_block.get_hash()
|
513 | 537 | == timelord_unf_block.reward_chain_block.get_hash()
|
514 | 538 | )
|
515 |
| - new_peak = timelord_peak_from_block(b1, block_1) |
516 | 539 |
|
| 540 | + new_peak = timelord_peak_from_block(b1, block_1) |
517 | 541 | assert timelord_unf_block.reward_chain_block.total_iters == new_peak.reward_chain_block.total_iters
|
518 | 542 | await timelord_api.new_peak_timelord(new_peak)
|
519 | 543 | await time_out_assert(60, tl_new_peak_is_none, True, timelord_api)
|
|
0 commit comments