Repository: Chia-Network/chia-blockchain
File: chia/_tests/core/full_node/stores/test_coin_store.py (Python)

Code before:
            records = [await coin_store.get_coin_record(coin.name()) for coin in coins]
            for record in records:
                assert record is not None
                assert record.spent
                assert record.spent_block_index == block.height


@pytest.mark.limit_consensus_modes(reason="save time")
@pytest.mark.anyio
async def test_num_unspent(bt: BlockTools, db_version: int) -> None:
    blocks = bt.get_consecutive_blocks(37, [])

    expect_unspent = 0
    test_excercised = False

    async with DBConnection(db_version) as db_wrapper:
        coin_store = await CoinStore.create(db_wrapper)

        for block in blocks:
            if not block.is_transaction_block():
                continue
            assert block.foliage_transaction_block is not None
            reward_coins = block.get_included_reward_coins()
            await coin_store.new_block(block.height, block.foliage_transaction_block.timestamp, reward_coins, [], [])
            expect_unspent += len(reward_coins)
            assert await coin_store.num_unspent() == expect_unspent
            test_excercised = expect_unspent > 0

    assert test_excercised


@pytest.mark.limit_consensus_modes(reason="save time")
@pytest.mark.anyio
async def test_rollback(db_version: int, bt: BlockTools) -> None:
    blocks = bt.get_consecutive_blocks(20)

    async with DBConnection(db_version) as db_wrapper:
        coin_store = await CoinStore.create(db_wrapper)

        selected_coin: Optional[CoinRecord] = None
        all_coins: list[Coin] = []

        for block in blocks:
            if not block.is_transaction_block():
                continue
            assert block.foliage_transaction_block is not None
            reward_coins = block.get_included_reward_coins()
            all_coins += reward_coins
            await coin_store.new_block(block.height, block.foliage_transaction_block.timestamp, reward_coins, [], [])
            records = [await coin_store.get_coin_record(coin.name()) for coin in reward_coins]


Review comment (refactor):
this refactoring, of turning this into an early-continue is already made in a separate PR, isn't it? Are there *other* changes mixed in with this refactoring too? If so, the refactoring should probably be turned into a separate commit so the logic change becomes visible by itself

Code after:

            records = [await coin_store.get_coin_record(coin.name()) for coin in coins]
            for record in records:
                assert record is not None
                assert record.spent
                assert record.spent_block_index == block.height


@pytest.mark.limit_consensus_modes(reason="save time")
@pytest.mark.anyio
async def test_num_unspent(bt: BlockTools, db_version: int) -> None:
    blocks = bt.get_consecutive_blocks(37, [])

    expect_unspent = 0
    test_excercised = False

    async with DBConnection(db_version) as db_wrapper:
        coin_store = await CoinStore.create(db_wrapper)

        for block in blocks:
            if not block.is_transaction_block():
                continue
            assert block.foliage_transaction_block is not None
            reward_coins = block.get_included_reward_coins()
            await coin_store.new_block(block.height, block.foliage_transaction_block.timestamp, reward_coins, [], [])
            expect_unspent += len(reward_coins)
            assert await coin_store.num_unspent() == expect_unspent
            test_excercised = expect_unspent > 0

    assert test_excercised


@pytest.mark.limit_consensus_modes(reason="save time")
@pytest.mark.anyio
async def test_rollback(db_version: int, bt: BlockTools) -> None:
    blocks = bt.get_consecutive_blocks(20)

    async with DBConnection(db_version) as db_wrapper:
        coin_store = await CoinStore.create(db_wrapper)

        selected_coin: Optional[CoinRecord] = None
        all_coins: list[Coin] = []

        for block in blocks:
            if not block.is_transaction_block():
                continue
            assert block.foliage_transaction_block is not None
            reward_coins = block.get_included_reward_coins()
            all_coins += reward_coins
            await coin_store.new_block(block.height, block.foliage_transaction_block.timestamp, reward_coins, [], [])
            records = [await coin_store.get_coin_record(coin.name()) for coin in reward_coins]