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 : : #include <ranges>
11 : :
12 : :
13 : 20033 : PrivateBroadcast::AddResult PrivateBroadcast::Add(const CTransactionRef& tx)
14 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
15 : : {
16 : 20033 : LOCK(m_mutex);
17 [ + + ]: 20033 : if (const auto it{m_transactions.find(tx)}; it != m_transactions.end()) {
18 [ + - + + ]: 6 : if (IsPending(it->second)) return AddResult::AlreadyPresent;
19 : :
20 : : // An exhausted transaction can be explicitly retried by adding it again.
21 : 2 : it->second.time_added = NodeClock::now();
22 : 2 : it->second.send_statuses.clear();
23 : 2 : return AddResult::Added;
24 : : }
25 : :
26 [ + + ]: 20027 : if (m_transactions.size() >= m_max_transactions) return AddResult::QueueFull;
27 : :
28 [ + - ]: 20017 : m_transactions.try_emplace(tx);
29 : : return AddResult::Added;
30 : 20033 : }
31 : :
32 : 14704 : std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
33 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
34 : : {
35 : 14704 : LOCK(m_mutex);
36 : 14704 : const auto handle{m_transactions.extract(tx)};
37 [ + + ]: 14704 : if (handle) {
38 [ + - ]: 9 : const auto p{DerivePriority(handle.mapped().send_statuses)};
39 : 9 : return p.num_confirmed;
40 : : }
41 : 14695 : return std::nullopt;
42 [ + - ]: 29408 : }
43 : :
44 : 30 : std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
45 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
46 : : {
47 : 30 : LOCK(m_mutex);
48 : :
49 [ + - - + ]: 30 : if (GetSendStatusByNode(will_send_to_nodeid).has_value()) { // nodeid reuse, shouldn't send >1 tx to a given node
50 : 0 : Assume(false);
51 : 0 : return std::nullopt;
52 : : }
53 : :
54 [ + - ]: 75 : auto pending_transactions{m_transactions | std::views::filter([this](const auto& entry) { return IsPending(entry.second); })};
55 [ + - ]: 30 : const auto it{std::ranges::max_element(
56 : : pending_transactions,
57 : 16 : [](const auto& a, const auto& b) { return a < b; },
58 : 16 : [](const auto& el) { return DerivePriority(el.second.send_statuses); })};
59 : :
60 [ + + ]: 30 : if (it != pending_transactions.end()) {
61 : 27 : auto& [tx, state]{*it};
62 [ + - ]: 27 : state.send_statuses.emplace_back(will_send_to_nodeid, will_send_to_address, NodeClock::now());
63 : 27 : return tx;
64 : : }
65 : :
66 : 3 : return std::nullopt;
67 : 30 : }
68 : :
69 : 21 : std::optional<CTransactionRef> PrivateBroadcast::GetTxForNode(const NodeId& nodeid)
70 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
71 : : {
72 : 21 : LOCK(m_mutex);
73 [ + - ]: 21 : const auto tx_and_status{GetSendStatusByNode(nodeid)};
74 [ + + ]: 21 : if (tx_and_status.has_value()) {
75 : 19 : return tx_and_status.value().tx;
76 : : }
77 : 2 : return std::nullopt;
78 : 21 : }
79 : :
80 : 20 : void PrivateBroadcast::NodeConfirmedReception(const NodeId& nodeid)
81 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
82 : : {
83 : 20 : LOCK(m_mutex);
84 [ + - ]: 20 : const auto tx_and_status{GetSendStatusByNode(nodeid)};
85 [ + + ]: 20 : if (tx_and_status.has_value()) {
86 [ - + + - ]: 39 : tx_and_status.value().send_status.confirmed = NodeClock::now();
87 : : }
88 : 20 : }
89 : :
90 : 31 : bool PrivateBroadcast::DidNodeConfirmReception(const NodeId& nodeid)
91 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
92 : : {
93 : 31 : LOCK(m_mutex);
94 [ + - ]: 31 : const auto tx_and_status{GetSendStatusByNode(nodeid)};
95 [ + + ]: 31 : if (tx_and_status.has_value()) {
96 : 22 : return tx_and_status.value().send_status.confirmed.has_value();
97 : : }
98 : : return false;
99 : 31 : }
100 : :
101 : 18 : bool PrivateBroadcast::HavePendingTransactions()
102 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
103 : : {
104 : 18 : LOCK(m_mutex);
105 [ + - + - ]: 34 : return std::ranges::any_of(m_transactions, [this](const auto& entry) { return IsPending(entry.second); });
106 : 18 : }
107 : :
108 : 17 : std::vector<CTransactionRef> PrivateBroadcast::GetStale() const
109 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
110 : : {
111 : 17 : LOCK(m_mutex);
112 : 17 : const auto now{NodeClock::now()};
113 : 17 : std::vector<CTransactionRef> stale;
114 [ + + + - ]: 33 : for (const auto& [tx, state] : m_transactions) {
115 [ + + + - ]: 16 : if (!IsPending(state)) continue;
116 [ + - ]: 15 : const Priority p{DerivePriority(state.send_statuses)};
117 [ + + ]: 15 : if (p.num_confirmed == 0) {
118 [ + + + - ]: 11 : if (state.time_added < now - INITIAL_STALE_DURATION) stale.push_back(tx);
119 : : } else {
120 [ + + + - ]: 4 : if (p.last_confirmed < now - STALE_DURATION) stale.push_back(tx);
121 : : }
122 : : }
123 [ + - ]: 17 : return stale;
124 : 17 : }
125 : :
126 : 30 : std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
127 : : EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
128 : : {
129 : 30 : LOCK(m_mutex);
130 : 30 : std::vector<TxBroadcastInfo> entries;
131 [ + - ]: 30 : entries.reserve(m_transactions.size());
132 : :
133 [ + + - + ]: 130057 : for (const auto& [tx, state] : m_transactions) {
134 : 130027 : std::vector<PeerSendInfo> peers;
135 [ + - - + ]: 130027 : peers.reserve(state.send_statuses.size());
136 [ + + ]: 130069 : for (const auto& status : state.send_statuses) {
137 [ + - ]: 84 : peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
138 : : }
139 [ - + + - ]: 130027 : const size_t attempts_remaining{m_max_send_attempts - std::min(state.send_statuses.size(), m_max_send_attempts)};
140 [ + - + - ]: 260054 : entries.emplace_back(TxBroadcastInfo{.tx = tx, .time_added = state.time_added, .attempts_remaining = attempts_remaining, .peers = std::move(peers)});
141 : 130027 : }
142 : :
143 [ + - ]: 30 : return entries;
144 : 30 : }
145 : :
146 : 83 : bool PrivateBroadcast::IsPending(const TxSendStatus& status) const
147 : : {
148 [ - + ]: 83 : return status.send_statuses.size() < m_max_send_attempts;
149 : : }
150 : :
151 : 56 : PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
152 : : {
153 : 56 : Priority p;
154 [ - + ]: 56 : p.num_picked = sent_to.size();
155 [ + + ]: 117 : for (const auto& send_status : sent_to) {
156 : 61 : p.last_picked = std::max(p.last_picked, send_status.picked);
157 [ + + ]: 61 : if (send_status.confirmed.has_value()) {
158 : 50 : ++p.num_confirmed;
159 : 50 : p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
160 : : }
161 : : }
162 : 56 : return p;
163 : : }
164 : :
165 : 102 : std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
166 : : EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
167 : : {
168 : 102 : AssertLockHeld(m_mutex);
169 [ + + ]: 179 : for (auto& [tx, state] : m_transactions) {
170 [ + + ]: 297 : for (auto& send_status : state.send_statuses) {
171 [ + + ]: 220 : if (send_status.nodeid == nodeid) {
172 : 60 : return TxAndSendStatusForNode{.tx = tx, .send_status = send_status};
173 : : }
174 : : }
175 : : }
176 : 42 : return std::nullopt;
177 : : }
|