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

Code before:
    return true;
}

FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
{
    unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
    FlatFilePos blockPos;
    const auto position_known {dbp != nullptr};
    if (position_known) {
        blockPos = *dbp;
    } else {
        // when known, blockPos.nPos points at the offset of the block data in the blk file. that already accounts for
        // the serialization header present in the file (the 4 magic message start bytes + the 4 length bytes = 8 bytes = BLOCK_SERIALIZATION_HEADER_SIZE).
        // we add BLOCK_SERIALIZATION_HEADER_SIZE only for new blocks since they will have the serialization header added when written to disk.
        nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
    }
    if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime(), position_known)) {
        LogError("%s: FindBlockPos failed\n", __func__);
        return FlatFilePos();
    }
    if (!position_known) {
        if (!WriteBlockToDisk(block, blockPos)) {
            m_opts.notifications.fatalError(_("Failed to write block."));
            return FlatFilePos();
        }
    }
    return blockPos;
}

class ImportingNow
{
    std::atomic<bool>& m_importing;

public:
    ImportingNow(std::atomic<bool>& importing) : m_importing{importing}
    {
        assert(m_importing == false);
        m_importing = true;
    }
    ~ImportingNow()
    {
        assert(m_importing == true);
        m_importing = false;
    }
};

void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
{
    ImportingNow imp{chainman.m_blockman.m_importing};

    // -reindex

Review comment (suggestion):
In commit "blockstorage: split up FindBlockPos function" (a2ae0a33c7c30678721d7e7d37d8e6170b013383)

I think this comment is a little confusing, because it isn't obvious that `position_known` can only be true during reindexing. Could potentially clarify this, though not necessary since this code will be deleted in the next commit.

Code after:
        if (blk_size > MAX_SIZE) {
            LogError("%s: Block data is larger than maximum deserialization size for %s: %s versus %s\n", __func__, pos.ToString(),
                         blk_size, MAX_SIZE);
            return false;
        }

        block.resize(blk_size); // Zeroing of memory is intentional here
        filein.read(MakeWritableByteSpan(block));
    } catch (const std::exception& e) {
        LogError("%s: Read from block file failed: %s for %s\n", __func__, e.what(), pos.ToString());
        return false;
    }

    return true;
}

FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
{
    unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
    // Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total,
    // defined as BLOCK_SERIALIZATION_HEADER_SIZE)
    nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
    FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
    if (blockPos.IsNull()) {
        LogError("%s: FindNextBlockPos failed\n", __func__);
        return FlatFilePos();
    }
    if (!WriteBlockToDisk(block, blockPos)) {
        m_opts.notifications.fatalError(_("Failed to write block."));
        return FlatFilePos();
    }
    return blockPos;
}

class ImportingNow
{
    std::atomic<bool>& m_importing;

public:
    ImportingNow(std::atomic<bool>& importing) : m_importing{importing}
    {
        assert(m_importing == false);
        m_importing = true;
    }
    ~ImportingNow()
    {
        assert(m_importing == true);
        m_importing = false;
    }
};