]> Git Repo - VerusCoin.git/blob - src/txmempool.cpp
Auto merge of #3098 - str4d:2343-overwinter-disable-mempooltxinputlimit, r=str4d
[VerusCoin.git] / src / txmempool.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 "clientversion.h"
9 #include "consensus/consensus.h"
10 #include "consensus/validation.h"
11 #include "main.h"
12 #include "policy/fees.h"
13 #include "streams.h"
14 #include "timedata.h"
15 #include "util.h"
16 #include "utilmoneystr.h"
17 #include "version.h"
18
19 using namespace std;
20
21 CTxMemPoolEntry::CTxMemPoolEntry():
22     nFee(0), nTxSize(0), nModSize(0), nUsageSize(0), nTime(0), dPriority(0.0),
23     hadNoDependencies(false), spendsCoinbase(false)
24 {
25     nHeight = MEMPOOL_HEIGHT;
26 }
27
28 CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
29                                  int64_t _nTime, double _dPriority,
30                                  unsigned int _nHeight, bool poolHasNoInputsOf,
31                                  bool _spendsCoinbase, uint32_t _nBranchId):
32     tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight),
33     hadNoDependencies(poolHasNoInputsOf),
34     spendsCoinbase(_spendsCoinbase), nBranchId(_nBranchId)
35 {
36     nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
37     nModSize = tx.CalculateModifiedSize(nTxSize);
38     nUsageSize = RecursiveDynamicUsage(tx);
39     feeRate = CFeeRate(nFee, nTxSize);
40 }
41
42 CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
43 {
44     *this = other;
45 }
46
47 double
48 CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
49 {
50     CAmount nValueIn = tx.GetValueOut()+nFee;
51     double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
52     double dResult = dPriority + deltaPriority;
53     return dResult;
54 }
55
56 CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
57     nTransactionsUpdated(0)
58 {
59     // Sanity checks off by default for performance, because otherwise
60     // accepting transactions becomes O(N^2) where N is the number
61     // of transactions in the pool
62     nCheckFrequency = 0;
63
64     minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
65 }
66
67 CTxMemPool::~CTxMemPool()
68 {
69     delete minerPolicyEstimator;
70 }
71
72 void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
73 {
74     LOCK(cs);
75
76     std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
77
78     // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
79     while (it != mapNextTx.end() && it->first.hash == hashTx) {
80         coins.Spend(it->first.n); // and remove those outputs from coins
81         it++;
82     }
83 }
84
85 unsigned int CTxMemPool::GetTransactionsUpdated() const
86 {
87     LOCK(cs);
88     return nTransactionsUpdated;
89 }
90
91 void CTxMemPool::AddTransactionsUpdated(unsigned int n)
92 {
93     LOCK(cs);
94     nTransactionsUpdated += n;
95 }
96
97
98 bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
99 {
100     // Add to memory pool without checking anything.
101     // Used by main.cpp AcceptToMemoryPool(), which DOES do
102     // all the appropriate checks.
103     LOCK(cs);
104     mapTx.insert(entry);
105     const CTransaction& tx = mapTx.find(hash)->GetTx();
106     for (unsigned int i = 0; i < tx.vin.size(); i++)
107         mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
108     BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
109         BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
110             mapNullifiers[nf] = &tx;
111         }
112     }
113     nTransactionsUpdated++;
114     totalTxSize += entry.GetTxSize();
115     cachedInnerUsage += entry.DynamicMemoryUsage();
116     minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
117
118     return true;
119 }
120
121
122 void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
123 {
124     // Remove transaction from memory pool
125     {
126         LOCK(cs);
127         std::deque<uint256> txToRemove;
128         txToRemove.push_back(origTx.GetHash());
129         if (fRecursive && !mapTx.count(origTx.GetHash())) {
130             // If recursively removing but origTx isn't in the mempool
131             // be sure to remove any children that are in the pool. This can
132             // happen during chain re-orgs if origTx isn't re-accepted into
133             // the mempool for any reason.
134             for (unsigned int i = 0; i < origTx.vout.size(); i++) {
135                 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
136                 if (it == mapNextTx.end())
137                     continue;
138                 txToRemove.push_back(it->second.ptx->GetHash());
139             }
140         }
141         while (!txToRemove.empty())
142         {
143             uint256 hash = txToRemove.front();
144             txToRemove.pop_front();
145             if (!mapTx.count(hash))
146                 continue;
147             const CTransaction& tx = mapTx.find(hash)->GetTx();
148             if (fRecursive) {
149                 for (unsigned int i = 0; i < tx.vout.size(); i++) {
150                     std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
151                     if (it == mapNextTx.end())
152                         continue;
153                     txToRemove.push_back(it->second.ptx->GetHash());
154                 }
155             }
156             BOOST_FOREACH(const CTxIn& txin, tx.vin)
157                 mapNextTx.erase(txin.prevout);
158             BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
159                 BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) {
160                     mapNullifiers.erase(nf);
161                 }
162             }
163
164             removed.push_back(tx);
165             totalTxSize -= mapTx.find(hash)->GetTxSize();
166             cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
167             mapTx.erase(hash);
168             nTransactionsUpdated++;
169             minerPolicyEstimator->removeTx(hash);
170         }
171     }
172 }
173
174 void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
175 {
176     // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
177     LOCK(cs);
178     list<CTransaction> transactionsToRemove;
179     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
180         const CTransaction& tx = it->GetTx();
181         if (!CheckFinalTx(tx, flags)) {
182             transactionsToRemove.push_back(tx);
183         } else if (it->GetSpendsCoinbase()) {
184             BOOST_FOREACH(const CTxIn& txin, tx.vin) {
185                 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
186                 if (it2 != mapTx.end())
187                     continue;
188                 const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
189                 if (nCheckFrequency != 0) assert(coins);
190                 if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
191                     transactionsToRemove.push_back(tx);
192                     break;
193                 }
194             }
195         }
196     }
197     BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
198         list<CTransaction> removed;
199         remove(tx, removed, true);
200     }
201 }
202
203
204 void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot)
205 {
206     // If a block is disconnected from the tip, and the root changed,
207     // we must invalidate transactions from the mempool which spend
208     // from that root -- almost as though they were spending coinbases
209     // which are no longer valid to spend due to coinbase maturity.
210     LOCK(cs);
211     list<CTransaction> transactionsToRemove;
212
213     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
214         const CTransaction& tx = it->GetTx();
215         BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
216             if (joinsplit.anchor == invalidRoot) {
217                 transactionsToRemove.push_back(tx);
218                 break;
219             }
220         }
221     }
222
223     BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
224         list<CTransaction> removed;
225         remove(tx, removed, true);
226     }
227 }
228
229 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
230 {
231     // Remove transactions which depend on inputs of tx, recursively
232     list<CTransaction> result;
233     LOCK(cs);
234     BOOST_FOREACH(const CTxIn &txin, tx.vin) {
235         std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
236         if (it != mapNextTx.end()) {
237             const CTransaction &txConflict = *it->second.ptx;
238             if (txConflict != tx)
239             {
240                 remove(txConflict, removed, true);
241             }
242         }
243     }
244
245     BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
246         BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
247             std::map<uint256, const CTransaction*>::iterator it = mapNullifiers.find(nf);
248             if (it != mapNullifiers.end()) {
249                 const CTransaction &txConflict = *it->second;
250                 if (txConflict != tx)
251                 {
252                     remove(txConflict, removed, true);
253                 }
254             }
255         }
256     }
257 }
258
259 void CTxMemPool::removeExpired(unsigned int nBlockHeight)
260 {
261     // Remove expired txs from the mempool
262     LOCK(cs);
263     list<CTransaction> transactionsToRemove;
264     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
265     {
266         const CTransaction& tx = it->GetTx();
267         if (IsExpiredTx(tx, nBlockHeight)) {
268             transactionsToRemove.push_back(tx);
269         }
270     }
271     for (const CTransaction& tx : transactionsToRemove) {
272         list<CTransaction> removed;
273         remove(tx, removed, true);
274         LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString());
275     }
276 }
277
278 /**
279  * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
280  */
281 void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
282                                 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
283 {
284     LOCK(cs);
285     std::vector<CTxMemPoolEntry> entries;
286     BOOST_FOREACH(const CTransaction& tx, vtx)
287     {
288         uint256 hash = tx.GetHash();
289
290         indexed_transaction_set::iterator i = mapTx.find(hash);
291         if (i != mapTx.end())
292             entries.push_back(*i);
293     }
294     BOOST_FOREACH(const CTransaction& tx, vtx)
295     {
296         std::list<CTransaction> dummy;
297         remove(tx, dummy, false);
298         removeConflicts(tx, conflicts);
299         ClearPrioritisation(tx.GetHash());
300     }
301     // After the txs in the new block have been removed from the mempool, update policy estimates
302     minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
303 }
304
305 /**
306  * Called whenever the tip changes. Removes transactions which don't commit to
307  * the given branch ID from the mempool.
308  */
309 void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
310 {
311     LOCK(cs);
312     std::list<CTransaction> transactionsToRemove;
313
314     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
315         const CTransaction& tx = it->GetTx();
316         if (it->GetValidatedBranchId() != nMemPoolBranchId) {
317             transactionsToRemove.push_back(tx);
318         }
319     }
320
321     for (const CTransaction& tx : transactionsToRemove) {
322         std::list<CTransaction> removed;
323         remove(tx, removed, true);
324     }
325 }
326
327 void CTxMemPool::clear()
328 {
329     LOCK(cs);
330     mapTx.clear();
331     mapNextTx.clear();
332     totalTxSize = 0;
333     cachedInnerUsage = 0;
334     ++nTransactionsUpdated;
335 }
336
337 void CTxMemPool::check(const CCoinsViewCache *pcoins) const
338 {
339     if (nCheckFrequency == 0)
340         return;
341
342     if (insecure_rand() >= nCheckFrequency)
343         return;
344
345     LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
346
347     uint64_t checkTotal = 0;
348     uint64_t innerUsage = 0;
349
350     CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
351     const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
352
353     LOCK(cs);
354     list<const CTxMemPoolEntry*> waitingOnDependants;
355     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
356         unsigned int i = 0;
357         checkTotal += it->GetTxSize();
358         innerUsage += it->DynamicMemoryUsage();
359         const CTransaction& tx = it->GetTx();
360         bool fDependsWait = false;
361         BOOST_FOREACH(const CTxIn &txin, tx.vin) {
362             // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
363             indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
364             if (it2 != mapTx.end()) {
365                 const CTransaction& tx2 = it2->GetTx();
366                 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
367                 fDependsWait = true;
368             } else {
369                 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
370                 assert(coins && coins->IsAvailable(txin.prevout.n));
371             }
372             // Check whether its inputs are marked in mapNextTx.
373             std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
374             assert(it3 != mapNextTx.end());
375             assert(it3->second.ptx == &tx);
376             assert(it3->second.n == i);
377             i++;
378         }
379
380         boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
381
382         BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
383             BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
384                 assert(!pcoins->GetNullifier(nf));
385             }
386
387             ZCIncrementalMerkleTree tree;
388             auto it = intermediates.find(joinsplit.anchor);
389             if (it != intermediates.end()) {
390                 tree = it->second;
391             } else {
392                 assert(pcoins->GetAnchorAt(joinsplit.anchor, tree));
393             }
394
395             BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
396             {
397                 tree.append(commitment);
398             }
399
400             intermediates.insert(std::make_pair(tree.root(), tree));
401         }
402         if (fDependsWait)
403             waitingOnDependants.push_back(&(*it));
404         else {
405             CValidationState state;
406             bool fCheckResult = tx.IsCoinBase() ||
407                 Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
408             assert(fCheckResult);
409             UpdateCoins(tx, mempoolDuplicate, 1000000);
410         }
411     }
412     unsigned int stepsSinceLastRemove = 0;
413     while (!waitingOnDependants.empty()) {
414         const CTxMemPoolEntry* entry = waitingOnDependants.front();
415         waitingOnDependants.pop_front();
416         CValidationState state;
417         if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
418             waitingOnDependants.push_back(entry);
419             stepsSinceLastRemove++;
420             assert(stepsSinceLastRemove < waitingOnDependants.size());
421         } else {
422             bool fCheckResult = entry->GetTx().IsCoinBase() ||
423                 Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
424             assert(fCheckResult);
425             UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000);
426             stepsSinceLastRemove = 0;
427         }
428     }
429     for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
430         uint256 hash = it->second.ptx->GetHash();
431         indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
432         const CTransaction& tx = it2->GetTx();
433         assert(it2 != mapTx.end());
434         assert(&tx == it->second.ptx);
435         assert(tx.vin.size() > it->second.n);
436         assert(it->first == it->second.ptx->vin[it->second.n].prevout);
437     }
438
439     for (std::map<uint256, const CTransaction*>::const_iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); it++) {
440         uint256 hash = it->second->GetHash();
441         indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
442         const CTransaction& tx = it2->GetTx();
443         assert(it2 != mapTx.end());
444         assert(&tx == it->second);
445     }
446
447     assert(totalTxSize == checkTotal);
448     assert(innerUsage == cachedInnerUsage);
449 }
450
451 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
452 {
453     vtxid.clear();
454
455     LOCK(cs);
456     vtxid.reserve(mapTx.size());
457     for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
458         vtxid.push_back(mi->GetTx().GetHash());
459 }
460
461 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
462 {
463     LOCK(cs);
464     indexed_transaction_set::const_iterator i = mapTx.find(hash);
465     if (i == mapTx.end()) return false;
466     result = i->GetTx();
467     return true;
468 }
469
470 CFeeRate CTxMemPool::estimateFee(int nBlocks) const
471 {
472     LOCK(cs);
473     return minerPolicyEstimator->estimateFee(nBlocks);
474 }
475 double CTxMemPool::estimatePriority(int nBlocks) const
476 {
477     LOCK(cs);
478     return minerPolicyEstimator->estimatePriority(nBlocks);
479 }
480
481 bool
482 CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
483 {
484     try {
485         LOCK(cs);
486         fileout << 109900; // version required to read: 0.10.99 or later
487         fileout << CLIENT_VERSION; // version that wrote the file
488         minerPolicyEstimator->Write(fileout);
489     }
490     catch (const std::exception&) {
491         LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
492         return false;
493     }
494     return true;
495 }
496
497 bool
498 CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
499 {
500     try {
501         int nVersionRequired, nVersionThatWrote;
502         filein >> nVersionRequired >> nVersionThatWrote;
503         if (nVersionRequired > CLIENT_VERSION)
504             return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
505
506         LOCK(cs);
507         minerPolicyEstimator->Read(filein);
508     }
509     catch (const std::exception&) {
510         LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
511         return false;
512     }
513     return true;
514 }
515
516 void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
517 {
518     {
519         LOCK(cs);
520         std::pair<double, CAmount> &deltas = mapDeltas[hash];
521         deltas.first += dPriorityDelta;
522         deltas.second += nFeeDelta;
523     }
524     LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
525 }
526
527 void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
528 {
529     LOCK(cs);
530     std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
531     if (pos == mapDeltas.end())
532         return;
533     const std::pair<double, CAmount> &deltas = pos->second;
534     dPriorityDelta += deltas.first;
535     nFeeDelta += deltas.second;
536 }
537
538 void CTxMemPool::ClearPrioritisation(const uint256 hash)
539 {
540     LOCK(cs);
541     mapDeltas.erase(hash);
542 }
543
544 bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
545 {
546     for (unsigned int i = 0; i < tx.vin.size(); i++)
547         if (exists(tx.vin[i].prevout.hash))
548             return false;
549     return true;
550 }
551
552 CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
553
554 bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const {
555     if (mempool.mapNullifiers.count(nf))
556         return true;
557
558     return base->GetNullifier(nf);
559 }
560
561 bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
562     // If an entry in the mempool exists, always return that one, as it's guaranteed to never
563     // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
564     // transactions. First checking the underlying cache risks returning a pruned entry instead.
565     CTransaction tx;
566     if (mempool.lookup(txid, tx)) {
567         coins = CCoins(tx, MEMPOOL_HEIGHT);
568         return true;
569     }
570     return (base->GetCoins(txid, coins) && !coins.IsPruned());
571 }
572
573 bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
574     return mempool.exists(txid) || base->HaveCoins(txid);
575 }
576
577 size_t CTxMemPool::DynamicMemoryUsage() const {
578     LOCK(cs);
579     // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
580     return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;
581 }
This page took 0.0554519999999999 seconds and 4 git commands to generate.