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

Code before:
                pos.ToString(), blk_size, MAX_SIZE);
            return false;
        }

        block.resize(blk_size); // Zeroing of memory is intentional here
        filein.read(block);
    } 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};

        // 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();
    }

    return pos;
}

static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
{
    // Bytes are serialized without length indicator, so this is also the exact
    // size of the XOR-key file.

Review comment (suggestion):
`FindNextBlockPos` has `unsigned int nAddSize` arg still:
```C++
FlatFilePos BlockManager::FindNextBlockPos(uint32_t nAddSize, unsigned int nHeight, uint64_t nTime)
```

Code after:
                pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart()));
            return false;
        }

        if (blk_size > MAX_SIZE) {
            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;
        }

        block.resize(blk_size); // Zeroing of memory is intentional here
        filein.read(block);
    } 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};

        // 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();
    }

    return pos;