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

Code before:
    const auto stale_time{NodeClock::now() - STALE_DURATION};
    std::vector<CTransactionRef> stale;
    for (const auto& [tx, send_status] : m_transactions) {
        const Priority p{DerivePriority(send_status)};
        if (p.last_confirmed < stale_time) {
            stale.push_back(tx);
        }
    }
    return stale;
}

std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
    LOCK(m_mutex);
    std::vector<TxBroadcastInfo> entries;
    entries.reserve(m_transactions.size());

    for (const auto& [tx, sent_to] : m_transactions) {
        TxBroadcastInfo info;
        info.tx = tx;
        info.peers.reserve(sent_to.size());
        for (const auto& status : sent_to) {
            info.peers.push_back(PeerSendInfo{
                .address = status.address,
                .sent = status.picked,
                .received = status.confirmed,
            });
        }
        entries.push_back(std::move(info));
    }

    return entries;
}

PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
{
    Priority p;
    p.num_picked = sent_to.size();
    for (const auto& send_status : sent_to) {
        p.last_picked = std::max(p.last_picked, send_status.picked);
        if (send_status.confirmed.has_value()) {
            ++p.num_confirmed;
            p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
        }
    }
    return p;
}

std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
    EXCLUSIVE_LOCKS_REQUIRED(m_mutex)

Review comment (suggestion):
nit: alternatively we could just construct the peers before attempting to add them to the info and filling it later:
```suggestion
    for (const auto& [tx, sent_to] : m_transactions) {
        std::vector<PeerSendInfo> peers;
        peers.reserve(sent_to.size());
        for (const auto& status : sent_to) {
            peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
        }
        entries.emplace_back(TxBroadcastInfo{.tx = tx, .peers = std::move(peers)});
    }
```
or maybe even
```suggestion
    for (const auto& sent_to : m_transactions | std::views::values) {
        std::vector<PeerSendInfo> peers;
        peers.reserve(sent_to.size());
        std::ranges::transform(sent_to, std::back_inserter(peers), [](const auto& status) {
            return PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed};
        });
    }
```

Code after:
    const auto stale_time{NodeClock::now() - STALE_DURATION};
    std::vector<CTransactionRef> stale;
    for (const auto& [tx, send_status] : m_transactions) {
        const Priority p{DerivePriority(send_status)};
        if (p.last_confirmed < stale_time) {
            stale.push_back(tx);
        }
    }
    return stale;
}

std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
    LOCK(m_mutex);
    std::vector<TxBroadcastInfo> entries;
    entries.reserve(m_transactions.size());

    for (const auto& [tx, sent_to] : m_transactions) {
        std::vector<PeerSendInfo> peers;
        peers.reserve(sent_to.size());
        for (const auto& status : sent_to) {
            peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
        }
        entries.emplace_back(TxBroadcastInfo{.tx = tx, .peers = std::move(peers)});
    }

    return entries;
}

PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
{
    Priority p;
    p.num_picked = sent_to.size();
    for (const auto& send_status : sent_to) {
        p.last_picked = std::max(p.last_picked, send_status.picked);
        if (send_status.confirmed.has_value()) {
            ++p.num_confirmed;
            p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
        }
    }
    return p;
}

std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
    EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
{
    AssertLockHeld(m_mutex);
    for (auto& [tx, sent_to] : m_transactions) {
        for (auto& send_status : sent_to) {
            if (send_status.nodeid == nodeid) {