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