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