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

Code before:
        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])
                coinbase = False if row[2] == 0 else True
                record = CoinRecord(coin, uint32(0), spent_index, coinbase, 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)
                coinbase = False if row[2] == 0 else True
                record = CoinRecord(coin, row[0], uint32(0), coinbase, 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(
                """
                UPDATE coin_record INDEXED BY coin_spent_index
                SET spent_index = CASE
                    WHEN
                        coinbase = 0 AND
                        EXISTS (
                            SELECT 1
                            FROM coin_record AS parent INDEXED BY sqlite_autoindex_coin_record_1
                            WHERE
                                parent.coin_name = coin_record.coin_parent AND
                                parent.puzzle_hash = coin_record.puzzle_hash AND
                                parent.amount = coin_record.amount AND
                                parent.spent_index > 0
                        )
                    THEN -1

Review comment (suggestion):
I would suggest this or the alternative below.
```suggestion
                coinbase = row[2] != 0
```

Code after:
    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] != 0, 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] != 0, 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(
                """
                UPDATE coin_record INDEXED BY coin_spent_index
                SET spent_index = CASE
                    WHEN
                        coinbase = 0 AND
                        EXISTS (
                            SELECT 1
                            FROM coin_record AS parent INDEXED BY sqlite_autoindex_coin_record_1
                            WHERE