LCOV - code coverage report
Current view: top level - src - private_broadcast.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 87 87
Test Date: 2026-02-22 05:32:45 Functions: 100.0 % 11 11
Branches: 67.9 % 78 53

             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                 :          10 : bool PrivateBroadcast::Add(const CTransactionRef& tx)
      15                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      16                 :             : {
      17                 :          10 :     LOCK(m_mutex);
      18         [ +  - ]:          10 :     const bool inserted{m_transactions.try_emplace(tx).second};
      19         [ +  - ]:          10 :     return inserted;
      20                 :          10 : }
      21                 :             : 
      22                 :       16552 : std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
      23                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      24                 :             : {
      25                 :       16552 :     LOCK(m_mutex);
      26                 :       16552 :     const auto handle{m_transactions.extract(tx)};
      27         [ +  + ]:       16552 :     if (handle) {
      28         [ +  - ]:           5 :         const auto p{DerivePriority(handle.mapped())};
      29                 :           5 :         return p.num_confirmed;
      30                 :             :     }
      31                 :       16547 :     return std::nullopt;
      32         [ +  - ]:       33104 : }
      33                 :             : 
      34                 :          14 : std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
      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, will_send_to_address, 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                 :          14 : std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
     108                 :             :     EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     109                 :             : {
     110                 :          14 :     LOCK(m_mutex);
     111                 :          14 :     std::vector<TxBroadcastInfo> entries;
     112         [ +  - ]:          14 :     entries.reserve(m_transactions.size());
     113                 :             : 
     114   [ +  +  -  + ]:          27 :     for (const auto& [tx, sent_to] : m_transactions) {
     115                 :          13 :         std::vector<PeerSendInfo> peers;
     116   [ +  -  -  + ]:          13 :         peers.reserve(sent_to.size());
     117         [ +  + ]:          33 :         for (const auto& status : sent_to) {
     118         [ +  - ]:          40 :             peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
     119                 :             :         }
     120   [ +  -  +  - ]:          26 :         entries.emplace_back(TxBroadcastInfo{.tx = tx, .peers = std::move(peers)});
     121                 :          13 :     }
     122                 :             : 
     123         [ +  - ]:          14 :     return entries;
     124                 :          14 : }
     125                 :             : 
     126                 :          25 : PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
     127                 :             : {
     128                 :          25 :     Priority p;
     129         [ -  + ]:          25 :     p.num_picked = sent_to.size();
     130         [ +  + ]:          60 :     for (const auto& send_status : sent_to) {
     131                 :          35 :         p.last_picked = std::max(p.last_picked, send_status.picked);
     132         [ +  + ]:          35 :         if (send_status.confirmed.has_value()) {
     133                 :          28 :             ++p.num_confirmed;
     134                 :          28 :             p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
     135                 :             :         }
     136                 :             :     }
     137                 :          25 :     return p;
     138                 :             : }
     139                 :             : 
     140                 :          41 : std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
     141                 :             :     EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
     142                 :             : {
     143                 :          41 :     AssertLockHeld(m_mutex);
     144         [ +  + ]:          53 :     for (auto& [tx, sent_to] : m_transactions) {
     145         [ +  + ]:          98 :         for (auto& send_status : sent_to) {
     146         [ +  + ]:          86 :             if (send_status.nodeid == nodeid) {
     147                 :          37 :                 return TxAndSendStatusForNode{.tx = tx, .send_status = send_status};
     148                 :             :             }
     149                 :             :         }
     150                 :             :     }
     151                 :           4 :     return std::nullopt;
     152                 :             : }
        

Generated by: LCOV version 2.0-1