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 : :
7 : : #include <util/check.h>
8 : :
9 : : #include <algorithm>
10 : :
11 : :
12 : 10012 : PrivateBroadcast::AddResult PrivateBroadcast::Add(const CTransactionRef& tx)
13 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
14 : : {
15 : 10012 : LOCK(m_mutex);
16 : : // Re-adding an already-tracked transaction is a no-op regardless of the cap.
17 [ + + ]: 10012 : if (m_transactions.contains(tx)) return AddResult::AlreadyPresent;
18 : :
19 [ + + ]: 10010 : if (m_transactions.size() >= m_max_transactions) return AddResult::QueueFull;
20 : :
21 [ + - ]: 10005 : m_transactions.try_emplace(tx);
22 : : return AddResult::Added;
23 : 10012 : }
24 : :
25 : 6 : std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
26 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
27 : : {
28 : 6 : LOCK(m_mutex);
29 : 6 : const auto handle{m_transactions.extract(tx)};
30 [ + + ]: 6 : if (handle) {
31 [ + - ]: 4 : const auto p{DerivePriority(handle.mapped().send_statuses)};
32 : 4 : return p.num_confirmed;
33 : : }
34 : 2 : return std::nullopt;
35 [ + - ]: 12 : }
36 : :
37 : 4 : std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
38 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
39 : : {
40 : 4 : LOCK(m_mutex);
41 : :
42 [ + - - + ]: 4 : if (GetSendStatusByNode(will_send_to_nodeid).has_value()) { // nodeid reuse, shouldn't send >1 tx to a given node
43 : 0 : Assume(false);
44 : 0 : return std::nullopt;
45 : : }
46 : :
47 : 4 : const auto it{std::ranges::max_element(
48 [ + - ]: 4 : m_transactions,
49 : 2 : [](const auto& a, const auto& b) { return a < b; },
50 : 2 : [](const auto& el) { return DerivePriority(el.second.send_statuses); })};
51 : :
52 [ + + ]: 4 : if (it != m_transactions.end()) {
53 : 2 : auto& [tx, state]{*it};
54 [ + - ]: 2 : state.send_statuses.emplace_back(will_send_to_nodeid, will_send_to_address, NodeClock::now());
55 : 2 : return tx;
56 : : }
57 : :
58 : 2 : return std::nullopt;
59 : 4 : }
60 : :
61 : 3 : std::optional<CTransactionRef> PrivateBroadcast::GetTxForNode(const NodeId& nodeid)
62 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
63 : : {
64 : 3 : LOCK(m_mutex);
65 [ + - ]: 3 : const auto tx_and_status{GetSendStatusByNode(nodeid)};
66 [ + + ]: 3 : if (tx_and_status.has_value()) {
67 : 2 : return tx_and_status.value().tx;
68 : : }
69 : 1 : return std::nullopt;
70 : 3 : }
71 : :
72 : 2 : void PrivateBroadcast::NodeConfirmedReception(const NodeId& nodeid)
73 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
74 : : {
75 : 2 : LOCK(m_mutex);
76 [ + - ]: 2 : const auto tx_and_status{GetSendStatusByNode(nodeid)};
77 [ + + ]: 2 : if (tx_and_status.has_value()) {
78 [ - + + - ]: 3 : tx_and_status.value().send_status.confirmed = NodeClock::now();
79 : : }
80 : 2 : }
81 : :
82 : 6 : bool PrivateBroadcast::DidNodeConfirmReception(const NodeId& nodeid)
83 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
84 : : {
85 : 6 : LOCK(m_mutex);
86 [ + - ]: 6 : const auto tx_and_status{GetSendStatusByNode(nodeid)};
87 [ + + ]: 6 : if (tx_and_status.has_value()) {
88 : 4 : return tx_and_status.value().send_status.confirmed.has_value();
89 : : }
90 : : return false;
91 : 6 : }
92 : :
93 : 2 : bool PrivateBroadcast::HavePendingTransactions()
94 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
95 : : {
96 : 2 : LOCK(m_mutex);
97 [ + - ]: 2 : return !m_transactions.empty();
98 : 2 : }
99 : :
100 : 8 : std::vector<CTransactionRef> PrivateBroadcast::GetStale() const
101 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
102 : : {
103 : 8 : LOCK(m_mutex);
104 : 8 : const auto now{NodeClock::now()};
105 : 8 : std::vector<CTransactionRef> stale;
106 [ + + + - ]: 19 : for (const auto& [tx, state] : m_transactions) {
107 [ + - ]: 11 : const Priority p{DerivePriority(state.send_statuses)};
108 [ + + ]: 11 : if (p.num_confirmed == 0) {
109 [ + + + - ]: 9 : if (state.time_added < now - INITIAL_STALE_DURATION) stale.push_back(tx);
110 : : } else {
111 [ + + + - ]: 2 : if (p.last_confirmed < now - STALE_DURATION) stale.push_back(tx);
112 : : }
113 : : }
114 [ + - ]: 8 : return stale;
115 : 8 : }
116 : :
117 : 12 : std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
118 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
119 : : {
120 : 12 : LOCK(m_mutex);
121 : 12 : std::vector<TxBroadcastInfo> entries;
122 [ + - ]: 12 : entries.reserve(m_transactions.size());
123 : :
124 [ + + - + ]: 70017 : for (const auto& [tx, state] : m_transactions) {
125 : 70005 : std::vector<PeerSendInfo> peers;
126 [ + - - + ]: 70005 : peers.reserve(state.send_statuses.size());
127 [ + + ]: 70009 : for (const auto& status : state.send_statuses) {
128 [ + - ]: 8 : peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
129 : : }
130 [ + - + - ]: 140010 : entries.emplace_back(TxBroadcastInfo{.tx = tx, .time_added = state.time_added, .peers = std::move(peers)});
131 : 70005 : }
132 : :
133 [ + - ]: 12 : return entries;
134 : 12 : }
135 : :
136 : 19 : PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
137 : : {
138 : 19 : Priority p;
139 [ - + ]: 19 : p.num_picked = sent_to.size();
140 [ + + ]: 30 : for (const auto& send_status : sent_to) {
141 : 11 : p.last_picked = std::max(p.last_picked, send_status.picked);
142 [ + + ]: 11 : if (send_status.confirmed.has_value()) {
143 : 3 : ++p.num_confirmed;
144 : 3 : p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
145 : : }
146 : : }
147 : 19 : return p;
148 : : }
149 : :
150 : 15 : std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
151 : : EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
152 : : {
153 : 15 : AssertLockHeld(m_mutex);
154 [ + + ]: 28 : for (auto& [tx, state] : m_transactions) {
155 [ + + ]: 30 : for (auto& send_status : state.send_statuses) {
156 [ + + ]: 17 : if (send_status.nodeid == nodeid) {
157 : 7 : return TxAndSendStatusForNode{.tx = tx, .send_status = send_status};
158 : : }
159 : : }
160 : : }
161 : 8 : return std::nullopt;
162 : : }
|