fuzz coverage

Coverage Report

Created: 2026-04-24 13:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/private_broadcast.h
Line
Count
Source
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
#ifndef BITCOIN_PRIVATE_BROADCAST_H
6
#define BITCOIN_PRIVATE_BROADCAST_H
7
8
#include <net.h>
9
#include <primitives/transaction.h>
10
#include <primitives/transaction_identifier.h>
11
#include <sync.h>
12
#include <util/time.h>
13
14
#include <optional>
15
#include <tuple>
16
#include <unordered_map>
17
#include <vector>
18
19
/**
20
 * Store a list of transactions to be broadcast privately. Supports the following operations:
21
 * - Add a new transaction
22
 * - Remove a transaction
23
 * - Pick a transaction for sending to one recipient
24
 * - Query which transaction has been picked for sending to a given recipient node
25
 * - Mark that a given recipient node has confirmed receipt of a transaction
26
 * - Query whether a given recipient node has confirmed reception
27
 * - Query whether any transactions that need sending are currently on the list
28
 */
29
class PrivateBroadcast
30
{
31
public:
32
    struct PeerSendInfo {
33
        CService address;
34
        NodeClock::time_point sent;
35
        std::optional<NodeClock::time_point> received;
36
    };
37
38
    struct TxBroadcastInfo {
39
        CTransactionRef tx;
40
        std::vector<PeerSendInfo> peers;
41
    };
42
43
    /**
44
     * Add a transaction to the storage.
45
     * @param[in] tx The transaction to add.
46
     * @retval true The transaction was added.
47
     * @retval false The transaction was already present.
48
     */
49
    bool Add(const CTransactionRef& tx)
50
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
51
52
    /**
53
     * Forget a transaction.
54
     * @param[in] tx Transaction to forget.
55
     * @retval !nullopt The number of times the transaction was sent and confirmed
56
     * by the recipient (if the transaction existed and was removed).
57
     * @retval nullopt The transaction was not in the storage.
58
     */
59
    std::optional<size_t> Remove(const CTransactionRef& tx)
60
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
61
62
    /**
63
     * Pick the transaction with the fewest send attempts, and confirmations,
64
     * and oldest send/confirm times.
65
     * @param[in] will_send_to_nodeid Will remember that the returned transaction
66
     * was picked for sending to this node.
67
     * @param[in] will_send_to_address Address of the peer to which this transaction
68
     * will be sent.
69
     * @return Most urgent transaction or nullopt if there are no transactions.
70
     */
71
    std::optional<CTransactionRef> PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
72
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
73
74
    /**
75
     * Get the transaction that was picked for sending to a given node by PickTxForSend().
76
     * @param[in] nodeid Node to which a transaction is being (or was) sent.
77
     * @return Transaction or nullopt if the nodeid is unknown.
78
     */
79
    std::optional<CTransactionRef> GetTxForNode(const NodeId& nodeid)
80
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
81
82
    /**
83
     * Mark that the node has confirmed reception of the transaction we sent it by
84
     * responding with `PONG` to our `PING` message.
85
     * @param[in] nodeid Node that we sent a transaction to.
86
     */
87
    void NodeConfirmedReception(const NodeId& nodeid)
88
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
89
90
    /**
91
     * Check if the node has confirmed reception of the transaction.
92
     * @retval true Node has confirmed, `NodeConfirmedReception()` has been called.
93
     * @retval false Node has not confirmed, `NodeConfirmedReception()` has not been called.
94
     */
95
    bool DidNodeConfirmReception(const NodeId& nodeid)
96
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
97
98
    /**
99
     * Check if there are transactions that need to be broadcast.
100
     */
101
    bool HavePendingTransactions()
102
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
103
104
    /**
105
     * Get the transactions that have not been broadcast recently.
106
     */
107
    std::vector<CTransactionRef> GetStale() const
108
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
109
110
    /**
111
     * Get stats about all transactions currently being privately broadcast.
112
     */
113
    std::vector<TxBroadcastInfo> GetBroadcastInfo() const
114
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
115
116
private:
117
    /// Status of a transaction sent to a given node.
118
    struct SendStatus {
119
        const NodeId nodeid; /// Node to which the transaction will be sent (or was sent).
120
        const CService address; /// Address of the node.
121
        const NodeClock::time_point picked; ///< When was the transaction picked for sending to the node.
122
        std::optional<NodeClock::time_point> confirmed; ///< When was the transaction reception confirmed by the node (by PONG).
123
124
0
        SendStatus(const NodeId& nodeid, const CService& address, const NodeClock::time_point& picked) : nodeid{nodeid}, address{address}, picked{picked} {}
125
    };
126
127
    /// Cumulative stats from all the send attempts for a transaction. Used to prioritize transactions.
128
    struct Priority {
129
        size_t num_picked{0}; ///< Number of times the transaction was picked for sending.
130
        NodeClock::time_point last_picked{}; ///< The most recent time when the transaction was picked for sending.
131
        size_t num_confirmed{0}; ///< Number of nodes that have confirmed reception of a transaction (by PONG).
132
        NodeClock::time_point last_confirmed{}; ///< The most recent time when the transaction was confirmed.
133
134
        auto operator<=>(const Priority& other) const
135
0
        {
136
            // Invert `other` and `this` in the comparison because smaller num_picked, num_confirmed or
137
            // earlier times mean greater priority. In other words, if this.num_picked < other.num_picked
138
            // then this > other.
139
0
            return std::tie(other.num_picked, other.num_confirmed, other.last_picked, other.last_confirmed) <=>
140
0
                   std::tie(num_picked, num_confirmed, last_picked, last_confirmed);
141
0
        }
142
    };
143
144
    /// A pair of a transaction and a sent status for a given node. Convenience return type of GetSendStatusByNode().
145
    struct TxAndSendStatusForNode {
146
        const CTransactionRef& tx;
147
        SendStatus& send_status;
148
    };
149
150
    // No need for salted hasher because we are going to store just a bunch of locally originating transactions.
151
152
    struct CTransactionRefHash {
153
        size_t operator()(const CTransactionRef& tx) const
154
17.6M
        {
155
17.6M
            return static_cast<size_t>(tx->GetWitnessHash().ToUint256().GetUint64(0));
156
17.6M
        }
157
    };
158
159
    struct CTransactionRefComp {
160
        bool operator()(const CTransactionRef& a, const CTransactionRef& b) const
161
0
        {
162
0
            return a->GetWitnessHash() == b->GetWitnessHash(); // If wtxid equals, then txid also equals.
163
0
        }
164
    };
165
166
    /**
167
     * Derive the sending priority of a transaction.
168
     * @param[in] sent_to List of nodes that the transaction has been sent to.
169
     */
170
    static Priority DerivePriority(const std::vector<SendStatus>& sent_to);
171
172
    /**
173
     * Find which transaction we sent to a given node (marked by PickTxForSend()).
174
     * @return That transaction together with the send status or nullopt if we did not
175
     * send any transaction to the given node.
176
     */
177
    std::optional<TxAndSendStatusForNode> GetSendStatusByNode(const NodeId& nodeid)
178
        EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
179
180
    mutable Mutex m_mutex;
181
    std::unordered_map<CTransactionRef, std::vector<SendStatus>, CTransactionRefHash, CTransactionRefComp>
182
        m_transactions GUARDED_BY(m_mutex);
183
};
184
185
#endif // BITCOIN_PRIVATE_BROADCAST_H