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 if (!tx.IsCoinImport()) {
108 for (unsigned int i = 0; i < tx.vin.size(); i++)
109 mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
111 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
112 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
113 mapNullifiers[nf] = &tx;
116 nTransactionsUpdated++;
117 totalTxSize += entry.GetTxSize();
118 cachedInnerUsage += entry.DynamicMemoryUsage();
119 minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
124 void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
127 const CTransaction& tx = entry.GetTx();
128 std::vector<CMempoolAddressDeltaKey> inserted;
130 uint256 txhash = tx.GetHash();
131 for (unsigned int j = 0; j < tx.vin.size(); j++) {
132 const CTxIn input = tx.vin[j];
133 const CTxOut &prevout = view.GetOutputFor(input);
134 if (prevout.scriptPubKey.IsPayToScriptHash()) {
135 vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22);
136 CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, j, 1);
137 CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
138 mapAddress.insert(make_pair(key, delta));
139 inserted.push_back(key);
141 else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
142 vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23);
143 CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, j, 1);
144 CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
145 mapAddress.insert(make_pair(key, delta));
146 inserted.push_back(key);
148 else if (prevout.scriptPubKey.IsPayToPublicKey()) {
149 vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.begin()+34);
150 CMempoolAddressDeltaKey key(1, Hash160(hashBytes), txhash, j, 1);
151 CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
152 mapAddress.insert(make_pair(key, delta));
153 inserted.push_back(key);
157 for (unsigned int k = 0; k < tx.vout.size(); k++) {
158 const CTxOut &out = tx.vout[k];
159 if (out.scriptPubKey.IsPayToScriptHash()) {
160 vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
161 CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, k, 0);
162 mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
163 inserted.push_back(key);
165 else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
166 vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
167 std::pair<addressDeltaMap::iterator,bool> ret;
168 CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, 0);
169 mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
170 inserted.push_back(key);
172 else if (out.scriptPubKey.IsPayToPublicKey()) {
173 vector<unsigned char> hashBytes(out.scriptPubKey.begin()+1, out.scriptPubKey.begin()+34);
174 std::pair<addressDeltaMap::iterator,bool> ret;
175 CMempoolAddressDeltaKey key(1, Hash160(hashBytes), txhash, k, 0);
176 mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
177 inserted.push_back(key);
181 mapAddressInserted.insert(make_pair(txhash, inserted));
184 bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint160, int> > &addresses,
185 std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results)
188 for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) {
189 addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first));
190 while (ait != mapAddress.end() && (*ait).first.addressBytes == (*it).first && (*ait).first.type == (*it).second) {
191 results.push_back(*ait);
198 bool CTxMemPool::removeAddressIndex(const uint256 txhash)
201 addressDeltaMapInserted::iterator it = mapAddressInserted.find(txhash);
203 if (it != mapAddressInserted.end()) {
204 std::vector<CMempoolAddressDeltaKey> keys = (*it).second;
205 for (std::vector<CMempoolAddressDeltaKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
206 mapAddress.erase(*mit);
208 mapAddressInserted.erase(it);
214 void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
218 const CTransaction& tx = entry.GetTx();
219 std::vector<CSpentIndexKey> inserted;
221 uint256 txhash = tx.GetHash();
222 for (unsigned int j = 0; j < tx.vin.size(); j++) {
223 const CTxIn input = tx.vin[j];
224 const CTxOut &prevout = view.GetOutputFor(input);
228 if (prevout.scriptPubKey.IsPayToScriptHash()) {
229 addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22));
232 else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
233 addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23));
236 else if (prevout.scriptPubKey.IsPayToPublicKey()) {
237 addressHash = Hash160(vector<unsigned char> (prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.begin()+34));
241 addressHash.SetNull();
245 CSpentIndexKey key = CSpentIndexKey(input.prevout.hash, input.prevout.n);
246 CSpentIndexValue value = CSpentIndexValue(txhash, j, -1, prevout.nValue, addressType, addressHash);
248 mapSpent.insert(make_pair(key, value));
249 inserted.push_back(key);
253 mapSpentInserted.insert(make_pair(txhash, inserted));
256 bool CTxMemPool::getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
259 mapSpentIndex::iterator it;
261 it = mapSpent.find(key);
262 if (it != mapSpent.end()) {
269 bool CTxMemPool::removeSpentIndex(const uint256 txhash)
272 mapSpentIndexInserted::iterator it = mapSpentInserted.find(txhash);
274 if (it != mapSpentInserted.end()) {
275 std::vector<CSpentIndexKey> keys = (*it).second;
276 for (std::vector<CSpentIndexKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
277 mapSpent.erase(*mit);
279 mapSpentInserted.erase(it);
285 void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
287 // Remove transaction from memory pool
290 std::deque<uint256> txToRemove;
291 txToRemove.push_back(origTx.GetHash());
292 if (fRecursive && !mapTx.count(origTx.GetHash())) {
293 // If recursively removing but origTx isn't in the mempool
294 // be sure to remove any children that are in the pool. This can
295 // happen during chain re-orgs if origTx isn't re-accepted into
296 // the mempool for any reason.
297 for (unsigned int i = 0; i < origTx.vout.size(); i++) {
298 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
299 if (it == mapNextTx.end())
301 txToRemove.push_back(it->second.ptx->GetHash());
304 while (!txToRemove.empty())
306 uint256 hash = txToRemove.front();
307 txToRemove.pop_front();
308 if (!mapTx.count(hash))
310 const CTransaction& tx = mapTx.find(hash)->GetTx();
312 for (unsigned int i = 0; i < tx.vout.size(); i++) {
313 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
314 if (it == mapNextTx.end())
316 txToRemove.push_back(it->second.ptx->GetHash());
319 BOOST_FOREACH(const CTxIn& txin, tx.vin)
320 mapNextTx.erase(txin.prevout);
321 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
322 BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) {
323 mapNullifiers.erase(nf);
327 removed.push_back(tx);
328 totalTxSize -= mapTx.find(hash)->GetTxSize();
329 cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
331 nTransactionsUpdated++;
332 minerPolicyEstimator->removeTx(hash);
333 removeAddressIndex(hash);
334 removeSpentIndex(hash);
339 void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
341 // Remove transactions spending a coinbase which are now immature
342 extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
343 if ( ASSETCHAINS_SYMBOL[0] == 0 )
344 COINBASE_MATURITY = _COINBASE_MATURITY;
345 // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
347 list<CTransaction> transactionsToRemove;
348 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
349 const CTransaction& tx = it->GetTx();
350 if (!CheckFinalTx(tx, flags)) {
351 transactionsToRemove.push_back(tx);
352 } else if (it->GetSpendsCoinbase()) {
353 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
354 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
355 if (it2 != mapTx.end())
357 const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
358 if (nCheckFrequency != 0) assert(coins);
359 if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
360 transactionsToRemove.push_back(tx);
366 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
367 list<CTransaction> removed;
368 remove(tx, removed, true);
373 void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot)
375 // If a block is disconnected from the tip, and the root changed,
376 // we must invalidate transactions from the mempool which spend
377 // from that root -- almost as though they were spending coinbases
378 // which are no longer valid to spend due to coinbase maturity.
380 list<CTransaction> transactionsToRemove;
382 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
383 const CTransaction& tx = it->GetTx();
384 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
385 if (joinsplit.anchor == invalidRoot) {
386 transactionsToRemove.push_back(tx);
392 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
393 list<CTransaction> removed;
394 remove(tx, removed, true);
398 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
400 // Remove transactions which depend on inputs of tx, recursively
401 list<CTransaction> result;
403 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
404 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
405 if (it != mapNextTx.end()) {
406 const CTransaction &txConflict = *it->second.ptx;
407 if (txConflict != tx)
409 remove(txConflict, removed, true);
414 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
415 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
416 std::map<uint256, const CTransaction*>::iterator it = mapNullifiers.find(nf);
417 if (it != mapNullifiers.end()) {
418 const CTransaction &txConflict = *it->second;
419 if (txConflict != tx)
421 remove(txConflict, removed, true);
428 int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t nTime,int32_t dispflag);
429 extern char ASSETCHAINS_SYMBOL[];
431 void CTxMemPool::removeExpired(unsigned int nBlockHeight)
433 CBlockIndex *tipindex;
434 // Remove expired txs from the mempool
436 list<CTransaction> transactionsToRemove;
437 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
439 const CTransaction& tx = it->GetTx();
440 tipindex = chainActive.LastTip();
441 if (IsExpiredTx(tx, nBlockHeight) || (ASSETCHAINS_SYMBOL[0] == 0 && tipindex != 0 && komodo_validate_interest(tx,tipindex->nHeight+1,tipindex->GetMedianTimePast() + 777,0)) < 0)
443 transactionsToRemove.push_back(tx);
446 for (const CTransaction& tx : transactionsToRemove) {
447 list<CTransaction> removed;
448 remove(tx, removed, true);
449 LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString());
454 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
456 void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
457 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
460 std::vector<CTxMemPoolEntry> entries;
461 BOOST_FOREACH(const CTransaction& tx, vtx)
463 uint256 hash = tx.GetHash();
465 indexed_transaction_set::iterator i = mapTx.find(hash);
466 if (i != mapTx.end())
467 entries.push_back(*i);
469 BOOST_FOREACH(const CTransaction& tx, vtx)
471 std::list<CTransaction> dummy;
472 remove(tx, dummy, false);
473 removeConflicts(tx, conflicts);
474 ClearPrioritisation(tx.GetHash());
476 // After the txs in the new block have been removed from the mempool, update policy estimates
477 minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
481 * Called whenever the tip changes. Removes transactions which don't commit to
482 * the given branch ID from the mempool.
484 void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
487 std::list<CTransaction> transactionsToRemove;
489 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
490 const CTransaction& tx = it->GetTx();
491 if (it->GetValidatedBranchId() != nMemPoolBranchId) {
492 transactionsToRemove.push_back(tx);
496 for (const CTransaction& tx : transactionsToRemove) {
497 std::list<CTransaction> removed;
498 remove(tx, removed, true);
502 void CTxMemPool::clear()
508 cachedInnerUsage = 0;
509 ++nTransactionsUpdated;
512 void CTxMemPool::check(const CCoinsViewCache *pcoins) const
514 if (nCheckFrequency == 0)
517 if (insecure_rand() >= nCheckFrequency)
520 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
522 uint64_t checkTotal = 0;
523 uint64_t innerUsage = 0;
525 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
526 const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
529 list<const CTxMemPoolEntry*> waitingOnDependants;
530 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
532 checkTotal += it->GetTxSize();
533 innerUsage += it->DynamicMemoryUsage();
534 const CTransaction& tx = it->GetTx();
535 bool fDependsWait = false;
536 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
537 // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
538 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
539 if (it2 != mapTx.end()) {
540 const CTransaction& tx2 = it2->GetTx();
541 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
544 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
545 assert(coins && coins->IsAvailable(txin.prevout.n));
547 // Check whether its inputs are marked in mapNextTx.
548 std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
549 assert(it3 != mapNextTx.end());
550 assert(it3->second.ptx == &tx);
551 assert(it3->second.n == i);
555 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
557 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
558 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
559 assert(!pcoins->GetNullifier(nf));
562 ZCIncrementalMerkleTree tree;
563 auto it = intermediates.find(joinsplit.anchor);
564 if (it != intermediates.end()) {
567 assert(pcoins->GetAnchorAt(joinsplit.anchor, tree));
570 BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
572 tree.append(commitment);
575 intermediates.insert(std::make_pair(tree.root(), tree));
578 waitingOnDependants.push_back(&(*it));
580 CValidationState state;
581 bool fCheckResult = tx.IsCoinBase() ||
582 Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
583 assert(fCheckResult);
584 UpdateCoins(tx, mempoolDuplicate, 1000000);
587 unsigned int stepsSinceLastRemove = 0;
588 while (!waitingOnDependants.empty()) {
589 const CTxMemPoolEntry* entry = waitingOnDependants.front();
590 waitingOnDependants.pop_front();
591 CValidationState state;
592 if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
593 waitingOnDependants.push_back(entry);
594 stepsSinceLastRemove++;
595 assert(stepsSinceLastRemove < waitingOnDependants.size());
597 bool fCheckResult = entry->GetTx().IsCoinBase() ||
598 Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
599 assert(fCheckResult);
600 UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000);
601 stepsSinceLastRemove = 0;
604 for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
605 uint256 hash = it->second.ptx->GetHash();
606 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
607 const CTransaction& tx = it2->GetTx();
608 assert(it2 != mapTx.end());
609 assert(&tx == it->second.ptx);
610 assert(tx.vin.size() > it->second.n);
611 assert(it->first == it->second.ptx->vin[it->second.n].prevout);
614 for (std::map<uint256, const CTransaction*>::const_iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); it++) {
615 uint256 hash = it->second->GetHash();
616 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
617 const CTransaction& tx = it2->GetTx();
618 assert(it2 != mapTx.end());
619 assert(&tx == it->second);
622 assert(totalTxSize == checkTotal);
623 assert(innerUsage == cachedInnerUsage);
626 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
631 vtxid.reserve(mapTx.size());
632 for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
633 vtxid.push_back(mi->GetTx().GetHash());
636 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
639 indexed_transaction_set::const_iterator i = mapTx.find(hash);
640 if (i == mapTx.end()) return false;
645 CFeeRate CTxMemPool::estimateFee(int nBlocks) const
648 return minerPolicyEstimator->estimateFee(nBlocks);
650 double CTxMemPool::estimatePriority(int nBlocks) const
653 return minerPolicyEstimator->estimatePriority(nBlocks);
657 CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
661 fileout << 109900; // version required to read: 0.10.99 or later
662 fileout << CLIENT_VERSION; // version that wrote the file
663 minerPolicyEstimator->Write(fileout);
665 catch (const std::exception&) {
666 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
673 CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
676 int nVersionRequired, nVersionThatWrote;
677 filein >> nVersionRequired >> nVersionThatWrote;
678 if (nVersionRequired > CLIENT_VERSION)
679 return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
682 minerPolicyEstimator->Read(filein);
684 catch (const std::exception&) {
685 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
691 void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
695 std::pair<double, CAmount> &deltas = mapDeltas[hash];
696 deltas.first += dPriorityDelta;
697 deltas.second += nFeeDelta;
699 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
702 void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
705 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
706 if (pos == mapDeltas.end())
708 const std::pair<double, CAmount> &deltas = pos->second;
709 dPriorityDelta += deltas.first;
710 nFeeDelta += deltas.second;
713 void CTxMemPool::ClearPrioritisation(const uint256 hash)
716 mapDeltas.erase(hash);
719 bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
721 for (unsigned int i = 0; i < tx.vin.size(); i++)
722 if (exists(tx.vin[i].prevout.hash))
727 CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
729 bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const {
730 if (mempool.mapNullifiers.count(nf))
733 return base->GetNullifier(nf);
736 bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
737 // If an entry in the mempool exists, always return that one, as it's guaranteed to never
738 // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
739 // transactions. First checking the underlying cache risks returning a pruned entry instead.
741 if (mempool.lookup(txid, tx)) {
742 coins = CCoins(tx, MEMPOOL_HEIGHT);
745 return (base->GetCoins(txid, coins) && !coins.IsPruned());
748 bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
749 return mempool.exists(txid) || base->HaveCoins(txid);
752 size_t CTxMemPool::DynamicMemoryUsage() const {
754 // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
755 return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;