]>
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" |
7e2cbee1 | 18 | #define _COINBASE_MATURITY 100 |
319b1160 GA |
19 | |
20 | using namespace std; | |
21 | ||
8bdd2877 | 22 | CTxMemPoolEntry::CTxMemPoolEntry(): |
a4b25180 SD |
23 | nFee(0), nTxSize(0), nModSize(0), nUsageSize(0), nTime(0), dPriority(0.0), |
24 | hadNoDependencies(false), spendsCoinbase(false) | |
4d707d51 GA |
25 | { |
26 | nHeight = MEMPOOL_HEIGHT; | |
27 | } | |
28 | ||
a372168e | 29 | CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee, |
4d707d51 | 30 | int64_t _nTime, double _dPriority, |
a4b25180 | 31 | unsigned int _nHeight, bool poolHasNoInputsOf, |
34a64fe0 | 32 | bool _spendsCoinbase, uint32_t _nBranchId): |
b649e039 | 33 | tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight), |
a4b25180 | 34 | hadNoDependencies(poolHasNoInputsOf), |
34a64fe0 | 35 | spendsCoinbase(_spendsCoinbase), nBranchId(_nBranchId) |
4d707d51 GA |
36 | { |
37 | nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | |
c26649f9 | 38 | nModSize = tx.CalculateModifiedSize(nTxSize); |
6bd1d60c | 39 | nUsageSize = RecursiveDynamicUsage(tx); |
e328fa32 | 40 | feeRate = CFeeRate(nFee, nTxSize); |
4d707d51 GA |
41 | } |
42 | ||
43 | CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other) | |
44 | { | |
45 | *this = other; | |
46 | } | |
47 | ||
48 | double | |
49 | CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const | |
50 | { | |
a372168e | 51 | CAmount nValueIn = tx.GetValueOut()+nFee; |
c26649f9 | 52 | double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize; |
4d707d51 GA |
53 | double dResult = dPriority + deltaPriority; |
54 | return dResult; | |
55 | } | |
56 | ||
8bdd2877 | 57 | CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) : |
b649e039 | 58 | nTransactionsUpdated(0) |
319b1160 GA |
59 | { |
60 | // Sanity checks off by default for performance, because otherwise | |
61 | // accepting transactions becomes O(N^2) where N is the number | |
62 | // of transactions in the pool | |
934fd197 | 63 | nCheckFrequency = 0; |
171ca774 | 64 | |
b649e039 | 65 | minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee); |
171ca774 GA |
66 | } |
67 | ||
68 | CTxMemPool::~CTxMemPool() | |
69 | { | |
70 | delete minerPolicyEstimator; | |
319b1160 GA |
71 | } |
72 | ||
73 | void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins) | |
74 | { | |
75 | LOCK(cs); | |
76 | ||
77 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0)); | |
78 | ||
79 | // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx | |
80 | while (it != mapNextTx.end() && it->first.hash == hashTx) { | |
81 | coins.Spend(it->first.n); // and remove those outputs from coins | |
82 | it++; | |
83 | } | |
84 | } | |
85 | ||
86 | unsigned int CTxMemPool::GetTransactionsUpdated() const | |
87 | { | |
88 | LOCK(cs); | |
89 | return nTransactionsUpdated; | |
90 | } | |
91 | ||
92 | void CTxMemPool::AddTransactionsUpdated(unsigned int n) | |
93 | { | |
94 | LOCK(cs); | |
95 | nTransactionsUpdated += n; | |
96 | } | |
97 | ||
98 | ||
b649e039 | 99 | bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate) |
319b1160 GA |
100 | { |
101 | // Add to memory pool without checking anything. | |
102 | // Used by main.cpp AcceptToMemoryPool(), which DOES do | |
103 | // all the appropriate checks. | |
104 | LOCK(cs); | |
e328fa32 AH |
105 | mapTx.insert(entry); |
106 | const CTransaction& tx = mapTx.find(hash)->GetTx(); | |
b649e039 AM |
107 | for (unsigned int i = 0; i < tx.vin.size(); i++) |
108 | mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i); | |
b7e4abd6 | 109 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
110 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
111 | mapNullifiers[nf] = &tx; | |
d66877af SB |
112 | } |
113 | } | |
b649e039 AM |
114 | nTransactionsUpdated++; |
115 | totalTxSize += entry.GetTxSize(); | |
bde5c8b0 | 116 | cachedInnerUsage += entry.DynamicMemoryUsage(); |
b649e039 AM |
117 | minerPolicyEstimator->processTransaction(entry, fCurrentEstimate); |
118 | ||
319b1160 GA |
119 | return true; |
120 | } | |
121 | ||
8b78a819 T |
122 | void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view) |
123 | { | |
124 | LOCK(cs); | |
125 | const CTransaction& tx = entry.GetTx(); | |
126 | std::vector<CMempoolAddressDeltaKey> inserted; | |
127 | ||
128 | uint256 txhash = tx.GetHash(); | |
129 | for (unsigned int j = 0; j < tx.vin.size(); j++) { | |
130 | const CTxIn input = tx.vin[j]; | |
131 | const CTxOut &prevout = view.GetOutputFor(input); | |
132 | if (prevout.scriptPubKey.IsPayToScriptHash()) { | |
133 | vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22); | |
134 | CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, j, 1); | |
135 | CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n); | |
136 | mapAddress.insert(make_pair(key, delta)); | |
137 | inserted.push_back(key); | |
138 | } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { | |
139 | vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23); | |
140 | CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, j, 1); | |
141 | CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n); | |
142 | mapAddress.insert(make_pair(key, delta)); | |
143 | inserted.push_back(key); | |
144 | } | |
145 | } | |
146 | ||
147 | for (unsigned int k = 0; k < tx.vout.size(); k++) { | |
148 | const CTxOut &out = tx.vout[k]; | |
149 | if (out.scriptPubKey.IsPayToScriptHash()) { | |
150 | vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22); | |
151 | CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, k, 0); | |
152 | mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue))); | |
153 | inserted.push_back(key); | |
154 | } else if (out.scriptPubKey.IsPayToPublicKeyHash()) { | |
155 | vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); | |
156 | std::pair<addressDeltaMap::iterator,bool> ret; | |
157 | CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, 0); | |
158 | mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue))); | |
159 | inserted.push_back(key); | |
160 | } | |
161 | } | |
162 | ||
163 | mapAddressInserted.insert(make_pair(txhash, inserted)); | |
164 | } | |
165 | ||
166 | bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint160, int> > &addresses, | |
167 | std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results) | |
168 | { | |
169 | LOCK(cs); | |
170 | for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) { | |
171 | addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first)); | |
172 | while (ait != mapAddress.end() && (*ait).first.addressBytes == (*it).first && (*ait).first.type == (*it).second) { | |
173 | results.push_back(*ait); | |
174 | ait++; | |
175 | } | |
176 | } | |
177 | return true; | |
178 | } | |
179 | ||
180 | bool CTxMemPool::removeAddressIndex(const uint256 txhash) | |
181 | { | |
182 | LOCK(cs); | |
183 | addressDeltaMapInserted::iterator it = mapAddressInserted.find(txhash); | |
184 | ||
185 | if (it != mapAddressInserted.end()) { | |
186 | std::vector<CMempoolAddressDeltaKey> keys = (*it).second; | |
187 | for (std::vector<CMempoolAddressDeltaKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) { | |
188 | mapAddress.erase(*mit); | |
189 | } | |
190 | mapAddressInserted.erase(it); | |
191 | } | |
192 | ||
193 | return true; | |
194 | } | |
195 | ||
196 | void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view) | |
197 | { | |
198 | LOCK(cs); | |
199 | ||
200 | const CTransaction& tx = entry.GetTx(); | |
201 | std::vector<CSpentIndexKey> inserted; | |
202 | ||
203 | uint256 txhash = tx.GetHash(); | |
204 | for (unsigned int j = 0; j < tx.vin.size(); j++) { | |
205 | const CTxIn input = tx.vin[j]; | |
206 | const CTxOut &prevout = view.GetOutputFor(input); | |
207 | uint160 addressHash; | |
208 | int addressType; | |
209 | ||
210 | if (prevout.scriptPubKey.IsPayToScriptHash()) { | |
211 | addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22)); | |
212 | addressType = 2; | |
213 | } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { | |
214 | addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23)); | |
215 | addressType = 1; | |
216 | } else { | |
217 | addressHash.SetNull(); | |
218 | addressType = 0; | |
219 | } | |
220 | ||
221 | CSpentIndexKey key = CSpentIndexKey(input.prevout.hash, input.prevout.n); | |
222 | CSpentIndexValue value = CSpentIndexValue(txhash, j, -1, prevout.nValue, addressType, addressHash); | |
223 | ||
224 | mapSpent.insert(make_pair(key, value)); | |
225 | inserted.push_back(key); | |
226 | ||
227 | } | |
228 | ||
229 | mapSpentInserted.insert(make_pair(txhash, inserted)); | |
230 | } | |
231 | ||
232 | bool CTxMemPool::getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) | |
233 | { | |
234 | LOCK(cs); | |
235 | mapSpentIndex::iterator it; | |
236 | ||
237 | it = mapSpent.find(key); | |
238 | if (it != mapSpent.end()) { | |
239 | value = it->second; | |
240 | return true; | |
241 | } | |
242 | return false; | |
243 | } | |
244 | ||
245 | bool CTxMemPool::removeSpentIndex(const uint256 txhash) | |
246 | { | |
247 | LOCK(cs); | |
248 | mapSpentIndexInserted::iterator it = mapSpentInserted.find(txhash); | |
249 | ||
250 | if (it != mapSpentInserted.end()) { | |
251 | std::vector<CSpentIndexKey> keys = (*it).second; | |
252 | for (std::vector<CSpentIndexKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) { | |
253 | mapSpent.erase(*mit); | |
254 | } | |
255 | mapSpentInserted.erase(it); | |
256 | } | |
257 | ||
258 | return true; | |
259 | } | |
319b1160 | 260 | |
7fd6219a | 261 | void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive) |
319b1160 GA |
262 | { |
263 | // Remove transaction from memory pool | |
264 | { | |
265 | LOCK(cs); | |
7fd6219a | 266 | std::deque<uint256> txToRemove; |
805344dc S |
267 | txToRemove.push_back(origTx.GetHash()); |
268 | if (fRecursive && !mapTx.count(origTx.GetHash())) { | |
ad9e86dc GA |
269 | // If recursively removing but origTx isn't in the mempool |
270 | // be sure to remove any children that are in the pool. This can | |
271 | // happen during chain re-orgs if origTx isn't re-accepted into | |
272 | // the mempool for any reason. | |
273 | for (unsigned int i = 0; i < origTx.vout.size(); i++) { | |
805344dc | 274 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i)); |
ad9e86dc GA |
275 | if (it == mapNextTx.end()) |
276 | continue; | |
805344dc | 277 | txToRemove.push_back(it->second.ptx->GetHash()); |
ad9e86dc GA |
278 | } |
279 | } | |
7fd6219a | 280 | while (!txToRemove.empty()) |
319b1160 | 281 | { |
7fd6219a MC |
282 | uint256 hash = txToRemove.front(); |
283 | txToRemove.pop_front(); | |
284 | if (!mapTx.count(hash)) | |
285 | continue; | |
e328fa32 | 286 | const CTransaction& tx = mapTx.find(hash)->GetTx(); |
7fd6219a MC |
287 | if (fRecursive) { |
288 | for (unsigned int i = 0; i < tx.vout.size(); i++) { | |
289 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i)); | |
290 | if (it == mapNextTx.end()) | |
291 | continue; | |
805344dc | 292 | txToRemove.push_back(it->second.ptx->GetHash()); |
7fd6219a MC |
293 | } |
294 | } | |
319b1160 GA |
295 | BOOST_FOREACH(const CTxIn& txin, tx.vin) |
296 | mapNextTx.erase(txin.prevout); | |
b7e4abd6 | 297 | BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) { |
22de1602 SB |
298 | BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) { |
299 | mapNullifiers.erase(nf); | |
d66877af SB |
300 | } |
301 | } | |
6f2c26a4 | 302 | |
7fd6219a | 303 | removed.push_back(tx); |
e328fa32 AH |
304 | totalTxSize -= mapTx.find(hash)->GetTxSize(); |
305 | cachedInnerUsage -= mapTx.find(hash)->DynamicMemoryUsage(); | |
319b1160 GA |
306 | mapTx.erase(hash); |
307 | nTransactionsUpdated++; | |
b649e039 | 308 | minerPolicyEstimator->removeTx(hash); |
8b78a819 T |
309 | removeAddressIndex(hash); |
310 | removeSpentIndex(hash); | |
319b1160 GA |
311 | } |
312 | } | |
319b1160 GA |
313 | } |
314 | ||
233c9eb6 | 315 | void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) |
723d12c0 MC |
316 | { |
317 | // Remove transactions spending a coinbase which are now immature | |
9edf27ec | 318 | extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; |
7a90b9dd | 319 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
320 | COINBASE_MATURITY = _COINBASE_MATURITY; | |
c944d161 | 321 | // Remove transactions spending a coinbase which are now immature and no-longer-final transactions |
723d12c0 MC |
322 | LOCK(cs); |
323 | list<CTransaction> transactionsToRemove; | |
e328fa32 AH |
324 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
325 | const CTransaction& tx = it->GetTx(); | |
233c9eb6 | 326 | if (!CheckFinalTx(tx, flags)) { |
f5b35d23 | 327 | transactionsToRemove.push_back(tx); |
a4b25180 | 328 | } else if (it->GetSpendsCoinbase()) { |
f5b35d23 MC |
329 | BOOST_FOREACH(const CTxIn& txin, tx.vin) { |
330 | indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash); | |
331 | if (it2 != mapTx.end()) | |
332 | continue; | |
333 | const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash); | |
334 | if (nCheckFrequency != 0) assert(coins); | |
335 | if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) { | |
336 | transactionsToRemove.push_back(tx); | |
337 | break; | |
338 | } | |
723d12c0 MC |
339 | } |
340 | } | |
341 | } | |
342 | BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) { | |
343 | list<CTransaction> removed; | |
344 | remove(tx, removed, true); | |
345 | } | |
346 | } | |
347 | ||
a8ac403d SB |
348 | |
349 | void CTxMemPool::removeWithAnchor(const uint256 &invalidRoot) | |
350 | { | |
351 | // If a block is disconnected from the tip, and the root changed, | |
352 | // we must invalidate transactions from the mempool which spend | |
353 | // from that root -- almost as though they were spending coinbases | |
354 | // which are no longer valid to spend due to coinbase maturity. | |
355 | LOCK(cs); | |
356 | list<CTransaction> transactionsToRemove; | |
357 | ||
e328fa32 AH |
358 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
359 | const CTransaction& tx = it->GetTx(); | |
b7e4abd6 SB |
360 | BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) { |
361 | if (joinsplit.anchor == invalidRoot) { | |
a8ac403d SB |
362 | transactionsToRemove.push_back(tx); |
363 | break; | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) { | |
369 | list<CTransaction> removed; | |
370 | remove(tx, removed, true); | |
371 | } | |
372 | } | |
373 | ||
93a18a36 | 374 | void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed) |
319b1160 GA |
375 | { |
376 | // Remove transactions which depend on inputs of tx, recursively | |
98e84aae | 377 | list<CTransaction> result; |
319b1160 GA |
378 | LOCK(cs); |
379 | BOOST_FOREACH(const CTxIn &txin, tx.vin) { | |
380 | std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout); | |
381 | if (it != mapNextTx.end()) { | |
382 | const CTransaction &txConflict = *it->second.ptx; | |
383 | if (txConflict != tx) | |
93a18a36 GA |
384 | { |
385 | remove(txConflict, removed, true); | |
386 | } | |
319b1160 GA |
387 | } |
388 | } | |
d66877af | 389 | |
b7e4abd6 | 390 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
391 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
392 | std::map<uint256, const CTransaction*>::iterator it = mapNullifiers.find(nf); | |
bb64be52 | 393 | if (it != mapNullifiers.end()) { |
d66877af SB |
394 | const CTransaction &txConflict = *it->second; |
395 | if (txConflict != tx) | |
396 | { | |
397 | remove(txConflict, removed, true); | |
398 | } | |
399 | } | |
400 | } | |
401 | } | |
319b1160 GA |
402 | } |
403 | ||
9bb37bf0 JG |
404 | void CTxMemPool::removeExpired(unsigned int nBlockHeight) |
405 | { | |
406 | // Remove expired txs from the mempool | |
407 | LOCK(cs); | |
408 | list<CTransaction> transactionsToRemove; | |
409 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) | |
410 | { | |
411 | const CTransaction& tx = it->GetTx(); | |
412 | if (IsExpiredTx(tx, nBlockHeight)) { | |
413 | transactionsToRemove.push_back(tx); | |
414 | } | |
415 | } | |
416 | for (const CTransaction& tx : transactionsToRemove) { | |
417 | list<CTransaction> removed; | |
418 | remove(tx, removed, true); | |
eb138626 | 419 | LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString()); |
9bb37bf0 JG |
420 | } |
421 | } | |
422 | ||
7329fdd1 MF |
423 | /** |
424 | * Called when a block is connected. Removes from mempool and updates the miner fee estimator. | |
425 | */ | |
171ca774 | 426 | void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight, |
b649e039 | 427 | std::list<CTransaction>& conflicts, bool fCurrentEstimate) |
171ca774 GA |
428 | { |
429 | LOCK(cs); | |
430 | std::vector<CTxMemPoolEntry> entries; | |
431 | BOOST_FOREACH(const CTransaction& tx, vtx) | |
432 | { | |
805344dc | 433 | uint256 hash = tx.GetHash(); |
e328fa32 AH |
434 | |
435 | indexed_transaction_set::iterator i = mapTx.find(hash); | |
436 | if (i != mapTx.end()) | |
437 | entries.push_back(*i); | |
171ca774 | 438 | } |
171ca774 GA |
439 | BOOST_FOREACH(const CTransaction& tx, vtx) |
440 | { | |
441 | std::list<CTransaction> dummy; | |
442 | remove(tx, dummy, false); | |
443 | removeConflicts(tx, conflicts); | |
805344dc | 444 | ClearPrioritisation(tx.GetHash()); |
171ca774 | 445 | } |
b649e039 AM |
446 | // After the txs in the new block have been removed from the mempool, update policy estimates |
447 | minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate); | |
171ca774 GA |
448 | } |
449 | ||
34a64fe0 JG |
450 | /** |
451 | * Called whenever the tip changes. Removes transactions which don't commit to | |
452 | * the given branch ID from the mempool. | |
453 | */ | |
454 | void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId) | |
455 | { | |
456 | LOCK(cs); | |
457 | std::list<CTransaction> transactionsToRemove; | |
458 | ||
459 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { | |
460 | const CTransaction& tx = it->GetTx(); | |
461 | if (it->GetValidatedBranchId() != nMemPoolBranchId) { | |
462 | transactionsToRemove.push_back(tx); | |
463 | } | |
464 | } | |
465 | ||
466 | for (const CTransaction& tx : transactionsToRemove) { | |
467 | std::list<CTransaction> removed; | |
468 | remove(tx, removed, true); | |
469 | } | |
470 | } | |
471 | ||
319b1160 GA |
472 | void CTxMemPool::clear() |
473 | { | |
474 | LOCK(cs); | |
475 | mapTx.clear(); | |
476 | mapNextTx.clear(); | |
6f2c26a4 | 477 | totalTxSize = 0; |
bde5c8b0 | 478 | cachedInnerUsage = 0; |
319b1160 GA |
479 | ++nTransactionsUpdated; |
480 | } | |
481 | ||
d0867acb | 482 | void CTxMemPool::check(const CCoinsViewCache *pcoins) const |
319b1160 | 483 | { |
934fd197 PW |
484 | if (nCheckFrequency == 0) |
485 | return; | |
486 | ||
487 | if (insecure_rand() >= nCheckFrequency) | |
319b1160 GA |
488 | return; |
489 | ||
490 | LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size()); | |
491 | ||
6f2c26a4 | 492 | uint64_t checkTotal = 0; |
bde5c8b0 | 493 | uint64_t innerUsage = 0; |
6f2c26a4 | 494 | |
b7b4318f | 495 | CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins)); |
722d811f | 496 | const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate); |
b7b4318f | 497 | |
319b1160 | 498 | LOCK(cs); |
b7b4318f | 499 | list<const CTxMemPoolEntry*> waitingOnDependants; |
e328fa32 | 500 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
319b1160 | 501 | unsigned int i = 0; |
e328fa32 AH |
502 | checkTotal += it->GetTxSize(); |
503 | innerUsage += it->DynamicMemoryUsage(); | |
504 | const CTransaction& tx = it->GetTx(); | |
b7b4318f | 505 | bool fDependsWait = false; |
4d707d51 | 506 | BOOST_FOREACH(const CTxIn &txin, tx.vin) { |
319b1160 | 507 | // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's. |
e328fa32 | 508 | indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash); |
319b1160 | 509 | if (it2 != mapTx.end()) { |
e328fa32 | 510 | const CTransaction& tx2 = it2->GetTx(); |
4d707d51 | 511 | assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull()); |
b7b4318f | 512 | fDependsWait = true; |
319b1160 | 513 | } else { |
629d75fa PW |
514 | const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash); |
515 | assert(coins && coins->IsAvailable(txin.prevout.n)); | |
319b1160 GA |
516 | } |
517 | // Check whether its inputs are marked in mapNextTx. | |
518 | std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout); | |
519 | assert(it3 != mapNextTx.end()); | |
4d707d51 | 520 | assert(it3->second.ptx == &tx); |
319b1160 GA |
521 | assert(it3->second.n == i); |
522 | i++; | |
523 | } | |
a667caec SB |
524 | |
525 | boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates; | |
526 | ||
b7e4abd6 | 527 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
528 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
529 | assert(!pcoins->GetNullifier(nf)); | |
d66877af SB |
530 | } |
531 | ||
434f3284 | 532 | ZCIncrementalMerkleTree tree; |
b7e4abd6 | 533 | auto it = intermediates.find(joinsplit.anchor); |
a667caec SB |
534 | if (it != intermediates.end()) { |
535 | tree = it->second; | |
536 | } else { | |
b7e4abd6 | 537 | assert(pcoins->GetAnchorAt(joinsplit.anchor, tree)); |
a667caec SB |
538 | } |
539 | ||
b7e4abd6 | 540 | BOOST_FOREACH(const uint256& commitment, joinsplit.commitments) |
a667caec SB |
541 | { |
542 | tree.append(commitment); | |
543 | } | |
544 | ||
545 | intermediates.insert(std::make_pair(tree.root(), tree)); | |
d66877af | 546 | } |
b7b4318f | 547 | if (fDependsWait) |
e328fa32 | 548 | waitingOnDependants.push_back(&(*it)); |
b7b4318f | 549 | else { |
d7621ccf | 550 | CValidationState state; |
722d811f JT |
551 | bool fCheckResult = tx.IsCoinBase() || |
552 | Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus()); | |
553 | assert(fCheckResult); | |
8cb98d91 | 554 | UpdateCoins(tx, mempoolDuplicate, 1000000); |
b7b4318f MC |
555 | } |
556 | } | |
557 | unsigned int stepsSinceLastRemove = 0; | |
558 | while (!waitingOnDependants.empty()) { | |
559 | const CTxMemPoolEntry* entry = waitingOnDependants.front(); | |
560 | waitingOnDependants.pop_front(); | |
561 | CValidationState state; | |
562 | if (!mempoolDuplicate.HaveInputs(entry->GetTx())) { | |
563 | waitingOnDependants.push_back(entry); | |
564 | stepsSinceLastRemove++; | |
565 | assert(stepsSinceLastRemove < waitingOnDependants.size()); | |
566 | } else { | |
722d811f JT |
567 | bool fCheckResult = entry->GetTx().IsCoinBase() || |
568 | Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus()); | |
569 | assert(fCheckResult); | |
8cb98d91 | 570 | UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000); |
b7b4318f MC |
571 | stepsSinceLastRemove = 0; |
572 | } | |
319b1160 GA |
573 | } |
574 | for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) { | |
805344dc | 575 | uint256 hash = it->second.ptx->GetHash(); |
e328fa32 AH |
576 | indexed_transaction_set::const_iterator it2 = mapTx.find(hash); |
577 | const CTransaction& tx = it2->GetTx(); | |
319b1160 | 578 | assert(it2 != mapTx.end()); |
4d707d51 GA |
579 | assert(&tx == it->second.ptx); |
580 | assert(tx.vin.size() > it->second.n); | |
319b1160 GA |
581 | assert(it->first == it->second.ptx->vin[it->second.n].prevout); |
582 | } | |
6f2c26a4 | 583 | |
bb64be52 | 584 | for (std::map<uint256, const CTransaction*>::const_iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); it++) { |
805344dc | 585 | uint256 hash = it->second->GetHash(); |
e328fa32 AH |
586 | indexed_transaction_set::const_iterator it2 = mapTx.find(hash); |
587 | const CTransaction& tx = it2->GetTx(); | |
d66877af SB |
588 | assert(it2 != mapTx.end()); |
589 | assert(&tx == it->second); | |
590 | } | |
591 | ||
6f2c26a4 | 592 | assert(totalTxSize == checkTotal); |
bde5c8b0 | 593 | assert(innerUsage == cachedInnerUsage); |
319b1160 GA |
594 | } |
595 | ||
4d707d51 | 596 | void CTxMemPool::queryHashes(vector<uint256>& vtxid) |
319b1160 GA |
597 | { |
598 | vtxid.clear(); | |
599 | ||
600 | LOCK(cs); | |
601 | vtxid.reserve(mapTx.size()); | |
e328fa32 AH |
602 | for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) |
603 | vtxid.push_back(mi->GetTx().GetHash()); | |
319b1160 GA |
604 | } |
605 | ||
606 | bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const | |
607 | { | |
608 | LOCK(cs); | |
e328fa32 | 609 | indexed_transaction_set::const_iterator i = mapTx.find(hash); |
319b1160 | 610 | if (i == mapTx.end()) return false; |
e328fa32 | 611 | result = i->GetTx(); |
319b1160 GA |
612 | return true; |
613 | } | |
a0fa20a1 | 614 | |
171ca774 GA |
615 | CFeeRate CTxMemPool::estimateFee(int nBlocks) const |
616 | { | |
617 | LOCK(cs); | |
618 | return minerPolicyEstimator->estimateFee(nBlocks); | |
619 | } | |
620 | double CTxMemPool::estimatePriority(int nBlocks) const | |
621 | { | |
622 | LOCK(cs); | |
623 | return minerPolicyEstimator->estimatePriority(nBlocks); | |
624 | } | |
625 | ||
626 | bool | |
627 | CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const | |
628 | { | |
629 | try { | |
630 | LOCK(cs); | |
b649e039 | 631 | fileout << 109900; // version required to read: 0.10.99 or later |
171ca774 GA |
632 | fileout << CLIENT_VERSION; // version that wrote the file |
633 | minerPolicyEstimator->Write(fileout); | |
634 | } | |
27df4123 | 635 | catch (const std::exception&) { |
7ff9d122 | 636 | LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n"); |
171ca774 GA |
637 | return false; |
638 | } | |
639 | return true; | |
640 | } | |
641 | ||
642 | bool | |
643 | CTxMemPool::ReadFeeEstimates(CAutoFile& filein) | |
644 | { | |
645 | try { | |
646 | int nVersionRequired, nVersionThatWrote; | |
647 | filein >> nVersionRequired >> nVersionThatWrote; | |
648 | if (nVersionRequired > CLIENT_VERSION) | |
5262fde0 | 649 | return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired); |
171ca774 GA |
650 | |
651 | LOCK(cs); | |
b649e039 | 652 | minerPolicyEstimator->Read(filein); |
171ca774 | 653 | } |
27df4123 | 654 | catch (const std::exception&) { |
7ff9d122 | 655 | LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n"); |
171ca774 GA |
656 | return false; |
657 | } | |
658 | return true; | |
659 | } | |
660 | ||
a372168e | 661 | void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta) |
2a72d459 LD |
662 | { |
663 | { | |
664 | LOCK(cs); | |
a372168e | 665 | std::pair<double, CAmount> &deltas = mapDeltas[hash]; |
2a72d459 LD |
666 | deltas.first += dPriorityDelta; |
667 | deltas.second += nFeeDelta; | |
668 | } | |
a372168e | 669 | LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta)); |
2a72d459 LD |
670 | } |
671 | ||
a372168e | 672 | void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) |
2a72d459 LD |
673 | { |
674 | LOCK(cs); | |
a372168e | 675 | std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash); |
2a72d459 LD |
676 | if (pos == mapDeltas.end()) |
677 | return; | |
a372168e | 678 | const std::pair<double, CAmount> &deltas = pos->second; |
2a72d459 LD |
679 | dPriorityDelta += deltas.first; |
680 | nFeeDelta += deltas.second; | |
681 | } | |
682 | ||
683 | void CTxMemPool::ClearPrioritisation(const uint256 hash) | |
684 | { | |
685 | LOCK(cs); | |
686 | mapDeltas.erase(hash); | |
687 | } | |
688 | ||
b649e039 AM |
689 | bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const |
690 | { | |
691 | for (unsigned int i = 0; i < tx.vin.size(); i++) | |
692 | if (exists(tx.vin[i].prevout.hash)) | |
693 | return false; | |
694 | return true; | |
695 | } | |
171ca774 | 696 | |
7c70438d | 697 | CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { } |
a0fa20a1 | 698 | |
22de1602 SB |
699 | bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const { |
700 | if (mempool.mapNullifiers.count(nf)) | |
d66877af SB |
701 | return true; |
702 | ||
22de1602 | 703 | return base->GetNullifier(nf); |
d66877af SB |
704 | } |
705 | ||
a3dc587a | 706 | bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const { |
ad08d0b9 PW |
707 | // If an entry in the mempool exists, always return that one, as it's guaranteed to never |
708 | // conflict with the underlying cache, and it cannot have pruned entries (as it contains full) | |
709 | // transactions. First checking the underlying cache risks returning a pruned entry instead. | |
a0fa20a1 PW |
710 | CTransaction tx; |
711 | if (mempool.lookup(txid, tx)) { | |
712 | coins = CCoins(tx, MEMPOOL_HEIGHT); | |
713 | return true; | |
714 | } | |
ad08d0b9 | 715 | return (base->GetCoins(txid, coins) && !coins.IsPruned()); |
a0fa20a1 PW |
716 | } |
717 | ||
a3dc587a | 718 | bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const { |
a0fa20a1 PW |
719 | return mempool.exists(txid) || base->HaveCoins(txid); |
720 | } | |
bde5c8b0 PW |
721 | |
722 | size_t CTxMemPool::DynamicMemoryUsage() const { | |
723 | LOCK(cs); | |
e328fa32 AH |
724 | // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented. |
725 | return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage; | |
bde5c8b0 | 726 | } |