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"
18 #define _COINBASE_MATURITY 100
22 CTxMemPoolEntry::CTxMemPoolEntry():
23 nFee(0), nTxSize(0), nModSize(0), nUsageSize(0), nTime(0), dPriority(0.0),
24 hadNoDependencies(false), spendsCoinbase(false)
26 nHeight = MEMPOOL_HEIGHT;
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)
37 nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
38 nModSize = tx.CalculateModifiedSize(nTxSize);
39 nUsageSize = RecursiveDynamicUsage(tx);
40 feeRate = CFeeRate(nFee, nTxSize);
43 CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
49 CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
51 CAmount nValueIn = tx.GetValueOut()+nFee;
52 double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
53 double dResult = dPriority + deltaPriority;
57 CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
58 nTransactionsUpdated(0)
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
65 minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
68 CTxMemPool::~CTxMemPool()
70 delete minerPolicyEstimator;
73 void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
77 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
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
86 unsigned int CTxMemPool::GetTransactionsUpdated() const
89 return nTransactionsUpdated;
92 void CTxMemPool::AddTransactionsUpdated(unsigned int n)
95 nTransactionsUpdated += n;
99 bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
101 // Add to memory pool without checking anything.
102 // Used by main.cpp AcceptToMemoryPool(), which DOES do
103 // all the appropriate checks.
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;
114 nTransactionsUpdated++;
115 totalTxSize += entry.GetTxSize();
116 cachedInnerUsage += entry.DynamicMemoryUsage();
117 minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
123 void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
125 // Remove transaction from memory pool
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())
139 txToRemove.push_back(it->second.ptx->GetHash());
142 while (!txToRemove.empty())
144 uint256 hash = txToRemove.front();
145 txToRemove.pop_front();
146 if (!mapTx.count(hash))
148 const CTransaction& tx = mapTx.find(hash)->GetTx();
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())
154 txToRemove.push_back(it->second.ptx->GetHash());
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);
165 removed.push_back(tx);
166 totalTxSize -= mapTx.find(hash)->GetTxSize();
167 cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
169 nTransactionsUpdated++;
170 minerPolicyEstimator->removeTx(hash);
175 void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
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
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)
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();
220 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
221 if (joinsplit.anchor == invalidRoot) {
222 transactionsToRemove.push_back(tx);
228 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
229 list<CTransaction> removed;
230 remove(tx, removed, true);
234 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
236 // Remove transactions which depend on inputs of tx, recursively
237 list<CTransaction> result;
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)
245 remove(txConflict, removed, true);
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)
257 remove(txConflict, removed, true);
264 void CTxMemPool::removeExpired(unsigned int nBlockHeight)
266 // Remove expired txs from the mempool
268 list<CTransaction> transactionsToRemove;
269 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
271 const CTransaction& tx = it->GetTx();
272 if (IsExpiredTx(tx, nBlockHeight)) {
273 transactionsToRemove.push_back(tx);
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());
284 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
286 void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
287 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
290 std::vector<CTxMemPoolEntry> entries;
291 BOOST_FOREACH(const CTransaction& tx, vtx)
293 uint256 hash = tx.GetHash();
295 indexed_transaction_set::iterator i = mapTx.find(hash);
296 if (i != mapTx.end())
297 entries.push_back(*i);
299 BOOST_FOREACH(const CTransaction& tx, vtx)
301 std::list<CTransaction> dummy;
302 remove(tx, dummy, false);
303 removeConflicts(tx, conflicts);
304 ClearPrioritisation(tx.GetHash());
306 // After the txs in the new block have been removed from the mempool, update policy estimates
307 minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
311 * Called whenever the tip changes. Removes transactions which don't commit to
312 * the given branch ID from the mempool.
314 void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
317 std::list<CTransaction> transactionsToRemove;
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);
326 for (const CTransaction& tx : transactionsToRemove) {
327 std::list<CTransaction> removed;
328 remove(tx, removed, true);
332 void CTxMemPool::clear()
338 cachedInnerUsage = 0;
339 ++nTransactionsUpdated;
342 void CTxMemPool::check(const CCoinsViewCache *pcoins) const
344 if (nCheckFrequency == 0)
347 if (insecure_rand() >= nCheckFrequency)
350 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
352 uint64_t checkTotal = 0;
353 uint64_t innerUsage = 0;
355 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
356 const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
359 list<const CTxMemPoolEntry*> waitingOnDependants;
360 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
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());
374 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
375 assert(coins && coins->IsAvailable(txin.prevout.n));
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);
385 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
387 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
388 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
389 assert(!pcoins->GetNullifier(nf));
392 ZCIncrementalMerkleTree tree;
393 auto it = intermediates.find(joinsplit.anchor);
394 if (it != intermediates.end()) {
397 assert(pcoins->GetAnchorAt(joinsplit.anchor, tree));
400 BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
402 tree.append(commitment);
405 intermediates.insert(std::make_pair(tree.root(), tree));
408 waitingOnDependants.push_back(&(*it));
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);
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());
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;
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);
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);
452 assert(totalTxSize == checkTotal);
453 assert(innerUsage == cachedInnerUsage);
456 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
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());
466 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
469 indexed_transaction_set::const_iterator i = mapTx.find(hash);
470 if (i == mapTx.end()) return false;
475 CFeeRate CTxMemPool::estimateFee(int nBlocks) const
478 return minerPolicyEstimator->estimateFee(nBlocks);
480 double CTxMemPool::estimatePriority(int nBlocks) const
483 return minerPolicyEstimator->estimatePriority(nBlocks);
487 CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
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);
495 catch (const std::exception&) {
496 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
503 CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
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);
512 minerPolicyEstimator->Read(filein);
514 catch (const std::exception&) {
515 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
521 void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
525 std::pair<double, CAmount> &deltas = mapDeltas[hash];
526 deltas.first += dPriorityDelta;
527 deltas.second += nFeeDelta;
529 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
532 void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
535 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
536 if (pos == mapDeltas.end())
538 const std::pair<double, CAmount> &deltas = pos->second;
539 dPriorityDelta += deltas.first;
540 nFeeDelta += deltas.second;
543 void CTxMemPool::ClearPrioritisation(const uint256 hash)
546 mapDeltas.erase(hash);
549 bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
551 for (unsigned int i = 0; i < tx.vin.size(); i++)
552 if (exists(tx.vin[i].prevout.hash))
557 CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
559 bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const {
560 if (mempool.mapNullifiers.count(nf))
563 return base->GetNullifier(nf);
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.
571 if (mempool.lookup(txid, tx)) {
572 coins = CCoins(tx, MEMPOOL_HEIGHT);
575 return (base->GetCoins(txid, coins) && !coins.IsPruned());
578 bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
579 return mempool.exists(txid) || base->HaveCoins(txid);
582 size_t CTxMemPool::DynamicMemoryUsage() const {
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;