Bitcoin Core 30.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 <threadsafety.h>
13#include <util/time.h>
14
15#include <optional>
16#include <tuple>
17#include <unordered_map>
18#include <vector>
19
31{
32public:
39 bool Add(const CTransactionRef& tx)
41
49 std::optional<size_t> Remove(const CTransactionRef& tx)
51
59 std::optional<CTransactionRef> PickTxForSend(const NodeId& will_send_to_nodeid)
61
67 std::optional<CTransactionRef> GetTxForNode(const NodeId& nodeid)
69
75 void NodeConfirmedReception(const NodeId& nodeid)
77
83 bool DidNodeConfirmReception(const NodeId& nodeid)
85
91
95 std::vector<CTransactionRef> GetStale() const
97
98private:
100 struct SendStatus {
103 std::optional<NodeClock::time_point> confirmed;
104
106 };
107
109 struct Priority {
110 size_t num_picked{0};
112 size_t num_confirmed{0};
114
115 auto operator<=>(const Priority& other) const
116 {
117 // Invert `other` and `this` in the comparison because smaller num_picked, num_confirmed or
118 // earlier times mean greater priority. In other words, if this.num_picked < other.num_picked
119 // then this > other.
120 return std::tie(other.num_picked, other.num_confirmed, other.last_picked, other.last_confirmed) <=>
122 }
123 };
124
129 };
130
131 // No need for salted hasher because we are going to store just a bunch of locally originating transactions.
132
134 size_t operator()(const CTransactionRef& tx) const
135 {
136 return static_cast<size_t>(tx->GetWitnessHash().ToUint256().GetUint64(0));
137 }
138 };
139
141 bool operator()(const CTransactionRef& a, const CTransactionRef& b) const
142 {
143 return a->GetWitnessHash() == b->GetWitnessHash(); // If wtxid equals, then txid also equals.
144 }
145 };
146
151 static Priority DerivePriority(const std::vector<SendStatus>& sent_to);
152
158 std::optional<TxAndSendStatusForNode> GetSendStatusByNode(const NodeId& nodeid)
160
161 mutable Mutex m_mutex;
162 std::unordered_map<CTransactionRef, std::vector<SendStatus>, CTransactionRefHash, CTransactionRefComp>
163 m_transactions GUARDED_BY(m_mutex);
164};
165
166#endif // BITCOIN_PRIVATE_BROADCAST_H
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...
bool HavePendingTransactions() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if there are transactions that need to be broadcast.
std::unordered_map< CTransactionRef, std::vector< SendStatus >, CTransactionRefHash, CTransactionRefComp > m_transactions GUARDED_BY(m_mutex)
bool DidNodeConfirmReception(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if the node has confirmed reception of the transaction.
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) 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.
int64_t NodeId
Definition: net.h:103
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
bool operator()(const CTransactionRef &a, const CTransactionRef &b) const
size_t operator()(const CTransactionRef &tx) const
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.
std::optional< NodeClock::time_point > confirmed
When was the transaction reception confirmed by the node (by PONG).
const NodeClock::time_point picked
Node to which the transaction will be sent (or was sent).
SendStatus(const NodeId &nodeid, const NodeClock::time_point &picked)
A pair of a transaction and a sent status for a given node. Convenience return type of GetSendStatusB...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51