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.
8 #include "clientversion.h"
9 #include "consensus/consensus.h"
10 #include "consensus/validation.h"
12 #include "policy/fees.h"
16 #include "utilmoneystr.h"
21 CTxMemPoolEntry::CTxMemPoolEntry():
22 nFee(0), nTxSize(0), nModSize(0), nUsageSize(0), nTime(0), dPriority(0.0),
23 hadNoDependencies(false), spendsCoinbase(false)
25 nHeight = MEMPOOL_HEIGHT;
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)
36 nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
37 nModSize = tx.CalculateModifiedSize(nTxSize);
38 nUsageSize = RecursiveDynamicUsage(tx);
39 feeRate = CFeeRate(nFee, nTxSize);
42 CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
48 CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
50 CAmount nValueIn = tx.GetValueOut()+nFee;
51 double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
52 double dResult = dPriority + deltaPriority;
56 CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
57 nTransactionsUpdated(0)
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
64 minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
67 CTxMemPool::~CTxMemPool()
69 delete minerPolicyEstimator;
72 void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
76 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
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
85 unsigned int CTxMemPool::GetTransactionsUpdated() const
88 return nTransactionsUpdated;
91 void CTxMemPool::AddTransactionsUpdated(unsigned int n)
94 nTransactionsUpdated += n;
98 bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
100 // Add to memory pool without checking anything.
101 // Used by main.cpp AcceptToMemoryPool(), which DOES do
102 // all the appropriate checks.
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 mapSproutNullifiers[nf] = &tx;
113 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
114 mapSaplingNullifiers[spendDescription.nullifier] = &tx;
116 nTransactionsUpdated++;
117 totalTxSize += entry.GetTxSize();
118 cachedInnerUsage += entry.DynamicMemoryUsage();
119 minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
125 void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
127 // Remove transaction from memory pool
130 std::deque<uint256> txToRemove;
131 txToRemove.push_back(origTx.GetHash());
132 if (fRecursive && !mapTx.count(origTx.GetHash())) {
133 // If recursively removing but origTx isn't in the mempool
134 // be sure to remove any children that are in the pool. This can
135 // happen during chain re-orgs if origTx isn't re-accepted into
136 // the mempool for any reason.
137 for (unsigned int i = 0; i < origTx.vout.size(); i++) {
138 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
139 if (it == mapNextTx.end())
141 txToRemove.push_back(it->second.ptx->GetHash());
144 while (!txToRemove.empty())
146 uint256 hash = txToRemove.front();
147 txToRemove.pop_front();
148 if (!mapTx.count(hash))
150 const CTransaction& tx = mapTx.find(hash)->GetTx();
152 for (unsigned int i = 0; i < tx.vout.size(); i++) {
153 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
154 if (it == mapNextTx.end())
156 txToRemove.push_back(it->second.ptx->GetHash());
159 BOOST_FOREACH(const CTxIn& txin, tx.vin)
160 mapNextTx.erase(txin.prevout);
161 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
162 BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) {
163 mapSproutNullifiers.erase(nf);
166 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
167 mapSaplingNullifiers.erase(spendDescription.nullifier);
169 removed.push_back(tx);
170 totalTxSize -= mapTx.find(hash)->GetTxSize();
171 cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
173 nTransactionsUpdated++;
174 minerPolicyEstimator->removeTx(hash);
179 void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
181 // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
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())
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);
202 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
203 list<CTransaction> removed;
204 remove(tx, removed, true);
209 void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot, ShieldedType type)
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.
216 list<CTransaction> transactionsToRemove;
218 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
219 const CTransaction& tx = it->GetTx();
222 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
223 if (joinsplit.anchor == invalidRoot) {
224 transactionsToRemove.push_back(tx);
230 BOOST_FOREACH(const SpendDescription& spendDescription, tx.vShieldedSpend) {
231 if (spendDescription.anchor == invalidRoot) {
232 transactionsToRemove.push_back(tx);
238 throw runtime_error("Unknown shielded type");
243 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
244 list<CTransaction> removed;
245 remove(tx, removed, true);
249 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
251 // Remove transactions which depend on inputs of tx, recursively
252 list<CTransaction> result;
254 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
255 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
256 if (it != mapNextTx.end()) {
257 const CTransaction &txConflict = *it->second.ptx;
258 if (txConflict != tx)
260 remove(txConflict, removed, true);
265 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
266 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
267 std::map<uint256, const CTransaction*>::iterator it = mapSproutNullifiers.find(nf);
268 if (it != mapSproutNullifiers.end()) {
269 const CTransaction &txConflict = *it->second;
270 if (txConflict != tx) {
271 remove(txConflict, removed, true);
276 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
277 std::map<uint256, const CTransaction*>::iterator it = mapSaplingNullifiers.find(spendDescription.nullifier);
278 if (it != mapSaplingNullifiers.end()) {
279 const CTransaction &txConflict = *it->second;
280 if (txConflict != tx) {
281 remove(txConflict, removed, true);
287 void CTxMemPool::removeExpired(unsigned int nBlockHeight)
289 // Remove expired txs from the mempool
291 list<CTransaction> transactionsToRemove;
292 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
294 const CTransaction& tx = it->GetTx();
295 if (IsExpiredTx(tx, nBlockHeight)) {
296 transactionsToRemove.push_back(tx);
299 for (const CTransaction& tx : transactionsToRemove) {
300 list<CTransaction> removed;
301 remove(tx, removed, true);
302 LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString());
307 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
309 void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
310 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
313 std::vector<CTxMemPoolEntry> entries;
314 BOOST_FOREACH(const CTransaction& tx, vtx)
316 uint256 hash = tx.GetHash();
318 indexed_transaction_set::iterator i = mapTx.find(hash);
319 if (i != mapTx.end())
320 entries.push_back(*i);
322 BOOST_FOREACH(const CTransaction& tx, vtx)
324 std::list<CTransaction> dummy;
325 remove(tx, dummy, false);
326 removeConflicts(tx, conflicts);
327 ClearPrioritisation(tx.GetHash());
329 // After the txs in the new block have been removed from the mempool, update policy estimates
330 minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
334 * Called whenever the tip changes. Removes transactions which don't commit to
335 * the given branch ID from the mempool.
337 void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
340 std::list<CTransaction> transactionsToRemove;
342 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
343 const CTransaction& tx = it->GetTx();
344 if (it->GetValidatedBranchId() != nMemPoolBranchId) {
345 transactionsToRemove.push_back(tx);
349 for (const CTransaction& tx : transactionsToRemove) {
350 std::list<CTransaction> removed;
351 remove(tx, removed, true);
355 void CTxMemPool::clear()
361 cachedInnerUsage = 0;
362 ++nTransactionsUpdated;
365 void CTxMemPool::check(const CCoinsViewCache *pcoins) const
367 if (nCheckFrequency == 0)
370 if (insecure_rand() >= nCheckFrequency)
373 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
375 uint64_t checkTotal = 0;
376 uint64_t innerUsage = 0;
378 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
379 const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
382 list<const CTxMemPoolEntry*> waitingOnDependants;
383 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
385 checkTotal += it->GetTxSize();
386 innerUsage += it->DynamicMemoryUsage();
387 const CTransaction& tx = it->GetTx();
388 bool fDependsWait = false;
389 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
390 // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
391 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
392 if (it2 != mapTx.end()) {
393 const CTransaction& tx2 = it2->GetTx();
394 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
397 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
398 assert(coins && coins->IsAvailable(txin.prevout.n));
400 // Check whether its inputs are marked in mapNextTx.
401 std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
402 assert(it3 != mapNextTx.end());
403 assert(it3->second.ptx == &tx);
404 assert(it3->second.n == i);
408 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
410 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
411 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
412 assert(!pcoins->GetNullifier(nf, SPROUT));
415 ZCIncrementalMerkleTree tree;
416 auto it = intermediates.find(joinsplit.anchor);
417 if (it != intermediates.end()) {
420 assert(pcoins->GetSproutAnchorAt(joinsplit.anchor, tree));
423 BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
425 tree.append(commitment);
428 intermediates.insert(std::make_pair(tree.root(), tree));
430 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
431 ZCSaplingIncrementalMerkleTree tree;
433 assert(pcoins->GetSaplingAnchorAt(spendDescription.anchor, tree));
434 assert(!pcoins->GetNullifier(spendDescription.nullifier, SAPLING));
437 waitingOnDependants.push_back(&(*it));
439 CValidationState state;
440 bool fCheckResult = tx.IsCoinBase() ||
441 Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
442 assert(fCheckResult);
443 UpdateCoins(tx, mempoolDuplicate, 1000000);
446 unsigned int stepsSinceLastRemove = 0;
447 while (!waitingOnDependants.empty()) {
448 const CTxMemPoolEntry* entry = waitingOnDependants.front();
449 waitingOnDependants.pop_front();
450 CValidationState state;
451 if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
452 waitingOnDependants.push_back(entry);
453 stepsSinceLastRemove++;
454 assert(stepsSinceLastRemove < waitingOnDependants.size());
456 bool fCheckResult = entry->GetTx().IsCoinBase() ||
457 Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
458 assert(fCheckResult);
459 UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000);
460 stepsSinceLastRemove = 0;
463 for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
464 uint256 hash = it->second.ptx->GetHash();
465 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
466 const CTransaction& tx = it2->GetTx();
467 assert(it2 != mapTx.end());
468 assert(&tx == it->second.ptx);
469 assert(tx.vin.size() > it->second.n);
470 assert(it->first == it->second.ptx->vin[it->second.n].prevout);
473 checkNullifiers(SPROUT);
474 checkNullifiers(SAPLING);
476 assert(totalTxSize == checkTotal);
477 assert(innerUsage == cachedInnerUsage);
480 void CTxMemPool::checkNullifiers(ShieldedType type) const
482 const std::map<uint256, const CTransaction*>* mapToUse;
485 mapToUse = &mapSproutNullifiers;
488 mapToUse = &mapSaplingNullifiers;
491 throw runtime_error("Unknown nullifier type");
493 for (const auto& entry : *mapToUse) {
494 uint256 hash = entry.second->GetHash();
495 CTxMemPool::indexed_transaction_set::const_iterator findTx = mapTx.find(hash);
496 const CTransaction& tx = findTx->GetTx();
497 assert(findTx != mapTx.end());
498 assert(&tx == entry.second);
502 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
507 vtxid.reserve(mapTx.size());
508 for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
509 vtxid.push_back(mi->GetTx().GetHash());
512 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
515 indexed_transaction_set::const_iterator i = mapTx.find(hash);
516 if (i == mapTx.end()) return false;
521 CFeeRate CTxMemPool::estimateFee(int nBlocks) const
524 return minerPolicyEstimator->estimateFee(nBlocks);
526 double CTxMemPool::estimatePriority(int nBlocks) const
529 return minerPolicyEstimator->estimatePriority(nBlocks);
533 CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
537 fileout << 109900; // version required to read: 0.10.99 or later
538 fileout << CLIENT_VERSION; // version that wrote the file
539 minerPolicyEstimator->Write(fileout);
541 catch (const std::exception&) {
542 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
549 CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
552 int nVersionRequired, nVersionThatWrote;
553 filein >> nVersionRequired >> nVersionThatWrote;
554 if (nVersionRequired > CLIENT_VERSION)
555 return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
558 minerPolicyEstimator->Read(filein);
560 catch (const std::exception&) {
561 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
567 void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
571 std::pair<double, CAmount> &deltas = mapDeltas[hash];
572 deltas.first += dPriorityDelta;
573 deltas.second += nFeeDelta;
575 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
578 void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
581 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
582 if (pos == mapDeltas.end())
584 const std::pair<double, CAmount> &deltas = pos->second;
585 dPriorityDelta += deltas.first;
586 nFeeDelta += deltas.second;
589 void CTxMemPool::ClearPrioritisation(const uint256 hash)
592 mapDeltas.erase(hash);
595 bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
597 for (unsigned int i = 0; i < tx.vin.size(); i++)
598 if (exists(tx.vin[i].prevout.hash))
603 bool CTxMemPool::nullifierExists(const uint256& nullifier, ShieldedType type) const
607 return mapSproutNullifiers.count(nullifier);
609 return mapSaplingNullifiers.count(nullifier);
611 throw runtime_error("Unknown nullifier type");
615 CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
617 bool CCoinsViewMemPool::GetNullifier(const uint256 &nf, ShieldedType type) const
619 return mempool.nullifierExists(nf, type) || base->GetNullifier(nf, type);
622 bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
623 // If an entry in the mempool exists, always return that one, as it's guaranteed to never
624 // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
625 // transactions. First checking the underlying cache risks returning a pruned entry instead.
627 if (mempool.lookup(txid, tx)) {
628 coins = CCoins(tx, MEMPOOL_HEIGHT);
631 return (base->GetCoins(txid, coins) && !coins.IsPruned());
634 bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
635 return mempool.exists(txid) || base->HaveCoins(txid);
638 size_t CTxMemPool::DynamicMemoryUsage() const {
640 // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
641 return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;