]>
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 | ||
f045635e | 404 | int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t nTime,int32_t dispflag); |
405 | ||
9bb37bf0 JG |
406 | void CTxMemPool::removeExpired(unsigned int nBlockHeight) |
407 | { | |
f045635e | 408 | CBlockIndex *tipindex; |
9bb37bf0 JG |
409 | // Remove expired txs from the mempool |
410 | LOCK(cs); | |
411 | list<CTransaction> transactionsToRemove; | |
412 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) | |
413 | { | |
414 | const CTransaction& tx = it->GetTx(); | |
f045635e | 415 | tipindex = chainActive.Tip(); |
38215324 | 416 | if (IsExpiredTx(tx, nBlockHeight) || (ASSETCHAINS_SYMBOL[0] == 0 && tipindex != 0 && komodo_validate_interest(tx,tipindex->nHeight+1,tipindex->GetMedianTimePast() + 777,0)) < 0) |
4825cbeb | 417 | { |
9bb37bf0 JG |
418 | transactionsToRemove.push_back(tx); |
419 | } | |
420 | } | |
421 | for (const CTransaction& tx : transactionsToRemove) { | |
422 | list<CTransaction> removed; | |
423 | remove(tx, removed, true); | |
eb138626 | 424 | LogPrint("mempool", "Removing expired txid: %s\n", tx.GetHash().ToString()); |
9bb37bf0 JG |
425 | } |
426 | } | |
427 | ||
7329fdd1 MF |
428 | /** |
429 | * Called when a block is connected. Removes from mempool and updates the miner fee estimator. | |
430 | */ | |
171ca774 | 431 | void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight, |
b649e039 | 432 | std::list<CTransaction>& conflicts, bool fCurrentEstimate) |
171ca774 GA |
433 | { |
434 | LOCK(cs); | |
435 | std::vector<CTxMemPoolEntry> entries; | |
436 | BOOST_FOREACH(const CTransaction& tx, vtx) | |
437 | { | |
805344dc | 438 | uint256 hash = tx.GetHash(); |
e328fa32 AH |
439 | |
440 | indexed_transaction_set::iterator i = mapTx.find(hash); | |
441 | if (i != mapTx.end()) | |
442 | entries.push_back(*i); | |
171ca774 | 443 | } |
171ca774 GA |
444 | BOOST_FOREACH(const CTransaction& tx, vtx) |
445 | { | |
446 | std::list<CTransaction> dummy; | |
447 | remove(tx, dummy, false); | |
448 | removeConflicts(tx, conflicts); | |
805344dc | 449 | ClearPrioritisation(tx.GetHash()); |
171ca774 | 450 | } |
b649e039 AM |
451 | // After the txs in the new block have been removed from the mempool, update policy estimates |
452 | minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate); | |
171ca774 GA |
453 | } |
454 | ||
34a64fe0 JG |
455 | /** |
456 | * Called whenever the tip changes. Removes transactions which don't commit to | |
457 | * the given branch ID from the mempool. | |
458 | */ | |
459 | void CTxMemPool::removeWithoutBranchId(uint32_t nMemPoolBranchId) | |
460 | { | |
461 | LOCK(cs); | |
462 | std::list<CTransaction> transactionsToRemove; | |
463 | ||
464 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { | |
465 | const CTransaction& tx = it->GetTx(); | |
466 | if (it->GetValidatedBranchId() != nMemPoolBranchId) { | |
467 | transactionsToRemove.push_back(tx); | |
468 | } | |
469 | } | |
470 | ||
471 | for (const CTransaction& tx : transactionsToRemove) { | |
472 | std::list<CTransaction> removed; | |
473 | remove(tx, removed, true); | |
474 | } | |
475 | } | |
476 | ||
319b1160 GA |
477 | void CTxMemPool::clear() |
478 | { | |
479 | LOCK(cs); | |
480 | mapTx.clear(); | |
481 | mapNextTx.clear(); | |
6f2c26a4 | 482 | totalTxSize = 0; |
bde5c8b0 | 483 | cachedInnerUsage = 0; |
319b1160 GA |
484 | ++nTransactionsUpdated; |
485 | } | |
486 | ||
d0867acb | 487 | void CTxMemPool::check(const CCoinsViewCache *pcoins) const |
319b1160 | 488 | { |
934fd197 PW |
489 | if (nCheckFrequency == 0) |
490 | return; | |
491 | ||
492 | if (insecure_rand() >= nCheckFrequency) | |
319b1160 GA |
493 | return; |
494 | ||
495 | LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size()); | |
496 | ||
6f2c26a4 | 497 | uint64_t checkTotal = 0; |
bde5c8b0 | 498 | uint64_t innerUsage = 0; |
6f2c26a4 | 499 | |
b7b4318f | 500 | CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins)); |
722d811f | 501 | const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate); |
b7b4318f | 502 | |
319b1160 | 503 | LOCK(cs); |
b7b4318f | 504 | list<const CTxMemPoolEntry*> waitingOnDependants; |
e328fa32 | 505 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
319b1160 | 506 | unsigned int i = 0; |
e328fa32 AH |
507 | checkTotal += it->GetTxSize(); |
508 | innerUsage += it->DynamicMemoryUsage(); | |
509 | const CTransaction& tx = it->GetTx(); | |
b7b4318f | 510 | bool fDependsWait = false; |
4d707d51 | 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. |
e328fa32 | 513 | indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash); |
319b1160 | 514 | if (it2 != mapTx.end()) { |
e328fa32 | 515 | const CTransaction& tx2 = it2->GetTx(); |
4d707d51 | 516 | assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull()); |
b7b4318f | 517 | fDependsWait = true; |
319b1160 | 518 | } else { |
629d75fa PW |
519 | const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash); |
520 | assert(coins && coins->IsAvailable(txin.prevout.n)); | |
319b1160 GA |
521 | } |
522 | // Check whether its inputs are marked in mapNextTx. | |
523 | std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout); | |
524 | assert(it3 != mapNextTx.end()); | |
4d707d51 | 525 | assert(it3->second.ptx == &tx); |
319b1160 GA |
526 | assert(it3->second.n == i); |
527 | i++; | |
528 | } | |
a667caec SB |
529 | |
530 | boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates; | |
531 | ||
b7e4abd6 | 532 | BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { |
22de1602 SB |
533 | BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { |
534 | assert(!pcoins->GetNullifier(nf)); | |
d66877af SB |
535 | } |
536 | ||
434f3284 | 537 | ZCIncrementalMerkleTree tree; |
b7e4abd6 | 538 | auto it = intermediates.find(joinsplit.anchor); |
a667caec SB |
539 | if (it != intermediates.end()) { |
540 | tree = it->second; | |
541 | } else { | |
b7e4abd6 | 542 | assert(pcoins->GetAnchorAt(joinsplit.anchor, tree)); |
a667caec SB |
543 | } |
544 | ||
b7e4abd6 | 545 | BOOST_FOREACH(const uint256& commitment, joinsplit.commitments) |
a667caec SB |
546 | { |
547 | tree.append(commitment); | |
548 | } | |
549 | ||
550 | intermediates.insert(std::make_pair(tree.root(), tree)); | |
d66877af | 551 | } |
b7b4318f | 552 | if (fDependsWait) |
e328fa32 | 553 | waitingOnDependants.push_back(&(*it)); |
b7b4318f | 554 | else { |
d7621ccf | 555 | CValidationState state; |
722d811f JT |
556 | bool fCheckResult = tx.IsCoinBase() || |
557 | Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight, Params().GetConsensus()); | |
558 | assert(fCheckResult); | |
8cb98d91 | 559 | UpdateCoins(tx, mempoolDuplicate, 1000000); |
b7b4318f MC |
560 | } |
561 | } | |
562 | unsigned int stepsSinceLastRemove = 0; | |
563 | while (!waitingOnDependants.empty()) { | |
564 | const CTxMemPoolEntry* entry = waitingOnDependants.front(); | |
565 | waitingOnDependants.pop_front(); | |
566 | CValidationState state; | |
567 | if (!mempoolDuplicate.HaveInputs(entry->GetTx())) { | |
568 | waitingOnDependants.push_back(entry); | |
569 | stepsSinceLastRemove++; | |
570 | assert(stepsSinceLastRemove < waitingOnDependants.size()); | |
571 | } else { | |
722d811f JT |
572 | bool fCheckResult = entry->GetTx().IsCoinBase() || |
573 | Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight, Params().GetConsensus()); | |
574 | assert(fCheckResult); | |
8cb98d91 | 575 | UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000); |
b7b4318f MC |
576 | stepsSinceLastRemove = 0; |
577 | } | |
319b1160 GA |
578 | } |
579 | for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) { | |
805344dc | 580 | uint256 hash = it->second.ptx->GetHash(); |
e328fa32 AH |
581 | indexed_transaction_set::const_iterator it2 = mapTx.find(hash); |
582 | const CTransaction& tx = it2->GetTx(); | |
319b1160 | 583 | assert(it2 != mapTx.end()); |
4d707d51 GA |
584 | assert(&tx == it->second.ptx); |
585 | assert(tx.vin.size() > it->second.n); | |
319b1160 GA |
586 | assert(it->first == it->second.ptx->vin[it->second.n].prevout); |
587 | } | |
6f2c26a4 | 588 | |
bb64be52 | 589 | for (std::map<uint256, const CTransaction*>::const_iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); it++) { |
805344dc | 590 | uint256 hash = it->second->GetHash(); |
e328fa32 AH |
591 | indexed_transaction_set::const_iterator it2 = mapTx.find(hash); |
592 | const CTransaction& tx = it2->GetTx(); | |
d66877af SB |
593 | assert(it2 != mapTx.end()); |
594 | assert(&tx == it->second); | |
595 | } | |
596 | ||
6f2c26a4 | 597 | assert(totalTxSize == checkTotal); |
bde5c8b0 | 598 | assert(innerUsage == cachedInnerUsage); |
319b1160 GA |
599 | } |
600 | ||
4d707d51 | 601 | void CTxMemPool::queryHashes(vector<uint256>& vtxid) |
319b1160 GA |
602 | { |
603 | vtxid.clear(); | |
604 | ||
605 | LOCK(cs); | |
606 | vtxid.reserve(mapTx.size()); | |
e328fa32 AH |
607 | for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) |
608 | vtxid.push_back(mi->GetTx().GetHash()); | |
319b1160 GA |
609 | } |
610 | ||
611 | bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const | |
612 | { | |
613 | LOCK(cs); | |
e328fa32 | 614 | indexed_transaction_set::const_iterator i = mapTx.find(hash); |
319b1160 | 615 | if (i == mapTx.end()) return false; |
e328fa32 | 616 | result = i->GetTx(); |
319b1160 GA |
617 | return true; |
618 | } | |
a0fa20a1 | 619 | |
171ca774 GA |
620 | CFeeRate CTxMemPool::estimateFee(int nBlocks) const |
621 | { | |
622 | LOCK(cs); | |
623 | return minerPolicyEstimator->estimateFee(nBlocks); | |
624 | } | |
625 | double CTxMemPool::estimatePriority(int nBlocks) const | |
626 | { | |
627 | LOCK(cs); | |
628 | return minerPolicyEstimator->estimatePriority(nBlocks); | |
629 | } | |
630 | ||
631 | bool | |
632 | CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const | |
633 | { | |
634 | try { | |
635 | LOCK(cs); | |
b649e039 | 636 | fileout << 109900; // version required to read: 0.10.99 or later |
171ca774 GA |
637 | fileout << CLIENT_VERSION; // version that wrote the file |
638 | minerPolicyEstimator->Write(fileout); | |
639 | } | |
27df4123 | 640 | catch (const std::exception&) { |
7ff9d122 | 641 | LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n"); |
171ca774 GA |
642 | return false; |
643 | } | |
644 | return true; | |
645 | } | |
646 | ||
647 | bool | |
648 | CTxMemPool::ReadFeeEstimates(CAutoFile& filein) | |
649 | { | |
650 | try { | |
651 | int nVersionRequired, nVersionThatWrote; | |
652 | filein >> nVersionRequired >> nVersionThatWrote; | |
653 | if (nVersionRequired > CLIENT_VERSION) | |
5262fde0 | 654 | return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired); |
171ca774 GA |
655 | |
656 | LOCK(cs); | |
b649e039 | 657 | minerPolicyEstimator->Read(filein); |
171ca774 | 658 | } |
27df4123 | 659 | catch (const std::exception&) { |
7ff9d122 | 660 | LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n"); |
171ca774 GA |
661 | return false; |
662 | } | |
663 | return true; | |
664 | } | |
665 | ||
a372168e | 666 | void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta) |
2a72d459 LD |
667 | { |
668 | { | |
669 | LOCK(cs); | |
a372168e | 670 | std::pair<double, CAmount> &deltas = mapDeltas[hash]; |
2a72d459 LD |
671 | deltas.first += dPriorityDelta; |
672 | deltas.second += nFeeDelta; | |
673 | } | |
a372168e | 674 | LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta)); |
2a72d459 LD |
675 | } |
676 | ||
a372168e | 677 | void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) |
2a72d459 LD |
678 | { |
679 | LOCK(cs); | |
a372168e | 680 | std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash); |
2a72d459 LD |
681 | if (pos == mapDeltas.end()) |
682 | return; | |
a372168e | 683 | const std::pair<double, CAmount> &deltas = pos->second; |
2a72d459 LD |
684 | dPriorityDelta += deltas.first; |
685 | nFeeDelta += deltas.second; | |
686 | } | |
687 | ||
688 | void CTxMemPool::ClearPrioritisation(const uint256 hash) | |
689 | { | |
690 | LOCK(cs); | |
691 | mapDeltas.erase(hash); | |
692 | } | |
693 | ||
b649e039 AM |
694 | bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const |
695 | { | |
696 | for (unsigned int i = 0; i < tx.vin.size(); i++) | |
697 | if (exists(tx.vin[i].prevout.hash)) | |
698 | return false; | |
699 | return true; | |
700 | } | |
171ca774 | 701 | |
7c70438d | 702 | CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { } |
a0fa20a1 | 703 | |
22de1602 SB |
704 | bool CCoinsViewMemPool::GetNullifier(const uint256 &nf) const { |
705 | if (mempool.mapNullifiers.count(nf)) | |
d66877af SB |
706 | return true; |
707 | ||
22de1602 | 708 | return base->GetNullifier(nf); |
d66877af SB |
709 | } |
710 | ||
a3dc587a | 711 | bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const { |
ad08d0b9 PW |
712 | // If an entry in the mempool exists, always return that one, as it's guaranteed to never |
713 | // conflict with the underlying cache, and it cannot have pruned entries (as it contains full) | |
714 | // transactions. First checking the underlying cache risks returning a pruned entry instead. | |
a0fa20a1 PW |
715 | CTransaction tx; |
716 | if (mempool.lookup(txid, tx)) { | |
717 | coins = CCoins(tx, MEMPOOL_HEIGHT); | |
718 | return true; | |
719 | } | |
ad08d0b9 | 720 | return (base->GetCoins(txid, coins) && !coins.IsPruned()); |
a0fa20a1 PW |
721 | } |
722 | ||
a3dc587a | 723 | bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const { |
a0fa20a1 PW |
724 | return mempool.exists(txid) || base->HaveCoins(txid); |
725 | } | |
bde5c8b0 PW |
726 | |
727 | size_t CTxMemPool::DynamicMemoryUsage() const { | |
728 | LOCK(cs); | |
e328fa32 AH |
729 | // Estimate the overhead of mapTx to be 6 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented. |
730 | return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 6 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + cachedInnerUsage; | |
bde5c8b0 | 731 | } |