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