forked from Bitcoin-ABC/bitcoin-abc
-
Notifications
You must be signed in to change notification settings - Fork 28
/
txmempool.cpp
1400 lines (1232 loc) · 52.3 KB
/
txmempool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <txmempool.h>
#include <algorithm/contains.h>
#include <algorithm/erase_if.h>
#include <chain.h>
#include <chainparams.h> // for GetConsensus.
#include <clientversion.h>
#include <config.h>
#include <consensus/consensus.h>
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <dsproof/dsproof.h>
#include <dsproof/storage.h>
#include <policy/fees.h>
#include <policy/mempool.h>
#include <policy/policy.h>
#include <reverse_iterator.h>
#include <streams.h>
#include <timedata.h>
#include <util/moneystr.h>
#include <util/system.h>
#include <util/time.h>
#include <validation.h>
#include <version.h>
#include <algorithm>
#include <functional>
#include <limits>
#include <optional>
#include <stdexcept>
#include <tuple>
#include <utility>
/// Used in various places in this file to signify "no limit" for CalculateMemPoolAncestors
inline constexpr uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef &_tx, const Amount _nFee,
int64_t _nTime, unsigned int _entryHeight,
bool _spendsCoinbase, int64_t _sigOpCount,
LockPoints lp)
: tx(_tx), nFee(_nFee), nTxSize(tx->GetTotalSize()),
nUsageSize(RecursiveDynamicUsage(tx)), nTime(_nTime),
entryHeight(_entryHeight), spendsCoinbase(_spendsCoinbase),
sigOpCount(_sigOpCount), lockPoints(lp) {
nCountWithDescendants = 1;
nSizeWithDescendants = GetTxSize();
nSigOpCountWithDescendants = sigOpCount;
nModFeesWithDescendants = nFee;
feeDelta = Amount::zero();
}
size_t CTxMemPoolEntry::GetTxVirtualSize() const {
return GetVirtualTransactionSize(nTxSize, sigOpCount);
}
// Remove after tachyon
uint64_t CTxMemPoolEntry::GetVirtualSizeWithDescendants() const {
// note this is distinct from the sum of descendants' individual virtual
// sizes, and may be smaller.
return GetVirtualTransactionSize(nSizeWithDescendants,
nSigOpCountWithDescendants);
}
void CTxMemPoolEntry::UpdateFeeDelta(Amount newFeeDelta) {
nModFeesWithDescendants += newFeeDelta - feeDelta; // Remove after tachyon; this stat is unused after tachyon
feeDelta = newFeeDelta;
}
void CTxMemPoolEntry::UpdateLockPoints(const LockPoints &lp) {
lockPoints = lp;
}
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 {
setEntries parentHashes;
const CTransaction &tx = entry.GetTx();
if (fSearchForParents) {
// Get parents of this transaction that are in the mempool
// GetMemPoolParents() is only valid for entries in the mempool, so we
// iterate mapTx to find parents.
for (const CTxIn &in : tx.vin) {
std::optional<txiter> piter = GetIter(in.prevout.GetTxId());
if (!piter) {
continue;
}
parentHashes.insert(*piter);
if (parentHashes.size() + 1 > limitAncestorCount) {
errString =
strprintf("too many unconfirmed parents [limit: %u]",
limitAncestorCount);
return false;
}
}
} else {
// If we're not searching for parents, we require this to be an entry in
// the mempool already.
txiter it = mapTx.iterator_to(entry);
parentHashes = GetMemPoolParents(it);
}
size_t totalSizeWithAncestors = entry.GetTxSize();
while (!parentHashes.empty()) {
txiter stageit = *parentHashes.begin();
setAncestors.insert(stageit);
parentHashes.erase(parentHashes.begin());
totalSizeWithAncestors += stageit->GetTxSize();
if (stageit->GetSizeWithDescendants() + entry.GetTxSize() >
limitDescendantSize) {
errString = strprintf(
"exceeds descendant size limit for tx %s [limit: %u]",
stageit->GetTx().GetId().ToString(), limitDescendantSize);
return false;
}
if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) {
errString = strprintf("too many descendants for tx %s [limit: %u]",
stageit->GetTx().GetId().ToString(),
limitDescendantCount);
return false;
}
if (totalSizeWithAncestors > limitAncestorSize) {
errString = strprintf("exceeds ancestor size limit [limit: %u]",
limitAncestorSize);
return false;
}
const setEntries &setMemPoolParents = GetMemPoolParents(stageit);
for (txiter phash : setMemPoolParents) {
// If this is a new ancestor, add it.
if (setAncestors.count(phash) == 0) {
parentHashes.insert(phash);
}
if (parentHashes.size() + setAncestors.size() + 1 >
limitAncestorCount) {
errString =
strprintf("too many unconfirmed ancestors [limit: %u]",
limitAncestorCount);
return false;
}
}
}
return true;
}
void CTxMemPool::UpdateParentsOf(bool add, txiter it, const setEntries *setAncestors) {
// add or remove this tx as a child of each parent
for (txiter piter : GetMemPoolParents(it)) {
UpdateChild(piter, it, add);
}
// Remove this after tachyon
if (setAncestors && !tachyonLatched) {
const int64_t updateCount = (add ? 1 : -1);
const int64_t updateSize = updateCount * it->GetTxSize();
const int64_t updateSigOpCount = updateCount * it->GetSigOpCount();
const Amount updateFee = updateCount * it->GetModifiedFee();
for (txiter ancestorIt : *setAncestors) {
mapTx.modify(ancestorIt,
update_descendant_state(updateSize, updateFee, updateCount,
updateSigOpCount));
}
}
}
void CTxMemPool::UpdateChildrenForRemoval(txiter it) {
const setEntries &setMemPoolChildren = GetMemPoolChildren(it);
for (txiter updateIt : setMemPoolChildren) {
UpdateParent(updateIt, it, false);
}
}
void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove) {
if (!tachyonLatched) {
// remove this branch after tachyon
// slow quadratic branch, only for pre-activation compatibility
for (txiter removeIt : entriesToRemove) {
setEntries setAncestors;
const CTxMemPoolEntry &entry = *removeIt;
std::string dummy;
// Since this is a tx that is already in the mempool, we can call CMPA
// with fSearchForParents = false. If the mempool is in a consistent
// state, then using true or false should both be correct, though false
// should be a bit faster.
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit,
nNoLimit, nNoLimit, dummy, false);
// Note that UpdateParentsOf severs the child links that point to
// removeIt in the entries for the parents of removeIt.
UpdateParentsOf(false, removeIt, &setAncestors);
}
} else {
for (txiter removeIt : entriesToRemove) {
// Note that UpdateParentsOf severs the child links that point to
// removeIt in the mapLinks entries for the parents of removeIt.
UpdateParentsOf(false, removeIt);
}
}
// After updating all the parent links, we can now sever the link between
// each transaction being removed and any mempool children (ie, update
// setMemPoolParents for each direct child of a transaction being removed).
for (txiter removeIt : entriesToRemove) {
UpdateChildrenForRemoval(removeIt);
}
}
void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize,
Amount modifyFee,
int64_t modifyCount,
int64_t modifySigOpCount) {
nSizeWithDescendants += modifySize;
assert(int64_t(nSizeWithDescendants) > 0);
nModFeesWithDescendants += modifyFee;
nCountWithDescendants += modifyCount;
assert(int64_t(nCountWithDescendants) > 0);
nSigOpCountWithDescendants += modifySigOpCount;
assert(int64_t(nSigOpCountWithDescendants) >= 0);
}
CTxMemPool::CTxMemPool()
: nTransactionsUpdated(0),
m_dspStorage(std::make_unique<DoubleSpendProofStorage>())
{
// lock free clear
_clear();
// Sanity checks off by default for performance, because otherwise accepting
// transactions becomes O(N^2) where N is the number of transactions in the
// pool
nCheckFrequency = 0;
}
CTxMemPool::~CTxMemPool() {}
bool CTxMemPool::isSpent(const COutPoint &outpoint) const {
LOCK(cs);
return algo::contains(mapNextTx, outpoint);
}
unsigned int CTxMemPool::GetTransactionsUpdated() const {
LOCK(cs);
return nTransactionsUpdated;
}
void CTxMemPool::AddTransactionsUpdated(unsigned int n) {
LOCK(cs);
nTransactionsUpdated += n;
}
void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entryIn, const setEntries &setAncestors) {
CTxMemPoolEntry entry{entryIn};
// get a guaranteed unique id (in case tests re-use the same object)
entry.SetEntryId(nextEntryId++);
// Update transaction for any feeDelta created by PrioritiseTransaction
{
Amount feeDelta = Amount::zero();
ApplyDelta(entry.GetTx().GetId(), feeDelta);
entry.UpdateFeeDelta(feeDelta);
}
NotifyEntryAdded(entry.GetSharedTx());
// Add to memory pool without checking anything.
// Used by AcceptToMemoryPool(), which DOES do all the appropriate checks.
auto [newit, inserted] = mapTx.insert(entry);
// Sanity check: It is a programming error if insertion fails (uniqueness invariants in mapTx are violated, etc)
assert(inserted);
// Sanity check: We should always end up inserting at the end of the entry_id index
assert(&*mapTx.get<entry_id>().rbegin() == &*newit);
mapLinks.try_emplace(newit);
// Update cachedInnerUsage to include contained transaction's usage.
// (When we update the entry for in-mempool parents, memory usage will be
// further updated.)
cachedInnerUsage += entry.DynamicMemoryUsage();
const CTransaction &tx = newit->GetTx();
std::set<TxId> setParentTransactions;
for (const CTxIn &in : tx.vin) {
mapNextTx.emplace(&in.prevout, &tx);
setParentTransactions.insert(in.prevout.GetTxId());
}
// Don't bother worrying about child transactions of this one. It is
// guaranteed that a new transaction arriving will not have any children,
// because such children would be orphans.
// Update ancestors with information about this tx
for (const auto &pit : GetIterSet(setParentTransactions)) {
UpdateParent(newit, pit, true);
}
UpdateParentsOf(true, newit, tachyonLatched ? nullptr : &setAncestors);
nTransactionsUpdated++;
totalTxSize += entry.GetTxSize();
vTxHashes.emplace_back(tx.GetHash(), newit);
newit->vTxHashesIdx = vTxHashes.size() - 1;
}
void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason) {
NotifyEntryRemoved(it->GetSharedTx(), reason);
if (it->HasDsp()) {
// we put known dsproofs back into the orphan pool just in case there is
// a reorg in the future and this deleted tx comes back.
m_dspStorage->orphanExisting(it->GetDspId());
}
for (const CTxIn &txin : it->GetTx().vin) {
mapNextTx.erase(txin.prevout);
}
if (vTxHashes.size() > 1) {
vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
vTxHashes.pop_back();
if (vTxHashes.size() * 2 < vTxHashes.capacity()) {
vTxHashes.shrink_to_fit();
}
} else {
vTxHashes.clear();
}
totalTxSize -= it->GetTxSize();
cachedInnerUsage -= it->DynamicMemoryUsage();
if (const auto linksiter = mapLinks.find(it); linksiter != mapLinks.end()) {
cachedInnerUsage -= memusage::DynamicUsage(linksiter->second.parents) +
memusage::DynamicUsage(linksiter->second.children);
mapLinks.erase(linksiter);
}
mapTx.erase(it);
nTransactionsUpdated++;
}
// Calculates descendants of entry that are not already in setDescendants, and
// adds to setDescendants. Assumes entryit is already a tx in the mempool and
// setMemPoolChildren is correct for tx and all descendants. Also assumes that
// if an entry is in setDescendants already, then all in-mempool descendants of
// it are already in setDescendants as well, so that we can save time by not
// iterating over those entries.
void CTxMemPool::CalculateDescendants(txiter entryit,
setEntries &setDescendants) const {
setEntries stage;
if (setDescendants.count(entryit) == 0) {
stage.insert(entryit);
}
// Traverse down the children of entry, only adding children that are not
// accounted for in setDescendants already (because those children have
// either already been walked, or will be walked in this iteration).
while (!stage.empty()) {
txiter it = *stage.begin();
setDescendants.insert(it);
stage.erase(stage.begin());
const setEntries &setChildren = GetMemPoolChildren(it);
for (txiter childiter : setChildren) {
if (!algo::contains(setDescendants, childiter)) {
stage.insert(childiter);
}
}
}
}
void CTxMemPool::removeRecursive(const CTransaction &origTx,
MemPoolRemovalReason reason) {
// Remove transaction from memory pool.
LOCK(cs);
setEntries txToRemove;
txiter origit = mapTx.find(origTx.GetId());
if (origit != mapTx.end()) {
txToRemove.insert(origit);
} else {
// When recursively removing but origTx isn't in the mempool be sure to
// remove any children that are in the pool. This can happen during
// chain re-orgs if origTx isn't re-accepted into the mempool for any
// reason.
auto it = mapNextTx.lower_bound(COutPoint(origTx.GetId(), 0));
while (it != mapNextTx.end() && it->first->GetTxId() == origTx.GetId()) {
txiter nextit = mapTx.find(it->second->GetId());
assert(nextit != mapTx.end());
txToRemove.insert(nextit);
++it;
}
}
setEntries setAllRemoves;
for (txiter it : txToRemove) {
CalculateDescendants(it, setAllRemoves);
}
RemoveStaged(setAllRemoves, reason);
}
void CTxMemPool::removeConflicts(const CTransaction &tx) {
// Remove transactions which depend on inputs of tx, recursively
AssertLockHeld(cs);
for (const CTxIn &txin : tx.vin) {
auto it = mapNextTx.find(txin.prevout);
if (it != mapNextTx.end()) {
const CTransaction &txConflict = *it->second;
if (txConflict != tx) {
ClearPrioritisation(txConflict.GetId());
removeRecursive(txConflict, MemPoolRemovalReason::CONFLICT);
}
}
}
}
/**
* Called when a block is connected. Removes from mempool and updates the miner
* fee estimator.
*/
void CTxMemPool::removeForBlock(const std::vector<CTransactionRef> &vtx) {
LOCK(cs);
if (mapTx.empty() && mapDeltas.empty()) {
// fast-path for IBD and/or when mempool is empty; there is no need to
// do any of the set-up work below which eats precious cycles.
return;
}
DisconnectedBlockTransactions disconnectpool;
disconnectpool.addForBlock(vtx);
// iterate in topological order (parents before children)
for (const CTransactionRef &tx : reverse_iterate(disconnectpool.GetQueuedTx().get<insertion_order>())) {
const txiter it = mapTx.find(tx->GetId());
if (it != mapTx.end()) {
setEntries stage;
stage.insert(it);
RemoveStaged(stage, MemPoolRemovalReason::BLOCK);
} else {
removeConflicts(*tx);
}
}
// clear prioritisations (mapDeltas); optmized for the common case where
// mapDeltas is empty or much smaller than block.vtx
algo::erase_if(mapDeltas, [&disconnectpool](const auto &kv) {
return algo::contains(disconnectpool.GetQueuedTx(), kv.first);
});
lastRollingFeeUpdate = GetTime();
blockSinceLastRollingFeeBump = true;
disconnectpool.clear();
}
void CTxMemPool::_clear(bool clearDspOrphans /*= true*/) {
mapLinks.clear();
mapTx.clear();
mapNextTx.clear();
vTxHashes.clear();
totalTxSize = 0;
cachedInnerUsage = 0;
lastRollingFeeUpdate = GetTime();
blockSinceLastRollingFeeBump = false;
rollingMinimumFeeRate = 0;
m_dspStorage->clear(clearDspOrphans);
++nTransactionsUpdated;
}
void CTxMemPool::clear(bool clearDspOrphans /*= true*/) {
LOCK(cs);
_clear(clearDspOrphans);
}
static void CheckInputsAndUpdateCoins(const CTransaction &tx,
CCoinsViewCache &mempoolDuplicate,
const int64_t spendheight) {
CValidationState state;
Amount txfee = Amount::zero();
bool fCheckResult =
tx.IsCoinBase() || Consensus::CheckTxInputs(tx, state, mempoolDuplicate,
spendheight, txfee);
assert(fCheckResult);
UpdateCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max());
}
void CTxMemPool::check(const CCoinsViewCache *pcoins) const {
LOCK(cs);
if (nCheckFrequency == 0) {
return;
}
if (GetRand(std::numeric_limits<uint32_t>::max()) >= nCheckFrequency) {
return;
}
LogPrint(BCLog::MEMPOOL,
"Checking mempool with %u transactions and %u inputs\n",
(unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
uint64_t checkTotal = 0;
uint64_t innerUsage = 0;
CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache *>(pcoins));
const int64_t spendheight = GetSpendHeight(mempoolDuplicate);
std::list<const CTxMemPoolEntry *> waitingOnDependants;
for (txiter it = mapTx.begin(); it != mapTx.end(); ++it) {
unsigned int i = 0;
checkTotal += it->GetTxSize();
innerUsage += it->DynamicMemoryUsage();
const CTransaction &tx = it->GetTx();
auto linksiter = mapLinks.find(it);
assert(linksiter != mapLinks.end());
const TxLinks &links = linksiter->second;
innerUsage += memusage::DynamicUsage(links.parents) +
memusage::DynamicUsage(links.children);
bool fDependsWait = false;
setEntries setParentCheck;
for (const CTxIn &txin : tx.vin) {
// Check that every mempool transaction's inputs refer to available
// coins, or other mempool tx's.
txiter it2 = mapTx.find(txin.prevout.GetTxId());
if (it2 != mapTx.end()) {
const CTransaction &tx2 = it2->GetTx();
assert(tx2.vout.size() > txin.prevout.GetN() &&
!tx2.vout[txin.prevout.GetN()].IsNull());
fDependsWait = true;
setParentCheck.insert(it2);
// also check that parents have a topological ordering before their children
assert(it2->GetEntryId() < it->GetEntryId());
} else {
assert(pcoins->HaveCoin(txin.prevout));
}
// Check whether its inputs are marked in mapNextTx.
auto it3 = mapNextTx.find(txin.prevout);
assert(it3 != mapNextTx.end());
assert(it3->first == &txin.prevout);
assert(it3->second == &tx);
i++;
}
assert(setParentCheck == GetMemPoolParents(it));
// Verify ancestor state is correct.
setEntries setAncestors;
std::string dummy;
const bool ok = CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
assert(ok);
// all ancestors should have entryId < this tx's entryId
for (const auto &ancestor : setAncestors)
assert(ancestor->GetEntryId() < it->GetEntryId());
// Check children against mapNextTx
CTxMemPool::setEntries setChildrenCheck;
auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetId(), 0));
uint64_t child_sizes = 0;
int64_t child_sigop_counts = 0;
for (; iter != mapNextTx.end() &&
iter->first->GetTxId() == it->GetTx().GetId();
++iter) {
txiter childit = mapTx.find(iter->second->GetId());
// mapNextTx points to in-mempool transactions
assert(childit != mapTx.end());
if (setChildrenCheck.insert(childit).second) {
child_sizes += childit->GetTxSize();
child_sigop_counts += childit->GetSigOpCount();
}
}
assert(setChildrenCheck == GetMemPoolChildren(it));
if (!tachyonLatched) { //! Remove after tachyon
// Also check to make sure size is greater than sum with immediate
// children. Just a sanity check, not definitive that this calc is
// correct...
assert(it->GetSizeWithDescendants() >= child_sizes + it->GetTxSize());
assert(it->GetSigOpCountWithDescendants() >=
child_sigop_counts + it->GetSigOpCount());
}
if (fDependsWait) {
waitingOnDependants.push_back(&(*it));
} else {
CheckInputsAndUpdateCoins(tx, mempoolDuplicate, spendheight);
}
}
unsigned int stepsSinceLastRemove = 0;
while (!waitingOnDependants.empty()) {
const CTxMemPoolEntry *entry = waitingOnDependants.front();
waitingOnDependants.pop_front();
if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
waitingOnDependants.push_back(entry);
stepsSinceLastRemove++;
assert(stepsSinceLastRemove < waitingOnDependants.size());
} else {
CheckInputsAndUpdateCoins(entry->GetTx(), mempoolDuplicate,
spendheight);
stepsSinceLastRemove = 0;
}
}
for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
const TxId &txid = it->second->GetId();
indexed_transaction_set::const_iterator it2 = mapTx.find(txid);
const CTransaction &tx = it2->GetTx();
assert(it2 != mapTx.end());
assert(&tx == it->second);
}
assert(totalTxSize == checkTotal);
assert(innerUsage == cachedInnerUsage);
}
bool CTxMemPool::CompareTopologically(const TxId &txida, const TxId &txidb) const {
LOCK(cs);
auto it1 = mapTx.find(txida);
if (it1 == mapTx.end()) return false;
auto it2 = mapTx.find(txidb);
if (it2 == mapTx.end()) return true;
return it1->GetEntryId() < it2->GetEntryId();
}
void CTxMemPool::queryHashes(std::vector<uint256> &vtxid) const {
LOCK(cs);
vtxid.clear();
vtxid.reserve(mapTx.size());
for (const auto &entry : mapTx.get<entry_id>()) {
vtxid.push_back(entry.GetTx().GetId());
}
}
static TxMempoolInfo
GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it) {
return TxMempoolInfo{it->GetSharedTx(), it->GetTime(),
CFeeRate(it->GetFee(), it->GetTxSize()),
it->GetModifiedFee() - it->GetFee()};
}
std::vector<TxMempoolInfo> CTxMemPool::infoAll() const {
LOCK(cs);
std::vector<TxMempoolInfo> ret;
ret.reserve(mapTx.size());
const auto & index = mapTx.get<entry_id>();
for (auto it = index.begin(); it != index.end(); ++it) {
ret.push_back(GetInfo(mapTx.project<0>(it)));
}
return ret;
}
CTransactionRef CTxMemPool::get(const TxId &txid) const {
LOCK(cs);
indexed_transaction_set::const_iterator i = mapTx.find(txid);
if (i == mapTx.end()) {
return nullptr;
}
return i->GetSharedTx();
}
TxMempoolInfo CTxMemPool::info(const TxId &txid) const {
LOCK(cs);
indexed_transaction_set::const_iterator i = mapTx.find(txid);
if (i == mapTx.end()) {
return TxMempoolInfo();
}
return GetInfo(i);
}
CFeeRate CTxMemPool::estimateFee() const {
LOCK(cs);
const Config &config = GetConfig();
uint64_t maxMempoolSize = config.GetMaxMemPoolSize();
// minerPolicy uses recent blocks to figure out a reasonable fee. This
// may disagree with the rollingMinimumFeerate under certain scenarios
// where the mempool increases rapidly, or blocks are being mined which
// do not contain propagated transactions.
return std::max(::minRelayTxFee, GetMinFee(maxMempoolSize));
}
void CTxMemPool::PrioritiseTransaction(const TxId &txid,
const Amount nFeeDelta) {
{
LOCK(cs);
Amount &delta = mapDeltas[txid];
delta += nFeeDelta;
txiter it = mapTx.find(txid);
if (it != mapTx.end()) {
mapTx.modify(it, update_fee_delta(delta));
if (!tachyonLatched) { // Remove after tachyon
// Now update all ancestors' modified fees with descendants
setEntries setAncestors;
std::string dummy;
CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit,
nNoLimit, nNoLimit, dummy, false);
for (txiter ancestorIt : setAncestors) {
mapTx.modify(ancestorIt,
update_descendant_state(0, nFeeDelta, 0, 0));
}
}
++nTransactionsUpdated;
}
}
LogPrintf("PrioritiseTransaction: %s fee += %s\n", txid.ToString(),
FormatMoney(nFeeDelta));
}
void CTxMemPool::ApplyDelta(const TxId &txid, Amount &nFeeDelta) const {
LOCK(cs);
auto pos = mapDeltas.find(txid);
if (pos == mapDeltas.end())
return;
nFeeDelta += pos->second;
}
void CTxMemPool::ClearPrioritisation(const TxId &txid) {
LOCK(cs);
mapDeltas.erase(txid);
}
const CTransaction *CTxMemPool::GetConflictTx(const COutPoint &prevout) const {
const auto it = mapNextTx.find(prevout);
return it == mapNextTx.end() ? nullptr : it->second;
}
std::optional<CTxMemPool::txiter>
CTxMemPool::GetIter(const TxId &txid) const {
std::optional<CTxMemPool::txiter> ret;
auto it = mapTx.find(txid);
if (it != mapTx.end()) {
ret.emplace(it);
}
return ret;
}
CTxMemPool::setEntries
CTxMemPool::GetIterSet(const std::set<TxId> &txids) const {
CTxMemPool::setEntries ret;
for (const auto &txid : txids) {
const auto mi = GetIter(txid);
if (mi) {
ret.insert(*mi);
}
}
return ret;
}
bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const {
for (const CTxIn &in : tx.vin) {
if (exists(in.prevout.GetTxId())) {
return false;
}
}
return true;
}
CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn,
const CTxMemPool &mempoolIn)
: CCoinsViewBacked(baseIn), mempool(mempoolIn) {}
bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
// If an entry in the mempool exists, always return that one, as it's
// guaranteed to never conflict with the underlying cache, and it cannot
// have pruned entries (as it contains full) transactions. First checking
// the underlying cache risks returning a pruned entry instead.
CTransactionRef ptx = mempool.get(outpoint.GetTxId());
if (ptx) {
if (outpoint.GetN() < ptx->vout.size()) {
coin = Coin(ptx->vout[outpoint.GetN()], MEMPOOL_HEIGHT, false);
return true;
}
return false;
}
return base->GetCoin(outpoint, coin);
}
size_t CTxMemPool::DynamicMemoryUsage() const {
LOCK(cs);
// Estimate the overhead of mapTx to be 9 pointers + an allocation, as no
// exact formula for boost::multi_index_contained is implemented.
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) +
9 * sizeof(void *)) *
mapTx.size() +
memusage::DynamicUsage(mapNextTx) +
memusage::DynamicUsage(mapDeltas) +
memusage::DynamicUsage(mapLinks) +
memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
}
void CTxMemPool::RemoveStaged(const setEntries &stage, MemPoolRemovalReason reason) {
AssertLockHeld(cs);
UpdateForRemoveFromMempool(stage);
for (txiter it : stage) {
removeUnchecked(it, reason);
}
}
size_t CTxMemPool::Expire(int64_t time, bool fast /* = true */) {
LOCK(cs);
setEntries stage;
auto const& index = mapTx.get<entry_id>();
for (auto it = index.begin(); it != index.end(); ++it) {
if (it->GetTime() < time) {
CalculateDescendants(mapTx.project<0>(it), stage);
} else if (fast) {
break;
}
}
RemoveStaged(stage, MemPoolRemovalReason::EXPIRY);
return stage.size();
}
void CTxMemPool::LimitSize(size_t limit, unsigned long age) {
auto expired = Expire(GetTime() - age, /* fast */ true);
if (expired != 0) {
LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
}
std::vector<COutPoint> vNoSpendsRemaining;
TrimToSize(limit, &vNoSpendsRemaining);
for (const COutPoint &removed : vNoSpendsRemaining) {
pcoinsTip->Uncache(removed);
}
}
void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry) {
setEntries setAncestors;
if (!tachyonLatched) {
std::string dummy;
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit,
nNoLimit, dummy);
}
return addUnchecked(entry, setAncestors);
}
// NB: The pointer type is only used for template overload selection and never dereferenced so this is safe.
inline constexpr size_t setEntriesIncrementalUsage =
memusage::IncrementalDynamicUsage(static_cast<CTxMemPool::setEntries *>(nullptr));
void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add) {
if (add && mapLinks[entry].children.insert(child).second) {
cachedInnerUsage += setEntriesIncrementalUsage;
} else if (!add && mapLinks[entry].children.erase(child)) {
cachedInnerUsage -= setEntriesIncrementalUsage;
}
}
void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add) {
if (add && mapLinks[entry].parents.insert(parent).second) {
cachedInnerUsage += setEntriesIncrementalUsage;
} else if (!add && mapLinks[entry].parents.erase(parent)) {
cachedInnerUsage -= setEntriesIncrementalUsage;
}
}
const CTxMemPool::setEntries &
CTxMemPool::GetMemPoolParents(txiter entry) const {
assert(entry != mapTx.end());
auto it = mapLinks.find(entry);
assert(it != mapLinks.end());
return it->second.parents;
}
const CTxMemPool::setEntries &
CTxMemPool::GetMemPoolChildren(txiter entry) const {
assert(entry != mapTx.end());
auto it = mapLinks.find(entry);
assert(it != mapLinks.end());
return it->second.children;
}
CTransactionRef CTxMemPool::addDoubleSpendProof(const DoubleSpendProof &proof, const std::optional<txiter> &optIter) {
LOCK(cs);
txiter iter;
if (!optIter) {
auto spendingTx = mapNextTx.find(proof.outPoint());
if (spendingTx == mapNextTx.end()) {
// Nothing spent this or tx disappeared in the meantime
// -- proof no longer valid. Caller will accept the situation.
return CTransactionRef();
}
iter = mapTx.find(spendingTx->second->GetId());
} else
iter = *optIter;
if (iter->HasDsp()) {
// A DSProof already exists for this tx, don't propagate new one.
return CTransactionRef();
}
CTransactionRef ret{iter->GetSharedTx()};
// Add to storage. If this was an orphan it will implicitly be flagged as a non-orphan
m_dspStorage->add(proof);
// Update mempool entry to save the dspId
const auto &hash = proof.GetId();
mapTx.modify(iter, [&hash](CTxMemPoolEntry &entry){ entry.SetDspId(hash); });
return ret;
}
DoubleSpendProofStorage *CTxMemPool::doubleSpendProofStorage() const {
return m_dspStorage.get();
}
//! list all known proofs, optionally also returning all known orphans (orphans have an .IsNull() TxId)
auto CTxMemPool::listDoubleSpendProofs(const bool includeOrphans) const -> std::vector<DspTxIdPair> {
std::vector<DspTxIdPair> ret;
LOCK(cs);
auto proofs = m_dspStorage->getAll(includeOrphans);
ret.reserve(proofs.size());
for (auto & [proof, isOrphan] : proofs) {
TxId txId;
if (proof.isEmpty())
throw std::runtime_error("Internal error: m_dspStorage returned an empty proof");
if (isOrphan && !includeOrphans)
throw std::runtime_error("Internal error: m_dspStorage returned orphans unexpectedly");
if (!isOrphan) {
// find the txId for this proof
if (auto it = mapNextTx.find(proof.outPoint()); it != mapNextTx.end()) {
txId = it->second->GetId();
// Sanity check that actual CTxMemPoolEntry also has this DspId associated
if (auto optiter = GetIter(txId); !optiter || (*optiter)->GetDspId() != proof.GetId()) {
// should never happen, indicates bug in code
throw std::runtime_error(strprintf("Unexpected state: DspId %s for COutPoint %s is not associated"
" with the expected txId %s!",
proof.GetId().ToString(), proof.outPoint().ToString(),
txId.ToString()));
}
} else {
// should never happen, indicates bug in code
throw std::runtime_error(strprintf("Unexpected state: DspId %s for COutPoint %s is missing its tx from"
" mempool, yet is not marked as an orphan!",
proof.GetId().ToString(), proof.outPoint().ToString()));
}
}
ret.emplace_back(std::move(proof), std::move(txId) /* if orphan txId will be .IsNull() here */);
}
return ret;
}
//! Lookup a dsproof by dspId. If the proof is an orphan, it will have an .IsNull() TxId
auto CTxMemPool::getDoubleSpendProof(const DspId &dspId, DspDescendants *desc) const -> std::optional<DspTxIdPair> {
std::optional<DspTxIdPair> ret;
LOCK(cs);
if (auto dsproof = m_dspStorage->lookup(dspId); dsproof.isEmpty()) {
// not found, return nullopt
return ret;
} else {
// found, populate ret
ret.emplace(std::piecewise_construct, std::forward_as_tuple(std::move(dsproof)), std::forward_as_tuple());
}
// next, see if it's an orphan or not by looking up its COutPoint
auto & [dsproof, txId] = *ret;
if (auto it = mapNextTx.find(dsproof.outPoint()); it != mapNextTx.end()) {
// Not an orphan, set txId
txId = it->second->GetId();
if (desc) {
// caller supplied a descendants set they want populated, so populate it on this hit
if (auto optIter = GetIter(txId))
*desc = getDspDescendantsForIter(*optIter);
}
}
return ret;
}
//! Lookup a dsproof by TxId.
auto CTxMemPool::getDoubleSpendProof(const TxId &txId, DspDescendants *desc) const -> std::optional<DoubleSpendProof> {
LOCK(cs);
return getDoubleSpendProof_common(txId, nullptr, desc);
}
//! Helper (requires cs is held)
auto CTxMemPool::getDspDescendantsForIter(txiter it) const -> DspDescendants {
DspDescendants ret;
setEntries iters;
CalculateDescendants(it, iters);
for (const auto &iter : iters)
ret.emplace(iter->GetTx().GetId());
return ret;
}
auto CTxMemPool::getDoubleSpendProof_common(const TxId &txId, txiter *txit, DspDescendants *desc) const
-> std::optional<DoubleSpendProof> {
std::optional<DoubleSpendProof> ret;
auto it = mapTx.find(txId);
if (txit)