]> Git Repo - VerusCoin.git/blame - src/txmempool.cpp
Add regression tests and test vectors for Sapling merkle tree
[VerusCoin.git] / src / txmempool.cpp
CommitLineData
319b1160 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
7329fdd1 3// Distributed under the MIT software license, see the accompanying
319b1160
GA
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
319b1160 6#include "txmempool.h"
611116d4 7
71697f97 8#include "clientversion.h"
691161d4 9#include "consensus/consensus.h"
da29ecbc 10#include "consensus/validation.h"
b7b4318f 11#include "main.h"
b649e039 12#include "policy/fees.h"
fa736190 13#include "streams.h"
f5b35d23 14#include "timedata.h"
ad49c256 15#include "util.h"
a372168e 16#include "utilmoneystr.h"
85c579e3 17#include "version.h"
319b1160
GA
18
19using namespace std;
20
8bdd2877 21CTxMemPoolEntry::CTxMemPoolEntry():
a4b25180
SD
22 nFee(0), nTxSize(0), nModSize(0), nUsageSize(0), nTime(0), dPriority(0.0),
23 hadNoDependencies(false), spendsCoinbase(false)
4d707d51
GA
24{
25 nHeight = MEMPOOL_HEIGHT;
26}
27
a372168e 28CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
4d707d51 29 int64_t _nTime, double _dPriority,
a4b25180 30 unsigned int _nHeight, bool poolHasNoInputsOf,
34a64fe0 31 bool _spendsCoinbase, uint32_t _nBranchId):
b649e039 32 tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight),
a4b25180 33 hadNoDependencies(poolHasNoInputsOf),
34a64fe0 34 spendsCoinbase(_spendsCoinbase), nBranchId(_nBranchId)
4d707d51
GA
35{
36 nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
c26649f9 37 nModSize = tx.CalculateModifiedSize(nTxSize);
6bd1d60c 38 nUsageSize = RecursiveDynamicUsage(tx);
e328fa32 39 feeRate = CFeeRate(nFee, nTxSize);
4d707d51
GA
40}
41
42CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
43{
44 *this = other;
45}
46
47double
48CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
49{
a372168e 50 CAmount nValueIn = tx.GetValueOut()+nFee;
c26649f9 51 double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
4d707d51
GA
52 double dResult = dPriority + deltaPriority;
53 return dResult;
54}
55
8bdd2877 56CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
b649e039 57 nTransactionsUpdated(0)
319b1160
GA
58{
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
934fd197 62 nCheckFrequency = 0;
171ca774 63
b649e039 64 minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
171ca774
GA
65}
66
67CTxMemPool::~CTxMemPool()
68{
69 delete minerPolicyEstimator;
319b1160
GA
70}
71
72void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
73{
74 LOCK(cs);
75
76 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
77
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
81 it++;
82 }
83}
84
85unsigned int CTxMemPool::GetTransactionsUpdated() const
86{
87 LOCK(cs);
88 return nTransactionsUpdated;
89}
90
91void CTxMemPool::AddTransactionsUpdated(unsigned int n)
92{
93 LOCK(cs);
94 nTransactionsUpdated += n;
95}
96
97
b649e039 98bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
319b1160
GA
99{
100 // Add to memory pool without checking anything.
101 // Used by main.cpp AcceptToMemoryPool(), which DOES do
102 // all the appropriate checks.
103 LOCK(cs);
e328fa32
AH
104 mapTx.insert(entry);
105 const CTransaction& tx = mapTx.find(hash)->GetTx();
b649e039
AM
106 for (unsigned int i = 0; i < tx.vin.size(); i++)
107 mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
b7e4abd6 108 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
22de1602 109 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
9669920f 110 mapSproutNullifiers[nf] = &tx;
d66877af
SB
111 }
112 }
cab341e1
EOW
113 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
114 mapSaplingNullifiers[spendDescription.nullifier] = &tx;
115 }
b649e039
AM
116 nTransactionsUpdated++;
117 totalTxSize += entry.GetTxSize();
bde5c8b0 118 cachedInnerUsage += entry.DynamicMemoryUsage();
b649e039
AM
119 minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
120
319b1160
GA
121 return true;
122}
123
9669920f 124
7fd6219a 125void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
319b1160
GA
126{
127 // Remove transaction from memory pool
128 {
129 LOCK(cs);
7fd6219a 130 std::deque<uint256> txToRemove;
805344dc
S
131 txToRemove.push_back(origTx.GetHash());
132 if (fRecursive && !mapTx.count(origTx.GetHash())) {
ad9e86dc
GA
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++) {
805344dc 138 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
ad9e86dc
GA
139 if (it == mapNextTx.end())
140 continue;
805344dc 141 txToRemove.push_back(it->second.ptx->GetHash());
ad9e86dc
GA
142 }
143 }
7fd6219a 144 while (!txToRemove.empty())
319b1160 145 {
7fd6219a
MC
146 uint256 hash = txToRemove.front();
147 txToRemove.pop_front();
148 if (!mapTx.count(hash))
149 continue;
e328fa32 150 const CTransaction& tx = mapTx.find(hash)->GetTx();
7fd6219a
MC
151 if (fRecursive) {
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())
155 continue;
805344dc 156 txToRemove.push_back(it->second.ptx->GetHash());
7fd6219a
MC
157 }
158 }
319b1160
GA
159 BOOST_FOREACH(const CTxIn& txin, tx.vin)
160 mapNextTx.erase(txin.prevout);
b7e4abd6 161 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
22de1602 162 BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) {
9669920f 163 mapSproutNullifiers.erase(nf);
d66877af
SB
164 }
165 }
cab341e1
EOW
166 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
167 mapSaplingNullifiers.erase(spendDescription.nullifier);
168 }
7fd6219a 169 removed.push_back(tx);
e328fa32
AH
170 totalTxSize -= mapTx.find(hash)->GetTxSize();
171 cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage();
319b1160
GA
172 mapTx.erase(hash);
173 nTransactionsUpdated++;
b649e039 174 minerPolicyEstimator->removeTx(hash);
319b1160
GA
175 }
176 }
319b1160
GA
177}
178
233c9eb6 179void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
723d12c0 180{
c944d161 181 // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
723d12c0
MC
182 LOCK(cs);
183 list<CTransaction> transactionsToRemove;
e328fa32
AH
184 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
185 const CTransaction& tx = it->GetTx();
233c9eb6 186 if (!CheckFinalTx(tx, flags)) {
f5b35d23 187 transactionsToRemove.push_back(tx);
a4b25180 188 } else if (it->GetSpendsCoinbase()) {
f5b35d23
MC
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())
192 continue;
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);
197 break;
198 }
723d12c0
MC
199 }
200 }
201 }
202 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
203 list<CTransaction> removed;
204 remove(tx, removed, true);
205 }
206}
207
a8ac403d
SB
208
209void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot)
210{
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.
215 LOCK(cs);
216 list<CTransaction> transactionsToRemove;
217
e328fa32
AH
218 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
219 const CTransaction& tx = it->GetTx();
b7e4abd6
SB
220 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
221 if (joinsplit.anchor == invalidRoot) {
a8ac403d
SB
222 transactionsToRemove.push_back(tx);
223 break;
224 }
225 }
226 }
227
228 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
229 list<CTransaction> removed;
230 remove(tx, removed, true);
231 }
232}
233
93a18a36 234void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
319b1160
GA
235{
236 // Remove transactions which depend on inputs of tx, recursively
98e84aae 237 list<CTransaction> result;
319b1160
GA
238 LOCK(cs);
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)
93a18a36
GA
244 {
245 remove(txConflict, removed, true);
246 }
319b1160
GA
247 }
248 }
d66877af 249
b7e4abd6 250 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
22de1602 251 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
9669920f
EOW
252 std::map<uint256, const CTransaction*>::iterator it = mapSproutNullifiers.find(nf);
253 if (it != mapSproutNullifiers.end()) {
d66877af 254 const CTransaction &txConflict = *it->second;
9669920f 255 if (txConflict != tx) {
d66877af 256 remove(txConflict, removed, true);
9669920f 257 }
d66877af
SB
258 }
259 }
260 }
cab341e1
EOW
261 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
262 std::map<uint256, const CTransaction*>::iterator it = mapSaplingNullifiers.find(spendDescription.nullifier);
263 if (it != mapSaplingNullifiers.end()) {
264 const CTransaction &txConflict = *it->second;
9669920f 265 if (txConflict != tx) {
cab341e1 266 remove(txConflict, removed, true);
9669920f 267 }
cab341e1
EOW
268 }
269 }
319b1160
GA
270}
271
9bb37bf0
JG
272void CTxMemPool::removeExpired(unsigned int nBlockHeight)
273{
274 // Remove expired txs from the mempool
275 LOCK(cs);
276 list<CTransaction> transactionsToRemove;
277 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
278 {
279 const CTransaction& tx = it->GetTx();
280 if (IsExpiredTx(tx, nBlockHeight)) {
281 transactionsToRemove.push_back(tx);
282 }
283 }
284 for (const CTransaction& tx : transactionsToRemove) {
285 list<CTransaction> removed;
286 remove(tx, removed, true);
eb138626 287 LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString());
9bb37bf0
JG
288 }
289}
290
7329fdd1
MF
291/**
292 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
293 */
171ca774 294void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
b649e039 295 std::list<CTransaction>& conflicts, bool fCurrentEstimate)
171ca774
GA
296{
297 LOCK(cs);
298 std::vector<CTxMemPoolEntry> entries;
299 BOOST_FOREACH(const CTransaction& tx, vtx)
300 {
805344dc 301 uint256 hash = tx.GetHash();
e328fa32
AH
302
303 indexed_transaction_set::iterator i = mapTx.find(hash);
304 if (i != mapTx.end())
305 entries.push_back(*i);
171ca774 306 }
171ca774
GA
307 BOOST_FOREACH(const CTransaction& tx, vtx)
308 {
309 std::list<CTransaction> dummy;
310 remove(tx, dummy, false);
311 removeConflicts(tx, conflicts);
805344dc 312 ClearPrioritisation(tx.GetHash());
171ca774 313 }
b649e039
AM
314 // After the txs in the new block have been removed from the mempool, update policy estimates
315 minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
171ca774
GA
316}
317
34a64fe0
JG
318/**
319 * Called whenever the tip changes. Removes transactions which don't commit to
320 * the given branch ID from the mempool.
321 */
322void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId)
323{
324 LOCK(cs);
325 std::list<CTransaction> transactionsToRemove;
326
327 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
328 const CTransaction& tx = it->GetTx();
329 if (it->GetValidatedBranchId() != nMemPoolBranchId) {
330 transactionsToRemove.push_back(tx);
331 }
332 }
333
334 for (const CTransaction& tx : transactionsToRemove) {
335 std::list<CTransaction> removed;
336 remove(tx, removed, true);
337 }
338}
339
319b1160
GA
340void CTxMemPool::clear()
341{
342 LOCK(cs);
343 mapTx.clear();
344 mapNextTx.clear();
6f2c26a4 345 totalTxSize = 0;
bde5c8b0 346 cachedInnerUsage = 0;
319b1160
GA
347 ++nTransactionsUpdated;
348}
349
d0867acb 350void CTxMemPool::check(const CCoinsViewCache *pcoins) const
319b1160 351{
934fd197
PW
352 if (nCheckFrequency == 0)
353 return;
354
355 if (insecure_rand() >= nCheckFrequency)
319b1160
GA
356 return;
357
358 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
359
6f2c26a4 360 uint64_t checkTotal = 0;
bde5c8b0 361 uint64_t innerUsage = 0;
6f2c26a4 362
b7b4318f 363 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
722d811f 364 const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
b7b4318f 365
319b1160 366 LOCK(cs);
b7b4318f 367 list<const CTxMemPoolEntry*> waitingOnDependants;
e328fa32 368 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
319b1160 369 unsigned int i = 0;
e328fa32
AH
370 checkTotal += it->GetTxSize();
371 innerUsage += it->DynamicMemoryUsage();
372 const CTransaction& tx = it->GetTx();
b7b4318f 373 bool fDependsWait = false;
4d707d51 374 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
319b1160 375 // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
e328fa32 376 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
319b1160 377 if (it2 != mapTx.end()) {
e328fa32 378 const CTransaction& tx2 = it2->GetTx();
4d707d51 379 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
b7b4318f 380 fDependsWait = true;
319b1160 381 } else {
629d75fa
PW
382 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
383 assert(coins && coins->IsAvailable(txin.prevout.n));
319b1160
GA
384 }
385 // Check whether its inputs are marked in mapNextTx.
386 std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
387 assert(it3 != mapNextTx.end());
4d707d51 388 assert(it3->second.ptx == &tx);
319b1160
GA
389 assert(it3->second.n == i);
390 i++;
391 }
a667caec
SB
392
393 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
394
b7e4abd6 395 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
22de1602 396 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
708c87f1 397 assert(!pcoins->GetNullifier(nf, SPROUT_NULLIFIER));
d66877af
SB
398 }
399
434f3284 400 ZCIncrementalMerkleTree tree;
b7e4abd6 401 auto it = intermediates.find(joinsplit.anchor);
a667caec
SB
402 if (it != intermediates.end()) {
403 tree = it->second;
404 } else {
b7e4abd6 405 assert(pcoins->GetAnchorAt(joinsplit.anchor, tree));
a667caec
SB
406 }
407
b7e4abd6 408 BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
a667caec
SB
409 {
410 tree.append(commitment);
411 }
412
413 intermediates.insert(std::make_pair(tree.root(), tree));
d66877af 414 }
cab341e1
EOW
415 for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
416 assert(!pcoins->GetNullifier(spendDescription.nullifier, SAPLING_NULLIFIER));
417 }
b7b4318f 418 if (fDependsWait)
e328fa32 419 waitingOnDependants.push_back(&(*it));
b7b4318f 420 else {
d7621ccf 421 CValidationState state;
722d811f
JT
422 bool fCheckResult = tx.IsCoinBase() ||
423 Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
424 assert(fCheckResult);
8cb98d91 425 UpdateCoins(tx, mempoolDuplicate, 1000000);
b7b4318f
MC
426 }
427 }
428 unsigned int stepsSinceLastRemove = 0;
429 while (!waitingOnDependants.empty()) {
430 const CTxMemPoolEntry* entry = waitingOnDependants.front();
431 waitingOnDependants.pop_front();
432 CValidationState state;
433 if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
434 waitingOnDependants.push_back(entry);
435 stepsSinceLastRemove++;
436 assert(stepsSinceLastRemove < waitingOnDependants.size());
437 } else {
722d811f
JT
438 bool fCheckResult = entry->GetTx().IsCoinBase() ||
439 Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus());
440 assert(fCheckResult);
8cb98d91 441 UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000);
b7b4318f
MC
442 stepsSinceLastRemove = 0;
443 }
319b1160
GA
444 }
445 for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
805344dc 446 uint256 hash = it->second.ptx->GetHash();
e328fa32
AH
447 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
448 const CTransaction& tx = it2->GetTx();
319b1160 449 assert(it2 != mapTx.end());
4d707d51
GA
450 assert(&tx == it->second.ptx);
451 assert(tx.vin.size() > it->second.n);
319b1160
GA
452 assert(it->first == it->second.ptx->vin[it->second.n].prevout);
453 }
6f2c26a4 454
708c87f1
EOW
455 checkNullifiers(SPROUT_NULLIFIER);
456 checkNullifiers(SAPLING_NULLIFIER);
d66877af 457
6f2c26a4 458 assert(totalTxSize == checkTotal);
bde5c8b0 459 assert(innerUsage == cachedInnerUsage);
319b1160
GA
460}
461
708c87f1 462void CTxMemPool::checkNullifiers(NullifierType type) const
685e936c 463{
708c87f1
EOW
464 const std::map<uint256, const CTransaction*>* mapToUse;
465 switch (type) {
466 case SPROUT_NULLIFIER:
9669920f 467 mapToUse = &mapSproutNullifiers;
708c87f1
EOW
468 break;
469 case SAPLING_NULLIFIER:
470 mapToUse = &mapSaplingNullifiers;
471 break;
472 default:
1f9dfbb9 473 throw runtime_error("Unknown nullifier type");
708c87f1 474 }
685e936c
EOW
475 for (const auto& entry : *mapToUse) {
476 uint256 hash = entry.second->GetHash();
477 CTxMemPool::indexed_transaction_set::const_iterator findTx = mapTx.find(hash);
478 const CTransaction& tx = findTx->GetTx();
479 assert(findTx != mapTx.end());
480 assert(&tx == entry.second);
481 }
482}
483
4d707d51 484void CTxMemPool::queryHashes(vector<uint256>& vtxid)
319b1160
GA
485{
486 vtxid.clear();
487
488 LOCK(cs);
489 vtxid.reserve(mapTx.size());
e328fa32
AH
490 for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
491 vtxid.push_back(mi->GetTx().GetHash());
319b1160
GA
492}
493
494bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
495{
496 LOCK(cs);
e328fa32 497 indexed_transaction_set::const_iterator i = mapTx.find(hash);
319b1160 498 if (i == mapTx.end()) return false;
e328fa32 499 result = i->GetTx();
319b1160
GA
500 return true;
501}
a0fa20a1 502
171ca774
GA
503CFeeRate CTxMemPool::estimateFee(int nBlocks) const
504{
505 LOCK(cs);
506 return minerPolicyEstimator->estimateFee(nBlocks);
507}
508double CTxMemPool::estimatePriority(int nBlocks) const
509{
510 LOCK(cs);
511 return minerPolicyEstimator->estimatePriority(nBlocks);
512}
513
514bool
515CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
516{
517 try {
518 LOCK(cs);
b649e039 519 fileout << 109900; // version required to read: 0.10.99 or later
171ca774
GA
520 fileout << CLIENT_VERSION; // version that wrote the file
521 minerPolicyEstimator->Write(fileout);
522 }
27df4123 523 catch (const std::exception&) {
7ff9d122 524 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
171ca774
GA
525 return false;
526 }
527 return true;
528}
529
530bool
531CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
532{
533 try {
534 int nVersionRequired, nVersionThatWrote;
535 filein >> nVersionRequired >> nVersionThatWrote;
536 if (nVersionRequired > CLIENT_VERSION)
5262fde0 537 return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
171ca774
GA
538
539 LOCK(cs);
b649e039 540 minerPolicyEstimator->Read(filein);
171ca774 541 }
27df4123 542 catch (const std::exception&) {
7ff9d122 543 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
171ca774
GA
544 return false;
545 }
546 return true;
547}
548
a372168e 549void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
2a72d459
LD
550{
551 {
552 LOCK(cs);
a372168e 553 std::pair<double, CAmount> &deltas = mapDeltas[hash];
2a72d459
LD
554 deltas.first += dPriorityDelta;
555 deltas.second += nFeeDelta;
556 }
a372168e 557 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
2a72d459
LD
558}
559
a372168e 560void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
2a72d459
LD
561{
562 LOCK(cs);
a372168e 563 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
2a72d459
LD
564 if (pos == mapDeltas.end())
565 return;
a372168e 566 const std::pair<double, CAmount> &deltas = pos->second;
2a72d459
LD
567 dPriorityDelta += deltas.first;
568 nFeeDelta += deltas.second;
569}
570
571void CTxMemPool::ClearPrioritisation(const uint256 hash)
572{
573 LOCK(cs);
574 mapDeltas.erase(hash);
575}
576
b649e039
AM
577bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
578{
579 for (unsigned int i = 0; i < tx.vin.size(); i++)
580 if (exists(tx.vin[i].prevout.hash))
581 return false;
582 return true;
583}
171ca774 584
708c87f1 585bool CTxMemPool::nullifierExists(const uint256& nullifier, NullifierType type) const
685e936c 586{
708c87f1
EOW
587 switch (type) {
588 case SPROUT_NULLIFIER:
9669920f 589 return mapSproutNullifiers.count(nullifier);
708c87f1
EOW
590 case SAPLING_NULLIFIER:
591 return mapSaplingNullifiers.count(nullifier);
592 default:
1f9dfbb9 593 throw runtime_error("Unknown nullifier type");
708c87f1 594 }
685e936c 595}
a0fa20a1 596
685e936c 597CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
d66877af 598
708c87f1 599bool CCoinsViewMemPool::GetNullifier(const uint256 &nf, NullifierType type) const
685e936c 600{
708c87f1 601 return mempool.nullifierExists(nf, type) || base->GetNullifier(nf, type);
d66877af
SB
602}
603
a3dc587a 604bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
ad08d0b9
PW
605 // If an entry in the mempool exists, always return that one, as it's guaranteed to never
606 // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
607 // transactions. First checking the underlying cache risks returning a pruned entry instead.
a0fa20a1
PW
608 CTransaction tx;
609 if (mempool.lookup(txid, tx)) {
610 coins = CCoins(tx, MEMPOOL_HEIGHT);
611 return true;
612 }
ad08d0b9 613 return (base->GetCoins(txid, coins) && !coins.IsPruned());
a0fa20a1
PW
614}
615
a3dc587a 616bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
a0fa20a1
PW
617 return mempool.exists(txid) || base->HaveCoins(txid);
618}
bde5c8b0
PW
619
620size_t CTxMemPool::DynamicMemoryUsage() const {
621 LOCK(cs);
e328fa32
AH
622 // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
623 return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;
bde5c8b0 624}
This page took 0.274759 seconds and 4 git commands to generate.