]>
Commit | Line | Data |
---|---|---|
319b1160 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
7329fdd1 | 3 | // Distributed under the MIT software license, see the accompanying |
319b1160 GA |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
319b1160 | 6 | #include "txmempool.h" |
611116d4 | 7 | |
71697f97 | 8 | #include "clientversion.h" |
691161d4 | 9 | #include "consensus/consensus.h" |
da29ecbc | 10 | #include "consensus/validation.h" |
b7b4318f | 11 | #include "main.h" |
b649e039 | 12 | #include "policy/fees.h" |
fa736190 | 13 | #include "streams.h" |
f5b35d23 | 14 | #include "timedata.h" |
ad49c256 | 15 | #include "util.h" |
a372168e | 16 | #include "utilmoneystr.h" |
85c579e3 | 17 | #include "version.h" |
319b1160 GA |
18 | |
19 | using namespace std; | |
20 | ||
8bdd2877 | 21 | CTxMemPoolEntry::CTxMemPoolEntry(): |
a4b25180 SD |
22 | nFee(0), nTxSize(0), nModSize(0), nUsageSize(0), nTime(0), dPriority(0.0), |
23 | hadNoDependencies(false), spendsCoinbase(false) | |
4d707d51 GA |
24 | { |
25 | nHeight = MEMPOOL_HEIGHT; | |
26 | } | |
27 | ||
a372168e | 28 | CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee, |
4d707d51 | 29 | int64_t _nTime, double _dPriority, |
a4b25180 | 30 | unsigned int _nHeight, bool poolHasNoInputsOf, |
34a64fe0 | 31 | bool _spendsCoinbase, uint32_t _nBranchId): |
b649e039 | 32 | tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight), |
a4b25180 | 33 | hadNoDependencies(poolHasNoInputsOf), |
34a64fe0 | 34 | spendsCoinbase(_spendsCoinbase), nBranchId(_nBranchId) |
4d707d51 GA |
35 | { |
36 | nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | |
c26649f9 | 37 | nModSize = tx.CalculateModifiedSize(nTxSize); |
6bd1d60c | 38 | nUsageSize = RecursiveDynamicUsage(tx); |
e328fa32 | 39 | feeRate = CFeeRate(nFee, nTxSize); |
4d707d51 GA |
40 | } |
41 | ||
42 | CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other) | |
43 | { | |
44 | *this = other; | |
45 | } | |
46 | ||
47 | double | |
48 | CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const | |
49 | { | |
a372168e | 50 | CAmount nValueIn = tx.GetValueOut()+nFee; |
c26649f9 | 51 | double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize; |
4d707d51 GA |
52 | double dResult = dPriority + deltaPriority; |
53 | return dResult; | |
54 | } | |
55 | ||
8bdd2877 | 56 | CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) : |
b649e039 | 57 | nTransactionsUpdated(0) |
319b1160 GA |
58 | { |
59 | // Sanity checks off by default for performance, because otherwise | |
60 | // accepting transactions becomes O(N^2) where N is the number | |
61 | // of transactions in the pool | |
934fd197 | 62 | nCheckFrequency = 0; |
171ca774 | 63 | |
b649e039 | 64 | minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee); |
171ca774 GA |
65 | } |
66 | ||
67 | CTxMemPool::~CTxMemPool() | |
68 | { | |
69 | delete minerPolicyEstimator; | |
319b1160 GA |
70 | } |
71 | ||
72 | void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins) | |
73 | { | |
74 | LOCK(cs); | |
75 | ||
76 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0)); | |
77 | ||
78 | // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx | |
79 | while (it != mapNextTx.end() && it->first.hash == hashTx) { | |
80 | coins.Spend(it->first.n); // and remove those outputs from coins | |
81 | it++; | |
82 | } | |
83 | } | |
84 | ||
85 | unsigned int CTxMemPool::GetTransactionsUpdated() const | |
86 | { | |
87 | LOCK(cs); | |
88 | return nTransactionsUpdated; | |
89 | } | |
90 | ||
91 | void CTxMemPool::AddTransactionsUpdated(unsigned int n) | |
92 | { | |
93 | LOCK(cs); | |
94 | nTransactionsUpdated += n; | |
95 | } | |
96 | ||
97 | ||
b649e039 | 98 | bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate) |
319b1160 GA |
99 | { |
100 | // Add to memory pool without checking anything. | |
101 | // Used by main.cpp AcceptToMemoryPool(), which DOES do | |
102 | // all the appropriate checks. | |
103 | LOCK(cs); | |
e328fa32 AH |
104 | mapTx.insert(entry); |
105 | const CTransaction& tx = mapTx.find(hash)->GetTx(); | |
b649e039 AM |
106 | for (unsigned int i = 0; i < tx.vin.size(); i++) |
107 | mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i); | |
b7e4abd6 | 108 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
109 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
110 | mapNullifiers[nf] = &tx; | |
d66877af SB |
111 | } |
112 | } | |
b649e039 AM |
113 | nTransactionsUpdated++; |
114 | totalTxSize += entry.GetTxSize(); | |
bde5c8b0 | 115 | cachedInnerUsage += entry.DynamicMemoryUsage(); |
b649e039 AM |
116 | minerPolicyEstimator->processTransaction(entry, fCurrentEstimate); |
117 | ||
319b1160 GA |
118 | return true; |
119 | } | |
120 | ||
121 | ||
7fd6219a | 122 | void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive) |
319b1160 GA |
123 | { |
124 | // Remove transaction from memory pool | |
125 | { | |
126 | LOCK(cs); | |
7fd6219a | 127 | std::deque<uint256> txToRemove; |
805344dc S |
128 | txToRemove.push_back(origTx.GetHash()); |
129 | if (fRecursive && !mapTx.count(origTx.GetHash())) { | |
ad9e86dc GA |
130 | // If recursively removing but origTx isn't in the mempool |
131 | // be sure to remove any children that are in the pool. This can | |
132 | // happen during chain re-orgs if origTx isn't re-accepted into | |
133 | // the mempool for any reason. | |
134 | for (unsigned int i = 0; i < origTx.vout.size(); i++) { | |
805344dc | 135 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i)); |
ad9e86dc GA |
136 | if (it == mapNextTx.end()) |
137 | continue; | |
805344dc | 138 | txToRemove.push_back(it->second.ptx->GetHash()); |
ad9e86dc GA |
139 | } |
140 | } | |
7fd6219a | 141 | while (!txToRemove.empty()) |
319b1160 | 142 | { |
7fd6219a MC |
143 | uint256 hash = txToRemove.front(); |
144 | txToRemove.pop_front(); | |
145 | if (!mapTx.count(hash)) | |
146 | continue; | |
e328fa32 | 147 | const CTransaction& tx = mapTx.find(hash)->GetTx(); |
7fd6219a MC |
148 | if (fRecursive) { |
149 | for (unsigned int i = 0; i < tx.vout.size(); i++) { | |
150 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i)); | |
151 | if (it == mapNextTx.end()) | |
152 | continue; | |
805344dc | 153 | txToRemove.push_back(it->second.ptx->GetHash()); |
7fd6219a MC |
154 | } |
155 | } | |
319b1160 GA |
156 | BOOST_FOREACH(const CTxIn& txin, tx.vin) |
157 | mapNextTx.erase(txin.prevout); | |
b7e4abd6 | 158 | BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) { |
22de1602 SB |
159 | BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) { |
160 | mapNullifiers.erase(nf); | |
d66877af SB |
161 | } |
162 | } | |
6f2c26a4 | 163 | |
7fd6219a | 164 | removed.push_back(tx); |
e328fa32 AH |
165 | totalTxSize -= mapTx.find(hash)->GetTxSize(); |
166 | cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage(); | |
319b1160 GA |
167 | mapTx.erase(hash); |
168 | nTransactionsUpdated++; | |
b649e039 | 169 | minerPolicyEstimator->removeTx(hash); |
319b1160 GA |
170 | } |
171 | } | |
319b1160 GA |
172 | } |
173 | ||
233c9eb6 | 174 | void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) |
723d12c0 | 175 | { |
c944d161 | 176 | // Remove transactions spending a coinbase which are now immature and no-longer-final transactions |
723d12c0 MC |
177 | LOCK(cs); |
178 | list<CTransaction> transactionsToRemove; | |
e328fa32 AH |
179 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
180 | const CTransaction& tx = it->GetTx(); | |
233c9eb6 | 181 | if (!CheckFinalTx(tx, flags)) { |
f5b35d23 | 182 | transactionsToRemove.push_back(tx); |
a4b25180 | 183 | } else if (it->GetSpendsCoinbase()) { |
f5b35d23 MC |
184 | BOOST_FOREACH(const CTxIn& txin, tx.vin) { |
185 | indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash); | |
186 | if (it2 != mapTx.end()) | |
187 | continue; | |
188 | const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash); | |
189 | if (nCheckFrequency != 0) assert(coins); | |
190 | if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) { | |
191 | transactionsToRemove.push_back(tx); | |
192 | break; | |
193 | } | |
723d12c0 MC |
194 | } |
195 | } | |
196 | } | |
197 | BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) { | |
198 | list<CTransaction> removed; | |
199 | remove(tx, removed, true); | |
200 | } | |
201 | } | |
202 | ||
a8ac403d SB |
203 | |
204 | void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot) | |
205 | { | |
206 | // If a block is disconnected from the tip, and the root changed, | |
207 | // we must invalidate transactions from the mempool which spend | |
208 | // from that root -- almost as though they were spending coinbases | |
209 | // which are no longer valid to spend due to coinbase maturity. | |
210 | LOCK(cs); | |
211 | list<CTransaction> transactionsToRemove; | |
212 | ||
e328fa32 AH |
213 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
214 | const CTransaction& tx = it->GetTx(); | |
b7e4abd6 SB |
215 | BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) { |
216 | if (joinsplit.anchor == invalidRoot) { | |
a8ac403d SB |
217 | transactionsToRemove.push_back(tx); |
218 | break; | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) { | |
224 | list<CTransaction> removed; | |
225 | remove(tx, removed, true); | |
226 | } | |
227 | } | |
228 | ||
93a18a36 | 229 | void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed) |
319b1160 GA |
230 | { |
231 | // Remove transactions which depend on inputs of tx, recursively | |
98e84aae | 232 | list<CTransaction> result; |
319b1160 GA |
233 | LOCK(cs); |
234 | BOOST_FOREACH(const CTxIn &txin, tx.vin) { | |
235 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout); | |
236 | if (it != mapNextTx.end()) { | |
237 | const CTransaction &txConflict = *it->second.ptx; | |
238 | if (txConflict != tx) | |
93a18a36 GA |
239 | { |
240 | remove(txConflict, removed, true); | |
241 | } | |
319b1160 GA |
242 | } |
243 | } | |
d66877af | 244 | |
b7e4abd6 | 245 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
246 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
247 | std::map<uint256, const CTransaction*>::iterator it = mapNullifiers.find(nf); | |
bb64be52 | 248 | if (it != mapNullifiers.end()) { |
d66877af SB |
249 | const CTransaction &txConflict = *it->second; |
250 | if (txConflict != tx) | |
251 | { | |
252 | remove(txConflict, removed, true); | |
253 | } | |
254 | } | |
255 | } | |
256 | } | |
319b1160 GA |
257 | } |
258 | ||
7329fdd1 MF |
259 | /** |
260 | * Called when a block is connected. Removes from mempool and updates the miner fee estimator. | |
261 | */ | |
171ca774 | 262 | void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight, |
b649e039 | 263 | std::list<CTransaction>& conflicts, bool fCurrentEstimate) |
171ca774 GA |
264 | { |
265 | LOCK(cs); | |
266 | std::vector<CTxMemPoolEntry> entries; | |
267 | BOOST_FOREACH(const CTransaction& tx, vtx) | |
268 | { | |
805344dc | 269 | uint256 hash = tx.GetHash(); |
e328fa32 AH |
270 | |
271 | indexed_transaction_set::iterator i = mapTx.find(hash); | |
272 | if (i != mapTx.end()) | |
273 | entries.push_back(*i); | |
171ca774 | 274 | } |
171ca774 GA |
275 | BOOST_FOREACH(const CTransaction& tx, vtx) |
276 | { | |
277 | std::list<CTransaction> dummy; | |
278 | remove(tx, dummy, false); | |
279 | removeConflicts(tx, conflicts); | |
805344dc | 280 | ClearPrioritisation(tx.GetHash()); |
171ca774 | 281 | } |
b649e039 AM |
282 | // After the txs in the new block have been removed from the mempool, update policy estimates |
283 | minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate); | |
171ca774 GA |
284 | } |
285 | ||
34a64fe0 JG |
286 | /** |
287 | * Called whenever the tip changes. Removes transactions which don't commit to | |
288 | * the given branch ID from the mempool. | |
289 | */ | |
290 | void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId) | |
291 | { | |
292 | LOCK(cs); | |
293 | std::list<CTransaction> transactionsToRemove; | |
294 | ||
295 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { | |
296 | const CTransaction& tx = it->GetTx(); | |
297 | if (it->GetValidatedBranchId() != nMemPoolBranchId) { | |
298 | transactionsToRemove.push_back(tx); | |
299 | } | |
300 | } | |
301 | ||
302 | for (const CTransaction& tx : transactionsToRemove) { | |
303 | std::list<CTransaction> removed; | |
304 | remove(tx, removed, true); | |
305 | } | |
306 | } | |
307 | ||
319b1160 GA |
308 | void CTxMemPool::clear() |
309 | { | |
310 | LOCK(cs); | |
311 | mapTx.clear(); | |
312 | mapNextTx.clear(); | |
6f2c26a4 | 313 | totalTxSize = 0; |
bde5c8b0 | 314 | cachedInnerUsage = 0; |
319b1160 GA |
315 | ++nTransactionsUpdated; |
316 | } | |
317 | ||
d0867acb | 318 | void CTxMemPool::check(const CCoinsViewCache *pcoins) const |
319b1160 | 319 | { |
934fd197 PW |
320 | if (nCheckFrequency == 0) |
321 | return; | |
322 | ||
323 | if (insecure_rand() >= nCheckFrequency) | |
319b1160 GA |
324 | return; |
325 | ||
326 | LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size()); | |
327 | ||
6f2c26a4 | 328 | uint64_t checkTotal = 0; |
bde5c8b0 | 329 | uint64_t innerUsage = 0; |
6f2c26a4 | 330 | |
b7b4318f MC |
331 | CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins)); |
332 | ||
319b1160 | 333 | LOCK(cs); |
b7b4318f | 334 | list<const CTxMemPoolEntry*> waitingOnDependants; |
e328fa32 | 335 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
319b1160 | 336 | unsigned int i = 0; |
e328fa32 AH |
337 | checkTotal += it->GetTxSize(); |
338 | innerUsage += it->DynamicMemoryUsage(); | |
339 | const CTransaction& tx = it->GetTx(); | |
b7b4318f | 340 | bool fDependsWait = false; |
4d707d51 | 341 | BOOST_FOREACH(const CTxIn &txin, tx.vin) { |
319b1160 | 342 | // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's. |
e328fa32 | 343 | indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash); |
319b1160 | 344 | if (it2 != mapTx.end()) { |
e328fa32 | 345 | const CTransaction& tx2 = it2->GetTx(); |
4d707d51 | 346 | assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull()); |
b7b4318f | 347 | fDependsWait = true; |
319b1160 | 348 | } else { |
629d75fa PW |
349 | const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash); |
350 | assert(coins && coins->IsAvailable(txin.prevout.n)); | |
319b1160 GA |
351 | } |
352 | // Check whether its inputs are marked in mapNextTx. | |
353 | std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout); | |
354 | assert(it3 != mapNextTx.end()); | |
4d707d51 | 355 | assert(it3->second.ptx == &tx); |
319b1160 GA |
356 | assert(it3->second.n == i); |
357 | i++; | |
358 | } | |
a667caec SB |
359 | |
360 | boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates; | |
361 | ||
b7e4abd6 | 362 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
363 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
364 | assert(!pcoins->GetNullifier(nf)); | |
d66877af SB |
365 | } |
366 | ||
434f3284 | 367 | ZCIncrementalMerkleTree tree; |
b7e4abd6 | 368 | auto it = intermediates.find(joinsplit.anchor); |
a667caec SB |
369 | if (it != intermediates.end()) { |
370 | tree = it->second; | |
371 | } else { | |
b7e4abd6 | 372 | assert(pcoins->GetAnchorAt(joinsplit.anchor, tree)); |
a667caec SB |
373 | } |
374 | ||
b7e4abd6 | 375 | BOOST_FOREACH(const uint256& commitment, joinsplit.commitments) |
a667caec SB |
376 | { |
377 | tree.append(commitment); | |
378 | } | |
379 | ||
380 | intermediates.insert(std::make_pair(tree.root(), tree)); | |
d66877af | 381 | } |
b7b4318f | 382 | if (fDependsWait) |
e328fa32 | 383 | waitingOnDependants.push_back(&(*it)); |
b7b4318f | 384 | else { |
d7621ccf | 385 | CValidationState state; |
c0dde76d | 386 | assert(ContextualCheckInputs(tx, state, mempoolDuplicate, false, 0, false, Params().GetConsensus(), NULL)); |
8cb98d91 | 387 | UpdateCoins(tx, mempoolDuplicate, 1000000); |
b7b4318f MC |
388 | } |
389 | } | |
390 | unsigned int stepsSinceLastRemove = 0; | |
391 | while (!waitingOnDependants.empty()) { | |
392 | const CTxMemPoolEntry* entry = waitingOnDependants.front(); | |
393 | waitingOnDependants.pop_front(); | |
394 | CValidationState state; | |
395 | if (!mempoolDuplicate.HaveInputs(entry->GetTx())) { | |
396 | waitingOnDependants.push_back(entry); | |
397 | stepsSinceLastRemove++; | |
398 | assert(stepsSinceLastRemove < waitingOnDependants.size()); | |
399 | } else { | |
c0dde76d | 400 | assert(ContextualCheckInputs(entry->GetTx(), state, mempoolDuplicate, false, 0, false, Params().GetConsensus(), NULL)); |
8cb98d91 | 401 | UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000); |
b7b4318f MC |
402 | stepsSinceLastRemove = 0; |
403 | } | |
319b1160 GA |
404 | } |
405 | for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) { | |
805344dc | 406 | uint256 hash = it->second.ptx->GetHash(); |
e328fa32 AH |
407 | indexed_transaction_set::const_iterator it2 = mapTx.find(hash); |
408 | const CTransaction& tx = it2->GetTx(); | |
319b1160 | 409 | assert(it2 != mapTx.end()); |
4d707d51 GA |
410 | assert(&tx == it->second.ptx); |
411 | assert(tx.vin.size() > it->second.n); | |
319b1160 GA |
412 | assert(it->first == it->second.ptx->vin[it->second.n].prevout); |
413 | } | |
6f2c26a4 | 414 | |
bb64be52 | 415 | for (std::map<uint256, const CTransaction*>::const_iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); it++) { |
805344dc | 416 | uint256 hash = it->second->GetHash(); |
e328fa32 AH |
417 | indexed_transaction_set::const_iterator it2 = mapTx.find(hash); |
418 | const CTransaction& tx = it2->GetTx(); | |
d66877af SB |
419 | assert(it2 != mapTx.end()); |
420 | assert(&tx == it->second); | |
421 | } | |
422 | ||
6f2c26a4 | 423 | assert(totalTxSize == checkTotal); |
bde5c8b0 | 424 | assert(innerUsage == cachedInnerUsage); |
319b1160 GA |
425 | } |
426 | ||
4d707d51 | 427 | void CTxMemPool::queryHashes(vector<uint256>& vtxid) |
319b1160 GA |
428 | { |
429 | vtxid.clear(); | |
430 | ||
431 | LOCK(cs); | |
432 | vtxid.reserve(mapTx.size()); | |
e328fa32 AH |
433 | for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) |
434 | vtxid.push_back(mi->GetTx().GetHash()); | |
319b1160 GA |
435 | } |
436 | ||
437 | bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const | |
438 | { | |
439 | LOCK(cs); | |
e328fa32 | 440 | indexed_transaction_set::const_iterator i = mapTx.find(hash); |
319b1160 | 441 | if (i == mapTx.end()) return false; |
e328fa32 | 442 | result = i->GetTx(); |
319b1160 GA |
443 | return true; |
444 | } | |
a0fa20a1 | 445 | |
171ca774 GA |
446 | CFeeRate CTxMemPool::estimateFee(int nBlocks) const |
447 | { | |
448 | LOCK(cs); | |
449 | return minerPolicyEstimator->estimateFee(nBlocks); | |
450 | } | |
451 | double CTxMemPool::estimatePriority(int nBlocks) const | |
452 | { | |
453 | LOCK(cs); | |
454 | return minerPolicyEstimator->estimatePriority(nBlocks); | |
455 | } | |
456 | ||
457 | bool | |
458 | CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const | |
459 | { | |
460 | try { | |
461 | LOCK(cs); | |
b649e039 | 462 | fileout << 109900; // version required to read: 0.10.99 or later |
171ca774 GA |
463 | fileout << CLIENT_VERSION; // version that wrote the file |
464 | minerPolicyEstimator->Write(fileout); | |
465 | } | |
27df4123 | 466 | catch (const std::exception&) { |
7ff9d122 | 467 | LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n"); |
171ca774 GA |
468 | return false; |
469 | } | |
470 | return true; | |
471 | } | |
472 | ||
473 | bool | |
474 | CTxMemPool::ReadFeeEstimates(CAutoFile& filein) | |
475 | { | |
476 | try { | |
477 | int nVersionRequired, nVersionThatWrote; | |
478 | filein >> nVersionRequired >> nVersionThatWrote; | |
479 | if (nVersionRequired > CLIENT_VERSION) | |
5262fde0 | 480 | return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired); |
171ca774 GA |
481 | |
482 | LOCK(cs); | |
b649e039 | 483 | minerPolicyEstimator->Read(filein); |
171ca774 | 484 | } |
27df4123 | 485 | catch (const std::exception&) { |
7ff9d122 | 486 | LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n"); |
171ca774 GA |
487 | return false; |
488 | } | |
489 | return true; | |
490 | } | |
491 | ||
a372168e | 492 | void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta) |
2a72d459 LD |
493 | { |
494 | { | |
495 | LOCK(cs); | |
a372168e | 496 | std::pair<double, CAmount> &deltas = mapDeltas[hash]; |
2a72d459 LD |
497 | deltas.first += dPriorityDelta; |
498 | deltas.second += nFeeDelta; | |
499 | } | |
a372168e | 500 | LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta)); |
2a72d459 LD |
501 | } |
502 | ||
a372168e | 503 | void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) |
2a72d459 LD |
504 | { |
505 | LOCK(cs); | |
a372168e | 506 | std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash); |
2a72d459 LD |
507 | if (pos == mapDeltas.end()) |
508 | return; | |
a372168e | 509 | const std::pair<double, CAmount> &deltas = pos->second; |
2a72d459 LD |
510 | dPriorityDelta += deltas.first; |
511 | nFeeDelta += deltas.second; | |
512 | } | |
513 | ||
514 | void CTxMemPool::ClearPrioritisation(const uint256 hash) | |
515 | { | |
516 | LOCK(cs); | |
517 | mapDeltas.erase(hash); | |
518 | } | |
519 | ||
b649e039 AM |
520 | bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const |
521 | { | |
522 | for (unsigned int i = 0; i < tx.vin.size(); i++) | |
523 | if (exists(tx.vin[i].prevout.hash)) | |
524 | return false; | |
525 | return true; | |
526 | } | |
171ca774 | 527 | |
7c70438d | 528 | CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { } |
a0fa20a1 | 529 | |
22de1602 SB |
530 | bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const { |
531 | if (mempool.mapNullifiers.count(nf)) | |
d66877af SB |
532 | return true; |
533 | ||
22de1602 | 534 | return base->GetNullifier(nf); |
d66877af SB |
535 | } |
536 | ||
a3dc587a | 537 | bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const { |
ad08d0b9 PW |
538 | // If an entry in the mempool exists, always return that one, as it's guaranteed to never |
539 | // conflict with the underlying cache, and it cannot have pruned entries (as it contains full) | |
540 | // transactions. First checking the underlying cache risks returning a pruned entry instead. | |
a0fa20a1 PW |
541 | CTransaction tx; |
542 | if (mempool.lookup(txid, tx)) { | |
543 | coins = CCoins(tx, MEMPOOL_HEIGHT); | |
544 | return true; | |
545 | } | |
ad08d0b9 | 546 | return (base->GetCoins(txid, coins) && !coins.IsPruned()); |
a0fa20a1 PW |
547 | } |
548 | ||
a3dc587a | 549 | bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const { |
a0fa20a1 PW |
550 | return mempool.exists(txid) || base->HaveCoins(txid); |
551 | } | |
bde5c8b0 PW |
552 | |
553 | size_t CTxMemPool::DynamicMemoryUsage() const { | |
554 | LOCK(cs); | |
e328fa32 AH |
555 | // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented. |
556 | return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage; | |
bde5c8b0 | 557 | } |