Bitcoin Core 31.99.0
P2P Digital Currency
private_broadcast.h
Go to the documentation of this file.
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>
11#include <sync.h>
12#include <util/time.h>
13
14#include <optional>
15#include <tuple>
16#include <unordered_map>
17#include <vector>
18
30{
31public:
32
35 static constexpr auto INITIAL_STALE_DURATION{5min};
36
39 static constexpr auto STALE_DURATION{1min};
40
43 static constexpr size_t MAX_TRANSACTIONS{10'000};
44
48 static constexpr size_t MAX_SEND_ATTEMPTS{1'000};
49
54 explicit PrivateBroadcast(size_t max_transactions = MAX_TRANSACTIONS,
55 size_t max_send_attempts = MAX_SEND_ATTEMPTS)
56 : m_max_transactions{max_transactions}, m_max_send_attempts{max_send_attempts} {}
57
58 struct PeerSendInfo {
61 std::optional<NodeClock::time_point> received;
62 };
63
69 std::vector<PeerSendInfo> peers;
70 };
71
73 enum class AddResult {
75 Added,
77 AlreadyPresent,
79 QueueFull,
80 };
81
90 [[nodiscard]] AddResult Add(const CTransactionRef& tx)
92
100 std::optional<size_t> Remove(const CTransactionRef& tx)
102
115 std::optional<CTransactionRef> PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
117
123 std::optional<CTransactionRef> GetTxForNode(const NodeId& nodeid)
125
131 void NodeConfirmedReception(const NodeId& nodeid)
133
139 bool DidNodeConfirmReception(const NodeId& nodeid)
141
147
152 std::vector<CTransactionRef> GetStale() const
154
158 std::vector<TxBroadcastInfo> GetBroadcastInfo() const
160
161private:
163 struct SendStatus {
171 std::optional<NodeClock::time_point> confirmed;
172
173 SendStatus(const NodeId& nodeid, const CService& address, const NodeClock::time_point& picked) : nodeid{nodeid}, address{address}, picked{picked} {}
174 };
175
177 struct Priority {
178 size_t num_picked{0};
180 size_t num_confirmed{0};
182
183 auto operator<=>(const Priority& other) const
184 {
185 // Invert `other` and `this` in the comparison because smaller num_picked, num_confirmed or
186 // earlier times mean greater priority. In other words, if this.num_picked < other.num_picked
187 // then this > other.
188 return std::tie(other.num_picked, other.num_confirmed, other.last_picked, other.last_confirmed) <=>
190 }
191 };
192
197 };
198
199 // No need for salted hasher because we are going to store just a bunch of locally originating transactions.
200
202 size_t operator()(const CTransactionRef& tx) const
203 {
204 return static_cast<size_t>(tx->GetWitnessHash().ToUint256().GetUint64(0));
205 }
206 };
207
209 bool operator()(const CTransactionRef& a, const CTransactionRef& b) const
210 {
211 return a->GetWitnessHash() == b->GetWitnessHash(); // If wtxid equals, then txid also equals.
212 }
213 };
214
219 static Priority DerivePriority(const std::vector<SendStatus>& sent_to);
220
226 std::optional<TxAndSendStatusForNode> GetSendStatusByNode(const NodeId& nodeid)
230 std::vector<SendStatus> send_statuses;
231 };
232 bool IsPending(const TxSendStatus& status) const;
234 const size_t m_max_transactions;
237 mutable Mutex m_mutex;
238 std::unordered_map<CTransactionRef, TxSendStatus, CTransactionRefHash, CTransactionRefComp>
239 m_transactions GUARDED_BY(m_mutex);
240};
241
242#endif // BITCOIN_PRIVATE_BROADCAST_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
Store a list of transactions to be broadcast privately.
static Priority DerivePriority(const std::vector< SendStatus > &sent_to)
Derive the sending priority of a transaction.
void NodeConfirmedReception(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Mark that the node has confirmed reception of the transaction we sent it by responding with PONG to o...
AddResult Add(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a transaction to the storage, or reset an exhausted transaction so it can be broadcast again.
std::vector< TxBroadcastInfo > GetBroadcastInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get stats about all transactions currently being privately broadcast.
AddResult
Outcome of Add().
const size_t m_max_transactions
Cap on the number of simultaneously tracked transactions (see Add()).
PrivateBroadcast(size_t max_transactions=MAX_TRANSACTIONS, size_t max_send_attempts=MAX_SEND_ATTEMPTS)
bool HavePendingTransactions() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if there are transactions with send attempts remaining.
bool DidNodeConfirmReception(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if the node has confirmed reception of the transaction.
static constexpr auto STALE_DURATION
If a transaction is not received back from the network for this duration after it is broadcast,...
std::optional< size_t > Remove(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Forget a transaction.
std::optional< CTransactionRef > PickTxForSend(const NodeId &will_send_to_nodeid, const CService &will_send_to_address) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Pick the transaction with the fewest send attempts, and confirmations, and oldest send/confirm times.
bool IsPending(const TxSendStatus &status) const
static constexpr size_t MAX_TRANSACTIONS
Maximum number of transactions tracked simultaneously.
std::optional< TxAndSendStatusForNode > GetSendStatusByNode(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Find which transaction we sent to a given node (marked by PickTxForSend()).
const size_t m_max_send_attempts
Cap on the number of send attempts per transaction (see PickTxForSend()).
std::optional< CTransactionRef > GetTxForNode(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get the transaction that was picked for sending to a given node by PickTxForSend().
static constexpr size_t MAX_SEND_ATTEMPTS
Maximum number of send attempts for a transaction. Once this limit is reached, the transaction remain...
std::vector< CTransactionRef > GetStale() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get the transactions that have not been broadcast recently and have send attempts remaining.
std::unordered_map< CTransactionRef, TxSendStatus, CTransactionRefHash, CTransactionRefComp > m_transactions GUARDED_BY(m_mutex)
static constexpr auto INITIAL_STALE_DURATION
If a transaction is not sent to any peer for this duration, then we consider it stale / for rebroadca...
int64_t NodeId
Definition: net.h:105
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:38
std::chrono::time_point< NodeClock > time_point
Definition: time.h:28
bool operator()(const CTransactionRef &a, const CTransactionRef &b) const
size_t operator()(const CTransactionRef &tx) const
std::optional< NodeClock::time_point > received
Cumulative stats from all the send attempts for a transaction. Used to prioritize transactions.
size_t num_picked
Number of times the transaction was picked for sending.
auto operator<=>(const Priority &other) const
NodeClock::time_point last_confirmed
The most recent time when the transaction was confirmed.
size_t num_confirmed
Number of nodes that have confirmed reception of a transaction (by PONG).
NodeClock::time_point last_picked
The most recent time when the transaction was picked for sending.
Status of a transaction sent to a given node.
SendStatus(const NodeId &nodeid, const CService &address, const NodeClock::time_point &picked)
std::optional< NodeClock::time_point > confirmed
When was the transaction reception confirmed by the node (by PONG).
const NodeId nodeid
Node to which the transaction will be sent (or was sent).
const NodeClock::time_point picked
When was the transaction picked for sending to the node.
const CService address
Address of the node.
A pair of a transaction and a sent status for a given node. Convenience return type of GetSendStatusB...
std::vector< PeerSendInfo > peers
size_t attempts_remaining
Number of additional send attempts allowed for this transaction (0 if exhausted).
std::vector< SendStatus > send_statuses
NodeClock::time_point time_added
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49