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

Code before:
    and will make this function return false
    """
    if clvm_buffer == b"":
        return True

    offset = 0
    tokens_left = 1
    while True:
        b = clvm_buffer[offset]

        # pair
        if b == 0xFF:
            tokens_left += 1
            offset += 1
            continue

        # back references cannot be considered canonical, since they may be
        # encoded in many different ways
        if b == 0xFE:
            return False

        # small atom or NIL
        if b <= 0x80:
            tokens_left -= 1
            offset += 1
        else:
            atom_len, canonical = is_atom_canonical(clvm_buffer, offset)
            if not canonical:
                return False
            tokens_left -= 1
            offset += atom_len

        if tokens_left == 0:
            break

    # if there's garbage at the end, it's not canonical
    return offset == len(clvm_buffer)


def check_removals(
    removals: dict[bytes32, CoinRecord],
    bundle_coin_spends: dict[bytes32, BundleCoinSpend],
    *,
    get_items_by_coin_ids: Callable[[list[bytes32]], list[MempoolItem]],
) -> tuple[Optional[Err], list[MempoolItem]]:
    """
    This function checks for double spends, unknown spends and conflicting transactions in mempool.
    Returns Error (if any), the set of existing MempoolItems with conflicting spends (if any).
    Note that additions are not checked for duplicates, because having duplicate additions requires also
    having duplicate removals.
    """

Review comment (suggestion):
I don't see a check for `offset > len(clvm_buffer)` anywhere, which suggest ill-formed clvm will raise. But maybe you never pass in ill-formed clvm (in which case, you don't need the `clvm_buffer == b''` case either).

Code after:
    and will make this function return false
    """
    assert clvm_buffer != b""

    offset = 0
    tokens_left = 1
    while True:
        b = clvm_buffer[offset]

        # pair
        if b == 0xFF:
            tokens_left += 1
            offset += 1
            continue

        # back references cannot be considered canonical, since they may be
        # encoded in many different ways
        if b == 0xFE:
            return False

        # small atom or NIL
        if b <= 0x80:
            tokens_left -= 1
            offset += 1
        else:
            atom_len, canonical = is_atom_canonical(clvm_buffer, offset)
            if not canonical:
                return False
            tokens_left -= 1
            offset += atom_len

        if tokens_left == 0:
            break

    # if there's garbage at the end, it's not canonical
    return offset == len(clvm_buffer)


def check_removals(
    removals: dict[bytes32, CoinRecord],
    bundle_coin_spends: dict[bytes32, BundleCoinSpend],
    *,
    get_items_by_coin_ids: Callable[[list[bytes32]], list[MempoolItem]],
) -> tuple[Optional[Err], list[MempoolItem]]:
    """
    This function checks for double spends, unknown spends and conflicting transactions in mempool.
    Returns Error (if any), the set of existing MempoolItems with conflicting spends (if any).
    Note that additions are not checked for duplicates, because having duplicate additions requires also
    having duplicate removals.
    """
    conflicts = set()