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 mapNullifiers[nf] = &tx;
113 nTransactionsUpdated++;
114 totalTxSize += entry.GetTxSize();
115 cachedInnerUsage += entry.DynamicMemoryUsage();
116 minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
122 void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
124 // Remove transaction from memory pool
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())
138 txToRemove.push_back(it->second.ptx->GetHash());
141 while (!txToRemove.empty())
143 uint256 hash = txToRemove.front();
144 txToRemove.pop_front();
145 if (!mapTx.count(hash))
147 const CTransaction& tx = mapTx.find(hash)->GetTx();
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())
153 txToRemove.push_back(it->second.ptx->GetHash());
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);
164 removed.push_back(tx);
165 totalTxSize -= mapTx.find(hash)->GetTxSize();
166 cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
168 nTransactionsUpdated++;
169 minerPolicyEstimator->removeTx(hash);
174 void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
176 // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
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())
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);
197 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
198 list<CTransaction> removed;
199 remove(tx, removed, true);
204 void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot)
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.
211 list<CTransaction> transactionsToRemove;
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);
223 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
224 list<CTransaction> removed;
225 remove(tx, removed, true);
229 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
231 // Remove transactions which depend on inputs of tx, recursively
232 list<CTransaction> result;
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)
240 remove(txConflict, removed, true);
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)
252 remove(txConflict, removed, true);
259 void CTxMemPool::removeExpired(unsigned int nBlockHeight)
261 // Remove expired txs from the mempool
263 list<CTransaction> transactionsToRemove;
264 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
266 const CTransaction& tx = it->GetTx();
267 if (IsExpiredTx(tx, nBlockHeight)) {
268 transactionsToRemove.push_back(tx);
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());
279 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
281 void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
282 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
285 std::vector<CTxMemPoolEntry> entries;
286 BOOST_FOREACH(const CTransaction& tx, vtx)
288 uint256 hash = tx.GetHash();
290 indexed_transaction_set::iterator i = mapTx.find(hash);
291 if (i != mapTx.end())
292 entries.push_back(*i);
294 BOOST_FOREACH(const CTransaction& tx, vtx)
296 std::list<CTransaction> dummy;
297 remove(tx, dummy, false);
298 removeConflicts(tx, conflicts);
299 ClearPrioritisation(tx.GetHash());
301 // After the txs in the new block have been removed from the mempool, update policy estimates
302 minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
306 * Called whenever the tip changes. Removes transactions which don't commit to
307 * the given branch ID from the mempool.
309 void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
312 std::list<CTransaction> transactionsToRemove;
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);
321 for (const CTransaction& tx : transactionsToRemove) {
322 std::list<CTransaction> removed;
323 remove(tx, removed, true);
327 void CTxMemPool::clear()
333 cachedInnerUsage = 0;
334 ++nTransactionsUpdated;
337 void CTxMemPool::check(const CCoinsViewCache *pcoins) const
339 if (nCheckFrequency == 0)
342 if (insecure_rand() >= nCheckFrequency)
345 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
347 uint64_t checkTotal = 0;
348 uint64_t innerUsage = 0;
350 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
351 const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
354 list<const CTxMemPoolEntry*> waitingOnDependants;
355 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
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());
369 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
370 assert(coins && coins->IsAvailable(txin.prevout.n));
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);
380 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
382 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
383 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
384 assert(!pcoins->GetNullifier(nf));
387 ZCIncrementalMerkleTree tree;
388 auto it = intermediates.find(joinsplit.anchor);
389 if (it != intermediates.end()) {
392 assert(pcoins->GetAnchorAt(joinsplit.anchor, tree));
395 BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
397 tree.append(commitment);
400 intermediates.insert(std::make_pair(tree.root(), tree));
403 waitingOnDependants.push_back(&(*it));
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);
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());
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;
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);
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);
447 assert(totalTxSize == checkTotal);
448 assert(innerUsage == cachedInnerUsage);
451 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
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());
461 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
464 indexed_transaction_set::const_iterator i = mapTx.find(hash);
465 if (i == mapTx.end()) return false;
470 CFeeRate CTxMemPool::estimateFee(int nBlocks) const
473 return minerPolicyEstimator->estimateFee(nBlocks);
475 double CTxMemPool::estimatePriority(int nBlocks) const
478 return minerPolicyEstimator->estimatePriority(nBlocks);
482 CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
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);
490 catch (const std::exception&) {
491 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
498 CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
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);
507 minerPolicyEstimator->Read(filein);
509 catch (const std::exception&) {
510 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
516 void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
520 std::pair<double, CAmount> &deltas = mapDeltas[hash];
521 deltas.first += dPriorityDelta;
522 deltas.second += nFeeDelta;
524 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
527 void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
530 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
531 if (pos == mapDeltas.end())
533 const std::pair<double, CAmount> &deltas = pos->second;
534 dPriorityDelta += deltas.first;
535 nFeeDelta += deltas.second;
538 void CTxMemPool::ClearPrioritisation(const uint256 hash)
541 mapDeltas.erase(hash);
544 bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
546 for (unsigned int i = 0; i < tx.vin.size(); i++)
547 if (exists(tx.vin[i].prevout.hash))
552 CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
554 bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const {
555 if (mempool.mapNullifiers.count(nf))
558 return base->GetNullifier(nf);
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.
566 if (mempool.lookup(txid, tx)) {
567 coins = CCoins(tx, MEMPOOL_HEIGHT);
570 return (base->GetCoins(txid, coins) && !coins.IsPruned());
573 bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
574 return mempool.exists(txid) || base->HaveCoins(txid);
577 size_t CTxMemPool::DynamicMemoryUsage() const {
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;