Skip to content
Merged
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
27 changes: 20 additions & 7 deletions chia/_tests/timelord/test_new_peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional

import pytest
from chia_rs import BlockRecord, FullBlock, SubEpochSummary, UnfinishedBlock
from chia_rs import BlockRecord, FullBlock, SubEpochSummary, UnfinishedBlock, is_overflow_block
from chia_rs.sized_bytes import bytes32
from chia_rs.sized_ints import uint64, uint128

Expand All @@ -22,6 +22,13 @@
from chia.timelord.timelord_api import TimelordAPI


def last_unfinished(tl: TimelordAPI, *, overflow: bool) -> timelord_protocol.NewUnfinishedBlockTimelord:
if overflow:
return tl.timelord.overflow_blocks[-1]
else:
return tl.timelord.unfinished_blocks[-1]


class TestNewPeak:
@pytest.mark.anyio
async def test_timelord_new_peak_basic(
Expand Down Expand Up @@ -127,7 +134,8 @@ async def test_timelord_new_peak_unfinished_not_orphaned(
)
await timelord_api.new_unfinished_block_timelord(timelord_unf_block)

assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()
overflow = is_overflow_block(bt.constants, timelord_unf_block.reward_chain_block.signage_point_index)
assert last_unfinished(timelord_api, overflow=overflow).get_hash() == timelord_unf_block.get_hash()
new_peak = timelord_peak_from_block(b1, block_2)
assert timelord_unf_block.reward_chain_block.total_iters <= new_peak.reward_chain_block.total_iters
await timelord_api.new_peak_timelord(new_peak)
Expand Down Expand Up @@ -193,7 +201,9 @@ async def test_timelord_new_peak_unfinished_orphaned(
await get_rc_prev(b1, block_1),
)
await timelord_api.new_unfinished_block_timelord(timelord_unf_block)
assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()

overflow = is_overflow_block(bt.constants, timelord_unf_block.reward_chain_block.signage_point_index)
assert last_unfinished(timelord_api, overflow=overflow).get_hash() == timelord_unf_block.get_hash()
new_peak = timelord_peak_from_block(b2, block_2)

# timelord knows unfinished block_1 that has lower iterations,
Expand All @@ -208,7 +218,8 @@ async def test_timelord_new_peak_unfinished_orphaned(
== peak.reward_chain_block.get_hash()
)
# check unfinished block_1 is still in cache
assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()
overflow = is_overflow_block(bt.constants, timelord_unf_block.reward_chain_block.signage_point_index)
assert last_unfinished(timelord_api, overflow=overflow).get_hash() == timelord_unf_block.get_hash()

# full node gets block_1 unfinished
block_1_unf = UnfinishedBlock(
Expand Down Expand Up @@ -371,7 +382,8 @@ async def test_timelord_new_peak_unfinished_eos(

# add unfinished and make sure we cache it
await timelord_api.new_unfinished_block_timelord(timelord_unf_block)
assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()
overflow = is_overflow_block(bt.constants, timelord_unf_block.reward_chain_block.signage_point_index)
assert last_unfinished(timelord_api, overflow=overflow).get_hash() == timelord_unf_block.get_hash()
new_peak = timelord_peak_from_block(b1, block_1)
assert timelord_unf_block.reward_chain_block.total_iters >= new_peak.reward_chain_block.total_iters
await timelord_api.new_peak_timelord(new_peak)
Expand Down Expand Up @@ -531,9 +543,10 @@ async def test_timelord_new_peak_is_in_unfinished_cache(
)

await timelord_api.new_unfinished_block_timelord(timelord_unf_block)
assert timelord_api.timelord.unfinished_blocks[-1].get_hash() == timelord_unf_block.get_hash()
overflow = is_overflow_block(bt.constants, timelord_unf_block.reward_chain_block.signage_point_index)
assert last_unfinished(timelord_api, overflow=overflow).get_hash() == timelord_unf_block.get_hash()
assert (
timelord_api.timelord.unfinished_blocks[-1].reward_chain_block.get_hash()
last_unfinished(timelord_api, overflow=overflow).reward_chain_block.get_hash()
== timelord_unf_block.reward_chain_block.get_hash()
)

Expand Down
3 changes: 3 additions & 0 deletions chia/timelord/timelord.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class Timelord:

_protocol_check: ClassVar[RpcServiceProtocol] = cast("Timelord", None)

unfinished_blocks: list[timelord_protocol.NewUnfinishedBlockTimelord]
overflow_blocks: list[timelord_protocol.NewUnfinishedBlockTimelord]

@property
def server(self) -> ChiaServer:
# This is a stop gap until the class usage is refactored such the values of
Expand Down
Loading