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