Repository: Chia-Network/chia-blockchain
File: chia/consensus/block_body_validation.py (Python)

Code before:
        self.block_hashes.append(header_hash)
        self.peak_height = int(block.height)
        self.peak_hash = header_hash

    def include_reward_coins(self, block: FullBlock) -> None:
        """Updates `self` with `block`'s reward coins."""
        for coin in block.get_included_reward_coins():
            assert block.foliage_transaction_block is not None
            timestamp = block.foliage_transaction_block.timestamp
            coin_id = coin.name()
            assert coin_id not in self.additions_since_fork
            self.additions_since_fork[coin_id] = ForkAdd(coin, block.height, timestamp, None, True, False)

    def include_spends(self, conds: Optional[SpendBundleConditions], block: FullBlock, header_hash: bytes32) -> None:
        self.update_fork_peak(block, header_hash)
        if conds is not None:
            assert block.foliage_transaction_block is not None
            timestamp = block.foliage_transaction_block.timestamp
            for spend in conds.spends:
                spend_coin_id = bytes32(spend.coin_id)
                spend_ph = bytes32(spend.puzzle_hash)
                self.removals_since_fork[spend_coin_id] = ForkRem(spend_ph, block.height)
                # We're only relying on this flag here to tells us that the
                # coin has the same puzzle hash and amount as its parent and
                # the amount is odd (among other things).
                spend_same_as_parent = bool(spend.flags & ELIGIBLE_FOR_FF)
                for puzzle_hash, amount, hint in spend.create_coin:
                    child_ph = bytes32(puzzle_hash)
                    child_amount = uint64(amount)
                    child_coin = Coin(spend_coin_id, child_ph, child_amount)
                    child_coin_id = child_coin.name()
                    same_as_parent = (
                        spend_same_as_parent
                        and child_coin.puzzle_hash == spend.puzzle_hash
                        and amount == spend.coin_amount
                    )
                    self.additions_since_fork[child_coin_id] = ForkAdd(
                        child_coin, block.height, timestamp, hint, False, same_as_parent
                    )
        self.include_reward_coins(block)

    def include_block(
        self,
        additions: list[tuple[Coin, Optional[bytes]]],
        removals: list[Coin],
        block: FullBlock,
        header_hash: bytes32,
    ) -> None:
        self.update_fork_peak(block, header_hash)
        if block.foliage_transaction_block is not None:
            timestamp = block.foliage_transaction_block.timestamp

Review comment (performance):
This flag means more things than just same puzzle hash, same amount and odd amount.
I don't understand why this should be considered at all. This is setting the `spent_index` differently than it's set during a reorg, isn't it?
Imagine coming here and `ELIGIBLE_FOR_FF` is not set, but puzzle hash and amount do match. You would still set `same_as_parent` to `False`. If this coin is later spent, and then reorged back into being unspent, you will set this flag *only* based on whether the puzzle hasn and amount matches. That's my understanding at least. It would make this field set inconsistently. I think that adds unnecessary complexity.

Am I misunderstanding this?

Code after:
        self.block_hashes.append(header_hash)
        self.peak_height = int(block.height)
        self.peak_hash = header_hash

    def include_reward_coins(self, block: FullBlock) -> None:
        """Updates `self` with `block`'s reward coins."""
        for coin in block.get_included_reward_coins():
            assert block.foliage_transaction_block is not None
            timestamp = block.foliage_transaction_block.timestamp
            coin_id = coin.name()
            assert coin_id not in self.additions_since_fork
            self.additions_since_fork[coin_id] = ForkAdd(
                coin, block.height, timestamp, hint=None, is_coinbase=True, same_as_parent=False
            )

    def include_spends(self, conds: Optional[SpendBundleConditions], block: FullBlock, header_hash: bytes32) -> None:
        self.update_fork_peak(block, header_hash)
        if conds is not None:
            assert block.foliage_transaction_block is not None
            timestamp = block.foliage_transaction_block.timestamp
            for spend in conds.spends:
                spend_coin_id = bytes32(spend.coin_id)
                self.removals_since_fork[spend_coin_id] = ForkRem(bytes32(spend.puzzle_hash), block.height)
                for puzzle_hash, amount, hint in spend.create_coin:
                    coin = Coin(spend_coin_id, bytes32(puzzle_hash), uint64(amount))
                    same_as_parent = coin.puzzle_hash == spend.puzzle_hash and amount == spend.coin_amount
                    self.additions_since_fork[coin.name()] = ForkAdd(
                        coin, block.height, timestamp, hint=hint, is_coinbase=False, same_as_parent=same_as_parent
                    )
        self.include_reward_coins(block)

    def include_block(
        self,
        additions: list[tuple[Coin, Optional[bytes]]],
        removals: list[Coin],
        block: FullBlock,
        header_hash: bytes32,
    ) -> None:
        self.update_fork_peak(block, header_hash)
        if block.foliage_transaction_block is not None:
            timestamp = block.foliage_transaction_block.timestamp
            spent_coins: dict[bytes32, Coin] = {}
            for spend in removals:
                spend_id = bytes32(spend.name())
                spent_coins[spend_id] = spend
                self.removals_since_fork[spend_id] = ForkRem(bytes32(spend.puzzle_hash), block.height)
            for coin, hint in additions:
                parent = spent_coins.get(coin.parent_coin_info)
                assert parent is not None
                same_as_parent = coin.puzzle_hash == parent.puzzle_hash and coin.amount == parent.amount
                self.additions_since_fork[coin.name()] = ForkAdd(