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);
122 void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
125 const CTransaction& tx = entry.GetTx();
126 std::vector<CMempoolAddressDeltaKey> inserted;
128 uint256 txhash = tx.GetHash();
129 for (unsigned int j = 0; j < tx.vin.size(); j++) {
130 const CTxIn input = tx.vin[j];
131 const CTxOut &prevout = view.GetOutputFor(input);
132 if (prevout.scriptPubKey.IsPayToScriptHash()) {
133 vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22);
134 CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, j, 1);
135 CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
136 mapAddress.insert(make_pair(key, delta));
137 inserted.push_back(key);
138 } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
139 vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23);
140 CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, j, 1);
141 CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
142 mapAddress.insert(make_pair(key, delta));
143 inserted.push_back(key);
147 for (unsigned int k = 0; k < tx.vout.size(); k++) {
148 const CTxOut &out = tx.vout[k];
149 if (out.scriptPubKey.IsPayToScriptHash()) {
150 vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
151 CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, k, 0);
152 mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
153 inserted.push_back(key);
154 } else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
155 vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
156 std::pair<addressDeltaMap::iterator,bool> ret;
157 CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, 0);
158 mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
159 inserted.push_back(key);
163 mapAddressInserted.insert(make_pair(txhash, inserted));
166 bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint160, int> > &addresses,
167 std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results)
170 for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) {
171 addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first));
172 while (ait != mapAddress.end() && (*ait).first.addressBytes == (*it).first && (*ait).first.type == (*it).second) {
173 results.push_back(*ait);
180 bool CTxMemPool::removeAddressIndex(const uint256 txhash)
183 addressDeltaMapInserted::iterator it = mapAddressInserted.find(txhash);
185 if (it != mapAddressInserted.end()) {
186 std::vector<CMempoolAddressDeltaKey> keys = (*it).second;
187 for (std::vector<CMempoolAddressDeltaKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
188 mapAddress.erase(*mit);
190 mapAddressInserted.erase(it);
196 void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
200 const CTransaction& tx = entry.GetTx();
201 std::vector<CSpentIndexKey> inserted;
203 uint256 txhash = tx.GetHash();
204 for (unsigned int j = 0; j < tx.vin.size(); j++) {
205 const CTxIn input = tx.vin[j];
206 const CTxOut &prevout = view.GetOutputFor(input);
210 if (prevout.scriptPubKey.IsPayToScriptHash()) {
211 addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22));
213 } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
214 addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23));
217 addressHash.SetNull();
221 CSpentIndexKey key = CSpentIndexKey(input.prevout.hash, input.prevout.n);
222 CSpentIndexValue value = CSpentIndexValue(txhash, j, -1, prevout.nValue, addressType, addressHash);
224 mapSpent.insert(make_pair(key, value));
225 inserted.push_back(key);
229 mapSpentInserted.insert(make_pair(txhash, inserted));
232 bool CTxMemPool::getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
235 mapSpentIndex::iterator it;
237 it = mapSpent.find(key);
238 if (it != mapSpent.end()) {
245 bool CTxMemPool::removeSpentIndex(const uint256 txhash)
248 mapSpentIndexInserted::iterator it = mapSpentInserted.find(txhash);
250 if (it != mapSpentInserted.end()) {
251 std::vector<CSpentIndexKey> keys = (*it).second;
252 for (std::vector<CSpentIndexKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
253 mapSpent.erase(*mit);
255 mapSpentInserted.erase(it);
261 void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
263 // Remove transaction from memory pool
266 std::deque<uint256> txToRemove;
267 txToRemove.push_back(origTx.GetHash());
268 if (fRecursive && !mapTx.count(origTx.GetHash())) {
269 // If recursively removing but origTx isn't in the mempool
270 // be sure to remove any children that are in the pool. This can
271 // happen during chain re-orgs if origTx isn't re-accepted into
272 // the mempool for any reason.
273 for (unsigned int i = 0; i < origTx.vout.size(); i++) {
274 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
275 if (it == mapNextTx.end())
277 txToRemove.push_back(it->second.ptx->GetHash());
280 while (!txToRemove.empty())
282 uint256 hash = txToRemove.front();
283 txToRemove.pop_front();
284 if (!mapTx.count(hash))
286 const CTransaction& tx = mapTx.find(hash)->GetTx();
288 for (unsigned int i = 0; i < tx.vout.size(); i++) {
289 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
290 if (it == mapNextTx.end())
292 txToRemove.push_back(it->second.ptx->GetHash());
295 BOOST_FOREACH(const CTxIn& txin, tx.vin)
296 mapNextTx.erase(txin.prevout);
297 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
298 BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) {
299 mapNullifiers.erase(nf);
303 removed.push_back(tx);
304 totalTxSize -= mapTx.find(hash)->GetTxSize();
305 cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
307 nTransactionsUpdated++;
308 minerPolicyEstimator->removeTx(hash);
309 removeAddressIndex(hash);
310 removeSpentIndex(hash);
315 void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
317 // Remove transactions spending a coinbase which are now immature
318 extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
319 if ( ASSETCHAINS_SYMBOL[0] == 0 )
320 COINBASE_MATURITY = _COINBASE_MATURITY;
321 // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
323 list<CTransaction> transactionsToRemove;
324 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
325 const CTransaction& tx = it->GetTx();
326 if (!CheckFinalTx(tx, flags)) {
327 transactionsToRemove.push_back(tx);
328 } else if (it->GetSpendsCoinbase()) {
329 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
330 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
331 if (it2 != mapTx.end())
333 const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
334 if (nCheckFrequency != 0) assert(coins);
335 if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
336 transactionsToRemove.push_back(tx);
342 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
343 list<CTransaction> removed;
344 remove(tx, removed, true);
349 void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot)
351 // If a block is disconnected from the tip, and the root changed,
352 // we must invalidate transactions from the mempool which spend
353 // from that root -- almost as though they were spending coinbases
354 // which are no longer valid to spend due to coinbase maturity.
356 list<CTransaction> transactionsToRemove;
358 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
359 const CTransaction& tx = it->GetTx();
360 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
361 if (joinsplit.anchor == invalidRoot) {
362 transactionsToRemove.push_back(tx);
368 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
369 list<CTransaction> removed;
370 remove(tx, removed, true);
374 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
376 // Remove transactions which depend on inputs of tx, recursively
377 list<CTransaction> result;
379 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
380 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
381 if (it != mapNextTx.end()) {
382 const CTransaction &txConflict = *it->second.ptx;
383 if (txConflict != tx)
385 remove(txConflict, removed, true);
390 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
391 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
392 std::map<uint256, const CTransaction*>::iterator it = mapNullifiers.find(nf);
393 if (it != mapNullifiers.end()) {
394 const CTransaction &txConflict = *it->second;
395 if (txConflict != tx)
397 remove(txConflict, removed, true);
404 int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t nTime,int32_t dispflag);
405 extern char ASSETCHAINS_SYMBOL[];
407 void CTxMemPool::removeExpired(unsigned int nBlockHeight)
409 CBlockIndex *tipindex;
410 // Remove expired txs from the mempool
412 list<CTransaction> transactionsToRemove;
413 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
415 const CTransaction& tx = it->GetTx();
416 tipindex = chainActive.Tip();
417 if (IsExpiredTx(tx, nBlockHeight) || (ASSETCHAINS_SYMBOL[0] == 0 && tipindex != 0 && komodo_validate_interest(tx,tipindex->nHeight+1,tipindex->GetMedianTimePast() + 777,0)) < 0)
419 transactionsToRemove.push_back(tx);
422 for (const CTransaction& tx : transactionsToRemove) {
423 list<CTransaction> removed;
424 remove(tx, removed, true);
425 LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString());
430 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
432 void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
433 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
436 std::vector<CTxMemPoolEntry> entries;
437 BOOST_FOREACH(const CTransaction& tx, vtx)
439 uint256 hash = tx.GetHash();
441 indexed_transaction_set::iterator i = mapTx.find(hash);
442 if (i != mapTx.end())
443 entries.push_back(*i);
445 BOOST_FOREACH(const CTransaction& tx, vtx)
447 std::list<CTransaction> dummy;
448 remove(tx, dummy, false);
449 removeConflicts(tx, conflicts);
450 ClearPrioritisation(tx.GetHash());
452 // After the txs in the new block have been removed from the mempool, update policy estimates
453 minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
457 * Called whenever the tip changes. Removes transactions which don't commit to
458 * the given branch ID from the mempool.
460 void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
463 std::list<CTransaction> transactionsToRemove;
465 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
466 const CTransaction& tx = it->GetTx();
467 if (it->GetValidatedBranchId() != nMemPoolBranchId) {
468 transactionsToRemove.push_back(tx);
472 for (const CTransaction& tx : transactionsToRemove) {
473 std::list<CTransaction> removed;
474 remove(tx, removed, true);
478 void CTxMemPool::clear()
484 cachedInnerUsage = 0;
485 ++nTransactionsUpdated;
488 void CTxMemPool::check(const CCoinsViewCache *pcoins) const
490 if (nCheckFrequency == 0)
493 if (insecure_rand() >= nCheckFrequency)
496 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
498 uint64_t checkTotal = 0;
499 uint64_t innerUsage = 0;
501 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
502 const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
505 list<const CTxMemPoolEntry*> waitingOnDependants;
506 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
508 checkTotal += it->GetTxSize();
509 innerUsage += it->DynamicMemoryUsage();
510 const CTransaction& tx = it->GetTx();
511 bool fDependsWait = false;
512 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
513 // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
514 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
515 if (it2 != mapTx.end()) {
516 const CTransaction& tx2 = it2->GetTx();
517 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
520 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
521 assert(coins && coins->IsAvailable(txin.prevout.n));
523 // Check whether its inputs are marked in mapNextTx.
524 std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
525 assert(it3 != mapNextTx.end());
526 assert(it3->second.ptx == &tx);
527 assert(it3->second.n == i);
531 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
533 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
534 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
535 assert(!pcoins->GetNullifier(nf));
538 ZCIncrementalMerkleTree tree;
539 auto it = intermediates.find(joinsplit.anchor);
540 if (it != intermediates.end()) {
543 assert(pcoins->GetAnchorAt(joinsplit.anchor, tree));
546 BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
548 tree.append(commitment);
551 intermediates.insert(std::make_pair(tree.root(), tree));
554 waitingOnDependants.push_back(&(*it));
556 CValidationState state;
557 bool fCheckResult = tx.IsCoinBase() ||
558 Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
559 assert(fCheckResult);
560 UpdateCoins(tx, mempoolDuplicate, 1000000);
563 unsigned int stepsSinceLastRemove = 0;
564 while (!waitingOnDependants.empty()) {
565 const CTxMemPoolEntry* entry = waitingOnDependants.front();
566 waitingOnDependants.pop_front();
567 CValidationState state;
568 if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
569 waitingOnDependants.push_back(entry);
570 stepsSinceLastRemove++;
571 assert(stepsSinceLastRemove < waitingOnDependants.size());
573 bool fCheckResult = entry->GetTx().IsCoinBase() ||
574 Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
575 assert(fCheckResult);
576 UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000);
577 stepsSinceLastRemove = 0;
580 for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
581 uint256 hash = it->second.ptx->GetHash();
582 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
583 const CTransaction& tx = it2->GetTx();
584 assert(it2 != mapTx.end());
585 assert(&tx == it->second.ptx);
586 assert(tx.vin.size() > it->second.n);
587 assert(it->first == it->second.ptx->vin[it->second.n].prevout);
590 for (std::map<uint256, const CTransaction*>::const_iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); it++) {
591 uint256 hash = it->second->GetHash();
592 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
593 const CTransaction& tx = it2->GetTx();
594 assert(it2 != mapTx.end());
595 assert(&tx == it->second);
598 assert(totalTxSize == checkTotal);
599 assert(innerUsage == cachedInnerUsage);
602 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
607 vtxid.reserve(mapTx.size());
608 for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
609 vtxid.push_back(mi->GetTx().GetHash());
612 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
615 indexed_transaction_set::const_iterator i = mapTx.find(hash);
616 if (i == mapTx.end()) return false;
621 CFeeRate CTxMemPool::estimateFee(int nBlocks) const
624 return minerPolicyEstimator->estimateFee(nBlocks);
626 double CTxMemPool::estimatePriority(int nBlocks) const
629 return minerPolicyEstimator->estimatePriority(nBlocks);
633 CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
637 fileout << 109900; // version required to read: 0.10.99 or later
638 fileout << CLIENT_VERSION; // version that wrote the file
639 minerPolicyEstimator->Write(fileout);
641 catch (const std::exception&) {
642 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
649 CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
652 int nVersionRequired, nVersionThatWrote;
653 filein >> nVersionRequired >> nVersionThatWrote;
654 if (nVersionRequired > CLIENT_VERSION)
655 return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
658 minerPolicyEstimator->Read(filein);
660 catch (const std::exception&) {
661 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
667 void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
671 std::pair<double, CAmount> &deltas = mapDeltas[hash];
672 deltas.first += dPriorityDelta;
673 deltas.second += nFeeDelta;
675 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
678 void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
681 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
682 if (pos == mapDeltas.end())
684 const std::pair<double, CAmount> &deltas = pos->second;
685 dPriorityDelta += deltas.first;
686 nFeeDelta += deltas.second;
689 void CTxMemPool::ClearPrioritisation(const uint256 hash)
692 mapDeltas.erase(hash);
695 bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
697 for (unsigned int i = 0; i < tx.vin.size(); i++)
698 if (exists(tx.vin[i].prevout.hash))
703 CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
705 bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const {
706 if (mempool.mapNullifiers.count(nf))
709 return base->GetNullifier(nf);
712 bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
713 // If an entry in the mempool exists, always return that one, as it's guaranteed to never
714 // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
715 // transactions. First checking the underlying cache risks returning a pruned entry instead.
717 if (mempool.lookup(txid, tx)) {
718 coins = CCoins(tx, MEMPOOL_HEIGHT);
721 return (base->GetCoins(txid, coins) && !coins.IsPruned());
724 bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
725 return mempool.exists(txid) || base->HaveCoins(txid);
728 size_t CTxMemPool::DynamicMemoryUsage() const {
730 // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
731 return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;