Repository: bitcoin/bitcoin
File: src/index/blockfilterindex.cpp (C++)

Code before:
    }
    if (!file.Commit()) {
        LogError("Failed to commit filter file %d", pos.nFile);
        (void)file.fclose();
        return false;
    }
    if (file.fclose() != 0) {
        LogError("Failed to close filter file %d after commit: %s", pos.nFile, SysErrorString(errno));
        return false;
    }

    batch.Write(DB_FILTER_POS, pos);
    return true;
}

bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const
{
    AutoFile filein{m_filter_fileseq->Open(pos, true)};
    if (filein.IsNull()) {
        return false;
    }

    // Check that the hash of the encoded_filter matches the one stored in the db.
    uint256 block_hash;
    std::vector<uint8_t> encoded_filter;
    try {
        filein >> block_hash >> encoded_filter;
        if (Hash(encoded_filter) != hash) {
            LogError("Checksum mismatch in filter decode.");
            return false;
        }
        filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
    }
    catch (const std::exception& e) {
        LogError("Failed to deserialize block filter from disk: %s", e.what());
        return false;
    }

    return true;
}

size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter)
{
    assert(filter.GetFilterType() == GetFilterType());

    size_t data_size =
        GetSerializeSize(filter.GetBlockHash()) +
        GetSerializeSize(filter.GetEncodedFilter());

    // If writing the filter would overflow the file, flush and move to the next one.
    if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {

Review comment (suggestion):
This is used later
https://github.com/bitcoin/bitcoin/blob/fab75c2d0332fdb25030abb441363478cd486dc5/src/index/blockfilterindex.cpp#L236
for a method that still expects `size_t`
https://github.com/bitcoin/bitcoin/blob/c10e382d2a3b76b70ebb8a4eb5cd99fc9f14d702/src/flatfile.cpp#L57

Code after:
    }

    // Check that the hash of the encoded_filter matches the one stored in the db.
    uint256 block_hash;
    std::vector<uint8_t> encoded_filter;
    try {
        filein >> block_hash >> encoded_filter;
        if (Hash(encoded_filter) != hash) {
            LogError("Checksum mismatch in filter decode.");
            return false;
        }
        filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
    }
    catch (const std::exception& e) {
        LogError("Failed to deserialize block filter from disk: %s", e.what());
        return false;
    }

    return true;
}

size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter)
{
    assert(filter.GetFilterType() == GetFilterType());

    uint64_t data_size{
        GetSerializeSize(filter.GetBlockHash()) +
        GetSerializeSize(filter.GetEncodedFilter())};

    // If writing the filter would overflow the file, flush and move to the next one.
    if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
        AutoFile last_file{m_filter_fileseq->Open(pos)};
        if (last_file.IsNull()) {
            LogError("Failed to open filter file %d", pos.nFile);
            return 0;
        }
        if (!last_file.Truncate(pos.nPos)) {
            LogError("Failed to truncate filter file %d", pos.nFile);
            return 0;
        }
        if (!last_file.Commit()) {
            LogError("Failed to commit filter file %d", pos.nFile);
            (void)last_file.fclose();
            return 0;
        }
        if (last_file.fclose() != 0) {
            LogError("Failed to close filter file %d after commit: %s", pos.nFile, SysErrorString(errno));
            return 0;
        }

        pos.nFile++;