Repository: bitcoin/bitcoin
File: src/node/blockstorage.cpp (C++)

Code before:
            LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
                pos.ToString(), blk_size, MAX_SIZE);
            return false;
        }

        size_t offset = 0;
        if (part_offset.has_value()) {
            if (*part_offset > blk_size) {
                LogError("Read from block file failed: invalid offset: %u", *part_offset);
                return false;
            }
            offset = *part_offset;
        }

        size_t size = blk_size - offset;
        if (part_size.has_value()) {
            if (*part_size > size || *part_size == 0) {
                LogError("Read from block file failed: invalid size: %u", *part_size);
                return false;
            }
            size = *part_size;
        }


        data.resize(size); // Zeroing of memory is intentional here
        filein.seek(offset, SEEK_CUR);
        filein.read(data);
    } catch (const std::exception& e) {
        LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
        return false;
    }

    return true;
}

FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
{
    const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
    FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
    if (pos.IsNull()) {
        LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
        return FlatFilePos();
    }
    AutoFile file{OpenBlockFile(pos, /*fReadOnly=*/false)};
    if (file.IsNull()) {
        LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
        m_opts.notifications.fatalError(_("Failed to write block."));
        return FlatFilePos();
    }
    {
        BufferedWriter fileout{file};

Review comment (performance):
nit: Could avoid syscall in common case
```suggestion
        if (offset > 0) filein.seek(offset, SEEK_CUR);
```
Other PR avoiding similar syscall which was leading to slowdowns on Windows: #30884
Alternatively one could perform the noop-check inside of `AutoFile` itself.

Code after:
                pos.ToString(), blk_size, MAX_SIZE);
            return util::Unexpected{ReadRawError::IO};
        }

        if (block_part) {
            const auto [offset, size]{*block_part};
            if (size == 0 || offset >= blk_size || size > blk_size - offset) {
                return util::Unexpected{ReadRawError::BadPartRange}; // Avoid logging - offset/size come from untrusted REST input
            }
            filein.seek(offset, SEEK_CUR);
            blk_size = size;
        }

        std::vector<std::byte> data(blk_size); // Zeroing of memory is intentional here
        filein.read(data);
        return data;
    } catch (const std::exception& e) {
        LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
        return util::Unexpected{ReadRawError::IO};
    }
}

FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
{
    const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
    FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
    if (pos.IsNull()) {
        LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
        return FlatFilePos();
    }
    AutoFile file{OpenBlockFile(pos, /*fReadOnly=*/false)};
    if (file.IsNull()) {
        LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
        m_opts.notifications.fatalError(_("Failed to write block."));
        return FlatFilePos();
    }
    {
        BufferedWriter fileout{file};

        // Write index header
        fileout << GetParams().MessageStart() << block_size;
        pos.nPos += STORAGE_HEADER_BYTES;
        // Write block
        fileout << TX_WITH_WITNESS(block);
    }

    if (file.fclose() != 0) {
        LogError("Failed to close block file %s: %s", pos.ToString(), SysErrorString(errno));
        m_opts.notifications.fatalError(_("Failed to close file when writing block."));
        return FlatFilePos();
    }