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

Code before:
        tip = m_chainman.ActiveChain().Tip();
        // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
        if (!pfrom.HasPermission(NetPermissionFlags::NoBan) && (
                (((peer.m_our_services & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((peer.m_our_services & NODE_NETWORK) != NODE_NETWORK) && (tip->nHeight - pindex->nHeight > (int)NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */) )
           )) {
            LogDebug(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold, %s\n", pfrom.DisconnectMsg(fLogIPs));
            //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
            pfrom.fDisconnect = true;
            return;
        }
        // Pruned nodes may have deleted the block, so check whether
        // it's available before trying to send.
        if (!(pindex->nStatus & BLOCK_HAVE_DATA)) {
            return;
        }
        can_direct_fetch = CanDirectFetch();
        block_pos = pindex->GetBlockPos();
    }

    std::shared_ptr<const CBlock> pblock;
    if (a_recent_block && a_recent_block->GetHash() == inv.hash) {
        pblock = a_recent_block;
    } else if (inv.IsMsgWitnessBlk()) {
        // Fast-path: in this case it is possible to serve the block directly from disk,
        // as the network format matches the format on disk
        std::vector<std::byte> block_data;
        if (!m_chainman.m_blockman.ReadRawBlock(block_data, block_pos)) {
            if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
                LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
            } else {
                LogError("Cannot load block from disk, %s\n", pfrom.DisconnectMsg(fLogIPs));
            }
            pfrom.fDisconnect = true;
            return;
        }
        MakeAndPushMessage(pfrom, NetMsgType::BLOCK, std::span{block_data});
        // Don't set pblock as we've sent the block
    } else {
        // Send block from disk
        std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
        if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos, inv.hash)) {
            if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
                LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
            } else {
                LogError("Cannot load block from disk, %s\n", pfrom.DisconnectMsg(fLogIPs));
            }
            pfrom.fDisconnect = true;
            return;
        }
        pblock = pblockRead;
    }

Review comment (suggestion):
nit
```suggestion
        if (const auto block_data{m_chainman.m_blockman.ReadRawBlock(block_pos)}) {
```

Code after:
        tip = m_chainman.ActiveChain().Tip();
        // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
        if (!pfrom.HasPermission(NetPermissionFlags::NoBan) && (
                (((peer.m_our_services & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((peer.m_our_services & NODE_NETWORK) != NODE_NETWORK) && (tip->nHeight - pindex->nHeight > (int)NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */) )
           )) {
            LogDebug(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold, %s\n", pfrom.DisconnectMsg(fLogIPs));
            //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
            pfrom.fDisconnect = true;
            return;
        }
        // Pruned nodes may have deleted the block, so check whether
        // it's available before trying to send.
        if (!(pindex->nStatus & BLOCK_HAVE_DATA)) {
            return;
        }
        can_direct_fetch = CanDirectFetch();
        block_pos = pindex->GetBlockPos();
    }

    std::shared_ptr<const CBlock> pblock;
    if (a_recent_block && a_recent_block->GetHash() == inv.hash) {
        pblock = a_recent_block;
    } else if (inv.IsMsgWitnessBlk()) {
        // Fast-path: in this case it is possible to serve the block directly from disk,
        // as the network format matches the format on disk
        if (const auto block_data{m_chainman.m_blockman.ReadRawBlock(block_pos)}) {
            MakeAndPushMessage(pfrom, NetMsgType::BLOCK, std::span{*block_data});
        } else {
            if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
                LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
            } else {
                LogError("Cannot load block from disk, %s\n", pfrom.DisconnectMsg(fLogIPs));
            }
            pfrom.fDisconnect = true;
            return;
        }
        // Don't set pblock as we've sent the block
    } else {
        // Send block from disk
        std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
        if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos, inv.hash)) {
            if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
                LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
            } else {
                LogError("Cannot load block from disk, %s\n", pfrom.DisconnectMsg(fLogIPs));
            }
            pfrom.fDisconnect = true;
            return;
        }
        pblock = pblockRead;
    }