Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <txorphanage.h>
6 : :
7 : : #include <consensus/validation.h>
8 : : #include <logging.h>
9 : : #include <policy/policy.h>
10 : : #include <primitives/transaction.h>
11 : : #include <util/time.h>
12 : :
13 : : #include <cassert>
14 : :
15 : 234696 : bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
16 : : {
17 : 234696 : const Txid& hash = tx->GetHash();
18 : 234696 : const Wtxid& wtxid = tx->GetWitnessHash();
19 [ + + ]: 234696 : if (m_orphans.count(wtxid))
20 : 214004 : return false;
21 : :
22 : : // Ignore big transactions, to avoid a
23 : : // send-big-orphans memory exhaustion attack. If a peer has a legitimate
24 : : // large transaction with a missing parent then we assume
25 : : // it will rebroadcast it later, after the parent transaction(s)
26 : : // have been mined or received.
27 : : // 100 orphans, each of which is at most 100,000 bytes big is
28 : : // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
29 : 20692 : unsigned int sz = GetTransactionWeight(*tx);
30 [ + + ]: 20692 : if (sz > MAX_STANDARD_TX_WEIGHT)
31 : : {
32 [ + - # # : 1262 : LogPrint(BCLog::TXPACKAGES, "ignoring large orphan tx (size: %u, txid: %s, wtxid: %s)\n", sz, hash.ToString(), wtxid.ToString());
# # ]
33 : 1262 : return false;
34 : : }
35 : :
36 [ + - + - : 19430 : auto ret = m_orphans.emplace(wtxid, OrphanTx{tx, peer, Now<NodeSeconds>() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size()});
+ - # # ]
37 [ + - ]: 19430 : assert(ret.second);
38 : 19430 : m_orphan_list.push_back(ret.first);
39 [ + + ]: 2710617 : for (const CTxIn& txin : tx->vin) {
40 : 2691187 : m_outpoint_to_orphan_it[txin.prevout].insert(ret.first);
41 : 2691187 : }
42 : :
43 [ + - # # : 19430 : LogPrint(BCLog::TXPACKAGES, "stored orphan tx %s (wtxid=%s), weight: %u (mapsz %u outsz %u)\n", hash.ToString(), wtxid.ToString(), sz,
# # ]
44 : : m_orphans.size(), m_outpoint_to_orphan_it.size());
45 : 19430 : return true;
46 : 234696 : }
47 : :
48 : 2383458 : int TxOrphanage::EraseTx(const Wtxid& wtxid)
49 : : {
50 : 2383458 : std::map<Wtxid, OrphanTx>::iterator it = m_orphans.find(wtxid);
51 [ + + ]: 2383458 : if (it == m_orphans.end())
52 : 2368284 : return 0;
53 [ + + ]: 2335977 : for (const CTxIn& txin : it->second.tx->vin)
54 : : {
55 : 2320803 : auto itPrev = m_outpoint_to_orphan_it.find(txin.prevout);
56 [ + + ]: 2320803 : if (itPrev == m_outpoint_to_orphan_it.end())
57 : 1563988 : continue;
58 : 756815 : itPrev->second.erase(it);
59 [ + + ]: 756815 : if (itPrev->second.empty())
60 : 100749 : m_outpoint_to_orphan_it.erase(itPrev);
61 [ + + - + : 2320803 : }
+ ]
62 : :
63 : 15174 : size_t old_pos = it->second.list_pos;
64 [ + - ]: 15174 : assert(m_orphan_list[old_pos] == it);
65 [ + + ]: 15174 : if (old_pos + 1 != m_orphan_list.size()) {
66 : : // Unless we're deleting the last entry in m_orphan_list, move the last
67 : : // entry to the position we're deleting.
68 : 4350 : auto it_last = m_orphan_list.back();
69 : 4350 : m_orphan_list[old_pos] = it_last;
70 : 4350 : it_last->second.list_pos = old_pos;
71 : 4350 : }
72 : 15174 : const auto& txid = it->second.tx->GetHash();
73 : : // Time spent in orphanage = difference between current and entry time.
74 : : // Entry time is equal to ORPHAN_TX_EXPIRE_TIME earlier than entry's expiry.
75 [ + - # # : 15174 : LogPrint(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s) after %ds\n", txid.ToString(), wtxid.ToString(),
# # # # #
# # # ]
76 : : Ticks<std::chrono::seconds>(NodeClock::now() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire));
77 : 15174 : m_orphan_list.pop_back();
78 : :
79 : 15174 : m_orphans.erase(it);
80 : 15174 : return 1;
81 : 2383458 : }
82 : :
83 : 73034 : void TxOrphanage::EraseForPeer(NodeId peer)
84 : : {
85 : 73034 : m_peer_work_set.erase(peer);
86 : :
87 : 73034 : int nErased = 0;
88 : 73034 : std::map<Wtxid, OrphanTx>::iterator iter = m_orphans.begin();
89 [ + + ]: 192236 : while (iter != m_orphans.end())
90 : : {
91 : : // increment to avoid iterator becoming invalid after erasure
92 : 119202 : const auto& [wtxid, orphan] = *iter++;
93 [ + + ]: 119202 : if (orphan.fromPeer == peer) {
94 : 8131 : nErased += EraseTx(wtxid);
95 : 8131 : }
96 : 119202 : }
97 [ + + + - ]: 73034 : if (nErased > 0) LogPrint(BCLog::TXPACKAGES, "Erased %d orphan transaction(s) from peer=%d\n", nErased, peer);
98 : 73034 : }
99 : :
100 : 11738 : void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
101 : : {
102 : 11738 : unsigned int nEvicted = 0;
103 : 11738 : auto nNow{Now<NodeSeconds>()};
104 [ + + ]: 11738 : if (m_next_sweep <= nNow) {
105 : : // Sweep out expired orphan pool entries:
106 : 636 : int nErased = 0;
107 : 636 : auto nMinExpTime{nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL};
108 : 636 : std::map<Wtxid, OrphanTx>::iterator iter = m_orphans.begin();
109 [ + + ]: 2422 : while (iter != m_orphans.end())
110 : : {
111 : 1786 : std::map<Wtxid, OrphanTx>::iterator maybeErase = iter++;
112 [ + + ]: 1786 : if (maybeErase->second.nTimeExpire <= nNow) {
113 : 1198 : nErased += EraseTx(maybeErase->second.tx->GetWitnessHash());
114 : 1198 : } else {
115 : 588 : nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
116 : : }
117 : 1786 : }
118 : : // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
119 : 636 : m_next_sweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
120 [ + + + - ]: 636 : if (nErased > 0) LogPrint(BCLog::TXPACKAGES, "Erased %d orphan tx due to expiration\n", nErased);
121 : 636 : }
122 [ + + ]: 12458 : while (m_orphans.size() > max_orphans)
123 : : {
124 : : // Evict a random orphan:
125 : 720 : size_t randompos = rng.randrange(m_orphan_list.size());
126 : 720 : EraseTx(m_orphan_list[randompos]->second.tx->GetWitnessHash());
127 : 720 : ++nEvicted;
128 : 720 : }
129 [ + + + - ]: 11738 : if (nEvicted > 0) LogPrint(BCLog::TXPACKAGES, "orphanage overflow, removed %u tx\n", nEvicted);
130 : 11738 : }
131 : :
132 : 13928 : void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx)
133 : : {
134 [ + + ]: 1031778 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
135 : 1017850 : const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(tx.GetHash(), i));
136 [ + + ]: 1017850 : if (it_by_prev != m_outpoint_to_orphan_it.end()) {
137 [ + + ]: 453582 : for (const auto& elem : it_by_prev->second) {
138 : : // Get this source peer's work set, emplacing an empty set if it didn't exist
139 : : // (note: if this peer wasn't still connected, we would have removed the orphan tx already)
140 : 311197 : std::set<Wtxid>& orphan_work_set = m_peer_work_set.try_emplace(elem->second.fromPeer).first->second;
141 : : // Add this tx to the work set
142 : 311197 : orphan_work_set.insert(elem->first);
143 [ + - # # : 311197 : LogPrint(BCLog::TXPACKAGES, "added %s (wtxid=%s) to peer %d workset\n",
# # # # ]
144 : : tx.GetHash().ToString(), tx.GetWitnessHash().ToString(), elem->second.fromPeer);
145 : 311197 : }
146 : 142385 : }
147 : 1017850 : }
148 : 13928 : }
149 : :
150 : 2905408 : bool TxOrphanage::HaveTx(const Wtxid& wtxid) const
151 : : {
152 : 2905408 : return m_orphans.count(wtxid);
153 : : }
154 : :
155 : 959256 : CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer)
156 : : {
157 : 959256 : auto work_set_it = m_peer_work_set.find(peer);
158 [ + + ]: 959256 : if (work_set_it != m_peer_work_set.end()) {
159 : 5054 : auto& work_set = work_set_it->second;
160 [ + + ]: 5324 : while (!work_set.empty()) {
161 : 1826 : Wtxid wtxid = *work_set.begin();
162 : 1826 : work_set.erase(work_set.begin());
163 : :
164 : 1826 : const auto orphan_it = m_orphans.find(wtxid);
165 [ + + ]: 1826 : if (orphan_it != m_orphans.end()) {
166 : 1556 : return orphan_it->second.tx;
167 : : }
168 [ + + ]: 1826 : }
169 [ + + ]: 5054 : }
170 : 957700 : return nullptr;
171 : 959256 : }
172 : :
173 : 102251 : bool TxOrphanage::HaveTxToReconsider(NodeId peer)
174 : : {
175 : 102251 : auto work_set_it = m_peer_work_set.find(peer);
176 [ - + ]: 102251 : if (work_set_it != m_peer_work_set.end()) {
177 : 0 : auto& work_set = work_set_it->second;
178 : 0 : return !work_set.empty();
179 : 0 : }
180 : 102251 : return false;
181 : 102251 : }
182 : :
183 : 0 : void TxOrphanage::EraseForBlock(const CBlock& block)
184 : : {
185 : 0 : std::vector<Wtxid> vOrphanErase;
186 : :
187 [ # # ]: 0 : for (const CTransactionRef& ptx : block.vtx) {
188 : 0 : const CTransaction& tx = *ptx;
189 : :
190 : : // Which orphan pool entries must we evict?
191 [ # # ]: 0 : for (const auto& txin : tx.vin) {
192 [ # # ]: 0 : auto itByPrev = m_outpoint_to_orphan_it.find(txin.prevout);
193 [ # # ]: 0 : if (itByPrev == m_outpoint_to_orphan_it.end()) continue;
194 [ # # ]: 0 : for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
195 : 0 : const CTransaction& orphanTx = *(*mi)->second.tx;
196 [ # # # # ]: 0 : vOrphanErase.push_back(orphanTx.GetWitnessHash());
197 : 0 : }
198 [ # # # # : 0 : }
# ]
199 : 0 : }
200 : :
201 : : // Erase orphan transactions included or precluded by this block
202 [ # # ]: 0 : if (vOrphanErase.size()) {
203 : 0 : int nErased = 0;
204 [ # # ]: 0 : for (const auto& orphanHash : vOrphanErase) {
205 [ # # ]: 0 : nErased += EraseTx(orphanHash);
206 : 0 : }
207 [ # # # # : 0 : LogPrint(BCLog::TXPACKAGES, "Erased %d orphan transaction(s) included or conflicted by block\n", nErased);
# # ]
208 : 0 : }
209 : 0 : }
210 : :
211 : 13928 : std::vector<CTransactionRef> TxOrphanage::GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const
212 : : {
213 : : // First construct a vector of iterators to ensure we do not return duplicates of the same tx
214 : : // and so we can sort by nTimeExpire.
215 : 13928 : std::vector<OrphanMap::iterator> iters;
216 : :
217 : : // For each output, get all entries spending this prevout, filtering for ones from the specified peer.
218 [ + + ]: 1031778 : for (unsigned int i = 0; i < parent->vout.size(); i++) {
219 [ + - + - : 1017850 : const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(parent->GetHash(), i));
+ - ]
220 [ + + ]: 1017850 : if (it_by_prev != m_outpoint_to_orphan_it.end()) {
221 [ + + ]: 453582 : for (const auto& elem : it_by_prev->second) {
222 [ + + ]: 311197 : if (elem->second.fromPeer == nodeid) {
223 [ - + ]: 93016 : iters.emplace_back(elem);
224 : 93016 : }
225 : 311197 : }
226 : 142385 : }
227 : 1017850 : }
228 : :
229 : : // Sort by address so that duplicates can be deleted. At the same time, sort so that more recent
230 : : // orphans (which expire later) come first. Break ties based on address, as nTimeExpire is
231 : : // quantified in seconds and it is possible for orphans to have the same expiry.
232 [ + - ]: 594383 : std::sort(iters.begin(), iters.end(), [](const auto& lhs, const auto& rhs) {
233 [ + + ]: 580455 : if (lhs->second.nTimeExpire == rhs->second.nTimeExpire) {
234 : 541736 : return &(*lhs) < &(*rhs);
235 : : } else {
236 : 38719 : return lhs->second.nTimeExpire > rhs->second.nTimeExpire;
237 : : }
238 : 580455 : });
239 : : // Erase duplicates
240 [ + - + - ]: 13928 : iters.erase(std::unique(iters.begin(), iters.end()), iters.end());
241 : :
242 : : // Convert to a vector of CTransactionRef
243 : 13928 : std::vector<CTransactionRef> children_found;
244 [ + - ]: 13928 : children_found.reserve(iters.size());
245 [ + + ]: 35588 : for (const auto& child_iter : iters) {
246 [ + - ]: 21660 : children_found.emplace_back(child_iter->second.tx);
247 : 21660 : }
248 : 13928 : return children_found;
249 [ + - ]: 13928 : }
250 : :
251 : 13928 : std::vector<std::pair<CTransactionRef, NodeId>> TxOrphanage::GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const
252 : : {
253 : : // First construct vector of iterators to ensure we do not return duplicates of the same tx.
254 : 13928 : std::vector<OrphanMap::iterator> iters;
255 : :
256 : : // For each output, get all entries spending this prevout, filtering for ones not from the specified peer.
257 [ + + ]: 1031778 : for (unsigned int i = 0; i < parent->vout.size(); i++) {
258 [ + - + - : 1017850 : const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(parent->GetHash(), i));
+ - ]
259 [ + + ]: 1017850 : if (it_by_prev != m_outpoint_to_orphan_it.end()) {
260 [ + + ]: 453582 : for (const auto& elem : it_by_prev->second) {
261 [ + + ]: 311197 : if (elem->second.fromPeer != nodeid) {
262 [ - + ]: 218181 : iters.emplace_back(elem);
263 : 218181 : }
264 : 311197 : }
265 : 142385 : }
266 : 1017850 : }
267 : :
268 : : // Erase duplicates
269 [ + - ]: 13928 : std::sort(iters.begin(), iters.end(), IteratorComparator());
270 [ + - + - ]: 13928 : iters.erase(std::unique(iters.begin(), iters.end()), iters.end());
271 : :
272 : : // Convert iterators to pair<CTransactionRef, NodeId>
273 : 13928 : std::vector<std::pair<CTransactionRef, NodeId>> children_found;
274 [ + - ]: 13928 : children_found.reserve(iters.size());
275 [ + + ]: 75259 : for (const auto& child_iter : iters) {
276 [ + - ]: 61331 : children_found.emplace_back(child_iter->second.tx, child_iter->second.fromPeer);
277 : 61331 : }
278 : 13928 : return children_found;
279 [ + - ]: 13928 : }
|