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