LCOV - code coverage report
Current view: top level - src - private_broadcast.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.4 % 126 124
Test Date: 2026-11-27 05:37:43 Functions: 100.0 % 15 15
Branches: 72.6 % 146 106

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://opensource.org/license/mit/.
       4                 :             : 
       5                 :             : #include <private_broadcast.h>
       6                 :             : 
       7                 :             : #include <util/check.h>
       8                 :             : 
       9                 :             : #include <algorithm>
      10                 :             : #include <ranges>
      11                 :             : 
      12                 :             : 
      13                 :        5268 : PrivateBroadcast::AddResult PrivateBroadcast::Add(const CTransactionRef& tx)
      14                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      15                 :             : {
      16                 :        5268 :     LOCK(m_mutex);
      17                 :             :     // Cleanup finished transactions
      18         [ +  - ]:        5268 :     std::erase_if(m_transactions, [this](const auto& entry) {
      19                 :        8025 :         const auto& state{entry.second};
      20   [ +  +  +  +  :        8026 :         return state.resolved && !IsPending(state) &&
                   +  - ]
      21         [ -  + ]:           2 :                std::ranges::all_of(state.send_statuses, [](const auto& status) { return status.disconnected; });
      22                 :             :     });
      23                 :             : 
      24         [ +  + ]:        5268 :     if (const auto it{m_transactions.find(tx)}; it != m_transactions.end()) {
      25   [ -  +  +  + ]:         694 :         if (it->second.send_statuses.size() < m_max_send_attempts) return AddResult::AlreadyPresent;
      26                 :             : 
      27                 :             :         // A transaction that has reached m_max_send_attempts can be explicitly retried by adding it again.
      28                 :          11 :         it->second.time_added = NodeClock::now();
      29                 :          11 :         it->second.send_statuses.clear();
      30                 :          11 :         it->second.planned_sends = INITIAL_CONNECTION_COUNT;
      31                 :          11 :         it->second.resolved = false;
      32                 :          11 :         return AddResult::Added;
      33                 :             :     }
      34                 :             : 
      35         [ +  + ]:        4574 :     if (m_transactions.size() >= m_max_transactions) return AddResult::QueueFull;
      36                 :             : 
      37         [ +  - ]:        4375 :     m_transactions.try_emplace(tx);
      38                 :             :     return AddResult::Added;
      39                 :        5268 : }
      40                 :             : 
      41                 :         964 : std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
      42                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      43                 :             : {
      44                 :         964 :     LOCK(m_mutex);
      45                 :         964 :     const auto handle{m_transactions.extract(tx)};
      46         [ +  + ]:         964 :     if (handle) {
      47         [ +  + ]:         482 :         const auto& state{handle.mapped()};
      48         [ +  + ]:         482 :         const size_t planned{std::min(state.planned_sends, m_max_send_attempts)};
      49         [ -  + ]:         482 :         const size_t actual{state.send_statuses.size()};
      50         [ +  + ]:         482 :         return planned > actual ? planned - actual : 0;
      51                 :             :     }
      52                 :         482 :     return std::nullopt;
      53         [ +  - ]:         964 : }
      54                 :             : 
      55                 :      145557 : bool PrivateBroadcast::MarkResolved(const CTransactionRef& tx)
      56                 :             : {
      57                 :      145557 :     LOCK(m_mutex);
      58                 :      145557 :     const auto it{m_transactions.find(tx)};
      59   [ +  +  +  - ]:      149307 :     if (it == m_transactions.end()) return false;
      60                 :        3750 :     it->second.resolved = true;
      61                 :        3750 :     return true;
      62                 :      145557 : }
      63                 :             : 
      64                 :        3679 : void PrivateBroadcast::NodeDisconnected(NodeId nodeid)
      65                 :             : {
      66                 :        3679 :     LOCK(m_mutex);
      67   [ +  -  +  + ]:        3679 :     if (const auto entry{GetSendStatusByNode(nodeid)}) {
      68                 :        1312 :         entry->send_status.disconnected = true;
      69                 :             :     }
      70                 :        3679 : }
      71                 :             : 
      72                 :          74 : bool PrivateBroadcast::TryGrantRetry(const CTransactionRef& tx)
      73                 :             : {
      74                 :          74 :     LOCK(m_mutex);
      75                 :          74 :     const auto it{m_transactions.find(tx)};
      76   [ +  -  +  + ]:          74 :     if (it == m_transactions.end() ||
      77   [ +  +  +  + ]:         146 :         it->second.resolved ||
      78         [ +  - ]:          72 :         IsPending(it->second) ||
      79         [ +  + ]:          74 :         it->second.planned_sends >= m_max_send_attempts) return false;
      80                 :          13 :     ++it->second.planned_sends;
      81                 :          13 :     return true;
      82                 :          74 : }
      83                 :             : 
      84                 :        4917 : std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
      85                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      86                 :             : {
      87                 :        4917 :     LOCK(m_mutex);
      88                 :             : 
      89   [ +  -  -  + ]:        4917 :     if (GetSendStatusByNode(will_send_to_nodeid).has_value()) { // nodeid reuse, shouldn't send >1 tx to a given node
      90         [ #  # ]:           0 :         Assume(false);
      91                 :           0 :         return std::nullopt;
      92                 :             :     }
      93                 :             : 
      94         [ +  - ]:       10341 :     auto pending_transactions{m_transactions | std::views::filter([this](const auto& entry) { return IsPending(entry.second); })};
      95         [ +  - ]:        4917 :     const auto it{std::ranges::max_element(
      96                 :             :             pending_transactions,
      97                 :        1100 :             [](const auto& a, const auto& b) { return a < b; },
      98                 :        1100 :             [](const auto& el) { return DerivePriority(el.second.send_statuses); })};
      99                 :             : 
     100         [ +  + ]:        4917 :     if (it != pending_transactions.end()) {
     101                 :        1779 :         auto& [tx, state]{*it};
     102         [ +  - ]:        1779 :         state.send_statuses.emplace_back(will_send_to_nodeid, will_send_to_address, NodeClock::now());
     103                 :        1779 :         return tx;
     104                 :             :     }
     105                 :             : 
     106                 :        3138 :     return std::nullopt;
     107                 :        4917 : }
     108                 :             : 
     109                 :         773 : std::optional<CTransactionRef> PrivateBroadcast::GetTxForNode(const NodeId& nodeid)
     110                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     111                 :             : {
     112                 :         773 :     LOCK(m_mutex);
     113         [ +  - ]:         773 :     const auto tx_and_status{GetSendStatusByNode(nodeid)};
     114         [ +  + ]:         773 :     if (tx_and_status.has_value()) {
     115                 :         625 :         return tx_and_status.value().tx;
     116                 :             :     }
     117                 :         148 :     return std::nullopt;
     118                 :         773 : }
     119                 :             : 
     120                 :        1176 : void PrivateBroadcast::NodeConfirmedReception(const NodeId& nodeid)
     121                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     122                 :             : {
     123                 :        1176 :     LOCK(m_mutex);
     124         [ +  - ]:        1176 :     const auto tx_and_status{GetSendStatusByNode(nodeid)};
     125         [ +  + ]:        1176 :     if (tx_and_status.has_value()) {
     126   [ +  +  +  - ]:        1191 :         tx_and_status.value().send_status.confirmed = NodeClock::now();
     127                 :             :     }
     128                 :        1176 : }
     129                 :             : 
     130                 :        5800 : bool PrivateBroadcast::HavePendingTransactions()
     131                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     132                 :             : {
     133                 :        5800 :     LOCK(m_mutex);
     134   [ +  -  +  - ]:        8370 :     return std::ranges::any_of(m_transactions, [this](const auto& entry) { return IsPending(entry.second); });
     135                 :        5800 : }
     136                 :             : 
     137                 :        5011 : std::vector<CTransactionRef> PrivateBroadcast::GetStale() const
     138                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     139                 :             : {
     140                 :        5011 :     LOCK(m_mutex);
     141                 :        5011 :     const auto now{NodeClock::now()};
     142                 :        5011 :     std::vector<CTransactionRef> stale;
     143   [ +  +  +  + ]:        8389 :     for (const auto& [tx, state] : m_transactions) {
     144   [ +  +  +  + ]:        3378 :         if (state.resolved || state.planned_sends >= m_max_send_attempts) continue;
     145         [ +  - ]:        3231 :         const Priority p{DerivePriority(state.send_statuses)};
     146         [ +  + ]:        3231 :         if (p.num_confirmed == 0) {
     147   [ +  +  +  - ]:        3224 :             if (state.time_added < now - INITIAL_STALE_DURATION) stale.push_back(tx);
     148                 :             :         } else {
     149   [ -  +  -  - ]:           7 :             if (p.last_confirmed < now - STALE_DURATION) stale.push_back(tx);
     150                 :             :         }
     151                 :             :     }
     152         [ +  - ]:        5011 :     return stale;
     153                 :        5011 : }
     154                 :             : 
     155                 :          72 : std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
     156                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     157                 :             : {
     158                 :          72 :     LOCK(m_mutex);
     159                 :          72 :     std::vector<TxBroadcastInfo> entries;
     160         [ +  - ]:          72 :     entries.reserve(m_transactions.size());
     161                 :             : 
     162   [ +  +  +  + ]:         160 :     for (const auto& [tx, state] : m_transactions) {
     163         [ +  + ]:          88 :         if (state.resolved) continue;
     164                 :          84 :         std::vector<PeerSendInfo> peers;
     165   [ -  +  +  - ]:          84 :         peers.reserve(state.send_statuses.size());
     166         [ +  + ]:         139 :         for (const auto& status : state.send_statuses) {
     167         [ +  - ]:         110 :             peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
     168                 :             :         }
     169   [ -  +  +  - ]:          84 :         const size_t attempts_remaining{m_max_send_attempts - std::min(state.send_statuses.size(), m_max_send_attempts)};
     170   [ +  -  +  - ]:         168 :         entries.emplace_back(TxBroadcastInfo{.tx = tx, .time_added = state.time_added, .attempts_remaining = attempts_remaining, .peers = std::move(peers)});
     171                 :          84 :     }
     172                 :             : 
     173         [ +  - ]:          72 :     return entries;
     174                 :          72 : }
     175                 :             : 
     176                 :        8275 : bool PrivateBroadcast::IsPending(const TxSendStatus& status) const
     177                 :             : {
     178                 :             :     // Deliberately ignore resolved so all initially scheduled connections can complete.
     179         [ +  + ]:        8275 :     const size_t limit{std::min(status.planned_sends, m_max_send_attempts)};
     180         [ -  + ]:        8275 :     return status.send_statuses.size() < limit;
     181                 :             : }
     182                 :             : 
     183                 :        5431 : PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
     184                 :             : {
     185                 :        5431 :     Priority p;
     186         [ -  + ]:        5431 :     p.num_picked = sent_to.size();
     187         [ +  + ]:        7174 :     for (const auto& send_status : sent_to) {
     188                 :        1743 :         p.last_picked = std::max(p.last_picked, send_status.picked);
     189         [ +  + ]:        1743 :         if (send_status.confirmed.has_value()) {
     190                 :           9 :             ++p.num_confirmed;
     191                 :           9 :             p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
     192                 :             :         }
     193                 :             :     }
     194                 :        5431 :     return p;
     195                 :             : }
     196                 :             : 
     197                 :       10545 : std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
     198                 :             :     EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
     199                 :             : {
     200                 :       10545 :     AssertLockHeld(m_mutex);
     201         [ +  + ]:       18509 :     for (auto& [tx, state] : m_transactions) {
     202         [ +  + ]:       19463 :         for (auto& send_status : state.send_statuses) {
     203         [ +  + ]:       11499 :             if (send_status.nodeid == nodeid) {
     204                 :        1952 :                 return TxAndSendStatusForNode{.tx = tx, .send_status = send_status};
     205                 :             :             }
     206                 :             :         }
     207                 :             :     }
     208                 :        8593 :     return std::nullopt;
     209                 :             : }
        

Generated by: LCOV version 2.5.0-full