]> Git Repo - VerusCoin.git/blame - src/txmempool.cpp
Merge pull request #5360
[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"
b7b4318f 9#include "main.h"
fa736190 10#include "streams.h"
ad49c256 11#include "util.h"
a372168e 12#include "utilmoneystr.h"
85c579e3 13#include "version.h"
319b1160 14
171ca774
GA
15#include <boost/circular_buffer.hpp>
16
319b1160
GA
17using namespace std;
18
8bdd2877 19CTxMemPoolEntry::CTxMemPoolEntry():
f4fe2050 20 nFee(0), nTxSize(0), nModSize(0), nTime(0), dPriority(0.0)
4d707d51
GA
21{
22 nHeight = MEMPOOL_HEIGHT;
23}
24
a372168e 25CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
4d707d51
GA
26 int64_t _nTime, double _dPriority,
27 unsigned int _nHeight):
28 tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight)
29{
30 nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
c26649f9
AM
31
32 nModSize = tx.CalculateModifiedSize(nTxSize);
4d707d51
GA
33}
34
35CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
36{
37 *this = other;
38}
39
40double
41CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
42{
a372168e 43 CAmount nValueIn = tx.GetValueOut()+nFee;
c26649f9 44 double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
4d707d51
GA
45 double dResult = dPriority + deltaPriority;
46 return dResult;
47}
48
7329fdd1
MF
49/**
50 * Keep track of fee/priority for transactions confirmed within N blocks
51 */
171ca774
GA
52class CBlockAverage
53{
54private:
55 boost::circular_buffer<CFeeRate> feeSamples;
56 boost::circular_buffer<double> prioritySamples;
57
58 template<typename T> std::vector<T> buf2vec(boost::circular_buffer<T> buf) const
59 {
60 std::vector<T> vec(buf.begin(), buf.end());
61 return vec;
62 }
63
64public:
65 CBlockAverage() : feeSamples(100), prioritySamples(100) { }
66
67 void RecordFee(const CFeeRate& feeRate) {
68 feeSamples.push_back(feeRate);
69 }
70
71 void RecordPriority(double priority) {
72 prioritySamples.push_back(priority);
73 }
74
75 size_t FeeSamples() const { return feeSamples.size(); }
76 size_t GetFeeSamples(std::vector<CFeeRate>& insertInto) const
77 {
78 BOOST_FOREACH(const CFeeRate& f, feeSamples)
79 insertInto.push_back(f);
80 return feeSamples.size();
81 }
82 size_t PrioritySamples() const { return prioritySamples.size(); }
83 size_t GetPrioritySamples(std::vector<double>& insertInto) const
84 {
85 BOOST_FOREACH(double d, prioritySamples)
86 insertInto.push_back(d);
87 return prioritySamples.size();
88 }
89
7329fdd1
MF
90 /**
91 * Used as belt-and-suspenders check when reading to detect
92 * file corruption
93 */
64849306
GM
94 static bool AreSane(const CFeeRate fee, const CFeeRate& minRelayFee)
95 {
96 if (fee < CFeeRate(0))
97 return false;
98 if (fee.GetFeePerK() > minRelayFee.GetFeePerK() * 10000)
99 return false;
100 return true;
101 }
102 static bool AreSane(const std::vector<CFeeRate>& vecFee, const CFeeRate& minRelayFee)
171ca774
GA
103 {
104 BOOST_FOREACH(CFeeRate fee, vecFee)
105 {
64849306 106 if (!AreSane(fee, minRelayFee))
171ca774
GA
107 return false;
108 }
109 return true;
110 }
64849306
GM
111 static bool AreSane(const double priority)
112 {
113 return priority >= 0;
114 }
115 static bool AreSane(const std::vector<double> vecPriority)
171ca774
GA
116 {
117 BOOST_FOREACH(double priority, vecPriority)
118 {
64849306 119 if (!AreSane(priority))
171ca774
GA
120 return false;
121 }
122 return true;
123 }
124
125 void Write(CAutoFile& fileout) const
126 {
127 std::vector<CFeeRate> vecFee = buf2vec(feeSamples);
128 fileout << vecFee;
129 std::vector<double> vecPriority = buf2vec(prioritySamples);
130 fileout << vecPriority;
131 }
132
13fc83c7 133 void Read(CAutoFile& filein, const CFeeRate& minRelayFee) {
171ca774
GA
134 std::vector<CFeeRate> vecFee;
135 filein >> vecFee;
13fc83c7 136 if (AreSane(vecFee, minRelayFee))
171ca774
GA
137 feeSamples.insert(feeSamples.end(), vecFee.begin(), vecFee.end());
138 else
139 throw runtime_error("Corrupt fee value in estimates file.");
140 std::vector<double> vecPriority;
141 filein >> vecPriority;
142 if (AreSane(vecPriority))
143 prioritySamples.insert(prioritySamples.end(), vecPriority.begin(), vecPriority.end());
144 else
145 throw runtime_error("Corrupt priority value in estimates file.");
146 if (feeSamples.size() + prioritySamples.size() > 0)
147 LogPrint("estimatefee", "Read %d fee samples and %d priority samples\n",
148 feeSamples.size(), prioritySamples.size());
149 }
150};
151
152class CMinerPolicyEstimator
153{
154private:
7329fdd1
MF
155 /**
156 * Records observed averages transactions that confirmed within one block, two blocks,
157 * three blocks etc.
158 */
171ca774
GA
159 std::vector<CBlockAverage> history;
160 std::vector<CFeeRate> sortedFeeSamples;
161 std::vector<double> sortedPrioritySamples;
162
163 int nBestSeenHeight;
164
7329fdd1
MF
165 /**
166 * nBlocksAgo is 0 based, i.e. transactions that confirmed in the highest seen block are
167 * nBlocksAgo == 0, transactions in the block before that are nBlocksAgo == 1 etc.
168 */
13fc83c7 169 void seenTxConfirm(const CFeeRate& feeRate, const CFeeRate& minRelayFee, double dPriority, int nBlocksAgo)
171ca774
GA
170 {
171 // Last entry records "everything else".
172 int nBlocksTruncated = min(nBlocksAgo, (int) history.size() - 1);
173 assert(nBlocksTruncated >= 0);
174
175 // We need to guess why the transaction was included in a block-- either
176 // because it is high-priority or because it has sufficient fees.
13fc83c7 177 bool sufficientFee = (feeRate > minRelayFee);
171ca774
GA
178 bool sufficientPriority = AllowFree(dPriority);
179 const char* assignedTo = "unassigned";
64849306 180 if (sufficientFee && !sufficientPriority && CBlockAverage::AreSane(feeRate, minRelayFee))
171ca774
GA
181 {
182 history[nBlocksTruncated].RecordFee(feeRate);
183 assignedTo = "fee";
184 }
64849306 185 else if (sufficientPriority && !sufficientFee && CBlockAverage::AreSane(dPriority))
171ca774
GA
186 {
187 history[nBlocksTruncated].RecordPriority(dPriority);
188 assignedTo = "priority";
189 }
190 else
191 {
192 // Neither or both fee and priority sufficient to get confirmed:
193 // don't know why they got confirmed.
194 }
5262fde0 195 LogPrint("estimatefee", "Seen TX confirm: %s: %s fee/%g priority, took %d blocks\n",
171ca774
GA
196 assignedTo, feeRate.ToString(), dPriority, nBlocksAgo);
197 }
198
199public:
200 CMinerPolicyEstimator(int nEntries) : nBestSeenHeight(0)
201 {
202 history.resize(nEntries);
203 }
204
13fc83c7 205 void seenBlock(const std::vector<CTxMemPoolEntry>& entries, int nBlockHeight, const CFeeRate minRelayFee)
171ca774
GA
206 {
207 if (nBlockHeight <= nBestSeenHeight)
208 {
209 // Ignore side chains and re-orgs; assuming they are random
210 // they don't affect the estimate.
211 // And if an attacker can re-org the chain at will, then
212 // you've got much bigger problems than "attacker can influence
213 // transaction fees."
214 return;
215 }
216 nBestSeenHeight = nBlockHeight;
217
218 // Fill up the history buckets based on how long transactions took
219 // to confirm.
220 std::vector<std::vector<const CTxMemPoolEntry*> > entriesByConfirmations;
221 entriesByConfirmations.resize(history.size());
222 BOOST_FOREACH(const CTxMemPoolEntry& entry, entries)
223 {
224 // How many blocks did it take for miners to include this transaction?
225 int delta = nBlockHeight - entry.GetHeight();
226 if (delta <= 0)
227 {
228 // Re-org made us lose height, this should only happen if we happen
229 // to re-org on a difficulty transition point: very rare!
230 continue;
231 }
232 if ((delta-1) >= (int)history.size())
233 delta = history.size(); // Last bucket is catch-all
e59441f0 234 entriesByConfirmations.at(delta-1).push_back(&entry);
171ca774
GA
235 }
236 for (size_t i = 0; i < entriesByConfirmations.size(); i++)
237 {
238 std::vector<const CTxMemPoolEntry*> &e = entriesByConfirmations.at(i);
239 // Insert at most 10 random entries per bucket, otherwise a single block
240 // can dominate an estimate:
241 if (e.size() > 10) {
242 std::random_shuffle(e.begin(), e.end());
243 e.resize(10);
244 }
245 BOOST_FOREACH(const CTxMemPoolEntry* entry, e)
246 {
247 // Fees are stored and reported as BTC-per-kb:
248 CFeeRate feeRate(entry->GetFee(), entry->GetTxSize());
249 double dPriority = entry->GetPriority(entry->GetHeight()); // Want priority when it went IN
13fc83c7 250 seenTxConfirm(feeRate, minRelayFee, dPriority, i);
171ca774
GA
251 }
252 }
961ae93c 253
bf7835c2
PJ
254 // After new samples are added, we have to clear the sorted lists,
255 // so they'll be resorted the next time someone asks for an estimate
961ae93c
AM
256 sortedFeeSamples.clear();
257 sortedPrioritySamples.clear();
258
171ca774
GA
259 for (size_t i = 0; i < history.size(); i++) {
260 if (history[i].FeeSamples() + history[i].PrioritySamples() > 0)
261 LogPrint("estimatefee", "estimates: for confirming within %d blocks based on %d/%d samples, fee=%s, prio=%g\n",
262 i,
263 history[i].FeeSamples(), history[i].PrioritySamples(),
264 estimateFee(i+1).ToString(), estimatePriority(i+1));
265 }
171ca774
GA
266 }
267
7329fdd1
MF
268 /**
269 * Can return CFeeRate(0) if we don't have any data for that many blocks back. nBlocksToConfirm is 1 based.
270 */
171ca774
GA
271 CFeeRate estimateFee(int nBlocksToConfirm)
272 {
273 nBlocksToConfirm--;
274
275 if (nBlocksToConfirm < 0 || nBlocksToConfirm >= (int)history.size())
276 return CFeeRate(0);
277
278 if (sortedFeeSamples.size() == 0)
279 {
280 for (size_t i = 0; i < history.size(); i++)
281 history.at(i).GetFeeSamples(sortedFeeSamples);
282 std::sort(sortedFeeSamples.begin(), sortedFeeSamples.end(),
283 std::greater<CFeeRate>());
284 }
4b7b1bb1
GA
285 if (sortedFeeSamples.size() < 11)
286 {
287 // Eleven is Gavin's Favorite Number
288 // ... but we also take a maximum of 10 samples per block so eleven means
289 // we're getting samples from at least two different blocks
171ca774 290 return CFeeRate(0);
4b7b1bb1 291 }
171ca774
GA
292
293 int nBucketSize = history.at(nBlocksToConfirm).FeeSamples();
294
295 // Estimates should not increase as number of confirmations goes up,
296 // but the estimates are noisy because confirmations happen discretely
297 // in blocks. To smooth out the estimates, use all samples in the history
298 // and use the nth highest where n is (number of samples in previous bucket +
299 // half the samples in nBlocksToConfirm bucket):
300 size_t nPrevSize = 0;
301 for (int i = 0; i < nBlocksToConfirm; i++)
302 nPrevSize += history.at(i).FeeSamples();
303 size_t index = min(nPrevSize + nBucketSize/2, sortedFeeSamples.size()-1);
304 return sortedFeeSamples[index];
305 }
306 double estimatePriority(int nBlocksToConfirm)
307 {
308 nBlocksToConfirm--;
309
310 if (nBlocksToConfirm < 0 || nBlocksToConfirm >= (int)history.size())
311 return -1;
312
313 if (sortedPrioritySamples.size() == 0)
314 {
315 for (size_t i = 0; i < history.size(); i++)
316 history.at(i).GetPrioritySamples(sortedPrioritySamples);
317 std::sort(sortedPrioritySamples.begin(), sortedPrioritySamples.end(),
318 std::greater<double>());
319 }
4b7b1bb1 320 if (sortedPrioritySamples.size() < 11)
171ca774
GA
321 return -1.0;
322
323 int nBucketSize = history.at(nBlocksToConfirm).PrioritySamples();
324
325 // Estimates should not increase as number of confirmations needed goes up,
326 // but the estimates are noisy because confirmations happen discretely
327 // in blocks. To smooth out the estimates, use all samples in the history
328 // and use the nth highest where n is (number of samples in previous buckets +
329 // half the samples in nBlocksToConfirm bucket).
330 size_t nPrevSize = 0;
331 for (int i = 0; i < nBlocksToConfirm; i++)
332 nPrevSize += history.at(i).PrioritySamples();
17f15678 333 size_t index = min(nPrevSize + nBucketSize/2, sortedPrioritySamples.size()-1);
171ca774
GA
334 return sortedPrioritySamples[index];
335 }
336
337 void Write(CAutoFile& fileout) const
338 {
339 fileout << nBestSeenHeight;
9f4fac98 340 fileout << (uint32_t)history.size();
171ca774
GA
341 BOOST_FOREACH(const CBlockAverage& entry, history)
342 {
343 entry.Write(fileout);
344 }
345 }
346
13fc83c7 347 void Read(CAutoFile& filein, const CFeeRate& minRelayFee)
171ca774 348 {
e59441f0
AM
349 int nFileBestSeenHeight;
350 filein >> nFileBestSeenHeight;
9f4fac98 351 uint32_t numEntries;
171ca774 352 filein >> numEntries;
e59441f0 353 if (numEntries <= 0 || numEntries > 10000)
7329fdd1 354 throw runtime_error("Corrupt estimates file. Must have between 1 and 10k entries.");
e59441f0
AM
355
356 std::vector<CBlockAverage> fileHistory;
357
171ca774
GA
358 for (size_t i = 0; i < numEntries; i++)
359 {
360 CBlockAverage entry;
13fc83c7 361 entry.Read(filein, minRelayFee);
e59441f0 362 fileHistory.push_back(entry);
171ca774 363 }
e59441f0 364
771d5002
PK
365 // Now that we've processed the entire fee estimate data file and not
366 // thrown any errors, we can copy it to our history
e59441f0
AM
367 nBestSeenHeight = nFileBestSeenHeight;
368 history = fileHistory;
369 assert(history.size() > 0);
171ca774
GA
370 }
371};
372
373
8bdd2877
WL
374CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
375 nTransactionsUpdated(0),
376 minRelayFee(_minRelayFee)
319b1160
GA
377{
378 // Sanity checks off by default for performance, because otherwise
379 // accepting transactions becomes O(N^2) where N is the number
380 // of transactions in the pool
381 fSanityCheck = false;
171ca774
GA
382
383 // 25 blocks is a compromise between using a lot of disk/memory and
384 // trying to give accurate estimates to people who might be willing
385 // to wait a day or two to save a fraction of a penny in fees.
386 // Confirmation times for very-low-fee transactions that take more
387 // than an hour or three to confirm are highly variable.
388 minerPolicyEstimator = new CMinerPolicyEstimator(25);
389}
390
391CTxMemPool::~CTxMemPool()
392{
393 delete minerPolicyEstimator;
319b1160
GA
394}
395
396void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
397{
398 LOCK(cs);
399
400 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
401
402 // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
403 while (it != mapNextTx.end() && it->first.hash == hashTx) {
404 coins.Spend(it->first.n); // and remove those outputs from coins
405 it++;
406 }
407}
408
409unsigned int CTxMemPool::GetTransactionsUpdated() const
410{
411 LOCK(cs);
412 return nTransactionsUpdated;
413}
414
415void CTxMemPool::AddTransactionsUpdated(unsigned int n)
416{
417 LOCK(cs);
418 nTransactionsUpdated += n;
419}
420
421
4d707d51 422bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry)
319b1160
GA
423{
424 // Add to memory pool without checking anything.
425 // Used by main.cpp AcceptToMemoryPool(), which DOES do
426 // all the appropriate checks.
427 LOCK(cs);
428 {
4d707d51
GA
429 mapTx[hash] = entry;
430 const CTransaction& tx = mapTx[hash].GetTx();
319b1160 431 for (unsigned int i = 0; i < tx.vin.size(); i++)
4d707d51 432 mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
319b1160 433 nTransactionsUpdated++;
6f2c26a4 434 totalTxSize += entry.GetTxSize();
319b1160
GA
435 }
436 return true;
437}
438
439
7fd6219a 440void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
319b1160
GA
441{
442 // Remove transaction from memory pool
443 {
444 LOCK(cs);
7fd6219a
MC
445 std::deque<uint256> txToRemove;
446 txToRemove.push_back(origTx.GetHash());
447 while (!txToRemove.empty())
319b1160 448 {
7fd6219a
MC
449 uint256 hash = txToRemove.front();
450 txToRemove.pop_front();
451 if (!mapTx.count(hash))
452 continue;
453 const CTransaction& tx = mapTx[hash].GetTx();
454 if (fRecursive) {
455 for (unsigned int i = 0; i < tx.vout.size(); i++) {
456 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
457 if (it == mapNextTx.end())
458 continue;
459 txToRemove.push_back(it->second.ptx->GetHash());
460 }
461 }
319b1160
GA
462 BOOST_FOREACH(const CTxIn& txin, tx.vin)
463 mapNextTx.erase(txin.prevout);
6f2c26a4 464
7fd6219a 465 removed.push_back(tx);
6f2c26a4 466 totalTxSize -= mapTx[hash].GetTxSize();
319b1160
GA
467 mapTx.erase(hash);
468 nTransactionsUpdated++;
469 }
470 }
319b1160
GA
471}
472
723d12c0
MC
473void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
474{
475 // Remove transactions spending a coinbase which are now immature
476 LOCK(cs);
477 list<CTransaction> transactionsToRemove;
478 for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
479 const CTransaction& tx = it->second.GetTx();
480 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
481 std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
482 if (it2 != mapTx.end())
483 continue;
484 const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
485 if (fSanityCheck) assert(coins);
486 if (!coins || (coins->IsCoinBase() && nMemPoolHeight - coins->nHeight < COINBASE_MATURITY)) {
487 transactionsToRemove.push_back(tx);
488 break;
489 }
490 }
491 }
492 BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
493 list<CTransaction> removed;
494 remove(tx, removed, true);
495 }
496}
497
93a18a36 498void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
319b1160
GA
499{
500 // Remove transactions which depend on inputs of tx, recursively
98e84aae 501 list<CTransaction> result;
319b1160
GA
502 LOCK(cs);
503 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
504 std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
505 if (it != mapNextTx.end()) {
506 const CTransaction &txConflict = *it->second.ptx;
507 if (txConflict != tx)
93a18a36
GA
508 {
509 remove(txConflict, removed, true);
510 }
319b1160
GA
511 }
512 }
319b1160
GA
513}
514
7329fdd1
MF
515/**
516 * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
517 */
171ca774
GA
518void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
519 std::list<CTransaction>& conflicts)
520{
521 LOCK(cs);
522 std::vector<CTxMemPoolEntry> entries;
523 BOOST_FOREACH(const CTransaction& tx, vtx)
524 {
525 uint256 hash = tx.GetHash();
526 if (mapTx.count(hash))
527 entries.push_back(mapTx[hash]);
528 }
13fc83c7 529 minerPolicyEstimator->seenBlock(entries, nBlockHeight, minRelayFee);
171ca774
GA
530 BOOST_FOREACH(const CTransaction& tx, vtx)
531 {
532 std::list<CTransaction> dummy;
533 remove(tx, dummy, false);
534 removeConflicts(tx, conflicts);
2a72d459 535 ClearPrioritisation(tx.GetHash());
171ca774
GA
536 }
537}
538
539
319b1160
GA
540void CTxMemPool::clear()
541{
542 LOCK(cs);
543 mapTx.clear();
544 mapNextTx.clear();
6f2c26a4 545 totalTxSize = 0;
319b1160
GA
546 ++nTransactionsUpdated;
547}
548
d0867acb 549void CTxMemPool::check(const CCoinsViewCache *pcoins) const
319b1160
GA
550{
551 if (!fSanityCheck)
552 return;
553
554 LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
555
6f2c26a4
JG
556 uint64_t checkTotal = 0;
557
b7b4318f
MC
558 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
559
319b1160 560 LOCK(cs);
b7b4318f 561 list<const CTxMemPoolEntry*> waitingOnDependants;
4d707d51 562 for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
319b1160 563 unsigned int i = 0;
6f2c26a4 564 checkTotal += it->second.GetTxSize();
4d707d51 565 const CTransaction& tx = it->second.GetTx();
b7b4318f 566 bool fDependsWait = false;
4d707d51 567 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
319b1160 568 // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
4d707d51 569 std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
319b1160 570 if (it2 != mapTx.end()) {
4d707d51
GA
571 const CTransaction& tx2 = it2->second.GetTx();
572 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
b7b4318f 573 fDependsWait = true;
319b1160 574 } else {
629d75fa
PW
575 const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
576 assert(coins && coins->IsAvailable(txin.prevout.n));
319b1160
GA
577 }
578 // Check whether its inputs are marked in mapNextTx.
579 std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
580 assert(it3 != mapNextTx.end());
4d707d51 581 assert(it3->second.ptx == &tx);
319b1160
GA
582 assert(it3->second.n == i);
583 i++;
584 }
b7b4318f
MC
585 if (fDependsWait)
586 waitingOnDependants.push_back(&it->second);
587 else {
d7621ccf 588 CValidationState state;
b7b4318f 589 assert(CheckInputs(tx, state, mempoolDuplicate, false, 0, false, NULL));
d7621ccf 590 UpdateCoins(tx, state, mempoolDuplicate, 1000000);
b7b4318f
MC
591 }
592 }
593 unsigned int stepsSinceLastRemove = 0;
594 while (!waitingOnDependants.empty()) {
595 const CTxMemPoolEntry* entry = waitingOnDependants.front();
596 waitingOnDependants.pop_front();
597 CValidationState state;
598 if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
599 waitingOnDependants.push_back(entry);
600 stepsSinceLastRemove++;
601 assert(stepsSinceLastRemove < waitingOnDependants.size());
602 } else {
603 assert(CheckInputs(entry->GetTx(), state, mempoolDuplicate, false, 0, false, NULL));
d7621ccf 604 UpdateCoins(entry->GetTx(), state, mempoolDuplicate, 1000000);
b7b4318f
MC
605 stepsSinceLastRemove = 0;
606 }
319b1160
GA
607 }
608 for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
609 uint256 hash = it->second.ptx->GetHash();
4d707d51
GA
610 map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(hash);
611 const CTransaction& tx = it2->second.GetTx();
319b1160 612 assert(it2 != mapTx.end());
4d707d51
GA
613 assert(&tx == it->second.ptx);
614 assert(tx.vin.size() > it->second.n);
319b1160
GA
615 assert(it->first == it->second.ptx->vin[it->second.n].prevout);
616 }
6f2c26a4
JG
617
618 assert(totalTxSize == checkTotal);
319b1160
GA
619}
620
4d707d51 621void CTxMemPool::queryHashes(vector<uint256>& vtxid)
319b1160
GA
622{
623 vtxid.clear();
624
625 LOCK(cs);
626 vtxid.reserve(mapTx.size());
4d707d51 627 for (map<uint256, CTxMemPoolEntry>::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
319b1160
GA
628 vtxid.push_back((*mi).first);
629}
630
631bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
632{
633 LOCK(cs);
4d707d51 634 map<uint256, CTxMemPoolEntry>::const_iterator i = mapTx.find(hash);
319b1160 635 if (i == mapTx.end()) return false;
4d707d51 636 result = i->second.GetTx();
319b1160
GA
637 return true;
638}
a0fa20a1 639
171ca774
GA
640CFeeRate CTxMemPool::estimateFee(int nBlocks) const
641{
642 LOCK(cs);
643 return minerPolicyEstimator->estimateFee(nBlocks);
644}
645double CTxMemPool::estimatePriority(int nBlocks) const
646{
647 LOCK(cs);
648 return minerPolicyEstimator->estimatePriority(nBlocks);
649}
650
651bool
652CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
653{
654 try {
655 LOCK(cs);
656 fileout << 99900; // version required to read: 0.9.99 or later
657 fileout << CLIENT_VERSION; // version that wrote the file
658 minerPolicyEstimator->Write(fileout);
659 }
27df4123 660 catch (const std::exception&) {
5262fde0 661 LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)");
171ca774
GA
662 return false;
663 }
664 return true;
665}
666
667bool
668CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
669{
670 try {
671 int nVersionRequired, nVersionThatWrote;
672 filein >> nVersionRequired >> nVersionThatWrote;
673 if (nVersionRequired > CLIENT_VERSION)
5262fde0 674 return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
171ca774
GA
675
676 LOCK(cs);
13fc83c7 677 minerPolicyEstimator->Read(filein, minRelayFee);
171ca774 678 }
27df4123 679 catch (const std::exception&) {
5262fde0 680 LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)");
171ca774
GA
681 return false;
682 }
683 return true;
684}
685
a372168e 686void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
2a72d459
LD
687{
688 {
689 LOCK(cs);
a372168e 690 std::pair<double, CAmount> &deltas = mapDeltas[hash];
2a72d459
LD
691 deltas.first += dPriorityDelta;
692 deltas.second += nFeeDelta;
693 }
a372168e 694 LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
2a72d459
LD
695}
696
a372168e 697void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
2a72d459
LD
698{
699 LOCK(cs);
a372168e 700 std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
2a72d459
LD
701 if (pos == mapDeltas.end())
702 return;
a372168e 703 const std::pair<double, CAmount> &deltas = pos->second;
2a72d459
LD
704 dPriorityDelta += deltas.first;
705 nFeeDelta += deltas.second;
706}
707
708void CTxMemPool::ClearPrioritisation(const uint256 hash)
709{
710 LOCK(cs);
711 mapDeltas.erase(hash);
712}
713
171ca774 714
7c70438d 715CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
a0fa20a1 716
a3dc587a 717bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
ad08d0b9
PW
718 // If an entry in the mempool exists, always return that one, as it's guaranteed to never
719 // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
720 // transactions. First checking the underlying cache risks returning a pruned entry instead.
a0fa20a1
PW
721 CTransaction tx;
722 if (mempool.lookup(txid, tx)) {
723 coins = CCoins(tx, MEMPOOL_HEIGHT);
724 return true;
725 }
ad08d0b9 726 return (base->GetCoins(txid, coins) && !coins.IsPruned());
a0fa20a1
PW
727}
728
a3dc587a 729bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
a0fa20a1
PW
730 return mempool.exists(txid) || base->HaveCoins(txid);
731}
This page took 0.223787 seconds and 4 git commands to generate.