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
41 struct PeerSendInfo {
44 std::optional<NodeClock::time_point> received;
45 };
46
50 std::vector<PeerSendInfo> peers;
51 };
52
59 bool Add(const CTransactionRef& tx)
61
69 std::optional<size_t> Remove(const CTransactionRef& tx)
71
81 std::optional<CTransactionRef> PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
83
89 std::optional<CTransactionRef> GetTxForNode(const NodeId& nodeid)
91
97 void NodeConfirmedReception(const NodeId& nodeid)
99
105 bool DidNodeConfirmReception(const NodeId& nodeid)
107
113
117 std::vector<CTransactionRef> GetStale() const
119
123 std::vector<TxBroadcastInfo> GetBroadcastInfo() const
125
126private:
128 struct SendStatus {
136 std::optional<NodeClock::time_point> confirmed;
137
138 SendStatus(const NodeId& nodeid, const CService& address, const NodeClock::time_point& picked) : nodeid{nodeid}, address{address}, picked{picked} {}
139 };
140
142 struct Priority {
143 size_t num_picked{0};
145 size_t num_confirmed{0};
147
148 auto operator<=>(const Priority& other) const
149 {
150 // Invert `other` and `this` in the comparison because smaller num_picked, num_confirmed or
151 // earlier times mean greater priority. In other words, if this.num_picked < other.num_picked
152 // then this > other.
153 return std::tie(other.num_picked, other.num_confirmed, other.last_picked, other.last_confirmed) <=>
155 }
156 };
157
162 };
163
164 // No need for salted hasher because we are going to store just a bunch of locally originating transactions.
165
167 size_t operator()(const CTransactionRef& tx) const
168 {
169 return static_cast<size_t>(tx->GetWitnessHash().ToUint256().GetUint64(0));
170 }
171 };
172
174 bool operator()(const CTransactionRef& a, const CTransactionRef& b) const
175 {
176 return a->GetWitnessHash() == b->GetWitnessHash(); // If wtxid equals, then txid also equals.
177 }
178 };
179
184 static Priority DerivePriority(const std::vector<SendStatus>& sent_to);
185
191 std::optional<TxAndSendStatusForNode> GetSendStatusByNode(const NodeId& nodeid)
195 std::vector<SendStatus> send_statuses;
196 };
197 mutable Mutex m_mutex;
198 std::unordered_map<CTransactionRef, TxSendStatus, CTransactionRefHash, CTransactionRefComp>
199 m_transactions GUARDED_BY(m_mutex);
200};
201
202#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...
std::vector< TxBroadcastInfo > GetBroadcastInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get stats about all transactions currently being privately broadcast.
bool HavePendingTransactions() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if there are transactions that need to be broadcast.
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.
std::optional< TxAndSendStatusForNode > GetSendStatusByNode(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Find which transaction we sent to a given node (marked by 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().
bool Add(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a transaction to the storage.
std::vector< CTransactionRef > GetStale() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get the transactions that have not been broadcast recently.
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...
Definition: common.h:30
int64_t NodeId
Definition: net.h:103
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:36
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
std::vector< SendStatus > send_statuses
const NodeClock::time_point time_added
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49