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