Repository: Chia-Network/chia-blockchain
File: chia/full_node/coin_store.py (Python)

Code before:
            last_coin_state = coin_states[-1]
            height = uint32(max(last_coin_state.created_height or 0, last_coin_state.spent_height or 0))
            if height != next_height:
                break

            coin_states.pop()

        return coin_states, next_height

    async def rollback_to_block(self, block_index: int) -> dict[bytes32, CoinRecord]:
        """
        Note that block_index can be negative, in which case everything is rolled back
        Returns a map of coin ID to coin record for modified items.
        """

        coin_changes: dict[bytes32, CoinRecord] = {}
        # Add coins that are confirmed in the reverted blocks to the list of updated coins.
        async with self.db_wrapper.writer_maybe_transaction() as conn:
            rows = await conn.execute_fetchall(
                "SELECT confirmed_index, spent_index, coinbase, puzzle_hash, "
                "coin_parent, amount, timestamp, coin_name FROM coin_record WHERE confirmed_index>?",
                (block_index,),
            )
            for row in rows:
                coin = self.row_to_coin(row)
                spent_index = uint32(0) if row[1] <= 0 else row[1]
                record = CoinRecord(coin, uint32(0), spent_index, row[2], uint64(0))
                coin_name = bytes32(row[7])
                coin_changes[coin_name] = record

            # Delete reverted blocks from storage
            await conn.execute("DELETE FROM coin_record WHERE confirmed_index>?", (block_index,))

            # Add coins that are confirmed in the reverted blocks to the list of changed coins.
            rows = await conn.execute_fetchall(
                "SELECT confirmed_index, spent_index, coinbase, puzzle_hash, "
                "coin_parent, amount, timestamp, coin_name FROM coin_record WHERE spent_index>?",
                (block_index,),
            )
            for row in rows:
                coin = self.row_to_coin(row)
                record = CoinRecord(coin, row[0], uint32(0), row[2], row[6])
                coin_name = bytes32(row[7])
                if coin_name not in coin_changes:
                    coin_changes[coin_name] = record

            # If the coin to update is not a reward coin and its parent is
            # spent and has the same puzzle hash and amount, we set its
            # spent_index to -1 as a potential fast forward singleton unspent
            # otherwise we set it to 0 as a normal unspent.
            await conn.execute(

Review comment (suggestion):
```suggestion
                spent_index = uint32(0) if row[1] <= 0 else uint32(row[1])
```

Code after:
            last_coin_state = coin_states[-1]
            height = uint32(max(last_coin_state.created_height or 0, last_coin_state.spent_height or 0))
            if height != next_height:
                break

            coin_states.pop()

        return coin_states, next_height

    async def rollback_to_block(self, block_index: int) -> dict[bytes32, CoinRecord]:
        """
        Note that block_index can be negative, in which case everything is rolled back
        Returns a map of coin ID to coin record for modified items.
        """

        coin_changes: dict[bytes32, CoinRecord] = {}
        # Add coins that are confirmed in the reverted blocks to the list of updated coins.
        async with self.db_wrapper.writer_maybe_transaction() as conn:
            rows = await conn.execute_fetchall(
                "SELECT confirmed_index, spent_index, coinbase, puzzle_hash, "
                "coin_parent, amount, timestamp, coin_name FROM coin_record WHERE confirmed_index>?",
                (block_index,),
            )
            for row in rows:
                coin = self.row_to_coin(row)
                spent_index = uint32(0) if row[1] <= 0 else uint32(row[1])
                record = CoinRecord(coin, uint32(0), spent_index, row[2], uint64(0))
                coin_name = bytes32(row[7])
                coin_changes[coin_name] = record

            # Delete reverted blocks from storage
            await conn.execute("DELETE FROM coin_record WHERE confirmed_index>?", (block_index,))

            # Add coins that are confirmed in the reverted blocks to the list of changed coins.
            rows = await conn.execute_fetchall(
                "SELECT confirmed_index, spent_index, coinbase, puzzle_hash, "
                "coin_parent, amount, timestamp, coin_name FROM coin_record WHERE spent_index>?",
                (block_index,),
            )
            for row in rows:
                coin = self.row_to_coin(row)
                record = CoinRecord(coin, row[0], uint32(0), row[2], row[6])
                coin_name = bytes32(row[7])
                if coin_name not in coin_changes:
                    coin_changes[coin_name] = record

            # If the coin to update is not a reward coin and its parent is
            # spent and has the same puzzle hash and amount, we set its
            # spent_index to -1 as a potential fast forward singleton unspent
            # otherwise we set it to 0 as a normal unspent.
            await conn.execute(