Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2020 The Bitcoin Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : #include <txmempool.h>
7 :
8 : #include <consensus/consensus.h>
9 : #include <consensus/tx_verify.h>
10 : #include <consensus/validation.h>
11 : #include <optional.h>
12 : #include <validation.h>
13 : #include <policy/policy.h>
14 : #include <policy/fees.h>
15 : #include <policy/settings.h>
16 : #include <reverse_iterator.h>
17 : #include <util/system.h>
18 : #include <util/moneystr.h>
19 : #include <util/time.h>
20 : #include <validationinterface.h>
21 :
22 113540 : CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
23 : int64_t _nTime, unsigned int _entryHeight,
24 : bool _spendsCoinbase, int64_t _sigOpsCost, LockPoints lp)
25 113540 : : tx(_tx), nFee(_nFee), nTxWeight(GetTransactionWeight(*tx)), nUsageSize(RecursiveDynamicUsage(tx)), nTime(_nTime), entryHeight(_entryHeight),
26 56770 : spendsCoinbase(_spendsCoinbase), sigOpCost(_sigOpsCost), lockPoints(lp), m_epoch(0)
27 56770 : {
28 56770 : nCountWithDescendants = 1;
29 56770 : nSizeWithDescendants = GetTxSize();
30 56770 : nModFeesWithDescendants = nFee;
31 :
32 56770 : feeDelta = 0;
33 :
34 56770 : nCountWithAncestors = 1;
35 56770 : nSizeWithAncestors = GetTxSize();
36 56770 : nModFeesWithAncestors = nFee;
37 56770 : nSigOpCostWithAncestors = sigOpCost;
38 113540 : }
39 :
40 17 : void CTxMemPoolEntry::UpdateFeeDelta(int64_t newFeeDelta)
41 : {
42 17 : nModFeesWithDescendants += newFeeDelta - feeDelta;
43 17 : nModFeesWithAncestors += newFeeDelta - feeDelta;
44 17 : feeDelta = newFeeDelta;
45 17 : }
46 :
47 2 : void CTxMemPoolEntry::UpdateLockPoints(const LockPoints& lp)
48 : {
49 2 : lockPoints = lp;
50 2 : }
51 :
52 208142926 : size_t CTxMemPoolEntry::GetTxSize() const
53 : {
54 208142926 : return GetVirtualTransactionSize(nTxWeight, sigOpCost);
55 : }
56 :
57 : // Update the given tx for any in-mempool descendants.
58 : // Assumes that CTxMemPool::m_children is correct for the given tx and all
59 : // descendants.
60 282 : void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set<uint256> &setExclude)
61 : {
62 282 : CTxMemPoolEntry::Children stageEntries, descendants;
63 282 : stageEntries = updateIt->GetMemPoolChildrenConst();
64 :
65 5307 : while (!stageEntries.empty()) {
66 5025 : const CTxMemPoolEntry& descendant = *stageEntries.begin();
67 5025 : descendants.insert(descendant);
68 5025 : stageEntries.erase(descendant);
69 5025 : const CTxMemPoolEntry::Children& children = descendant.GetMemPoolChildrenConst();
70 164735 : for (const CTxMemPoolEntry& childEntry : children) {
71 159710 : cacheMap::iterator cacheIt = cachedDescendants.find(mapTx.iterator_to(childEntry));
72 159710 : if (cacheIt != cachedDescendants.end()) {
73 : // We've already calculated this one, just add the entries for this set
74 : // but don't traverse again.
75 351910 : for (txiter cacheEntry : cacheIt->second) {
76 345005 : descendants.insert(*cacheEntry);
77 345005 : }
78 159710 : } else if (!descendants.count(childEntry)) {
79 : // Schedule for later processing
80 25170 : stageEntries.insert(childEntry);
81 25170 : }
82 159710 : }
83 0 : }
84 : // descendants now contains all in-mempool descendants of updateIt.
85 : // Update and add to cached descendant map
86 5312 : int64_t modifySize = 0;
87 5312 : CAmount modifyFee = 0;
88 5312 : int64_t modifyCount = 0;
89 5312 : for (const CTxMemPoolEntry& descendant : descendants) {
90 5030 : if (!setExclude.count(descendant.GetTx().GetHash())) {
91 3767 : modifySize += descendant.GetTxSize();
92 3767 : modifyFee += descendant.GetModifiedFee();
93 3767 : modifyCount++;
94 3767 : cachedDescendants[updateIt].insert(mapTx.iterator_to(descendant));
95 : // Update ancestor state for each descendant
96 3767 : mapTx.modify(mapTx.iterator_to(descendant), update_ancestor_state(updateIt->GetTxSize(), updateIt->GetModifiedFee(), 1, updateIt->GetSigOpCost()));
97 : }
98 0 : }
99 282 : mapTx.modify(updateIt, update_descendant_state(modifySize, modifyFee, modifyCount));
100 282 : }
101 :
102 : // vHashesToUpdate is the set of transaction hashes from a disconnected block
103 : // which has been re-added to the mempool.
104 : // for each entry, look for descendants that are outside vHashesToUpdate, and
105 : // add fee/size information for such descendants to the parent.
106 : // for each such descendant, also update the ancestor state to include the parent.
107 488 : void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate)
108 : {
109 488 : AssertLockHeld(cs);
110 : // For each entry in vHashesToUpdate, store the set of in-mempool, but not
111 : // in-vHashesToUpdate transactions, so that we don't have to recalculate
112 : // descendants when we come across a previously seen entry.
113 488 : cacheMap mapMemPoolDescendantsToUpdate;
114 :
115 : // Use a set for lookups into vHashesToUpdate (these entries are already
116 : // accounted for in the state of their ancestors)
117 488 : std::set<uint256> setAlreadyIncluded(vHashesToUpdate.begin(), vHashesToUpdate.end());
118 :
119 : // Iterate in reverse, so that whenever we are looking at a transaction
120 : // we are sure that all in-mempool descendants have already been processed.
121 : // This maximizes the benefit of the descendant cache and guarantees that
122 : // CTxMemPool::m_children will be updated, an assumption made in
123 : // UpdateForDescendants.
124 770 : for (const uint256 &hash : reverse_iterate(vHashesToUpdate)) {
125 : // calculate children from mapNextTx
126 282 : txiter it = mapTx.find(hash);
127 282 : if (it == mapTx.end()) {
128 0 : continue;
129 : }
130 282 : auto iter = mapNextTx.lower_bound(COutPoint(hash, 0));
131 : // First calculate the children, and update CTxMemPool::m_children to
132 : // include them, and update their CTxMemPoolEntry::m_parents to include this tx.
133 : // we cache the in-mempool children to avoid duplicate updates
134 : {
135 282 : const auto epoch = GetFreshEpoch();
136 5006 : for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) {
137 4724 : const uint256 &childHash = iter->second->GetHash();
138 4724 : txiter childIter = mapTx.find(childHash);
139 4724 : assert(childIter != mapTx.end());
140 : // We can skip updating entries we've encountered before or that
141 : // are in the block (which are already accounted for).
142 4724 : if (!visited(childIter) && !setAlreadyIncluded.count(childHash)) {
143 3761 : UpdateChild(it, childIter, true);
144 3761 : UpdateParent(childIter, it, true);
145 : }
146 4724 : }
147 282 : } // release epoch guard for UpdateForDescendants
148 282 : UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
149 282 : }
150 488 : }
151 :
152 5285346 : bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents /* = true */) const
153 : {
154 5285346 : CTxMemPoolEntry::Parents staged_ancestors;
155 5285346 : const CTransaction &tx = entry.GetTx();
156 :
157 5285346 : if (fSearchForParents) {
158 : // Get parents of this transaction that are in the mempool
159 : // GetMemPoolParents() is only valid for entries in the mempool, so we
160 : // iterate mapTx to find parents.
161 12080400 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
162 6910710 : Optional<txiter> piter = GetIter(tx.vin[i].prevout.hash);
163 6910710 : if (piter) {
164 119376 : staged_ancestors.insert(**piter);
165 119376 : if (staged_ancestors.size() + 1 > limitAncestorCount) {
166 1 : errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
167 1 : return false;
168 : }
169 : }
170 6910710 : }
171 : } else {
172 : // If we're not searching for parents, we require this to be an
173 : // entry in the mempool already.
174 115655 : txiter it = mapTx.iterator_to(entry);
175 115655 : staged_ancestors = it->GetMemPoolParentsConst();
176 115655 : }
177 :
178 5285345 : size_t totalSizeWithAncestors = entry.GetTxSize();
179 :
180 10359758 : while (!staged_ancestors.empty()) {
181 5074524 : const CTxMemPoolEntry& stage = staged_ancestors.begin()->get();
182 5074524 : txiter stageit = mapTx.iterator_to(stage);
183 :
184 5074524 : setAncestors.insert(stageit);
185 5074524 : staged_ancestors.erase(stage);
186 5074524 : totalSizeWithAncestors += stageit->GetTxSize();
187 :
188 5074524 : if (stageit->GetSizeWithDescendants() + entry.GetTxSize() > limitDescendantSize) {
189 19 : errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
190 19 : return false;
191 5074505 : } else if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) {
192 44 : errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
193 44 : return false;
194 5074461 : } else if (totalSizeWithAncestors > limitAncestorSize) {
195 0 : errString = strprintf("exceeds ancestor size limit [limit: %u]", limitAncestorSize);
196 0 : return false;
197 : }
198 :
199 5074461 : const CTxMemPoolEntry::Parents& parents = stageit->GetMemPoolParentsConst();
200 80858876 : for (const CTxMemPoolEntry& parent : parents) {
201 75784415 : txiter parent_it = mapTx.iterator_to(parent);
202 :
203 : // If this is a new ancestor, add it.
204 75784415 : if (setAncestors.count(parent_it) == 0) {
205 38578520 : staged_ancestors.insert(parent);
206 38578520 : }
207 75784415 : if (staged_ancestors.size() + setAncestors.size() + 1 > limitAncestorCount) {
208 48 : errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
209 48 : return false;
210 : }
211 75784415 : }
212 10148937 : }
213 :
214 5285234 : return true;
215 5285346 : }
216 :
217 109301 : void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
218 : {
219 109301 : CTxMemPoolEntry::Parents parents = it->GetMemPoolParents();
220 : // add or remove this tx as a child of each parent
221 209077 : for (const CTxMemPoolEntry& parent : parents) {
222 99776 : UpdateChild(mapTx.iterator_to(parent), it, add);
223 0 : }
224 109301 : const int64_t updateCount = (add ? 1 : -1);
225 109301 : const int64_t updateSize = updateCount * it->GetTxSize();
226 109301 : const CAmount updateFee = updateCount * it->GetModifiedFee();
227 3167199 : for (txiter ancestorIt : setAncestors) {
228 3057898 : mapTx.modify(ancestorIt, update_descendant_state(updateSize, updateFee, updateCount));
229 0 : }
230 109301 : }
231 :
232 56614 : void CTxMemPool::UpdateEntryForAncestors(txiter it, const setEntries &setAncestors)
233 : {
234 56614 : int64_t updateCount = setAncestors.size();
235 : int64_t updateSize = 0;
236 : CAmount updateFee = 0;
237 : int64_t updateSigOpsCost = 0;
238 2092970 : for (txiter ancestorIt : setAncestors) {
239 2036356 : updateSize += ancestorIt->GetTxSize();
240 2036356 : updateFee += ancestorIt->GetModifiedFee();
241 2036356 : updateSigOpsCost += ancestorIt->GetSigOpCost();
242 2036356 : }
243 56614 : mapTx.modify(it, update_ancestor_state(updateSize, updateFee, updateCount, updateSigOpsCost));
244 56614 : }
245 :
246 52687 : void CTxMemPool::UpdateChildrenForRemoval(txiter it)
247 : {
248 52687 : const CTxMemPoolEntry::Children& children = it->GetMemPoolChildrenConst();
249 54241 : for (const CTxMemPoolEntry& updateIt : children) {
250 1554 : UpdateParent(mapTx.iterator_to(updateIt), it, false);
251 : }
252 52687 : }
253 :
254 93761 : void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants)
255 : {
256 : // For each entry, walk back all ancestors and decrement size associated with this
257 : // transaction
258 : const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
259 93761 : if (updateDescendants) {
260 : // updateDescendants should be true whenever we're not recursively
261 : // removing a tx and all its descendants, eg when a transaction is
262 : // confirmed in a block.
263 : // Here we only update statistics and not data in CTxMemPool::Parents
264 : // and CTxMemPoolEntry::Children (which we need to preserve until we're
265 : // finished with all operations that need to traverse the mempool).
266 84344 : for (txiter removeIt : entriesToRemove) {
267 42172 : setEntries setDescendants;
268 42172 : CalculateDescendants(removeIt, setDescendants);
269 42172 : setDescendants.erase(removeIt); // don't update state for self
270 42172 : int64_t modifySize = -((int64_t)removeIt->GetTxSize());
271 42172 : CAmount modifyFee = -removeIt->GetModifiedFee();
272 42172 : int modifySigOps = -removeIt->GetSigOpCost();
273 46177 : for (txiter dit : setDescendants) {
274 4005 : mapTx.modify(dit, update_ancestor_state(modifySize, modifyFee, -1, modifySigOps));
275 0 : }
276 42172 : }
277 42172 : }
278 146448 : for (txiter removeIt : entriesToRemove) {
279 52687 : setEntries setAncestors;
280 52687 : const CTxMemPoolEntry &entry = *removeIt;
281 52687 : std::string dummy;
282 : // Since this is a tx that is already in the mempool, we can call CMPA
283 : // with fSearchForParents = false. If the mempool is in a consistent
284 : // state, then using true or false should both be correct, though false
285 : // should be a bit faster.
286 : // However, if we happen to be in the middle of processing a reorg, then
287 : // the mempool can be in an inconsistent state. In this case, the set
288 : // of ancestors reachable via GetMemPoolParents()/GetMemPoolChildren()
289 : // will be the same as the set of ancestors whose packages include this
290 : // transaction, because when we add a new transaction to the mempool in
291 : // addUnchecked(), we assume it has no children, and in the case of a
292 : // reorg where that assumption is false, the in-mempool children aren't
293 : // linked to the in-block tx's until UpdateTransactionsFromBlock() is
294 : // called.
295 : // So if we're being called during a reorg, ie before
296 : // UpdateTransactionsFromBlock() has been called, then
297 : // GetMemPoolParents()/GetMemPoolChildren() will differ from the set of
298 : // mempool parents we'd calculate by searching, and it's important that
299 : // we use the cached notion of ancestor transactions as the set of
300 : // things to update for removal.
301 52687 : CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
302 : // Note that UpdateAncestorsOf severs the child links that point to
303 : // removeIt in the entries for the parents of removeIt.
304 52687 : UpdateAncestorsOf(false, removeIt, setAncestors);
305 52687 : }
306 : // After updating all the ancestor sizes, we can now sever the link between each
307 : // transaction being removed and any mempool children (ie, update CTxMemPoolEntry::m_parents
308 : // for each direct child of a transaction being removed).
309 146448 : for (txiter removeIt : entriesToRemove) {
310 52687 : UpdateChildrenForRemoval(removeIt);
311 : }
312 93761 : }
313 :
314 3058205 : void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
315 : {
316 3058205 : nSizeWithDescendants += modifySize;
317 3058205 : assert(int64_t(nSizeWithDescendants) > 0);
318 3058205 : nModFeesWithDescendants += modifyFee;
319 3058205 : nCountWithDescendants += modifyCount;
320 3058205 : assert(int64_t(nCountWithDescendants) > 0);
321 3058205 : }
322 :
323 64434 : void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
324 : {
325 64434 : nSizeWithAncestors += modifySize;
326 64434 : assert(int64_t(nSizeWithAncestors) > 0);
327 64434 : nModFeesWithAncestors += modifyFee;
328 64434 : nCountWithAncestors += modifyCount;
329 64434 : assert(int64_t(nCountWithAncestors) > 0);
330 64434 : nSigOpCostWithAncestors += modifySigOps;
331 64434 : assert(int(nSigOpCostWithAncestors) >= 0);
332 64434 : }
333 :
334 5970 : CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator)
335 2985 : : nTransactionsUpdated(0), minerPolicyEstimator(estimator), m_epoch(0), m_has_epoch_guard(false)
336 2985 : {
337 2985 : _clear(); //lock free clear
338 :
339 : // Sanity checks off by default for performance, because otherwise
340 : // accepting transactions becomes O(N^2) where N is the number
341 : // of transactions in the pool
342 2985 : nCheckFrequency = 0;
343 5970 : }
344 :
345 4987 : bool CTxMemPool::isSpent(const COutPoint& outpoint) const
346 : {
347 4987 : LOCK(cs);
348 4987 : return mapNextTx.count(outpoint);
349 4987 : }
350 :
351 19 : unsigned int CTxMemPool::GetTransactionsUpdated() const
352 : {
353 19 : return nTransactionsUpdated;
354 : }
355 :
356 50335 : void CTxMemPool::AddTransactionsUpdated(unsigned int n)
357 : {
358 50335 : nTransactionsUpdated += n;
359 50335 : }
360 :
361 56614 : void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate)
362 : {
363 : // Add to memory pool without checking anything.
364 : // Used by AcceptToMemoryPool(), which DOES do
365 : // all the appropriate checks.
366 56614 : indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
367 :
368 : // Update transaction for any feeDelta created by PrioritiseTransaction
369 : // TODO: refactor so that the fee delta is calculated before inserting
370 : // into mapTx.
371 56614 : CAmount delta{0};
372 56614 : ApplyDelta(entry.GetTx().GetHash(), delta);
373 56614 : if (delta) {
374 8 : mapTx.modify(newit, update_fee_delta(delta));
375 8 : }
376 :
377 : // Update cachedInnerUsage to include contained transaction's usage.
378 : // (When we update the entry for in-mempool parents, memory usage will be
379 : // further updated.)
380 56614 : cachedInnerUsage += entry.DynamicMemoryUsage();
381 :
382 56614 : const CTransaction& tx = newit->GetTx();
383 56614 : std::set<uint256> setParentTransactions;
384 232882 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
385 176268 : mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, &tx));
386 176268 : setParentTransactions.insert(tx.vin[i].prevout.hash);
387 : }
388 : // Don't bother worrying about child transactions of this one.
389 : // Normal case of a new transaction arriving is that there can't be any
390 : // children, because such children would be orphans.
391 : // An exception to that is if a transaction enters that used to be in a block.
392 : // In that case, our disconnect block logic will call UpdateTransactionsFromBlock
393 : // to clean up the mess we're leaving here.
394 :
395 : // Update ancestors with information about this tx
396 108998 : for (const auto& pit : GetIterSet(setParentTransactions)) {
397 52384 : UpdateParent(newit, pit, true);
398 0 : }
399 56614 : UpdateAncestorsOf(true, newit, setAncestors);
400 56614 : UpdateEntryForAncestors(newit, setAncestors);
401 :
402 56614 : nTransactionsUpdated++;
403 56614 : totalTxSize += entry.GetTxSize();
404 56614 : if (minerPolicyEstimator) {minerPolicyEstimator->processTransaction(entry, validFeeEstimate);}
405 :
406 56614 : vTxHashes.emplace_back(tx.GetWitnessHash(), newit);
407 56614 : newit->vTxHashesIdx = vTxHashes.size() - 1;
408 56614 : }
409 :
410 52687 : void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
411 : {
412 52687 : if (reason != MemPoolRemovalReason::BLOCK) {
413 : // Notify clients that a transaction has been removed from the mempool
414 : // for any reason except being included in a block. Clients interested
415 : // in transactions included in blocks can subscribe to the BlockConnected
416 : // notification.
417 10515 : GetMainSignals().TransactionRemovedFromMempool(it->GetSharedTx(), reason);
418 10515 : }
419 :
420 52687 : const uint256 hash = it->GetTx().GetHash();
421 219899 : for (const CTxIn& txin : it->GetTx().vin)
422 167212 : mapNextTx.erase(txin.prevout);
423 :
424 52687 : RemoveUnbroadcastTx(hash, true /* add logging because unchecked */ );
425 :
426 52687 : if (vTxHashes.size() > 1) {
427 51084 : vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
428 51084 : vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
429 51084 : vTxHashes.pop_back();
430 51084 : if (vTxHashes.size() * 2 < vTxHashes.capacity())
431 2821 : vTxHashes.shrink_to_fit();
432 : } else
433 1603 : vTxHashes.clear();
434 :
435 52687 : totalTxSize -= it->GetTxSize();
436 52687 : cachedInnerUsage -= it->DynamicMemoryUsage();
437 52687 : cachedInnerUsage -= memusage::DynamicUsage(it->GetMemPoolParentsConst()) + memusage::DynamicUsage(it->GetMemPoolChildrenConst());
438 52687 : mapTx.erase(it);
439 52687 : nTransactionsUpdated++;
440 52687 : if (minerPolicyEstimator) {minerPolicyEstimator->removeTx(hash, false);}
441 52687 : }
442 :
443 : // Calculates descendants of entry that are not already in setDescendants, and adds to
444 : // setDescendants. Assumes entryit is already a tx in the mempool and CTxMemPoolEntry::m_children
445 : // is correct for tx and all descendants.
446 : // Also assumes that if an entry is in setDescendants already, then all
447 : // in-mempool descendants of it are already in setDescendants as well, so that we
448 : // can save time by not iterating over those entries.
449 58433 : void CTxMemPool::CalculateDescendants(txiter entryit, setEntries& setDescendants) const
450 : {
451 58433 : setEntries stage;
452 58433 : if (setDescendants.count(entryit) == 0) {
453 58432 : stage.insert(entryit);
454 58432 : }
455 : // Traverse down the children of entry, only adding children that are not
456 : // accounted for in setDescendants already (because those children have either
457 : // already been walked, or will be walked in this iteration).
458 1140286 : while (!stage.empty()) {
459 1081853 : txiter it = *stage.begin();
460 1081853 : setDescendants.insert(it);
461 1081853 : stage.erase(it);
462 :
463 1081853 : const CTxMemPoolEntry::Children& children = it->GetMemPoolChildrenConst();
464 2122294 : for (const CTxMemPoolEntry& child : children) {
465 1040441 : txiter childiter = mapTx.iterator_to(child);
466 1040441 : if (!setDescendants.count(childiter)) {
467 1032041 : stage.insert(childiter);
468 1032041 : }
469 1040441 : }
470 1081853 : }
471 58433 : }
472 :
473 9026 : void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReason reason)
474 : {
475 : // Remove transaction from memory pool
476 9026 : AssertLockHeld(cs);
477 9026 : setEntries txToRemove;
478 9026 : txiter origit = mapTx.find(origTx.GetHash());
479 9026 : if (origit != mapTx.end()) {
480 56 : txToRemove.insert(origit);
481 56 : } else {
482 : // When recursively removing but origTx isn't in the mempool
483 : // be sure to remove any children that are in the pool. This can
484 : // happen during chain re-orgs if origTx isn't re-accepted into
485 : // the mempool for any reason.
486 19369 : for (unsigned int i = 0; i < origTx.vout.size(); i++) {
487 10399 : auto it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
488 10399 : if (it == mapNextTx.end())
489 10328 : continue;
490 71 : txiter nextit = mapTx.find(it->second->GetHash());
491 71 : assert(nextit != mapTx.end());
492 71 : txToRemove.insert(nextit);
493 10399 : }
494 : }
495 9026 : setEntries setAllRemoves;
496 9153 : for (txiter it : txToRemove) {
497 127 : CalculateDescendants(it, setAllRemoves);
498 0 : }
499 :
500 9026 : RemoveStaged(setAllRemoves, false, reason);
501 9026 : }
502 :
503 488 : void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
504 : {
505 : // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
506 3473 : AssertLockHeld(cs);
507 488 : setEntries txToRemove;
508 1222 : for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
509 734 : const CTransaction& tx = it->GetTx();
510 734 : LockPoints lp = it->GetLockPoints();
511 734 : bool validLP = TestLockPointValidity(&lp);
512 734 : if (!CheckFinalTx(tx, flags) || !CheckSequenceLocks(*this, tx, flags, &lp, validLP)) {
513 : // Note if CheckSequenceLocks fails the LockPoints may still be invalid
514 : // So it's critical that we remove the tx and not depend on the LockPoints.
515 4 : txToRemove.insert(it);
516 734 : } else if (it->GetSpendsCoinbase()) {
517 132 : for (const CTxIn& txin : tx.vin) {
518 66 : indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
519 66 : if (it2 != mapTx.end())
520 0 : continue;
521 66 : const Coin &coin = pcoins->AccessCoin(txin.prevout);
522 66 : if (nCheckFrequency != 0) assert(!coin.IsSpent());
523 66 : if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
524 6 : txToRemove.insert(it);
525 6 : break;
526 : }
527 126 : }
528 66 : }
529 734 : if (!validLP) {
530 2 : mapTx.modify(it, update_lock_points(lp));
531 : }
532 734 : }
533 488 : setEntries setAllRemoves;
534 498 : for (txiter it : txToRemove) {
535 10 : CalculateDescendants(it, setAllRemoves);
536 0 : }
537 488 : RemoveStaged(setAllRemoves, false, MemPoolRemovalReason::REORG);
538 488 : }
539 :
540 116633 : void CTxMemPool::removeConflicts(const CTransaction &tx)
541 : {
542 : // Remove transactions which depend on inputs of tx, recursively
543 116633 : AssertLockHeld(cs);
544 264055 : for (const CTxIn &txin : tx.vin) {
545 147422 : auto it = mapNextTx.find(txin.prevout);
546 147422 : if (it != mapNextTx.end()) {
547 48 : const CTransaction &txConflict = *it->second;
548 48 : if (txConflict != tx)
549 : {
550 48 : ClearPrioritisation(txConflict.GetHash());
551 48 : removeRecursive(txConflict, MemPoolRemovalReason::CONFLICT);
552 48 : }
553 48 : }
554 147422 : }
555 116633 : }
556 :
557 : /**
558 : * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
559 : */
560 47212 : void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight)
561 : {
562 47212 : AssertLockHeld(cs);
563 47212 : std::vector<const CTxMemPoolEntry*> entries;
564 163845 : for (const auto& tx : vtx)
565 : {
566 116633 : uint256 hash = tx->GetHash();
567 :
568 116633 : indexed_transaction_set::iterator i = mapTx.find(hash);
569 116633 : if (i != mapTx.end())
570 42172 : entries.push_back(&*i);
571 116633 : }
572 : // Before the txs in the new block have been removed from the mempool, update policy estimates
573 47212 : if (minerPolicyEstimator) {minerPolicyEstimator->processBlock(nBlockHeight, entries);}
574 163845 : for (const auto& tx : vtx)
575 : {
576 116633 : txiter it = mapTx.find(tx->GetHash());
577 116633 : if (it != mapTx.end()) {
578 42172 : setEntries stage;
579 42172 : stage.insert(it);
580 42172 : RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK);
581 42172 : }
582 116633 : removeConflicts(*tx);
583 116633 : ClearPrioritisation(tx->GetHash());
584 116633 : }
585 47212 : lastRollingFeeUpdate = GetTime();
586 47212 : blockSinceLastRollingFeeBump = true;
587 47212 : }
588 :
589 3594 : void CTxMemPool::_clear()
590 : {
591 3594 : mapTx.clear();
592 3594 : mapNextTx.clear();
593 3594 : totalTxSize = 0;
594 3594 : cachedInnerUsage = 0;
595 3594 : lastRollingFeeUpdate = GetTime();
596 3594 : blockSinceLastRollingFeeBump = false;
597 3594 : rollingMinimumFeeRate = 0;
598 3594 : ++nTransactionsUpdated;
599 3594 : }
600 :
601 609 : void CTxMemPool::clear()
602 : {
603 609 : LOCK(cs);
604 609 : _clear();
605 609 : }
606 :
607 5112895 : static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& mempoolDuplicate, const int64_t spendheight)
608 : {
609 5112895 : TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass
610 5112895 : CAmount txfee = 0;
611 5112895 : bool fCheckResult = tx.IsCoinBase() || Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee);
612 5112895 : assert(fCheckResult);
613 5112895 : UpdateCoins(tx, mempoolDuplicate, std::numeric_limits<int>::max());
614 5112895 : }
615 :
616 53381 : void CTxMemPool::check(const CCoinsViewCache *pcoins) const
617 : {
618 53381 : LOCK(cs);
619 53381 : if (nCheckFrequency == 0)
620 3 : return;
621 :
622 53378 : if (GetRand(std::numeric_limits<uint32_t>::max()) >= nCheckFrequency)
623 0 : return;
624 :
625 53378 : LogPrint(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
626 :
627 : uint64_t checkTotal = 0;
628 : uint64_t innerUsage = 0;
629 :
630 53378 : CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
631 53378 : const int64_t spendheight = GetSpendHeight(mempoolDuplicate);
632 :
633 53378 : std::list<const CTxMemPoolEntry*> waitingOnDependants;
634 5166273 : for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
635 : unsigned int i = 0;
636 5112895 : checkTotal += it->GetTxSize();
637 5112895 : innerUsage += it->DynamicMemoryUsage();
638 5112895 : const CTransaction& tx = it->GetTx();
639 5112895 : innerUsage += memusage::DynamicUsage(it->GetMemPoolParentsConst()) + memusage::DynamicUsage(it->GetMemPoolChildrenConst());
640 : bool fDependsWait = false;
641 5112895 : CTxMemPoolEntry::Parents setParentCheck;
642 11847026 : for (const CTxIn &txin : tx.vin) {
643 : // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
644 6734131 : indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
645 6734131 : if (it2 != mapTx.end()) {
646 1630 : const CTransaction& tx2 = it2->GetTx();
647 1630 : assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
648 : fDependsWait = true;
649 1630 : setParentCheck.insert(*it2);
650 1630 : } else {
651 6732501 : assert(pcoins->HaveCoin(txin.prevout));
652 : }
653 : // Check whether its inputs are marked in mapNextTx.
654 6734131 : auto it3 = mapNextTx.find(txin.prevout);
655 6734131 : assert(it3 != mapNextTx.end());
656 6734131 : assert(it3->first == &txin.prevout);
657 6734131 : assert(it3->second == &tx);
658 6734131 : i++;
659 6734131 : }
660 3250 : auto comp = [](const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) -> bool {
661 3250 : return a.GetTx().GetHash() == b.GetTx().GetHash();
662 : };
663 5112895 : assert(setParentCheck.size() == it->GetMemPoolParentsConst().size());
664 5112895 : assert(std::equal(setParentCheck.begin(), setParentCheck.end(), it->GetMemPoolParentsConst().begin(), comp));
665 : // Verify ancestor state is correct.
666 5112895 : setEntries setAncestors;
667 5112895 : uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
668 5112895 : std::string dummy;
669 5112895 : CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
670 5112895 : uint64_t nCountCheck = setAncestors.size() + 1;
671 5112895 : uint64_t nSizeCheck = it->GetTxSize();
672 5112895 : CAmount nFeesCheck = it->GetModifiedFee();
673 5112895 : int64_t nSigOpCheck = it->GetSigOpCost();
674 :
675 5114829 : for (txiter ancestorIt : setAncestors) {
676 1934 : nSizeCheck += ancestorIt->GetTxSize();
677 1934 : nFeesCheck += ancestorIt->GetModifiedFee();
678 1934 : nSigOpCheck += ancestorIt->GetSigOpCost();
679 1934 : }
680 :
681 5112895 : assert(it->GetCountWithAncestors() == nCountCheck);
682 5112895 : assert(it->GetSizeWithAncestors() == nSizeCheck);
683 5112895 : assert(it->GetSigOpCostWithAncestors() == nSigOpCheck);
684 5112895 : assert(it->GetModFeesWithAncestors() == nFeesCheck);
685 :
686 : // Check children against mapNextTx
687 5112895 : CTxMemPoolEntry::Children setChildrenCheck;
688 5112895 : auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0));
689 : uint64_t child_sizes = 0;
690 5114525 : for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) {
691 1630 : txiter childit = mapTx.find(iter->second->GetHash());
692 1630 : assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions
693 1630 : if (setChildrenCheck.insert(*childit).second) {
694 1625 : child_sizes += childit->GetTxSize();
695 1625 : }
696 1630 : }
697 5112895 : assert(setChildrenCheck.size() == it->GetMemPoolChildrenConst().size());
698 5112895 : assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(), it->GetMemPoolChildrenConst().begin(), comp));
699 : // Also check to make sure size is greater than sum with immediate children.
700 : // just a sanity check, not definitive that this calc is correct...
701 5112895 : assert(it->GetSizeWithDescendants() >= child_sizes + it->GetTxSize());
702 :
703 5112895 : if (fDependsWait)
704 1625 : waitingOnDependants.push_back(&(*it));
705 : else {
706 5111270 : CheckInputsAndUpdateCoins(tx, mempoolDuplicate, spendheight);
707 : }
708 5112895 : }
709 : unsigned int stepsSinceLastRemove = 0;
710 55072 : while (!waitingOnDependants.empty()) {
711 1694 : const CTxMemPoolEntry* entry = waitingOnDependants.front();
712 1694 : waitingOnDependants.pop_front();
713 1694 : if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
714 69 : waitingOnDependants.push_back(entry);
715 69 : stepsSinceLastRemove++;
716 69 : assert(stepsSinceLastRemove < waitingOnDependants.size());
717 : } else {
718 1625 : CheckInputsAndUpdateCoins(entry->GetTx(), mempoolDuplicate, spendheight);
719 : stepsSinceLastRemove = 0;
720 : }
721 1694 : }
722 6787509 : for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
723 6734131 : uint256 hash = it->second->GetHash();
724 6734131 : indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
725 6734131 : const CTransaction& tx = it2->GetTx();
726 6734131 : assert(it2 != mapTx.end());
727 6734131 : assert(&tx == it->second);
728 6734131 : }
729 :
730 53378 : assert(totalTxSize == checkTotal);
731 53378 : assert(innerUsage == cachedInnerUsage);
732 53381 : }
733 :
734 64099 : bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid)
735 : {
736 64099 : LOCK(cs);
737 64099 : indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha);
738 64099 : if (i == mapTx.end()) return false;
739 46727 : indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb);
740 46727 : if (j == mapTx.end()) return true;
741 45945 : uint64_t counta = i->GetCountWithAncestors();
742 45945 : uint64_t countb = j->GetCountWithAncestors();
743 45945 : if (counta == countb) {
744 45858 : return CompareTxMemPoolEntryByScore()(*i, *j);
745 : }
746 87 : return counta < countb;
747 64099 : }
748 :
749 : namespace {
750 : class DepthAndScoreComparator
751 : {
752 : public:
753 14170031 : bool operator()(const CTxMemPool::indexed_transaction_set::const_iterator& a, const CTxMemPool::indexed_transaction_set::const_iterator& b)
754 : {
755 14170031 : uint64_t counta = a->GetCountWithAncestors();
756 14170031 : uint64_t countb = b->GetCountWithAncestors();
757 14170031 : if (counta == countb) {
758 14165916 : return CompareTxMemPoolEntryByScore()(*a, *b);
759 : }
760 4115 : return counta < countb;
761 14170031 : }
762 : };
763 : } // namespace
764 :
765 5155 : std::vector<CTxMemPool::indexed_transaction_set::const_iterator> CTxMemPool::GetSortedDepthAndScore() const
766 : {
767 5155 : std::vector<indexed_transaction_set::const_iterator> iters;
768 5155 : AssertLockHeld(cs);
769 :
770 5155 : iters.reserve(mapTx.size());
771 :
772 1214388 : for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) {
773 1209233 : iters.push_back(mi);
774 : }
775 5155 : std::sort(iters.begin(), iters.end(), DepthAndScoreComparator());
776 : return iters;
777 5155 : }
778 :
779 4662 : void CTxMemPool::queryHashes(std::vector<uint256>& vtxid) const
780 : {
781 4662 : LOCK(cs);
782 4662 : auto iters = GetSortedDepthAndScore();
783 :
784 4662 : vtxid.clear();
785 4662 : vtxid.reserve(mapTx.size());
786 :
787 1213250 : for (auto it : iters) {
788 1208588 : vtxid.push_back(it->GetTx().GetHash());
789 1208588 : }
790 4662 : }
791 :
792 25058 : static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it) {
793 25058 : return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
794 0 : }
795 :
796 493 : std::vector<TxMempoolInfo> CTxMemPool::infoAll() const
797 : {
798 493 : LOCK(cs);
799 493 : auto iters = GetSortedDepthAndScore();
800 :
801 493 : std::vector<TxMempoolInfo> ret;
802 493 : ret.reserve(mapTx.size());
803 1138 : for (auto it : iters) {
804 645 : ret.push_back(GetInfo(it));
805 : }
806 :
807 : return ret;
808 493 : }
809 :
810 153634 : CTransactionRef CTxMemPool::get(const uint256& hash) const
811 : {
812 153634 : LOCK(cs);
813 153634 : indexed_transaction_set::const_iterator i = mapTx.find(hash);
814 153634 : if (i == mapTx.end())
815 115826 : return nullptr;
816 37808 : return i->GetSharedTx();
817 153634 : }
818 :
819 26902 : TxMempoolInfo CTxMemPool::info(const GenTxid& gtxid) const
820 : {
821 26902 : LOCK(cs);
822 26902 : indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash()));
823 26902 : if (i == mapTx.end())
824 2489 : return TxMempoolInfo();
825 24413 : return GetInfo(i);
826 26902 : }
827 :
828 0 : TxMempoolInfo CTxMemPool::info(const uint256& txid) const { return info(GenTxid{false, txid}); }
829 :
830 17 : void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta)
831 : {
832 : {
833 17 : LOCK(cs);
834 17 : CAmount &delta = mapDeltas[hash];
835 17 : delta += nFeeDelta;
836 17 : txiter it = mapTx.find(hash);
837 17 : if (it != mapTx.end()) {
838 9 : mapTx.modify(it, update_fee_delta(delta));
839 : // Now update all ancestors' modified fees with descendants
840 9 : setEntries setAncestors;
841 9 : uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
842 9 : std::string dummy;
843 9 : CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
844 34 : for (txiter ancestorIt : setAncestors) {
845 25 : mapTx.modify(ancestorIt, update_descendant_state(0, nFeeDelta, 0));
846 0 : }
847 : // Now update all descendants' modified fees with ancestors
848 9 : setEntries setDescendants;
849 9 : CalculateDescendants(it, setDescendants);
850 9 : setDescendants.erase(it);
851 57 : for (txiter descendantIt : setDescendants) {
852 48 : mapTx.modify(descendantIt, update_ancestor_state(0, nFeeDelta, 0, 0));
853 0 : }
854 9 : ++nTransactionsUpdated;
855 9 : }
856 17 : }
857 17 : LogPrintf("PrioritiseTransaction: %s feerate += %s\n", hash.ToString(), FormatMoney(nFeeDelta));
858 17 : }
859 :
860 75587 : void CTxMemPool::ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const
861 : {
862 75587 : AssertLockHeld(cs);
863 75587 : std::map<uint256, CAmount>::const_iterator pos = mapDeltas.find(hash);
864 75587 : if (pos == mapDeltas.end())
865 75571 : return;
866 16 : const CAmount &delta = pos->second;
867 16 : nFeeDelta += delta;
868 75587 : }
869 :
870 116681 : void CTxMemPool::ClearPrioritisation(const uint256& hash)
871 : {
872 116681 : AssertLockHeld(cs);
873 116681 : mapDeltas.erase(hash);
874 116681 : }
875 :
876 44852 : const CTransaction* CTxMemPool::GetConflictTx(const COutPoint& prevout) const
877 : {
878 44852 : const auto it = mapNextTx.find(prevout);
879 44852 : return it == mapNextTx.end() ? nullptr : it->second;
880 44852 : }
881 :
882 7030446 : Optional<CTxMemPool::txiter> CTxMemPool::GetIter(const uint256& txid) const
883 : {
884 7030446 : auto it = mapTx.find(txid);
885 7030446 : if (it != mapTx.end()) return it;
886 6848668 : return Optional<txiter>{};
887 7030446 : }
888 :
889 75570 : CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<uint256>& hashes) const
890 : {
891 75570 : CTxMemPool::setEntries ret;
892 185547 : for (const auto& h : hashes) {
893 109977 : const auto mi = GetIter(h);
894 109977 : if (mi) ret.insert(*mi);
895 109977 : }
896 : return ret;
897 75570 : }
898 :
899 18157 : bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
900 : {
901 47634 : for (unsigned int i = 0; i < tx.vin.size(); i++)
902 30492 : if (exists(tx.vin[i].prevout.hash))
903 1015 : return false;
904 17142 : return true;
905 18157 : }
906 :
907 99068 : CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
908 :
909 93234 : bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
910 : // If an entry in the mempool exists, always return that one, as it's guaranteed to never
911 : // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
912 : // transactions. First checking the underlying cache risks returning a pruned entry instead.
913 93234 : CTransactionRef ptx = mempool.get(outpoint.hash);
914 93234 : if (ptx) {
915 9362 : if (outpoint.n < ptx->vout.size()) {
916 9362 : coin = Coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false);
917 9362 : return true;
918 : } else {
919 0 : return false;
920 : }
921 : }
922 83872 : return base->GetCoin(outpoint, coin);
923 93234 : }
924 :
925 194583 : size_t CTxMemPool::DynamicMemoryUsage() const {
926 194583 : LOCK(cs);
927 : // Estimate the overhead of mapTx to be 15 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
928 194583 : return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 15 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
929 194583 : }
930 :
931 62264 : void CTxMemPool::RemoveUnbroadcastTx(const uint256& txid, const bool unchecked) {
932 62264 : LOCK(cs);
933 :
934 62264 : if (m_unbroadcast_txids.erase(txid))
935 : {
936 8740 : LogPrint(BCLog::MEMPOOL, "Removed %i from set of unbroadcast txns%s\n", txid.GetHex(), (unchecked ? " before confirmation that txn was sent out" : ""));
937 : }
938 62264 : }
939 :
940 93761 : void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) {
941 93761 : AssertLockHeld(cs);
942 93761 : UpdateForRemoveFromMempool(stage, updateDescendants);
943 146448 : for (txiter it : stage) {
944 52687 : removeUnchecked(it, reason);
945 : }
946 93761 : }
947 :
948 19028 : int CTxMemPool::Expire(std::chrono::seconds time)
949 : {
950 19028 : AssertLockHeld(cs);
951 19028 : indexed_transaction_set::index<entry_time>::type::iterator it = mapTx.get<entry_time>().begin();
952 19028 : setEntries toremove;
953 19032 : while (it != mapTx.get<entry_time>().end() && it->GetTime() < time) {
954 4 : toremove.insert(mapTx.project<0>(it));
955 4 : it++;
956 : }
957 19028 : setEntries stage;
958 19032 : for (txiter removeit : toremove) {
959 4 : CalculateDescendants(removeit, stage);
960 0 : }
961 19028 : RemoveStaged(stage, false, MemPoolRemovalReason::EXPIRY);
962 19028 : return stage.size();
963 19028 : }
964 :
965 37785 : void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate)
966 : {
967 37785 : setEntries setAncestors;
968 37785 : uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
969 37785 : std::string dummy;
970 37785 : CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
971 37785 : return addUnchecked(entry, setAncestors, validFeeEstimate);
972 37785 : }
973 :
974 103537 : void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add)
975 : {
976 103537 : AssertLockHeld(cs);
977 103537 : CTxMemPoolEntry::Children s;
978 103537 : if (add && entry->GetMemPoolChildren().insert(*child).second) {
979 56145 : cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
980 103537 : } else if (!add && entry->GetMemPoolChildren().erase(*child)) {
981 47392 : cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
982 47392 : }
983 103537 : }
984 :
985 57699 : void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add)
986 : {
987 57699 : AssertLockHeld(cs);
988 57699 : CTxMemPoolEntry::Parents s;
989 57699 : if (add && entry->GetMemPoolParents().insert(*parent).second) {
990 56145 : cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
991 57699 : } else if (!add && entry->GetMemPoolParents().erase(*parent)) {
992 1554 : cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
993 1554 : }
994 57699 : }
995 :
996 327707 : CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
997 327707 : LOCK(cs);
998 327707 : if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0)
999 327702 : return CFeeRate(llround(rollingMinimumFeeRate));
1000 :
1001 5 : int64_t time = GetTime();
1002 5 : if (time > lastRollingFeeUpdate + 10) {
1003 : double halflife = ROLLING_FEE_HALFLIFE;
1004 5 : if (DynamicMemoryUsage() < sizelimit / 4)
1005 1 : halflife /= 4;
1006 4 : else if (DynamicMemoryUsage() < sizelimit / 2)
1007 1 : halflife /= 2;
1008 :
1009 5 : rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
1010 5 : lastRollingFeeUpdate = time;
1011 :
1012 5 : if (rollingMinimumFeeRate < (double)incrementalRelayFee.GetFeePerK() / 2) {
1013 1 : rollingMinimumFeeRate = 0;
1014 1 : return CFeeRate(0);
1015 : }
1016 4 : }
1017 4 : return std::max(CFeeRate(llround(rollingMinimumFeeRate)), incrementalRelayFee);
1018 327707 : }
1019 :
1020 4222 : void CTxMemPool::trackPackageRemoved(const CFeeRate& rate) {
1021 4222 : AssertLockHeld(cs);
1022 4222 : if (rate.GetFeePerK() > rollingMinimumFeeRate) {
1023 193 : rollingMinimumFeeRate = rate.GetFeePerK();
1024 193 : blockSinceLastRollingFeeBump = false;
1025 193 : }
1026 4222 : }
1027 :
1028 19078 : void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining) {
1029 19078 : AssertLockHeld(cs);
1030 :
1031 19078 : unsigned nTxnRemoved = 0;
1032 19078 : CFeeRate maxFeeRateRemoved(0);
1033 23300 : while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
1034 4222 : indexed_transaction_set::index<descendant_score>::type::iterator it = mapTx.get<descendant_score>().begin();
1035 :
1036 : // We set the new mempool min fee to the feerate of the removed set, plus the
1037 : // "minimum reasonable fee rate" (ie some value under which we consider txn
1038 : // to have 0 fee). This way, we don't allow txn to enter mempool with feerate
1039 : // equal to txn which were removed with no block in between.
1040 4222 : CFeeRate removed(it->GetModFeesWithDescendants(), it->GetSizeWithDescendants());
1041 4222 : removed += incrementalRelayFee;
1042 4222 : trackPackageRemoved(removed);
1043 4222 : maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
1044 :
1045 4222 : setEntries stage;
1046 4222 : CalculateDescendants(mapTx.project<0>(it), stage);
1047 4222 : nTxnRemoved += stage.size();
1048 :
1049 4222 : std::vector<CTransaction> txn;
1050 4222 : if (pvNoSpendsRemaining) {
1051 26 : txn.reserve(stage.size());
1052 52 : for (txiter iter : stage)
1053 26 : txn.push_back(iter->GetTx());
1054 26 : }
1055 4222 : RemoveStaged(stage, false, MemPoolRemovalReason::SIZELIMIT);
1056 4222 : if (pvNoSpendsRemaining) {
1057 52 : for (const CTransaction& tx : txn) {
1058 52 : for (const CTxIn& txin : tx.vin) {
1059 26 : if (exists(txin.prevout.hash)) continue;
1060 26 : pvNoSpendsRemaining->push_back(txin.prevout);
1061 26 : }
1062 : }
1063 26 : }
1064 4222 : }
1065 :
1066 19078 : if (maxFeeRateRemoved > CFeeRate(0)) {
1067 74 : LogPrint(BCLog::MEMPOOL, "Removed %u txn, rolling minimum fee bumped to %s\n", nTxnRemoved, maxFeeRateRemoved.ToString());
1068 : }
1069 19078 : }
1070 :
1071 14273 : uint64_t CTxMemPool::CalculateDescendantMaximum(txiter entry) const {
1072 : // find parent with highest descendant count
1073 14273 : std::vector<txiter> candidates;
1074 14273 : setEntries counted;
1075 14273 : candidates.push_back(entry);
1076 14273 : uint64_t maximum = 0;
1077 29979 : while (candidates.size()) {
1078 15706 : txiter candidate = candidates.back();
1079 15706 : candidates.pop_back();
1080 15706 : if (!counted.insert(candidate).second) continue;
1081 15657 : const CTxMemPoolEntry::Parents& parents = candidate->GetMemPoolParentsConst();
1082 15657 : if (parents.size() == 0) {
1083 14286 : maximum = std::max(maximum, candidate->GetCountWithDescendants());
1084 14286 : } else {
1085 2804 : for (const CTxMemPoolEntry& i : parents) {
1086 1433 : candidates.push_back(mapTx.iterator_to(i));
1087 0 : }
1088 : }
1089 15706 : }
1090 14273 : return maximum;
1091 14273 : }
1092 :
1093 426834 : void CTxMemPool::GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const {
1094 426834 : LOCK(cs);
1095 426834 : auto it = mapTx.find(txid);
1096 426834 : ancestors = descendants = 0;
1097 426834 : if (it != mapTx.end()) {
1098 14273 : ancestors = it->GetCountWithAncestors();
1099 14273 : descendants = CalculateDescendantMaximum(it);
1100 14273 : }
1101 426834 : }
1102 :
1103 1339 : bool CTxMemPool::IsLoaded() const
1104 : {
1105 1339 : LOCK(cs);
1106 1339 : return m_is_loaded;
1107 1339 : }
1108 :
1109 495 : void CTxMemPool::SetIsLoaded(bool loaded)
1110 : {
1111 495 : LOCK(cs);
1112 495 : m_is_loaded = loaded;
1113 495 : }
1114 :
1115 :
1116 282 : CTxMemPool::EpochGuard CTxMemPool::GetFreshEpoch() const
1117 : {
1118 282 : return EpochGuard(*this);
1119 : }
1120 564 : CTxMemPool::EpochGuard::EpochGuard(const CTxMemPool& in) : pool(in)
1121 282 : {
1122 282 : assert(!pool.m_has_epoch_guard);
1123 282 : ++pool.m_epoch;
1124 282 : pool.m_has_epoch_guard = true;
1125 564 : }
1126 :
1127 564 : CTxMemPool::EpochGuard::~EpochGuard()
1128 282 : {
1129 : // prevents stale results being used
1130 282 : ++pool.m_epoch;
1131 282 : pool.m_has_epoch_guard = false;
1132 564 : }
1133 :
1134 100810 : SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|