diff --git a/src/full_node/weight_proof.py b/src/full_node/weight_proof.py index 4daab83827..4e55435364 100644 --- a/src/full_node/weight_proof.py +++ b/src/full_node/weight_proof.py @@ -330,7 +330,7 @@ class WeightProofHandler: while curr.height < header_block.height: if curr is None: log.error("failed fetching block") - return None + return None, None if len(curr.finished_sub_slots) > 0: for idx, sub_slot in enumerate(curr.finished_sub_slots): curr_icc_info = None @@ -896,7 +896,7 @@ def sub_slot_data_vdf_input( ssd: Optional[SubSlotData] = None if is_overflow and new_sub_slot and prev_segment is None: if sub_slots[sub_slot_idx - 2].cc_slot_end is None: - for ssd_idx in reversed(range(0, sub_slot_idx - 2)): + for ssd_idx in reversed(range(0, sub_slot_idx - 1)): ssd = sub_slots[ssd_idx] if ssd.cc_slot_end is not None: ssd = sub_slots[ssd_idx + 1] diff --git a/tests/core/consensus/test_weight_proof.py b/tests/core/consensus/test_weight_proof.py index 94768c0ebf..8c23459f82 100644 --- a/tests/core/consensus/test_weight_proof.py +++ b/tests/core/consensus/test_weight_proof.py @@ -32,7 +32,12 @@ from src.full_node.weight_proof import ( # type: ignore from src.types.full_block import FullBlock from src.types.header_block import HeaderBlock from src.util.ints import uint32, uint64 -from tests.core.fixtures import default_1000_blocks, default_400_blocks, default_10000_blocks +from tests.core.fixtures import ( + default_1000_blocks, + default_400_blocks, + default_10000_blocks, + pre_genesis_empty_slots_1000_blocks, +) @pytest.fixture(scope="session") @@ -199,6 +204,19 @@ class TestWeightProof: assert valid assert fork_point == 0 + @pytest.mark.asyncio + async def test_weight_proof1000_pre_genesis_empty_slots(self, pre_genesis_empty_slots_1000_blocks): + blocks = pre_genesis_empty_slots_1000_blocks + header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate(blocks) + wpf = WeightProofHandler(test_constants, BlockCache(sub_blocks, header_cache, height_to_hash, summaries)) + wp = await wpf.get_proof_of_weight(blocks[-1].header_hash) + assert wp is not None + wpf = WeightProofHandler(test_constants, BlockCache(sub_blocks, header_cache, height_to_hash, {})) + valid, fork_point = wpf.validate_weight_proof_single_proc(wp) + + assert valid + assert fork_point == 0 + @pytest.mark.asyncio async def test_weight_proof10000(self, default_10000_blocks): blocks = default_10000_blocks diff --git a/tests/core/fixtures.py b/tests/core/fixtures.py index 9e073a57fb..cd68b70841 100644 --- a/tests/core/fixtures.py +++ b/tests/core/fixtures.py @@ -46,6 +46,13 @@ async def default_1000_blocks(): return persistent_blocks(1000, f"test_blocks_1000_{block_format_version}.db") +@pytest.fixture(scope="session") +async def pre_genesis_empty_slots_1000_blocks(): + return persistent_blocks( + 1000, f"pre_genesis_empty_slots_1000_blocks{block_format_version}.db", seed=b"alternate2", empty_sub_slots=1 + ) + + @pytest.fixture(scope="session") async def default_10000_blocks(): return persistent_blocks(10000, f"test_blocks_10000_{block_format_version}.db") @@ -56,7 +63,7 @@ async def default_20000_blocks(): return persistent_blocks(20000, f"test_blocks_20000_{block_format_version}.db") -def persistent_blocks(num_of_blocks: int, db_name: str, seed: bytes = b""): +def persistent_blocks(num_of_blocks: int, db_name: str, seed: bytes = b"", empty_sub_slots=0): # try loading from disc, if not create new blocks.db file # TODO hash fixtures.py and blocktool.py, add to path, delete if the files changed block_path_dir = Path("~/.chia/blocks").expanduser() @@ -78,12 +85,12 @@ def persistent_blocks(num_of_blocks: int, db_name: str, seed: bytes = b""): except EOFError: print("\n error reading db file") - return new_test_db(file_path, num_of_blocks, seed) + return new_test_db(file_path, num_of_blocks, seed, empty_sub_slots) -def new_test_db(path: Path, num_of_blocks: int, seed: bytes): - print(f"create {path} with {num_of_blocks} blocks") - blocks: List[FullBlock] = bt.get_consecutive_blocks(num_of_blocks, seed=seed) +def new_test_db(path: Path, num_of_blocks: int, seed: bytes, empty_sub_slots: int): + print(f"create {path} with {num_of_blocks} blocks with ") + blocks: List[FullBlock] = bt.get_consecutive_blocks(num_of_blocks, seed=seed, skip_slots=empty_sub_slots) block_bytes_list: List[bytes] = [] for block in blocks: block_bytes_list.append(bytes(block))