LCOV - code coverage report
Current view: top level - src - private_broadcast.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 74 74
Test Date: 2026-01-13 05:19:40 Functions: 100.0 % 10 10
Branches: 70.7 % 58 41

             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                 :             : #include <util/check.h>
       7                 :             : 
       8                 :             : #include <algorithm>
       9                 :             : 
      10                 :             : /// If a transaction is not received back from the network for this duration
      11                 :             : /// after it is broadcast, then we consider it stale / for rebroadcasting.
      12                 :             : static constexpr auto STALE_DURATION{1min};
      13                 :             : 
      14                 :           9 : bool PrivateBroadcast::Add(const CTransactionRef& tx)
      15                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      16                 :             : {
      17                 :           9 :     LOCK(m_mutex);
      18         [ +  - ]:           9 :     const bool inserted{m_transactions.try_emplace(tx).second};
      19         [ +  - ]:           9 :     return inserted;
      20                 :           9 : }
      21                 :             : 
      22                 :       16129 : std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
      23                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      24                 :             : {
      25                 :       16129 :     LOCK(m_mutex);
      26                 :       16129 :     const auto handle{m_transactions.extract(tx)};
      27         [ +  + ]:       16129 :     if (handle) {
      28         [ +  - ]:           4 :         const auto p{DerivePriority(handle.mapped())};
      29                 :           4 :         return p.num_confirmed;
      30                 :             :     }
      31                 :       16125 :     return std::nullopt;
      32         [ +  - ]:       32258 : }
      33                 :             : 
      34                 :          14 : std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid)
      35                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      36                 :             : {
      37                 :          14 :     LOCK(m_mutex);
      38                 :             : 
      39                 :          14 :     const auto it{std::ranges::max_element(
      40         [ +  - ]:          14 :             m_transactions,
      41                 :           5 :             [](const auto& a, const auto& b) { return a < b; },
      42                 :           5 :             [](const auto& el) { return DerivePriority(el.second); })};
      43                 :             : 
      44         [ +  + ]:          14 :     if (it != m_transactions.end()) {
      45                 :          12 :         auto& [tx, sent_to]{*it};
      46         [ +  - ]:          12 :         sent_to.emplace_back(will_send_to_nodeid, NodeClock::now());
      47                 :          12 :         return tx;
      48                 :             :     }
      49                 :             : 
      50                 :           2 :     return std::nullopt;
      51                 :          14 : }
      52                 :             : 
      53                 :          13 : std::optional<CTransactionRef> PrivateBroadcast::GetTxForNode(const NodeId& nodeid)
      54                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      55                 :             : {
      56                 :          13 :     LOCK(m_mutex);
      57         [ +  - ]:          13 :     const auto tx_and_status{GetSendStatusByNode(nodeid)};
      58         [ +  + ]:          13 :     if (tx_and_status.has_value()) {
      59                 :          12 :         return tx_and_status.value().tx;
      60                 :             :     }
      61                 :           1 :     return std::nullopt;
      62                 :          13 : }
      63                 :             : 
      64                 :          12 : void PrivateBroadcast::NodeConfirmedReception(const NodeId& nodeid)
      65                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      66                 :             : {
      67                 :          12 :     LOCK(m_mutex);
      68         [ +  - ]:          12 :     const auto tx_and_status{GetSendStatusByNode(nodeid)};
      69         [ +  + ]:          12 :     if (tx_and_status.has_value()) {
      70   [ -  +  +  - ]:          23 :         tx_and_status.value().send_status.confirmed = NodeClock::now();
      71                 :             :     }
      72                 :          12 : }
      73                 :             : 
      74                 :          16 : bool PrivateBroadcast::DidNodeConfirmReception(const NodeId& nodeid)
      75                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      76                 :             : {
      77                 :          16 :     LOCK(m_mutex);
      78         [ +  - ]:          16 :     const auto tx_and_status{GetSendStatusByNode(nodeid)};
      79         [ +  + ]:          16 :     if (tx_and_status.has_value()) {
      80                 :          14 :         return tx_and_status.value().send_status.confirmed.has_value();
      81                 :             :     }
      82                 :             :     return false;
      83                 :          16 : }
      84                 :             : 
      85                 :           2 : bool PrivateBroadcast::HavePendingTransactions()
      86                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      87                 :             : {
      88                 :           2 :     LOCK(m_mutex);
      89         [ +  - ]:           2 :     return !m_transactions.empty();
      90                 :           2 : }
      91                 :             : 
      92                 :           9 : std::vector<CTransactionRef> PrivateBroadcast::GetStale() const
      93                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      94                 :             : {
      95                 :           9 :     LOCK(m_mutex);
      96                 :           9 :     const auto stale_time{NodeClock::now() - STALE_DURATION};
      97                 :           9 :     std::vector<CTransactionRef> stale;
      98   [ +  +  +  - ]:          19 :     for (const auto& [tx, send_status] : m_transactions) {
      99         [ +  - ]:          10 :         const Priority p{DerivePriority(send_status)};
     100         [ +  + ]:          10 :         if (p.last_confirmed < stale_time) {
     101         [ +  - ]:           8 :             stale.push_back(tx);
     102                 :             :         }
     103                 :             :     }
     104         [ +  - ]:           9 :     return stale;
     105                 :           9 : }
     106                 :             : 
     107                 :          24 : PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
     108                 :             : {
     109                 :          24 :     Priority p;
     110         [ -  + ]:          24 :     p.num_picked = sent_to.size();
     111         [ +  + ]:          59 :     for (const auto& send_status : sent_to) {
     112                 :          35 :         p.last_picked = std::max(p.last_picked, send_status.picked);
     113         [ +  + ]:          35 :         if (send_status.confirmed.has_value()) {
     114                 :          28 :             ++p.num_confirmed;
     115                 :          28 :             p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
     116                 :             :         }
     117                 :             :     }
     118                 :          24 :     return p;
     119                 :             : }
     120                 :             : 
     121                 :          41 : std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
     122                 :             :     EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
     123                 :             : {
     124                 :          41 :     AssertLockHeld(m_mutex);
     125         [ +  + ]:          53 :     for (auto& [tx, sent_to] : m_transactions) {
     126         [ +  + ]:          98 :         for (auto& send_status : sent_to) {
     127         [ +  + ]:          86 :             if (send_status.nodeid == nodeid) {
     128                 :          37 :                 return TxAndSendStatusForNode{.tx = tx, .send_status = send_status};
     129                 :             :             }
     130                 :             :         }
     131                 :             :     }
     132                 :           4 :     return std::nullopt;
     133                 :             : }
        

Generated by: LCOV version 2.0-1