You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

txmempool.cpp 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "txmempool.h"
  6. #include "clientversion.h"
  7. #include "consensus/consensus.h"
  8. #include "consensus/validation.h"
  9. #include "main.h"
  10. #include "policy/fees.h"
  11. #include "streams.h"
  12. #include "util.h"
  13. #include "utilmoneystr.h"
  14. #include "version.h"
  15. using namespace std;
  16. CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
  17. int64_t _nTime, double _dPriority,
  18. unsigned int _nHeight, bool poolHasNoInputsOf):
  19. tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight),
  20. hadNoDependencies(poolHasNoInputsOf)
  21. {
  22. nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
  23. nModSize = tx.CalculateModifiedSize(nTxSize);
  24. nUsageSize = RecursiveDynamicUsage(tx);
  25. nCountWithDescendants = 1;
  26. nSizeWithDescendants = nTxSize;
  27. nFeesWithDescendants = nFee;
  28. }
  29. CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
  30. {
  31. *this = other;
  32. }
  33. double
  34. CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
  35. {
  36. CAmount nValueIn = tx.GetValueOut()+nFee;
  37. double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
  38. double dResult = dPriority + deltaPriority;
  39. return dResult;
  40. }
  41. // Update the given tx for any in-mempool descendants.
  42. // Assumes that setMemPoolChildren is correct for the given tx and all
  43. // descendants.
  44. bool CTxMemPool::UpdateForDescendants(txiter updateIt, int maxDescendantsToVisit, cacheMap &cachedDescendants, const std::set<uint256> &setExclude)
  45. {
  46. // Track the number of entries (outside setExclude) that we'd need to visit
  47. // (will bail out if it exceeds maxDescendantsToVisit)
  48. int nChildrenToVisit = 0;
  49. setEntries stageEntries, setAllDescendants;
  50. stageEntries = GetMemPoolChildren(updateIt);
  51. while (!stageEntries.empty()) {
  52. const txiter cit = *stageEntries.begin();
  53. if (cit->IsDirty()) {
  54. // Don't consider any more children if any descendant is dirty
  55. return false;
  56. }
  57. setAllDescendants.insert(cit);
  58. stageEntries.erase(cit);
  59. const setEntries &setChildren = GetMemPoolChildren(cit);
  60. BOOST_FOREACH(const txiter childEntry, setChildren) {
  61. cacheMap::iterator cacheIt = cachedDescendants.find(childEntry);
  62. if (cacheIt != cachedDescendants.end()) {
  63. // We've already calculated this one, just add the entries for this set
  64. // but don't traverse again.
  65. BOOST_FOREACH(const txiter cacheEntry, cacheIt->second) {
  66. // update visit count only for new child transactions
  67. // (outside of setExclude and stageEntries)
  68. if (setAllDescendants.insert(cacheEntry).second &&
  69. !setExclude.count(cacheEntry->GetTx().GetHash()) &&
  70. !stageEntries.count(cacheEntry)) {
  71. nChildrenToVisit++;
  72. }
  73. }
  74. } else if (!setAllDescendants.count(childEntry)) {
  75. // Schedule for later processing and update our visit count
  76. if (stageEntries.insert(childEntry).second && !setExclude.count(childEntry->GetTx().GetHash())) {
  77. nChildrenToVisit++;
  78. }
  79. }
  80. if (nChildrenToVisit > maxDescendantsToVisit) {
  81. return false;
  82. }
  83. }
  84. }
  85. // setAllDescendants now contains all in-mempool descendants of updateIt.
  86. // Update and add to cached descendant map
  87. int64_t modifySize = 0;
  88. CAmount modifyFee = 0;
  89. int64_t modifyCount = 0;
  90. BOOST_FOREACH(txiter cit, setAllDescendants) {
  91. if (!setExclude.count(cit->GetTx().GetHash())) {
  92. modifySize += cit->GetTxSize();
  93. modifyFee += cit->GetFee();
  94. modifyCount++;
  95. cachedDescendants[updateIt].insert(cit);
  96. }
  97. }
  98. mapTx.modify(updateIt, update_descendant_state(modifySize, modifyFee, modifyCount));
  99. return true;
  100. }
  101. // vHashesToUpdate is the set of transaction hashes from a disconnected block
  102. // which has been re-added to the mempool.
  103. // for each entry, look for descendants that are outside hashesToUpdate, and
  104. // add fee/size information for such descendants to the parent.
  105. void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate)
  106. {
  107. LOCK(cs);
  108. // For each entry in vHashesToUpdate, store the set of in-mempool, but not
  109. // in-vHashesToUpdate transactions, so that we don't have to recalculate
  110. // descendants when we come across a previously seen entry.
  111. cacheMap mapMemPoolDescendantsToUpdate;
  112. // Use a set for lookups into vHashesToUpdate (these entries are already
  113. // accounted for in the state of their ancestors)
  114. std::set<uint256> setAlreadyIncluded(vHashesToUpdate.begin(), vHashesToUpdate.end());
  115. // Iterate in reverse, so that whenever we are looking at at a transaction
  116. // we are sure that all in-mempool descendants have already been processed.
  117. // This maximizes the benefit of the descendant cache and guarantees that
  118. // setMemPoolChildren will be updated, an assumption made in
  119. // UpdateForDescendants.
  120. BOOST_REVERSE_FOREACH(const uint256 &hash, vHashesToUpdate) {
  121. // we cache the in-mempool children to avoid duplicate updates
  122. setEntries setChildren;
  123. // calculate children from mapNextTx
  124. txiter it = mapTx.find(hash);
  125. if (it == mapTx.end()) {
  126. continue;
  127. }
  128. std::map<COutPoint, CInPoint>::iterator iter = mapNextTx.lower_bound(COutPoint(hash, 0));
  129. // First calculate the children, and update setMemPoolChildren to
  130. // include them, and update their setMemPoolParents to include this tx.
  131. for (; iter != mapNextTx.end() && iter->first.hash == hash; ++iter) {
  132. const uint256 &childHash = iter->second.ptx->GetHash();
  133. txiter childIter = mapTx.find(childHash);
  134. assert(childIter != mapTx.end());
  135. // We can skip updating entries we've encountered before or that
  136. // are in the block (which are already accounted for).
  137. if (setChildren.insert(childIter).second && !setAlreadyIncluded.count(childHash)) {
  138. UpdateChild(it, childIter, true);
  139. UpdateParent(childIter, it, true);
  140. }
  141. }
  142. if (!UpdateForDescendants(it, 100, mapMemPoolDescendantsToUpdate, setAlreadyIncluded)) {
  143. // Mark as dirty if we can't do the calculation.
  144. mapTx.modify(it, set_dirty());
  145. }
  146. }
  147. }
  148. bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString)
  149. {
  150. setEntries parentHashes;
  151. const CTransaction &tx = entry.GetTx();
  152. // Get parents of this transaction that are in the mempool
  153. // Entry may or may not already be in the mempool, and GetMemPoolParents()
  154. // is only valid for entries in the mempool, so we iterate mapTx to find
  155. // parents.
  156. // TODO: optimize this so that we only check limits and walk
  157. // tx.vin when called on entries not already in the mempool.
  158. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  159. txiter piter = mapTx.find(tx.vin[i].prevout.hash);
  160. if (piter != mapTx.end()) {
  161. parentHashes.insert(piter);
  162. if (parentHashes.size() + 1 > limitAncestorCount) {
  163. errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
  164. return false;
  165. }
  166. }
  167. }
  168. size_t totalSizeWithAncestors = entry.GetTxSize();
  169. while (!parentHashes.empty()) {
  170. txiter stageit = *parentHashes.begin();
  171. setAncestors.insert(stageit);
  172. parentHashes.erase(stageit);
  173. totalSizeWithAncestors += stageit->GetTxSize();
  174. if (stageit->GetSizeWithDescendants() + entry.GetTxSize() > limitDescendantSize) {
  175. errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
  176. return false;
  177. } else if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) {
  178. errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
  179. return false;
  180. } else if (totalSizeWithAncestors > limitAncestorSize) {
  181. errString = strprintf("exceeds ancestor size limit [limit: %u]", limitAncestorSize);
  182. return false;
  183. }
  184. const setEntries & setMemPoolParents = GetMemPoolParents(stageit);
  185. BOOST_FOREACH(const txiter &phash, setMemPoolParents) {
  186. // If this is a new ancestor, add it.
  187. if (setAncestors.count(phash) == 0) {
  188. parentHashes.insert(phash);
  189. }
  190. if (parentHashes.size() + setAncestors.size() + 1 > limitAncestorCount) {
  191. errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
  192. return false;
  193. }
  194. }
  195. }
  196. return true;
  197. }
  198. void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
  199. {
  200. setEntries parentIters = GetMemPoolParents(it);
  201. // add or remove this tx as a child of each parent
  202. BOOST_FOREACH(txiter piter, parentIters) {
  203. UpdateChild(piter, it, add);
  204. }
  205. const int64_t updateCount = (add ? 1 : -1);
  206. const int64_t updateSize = updateCount * it->GetTxSize();
  207. const CAmount updateFee = updateCount * it->GetFee();
  208. BOOST_FOREACH(txiter ancestorIt, setAncestors) {
  209. mapTx.modify(ancestorIt, update_descendant_state(updateSize, updateFee, updateCount));
  210. }
  211. }
  212. void CTxMemPool::UpdateChildrenForRemoval(txiter it)
  213. {
  214. const setEntries &setMemPoolChildren = GetMemPoolChildren(it);
  215. BOOST_FOREACH(txiter updateIt, setMemPoolChildren) {
  216. UpdateParent(updateIt, it, false);
  217. }
  218. }
  219. void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove)
  220. {
  221. // For each entry, walk back all ancestors and decrement size associated with this
  222. // transaction
  223. const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
  224. BOOST_FOREACH(txiter removeIt, entriesToRemove) {
  225. setEntries setAncestors;
  226. const CTxMemPoolEntry &entry = *removeIt;
  227. std::string dummy;
  228. CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
  229. // Note that UpdateAncestorsOf severs the child links that point to
  230. // removeIt in the entries for the parents of removeIt. This is
  231. // fine since we don't need to use the mempool children of any entries
  232. // to walk back over our ancestors (but we do need the mempool
  233. // parents!)
  234. UpdateAncestorsOf(false, removeIt, setAncestors);
  235. }
  236. // After updating all the ancestor sizes, we can now sever the link between each
  237. // transaction being removed and any mempool children (ie, update setMemPoolParents
  238. // for each direct child of a transaction being removed).
  239. BOOST_FOREACH(txiter removeIt, entriesToRemove) {
  240. UpdateChildrenForRemoval(removeIt);
  241. }
  242. }
  243. void CTxMemPoolEntry::SetDirty()
  244. {
  245. nCountWithDescendants = 0;
  246. nSizeWithDescendants = nTxSize;
  247. nFeesWithDescendants = nFee;
  248. }
  249. void CTxMemPoolEntry::UpdateState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
  250. {
  251. if (!IsDirty()) {
  252. nSizeWithDescendants += modifySize;
  253. assert(int64_t(nSizeWithDescendants) > 0);
  254. nFeesWithDescendants += modifyFee;
  255. assert(nFeesWithDescendants >= 0);
  256. nCountWithDescendants += modifyCount;
  257. assert(int64_t(nCountWithDescendants) > 0);
  258. }
  259. }
  260. CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
  261. nTransactionsUpdated(0)
  262. {
  263. // Sanity checks off by default for performance, because otherwise
  264. // accepting transactions becomes O(N^2) where N is the number
  265. // of transactions in the pool
  266. fSanityCheck = false;
  267. minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
  268. }
  269. CTxMemPool::~CTxMemPool()
  270. {
  271. delete minerPolicyEstimator;
  272. }
  273. void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
  274. {
  275. LOCK(cs);
  276. std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
  277. // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
  278. while (it != mapNextTx.end() && it->first.hash == hashTx) {
  279. coins.Spend(it->first.n); // and remove those outputs from coins
  280. it++;
  281. }
  282. }
  283. unsigned int CTxMemPool::GetTransactionsUpdated() const
  284. {
  285. LOCK(cs);
  286. return nTransactionsUpdated;
  287. }
  288. void CTxMemPool::AddTransactionsUpdated(unsigned int n)
  289. {
  290. LOCK(cs);
  291. nTransactionsUpdated += n;
  292. }
  293. bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool fCurrentEstimate)
  294. {
  295. // Add to memory pool without checking anything.
  296. // Used by main.cpp AcceptToMemoryPool(), which DOES do
  297. // all the appropriate checks.
  298. LOCK(cs);
  299. indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
  300. mapLinks.insert(make_pair(newit, TxLinks()));
  301. // Update cachedInnerUsage to include contained transaction's usage.
  302. // (When we update the entry for in-mempool parents, memory usage will be
  303. // further updated.)
  304. cachedInnerUsage += entry.DynamicMemoryUsage();
  305. const CTransaction& tx = newit->GetTx();
  306. std::set<uint256> setParentTransactions;
  307. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  308. mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
  309. setParentTransactions.insert(tx.vin[i].prevout.hash);
  310. }
  311. // Don't bother worrying about child transactions of this one.
  312. // Normal case of a new transaction arriving is that there can't be any
  313. // children, because such children would be orphans.
  314. // An exception to that is if a transaction enters that used to be in a block.
  315. // In that case, our disconnect block logic will call UpdateTransactionsFromBlock
  316. // to clean up the mess we're leaving here.
  317. // Update ancestors with information about this tx
  318. BOOST_FOREACH (const uint256 &phash, setParentTransactions) {
  319. txiter pit = mapTx.find(phash);
  320. if (pit != mapTx.end()) {
  321. UpdateParent(newit, pit, true);
  322. }
  323. }
  324. UpdateAncestorsOf(true, newit, setAncestors);
  325. nTransactionsUpdated++;
  326. totalTxSize += entry.GetTxSize();
  327. minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
  328. return true;
  329. }
  330. void CTxMemPool::removeUnchecked(txiter it)
  331. {
  332. const uint256 hash = it->GetTx().GetHash();
  333. BOOST_FOREACH(const CTxIn& txin, it->GetTx().vin)
  334. mapNextTx.erase(txin.prevout);
  335. totalTxSize -= it->GetTxSize();
  336. cachedInnerUsage -= it->DynamicMemoryUsage();
  337. cachedInnerUsage -= memusage::DynamicUsage(mapLinks[it].parents) + memusage::DynamicUsage(mapLinks[it].children);
  338. mapLinks.erase(it);
  339. mapTx.erase(it);
  340. nTransactionsUpdated++;
  341. minerPolicyEstimator->removeTx(hash);
  342. }
  343. // Calculates descendants of entry that are not already in setDescendants, and adds to
  344. // setDescendants. Assumes entryit is already a tx in the mempool and setMemPoolChildren
  345. // is correct for tx and all descendants.
  346. // Also assumes that if an entry is in setDescendants already, then all
  347. // in-mempool descendants of it are already in setDescendants as well, so that we
  348. // can save time by not iterating over those entries.
  349. void CTxMemPool::CalculateDescendants(txiter entryit, setEntries &setDescendants)
  350. {
  351. setEntries stage;
  352. if (setDescendants.count(entryit) == 0) {
  353. stage.insert(entryit);
  354. }
  355. // Traverse down the children of entry, only adding children that are not
  356. // accounted for in setDescendants already (because those children have either
  357. // already been walked, or will be walked in this iteration).
  358. while (!stage.empty()) {
  359. txiter it = *stage.begin();
  360. setDescendants.insert(it);
  361. stage.erase(it);
  362. const setEntries &setChildren = GetMemPoolChildren(it);
  363. BOOST_FOREACH(const txiter &childiter, setChildren) {
  364. if (!setDescendants.count(childiter)) {
  365. stage.insert(childiter);
  366. }
  367. }
  368. }
  369. }
  370. void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
  371. {
  372. // Remove transaction from memory pool
  373. {
  374. LOCK(cs);
  375. setEntries txToRemove;
  376. txiter origit = mapTx.find(origTx.GetHash());
  377. if (origit != mapTx.end()) {
  378. txToRemove.insert(origit);
  379. } else if (fRecursive) {
  380. // If recursively removing but origTx isn't in the mempool
  381. // be sure to remove any children that are in the pool. This can
  382. // happen during chain re-orgs if origTx isn't re-accepted into
  383. // the mempool for any reason.
  384. for (unsigned int i = 0; i < origTx.vout.size(); i++) {
  385. std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
  386. if (it == mapNextTx.end())
  387. continue;
  388. txiter nextit = mapTx.find(it->second.ptx->GetHash());
  389. assert(nextit != mapTx.end());
  390. txToRemove.insert(nextit);
  391. }
  392. }
  393. setEntries setAllRemoves;
  394. if (fRecursive) {
  395. BOOST_FOREACH(txiter it, txToRemove) {
  396. CalculateDescendants(it, setAllRemoves);
  397. }
  398. } else {
  399. setAllRemoves.swap(txToRemove);
  400. }
  401. BOOST_FOREACH(txiter it, setAllRemoves) {
  402. removed.push_back(it->GetTx());
  403. }
  404. RemoveStaged(setAllRemoves);
  405. }
  406. }
  407. void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
  408. {
  409. // Remove transactions spending a coinbase which are now immature
  410. LOCK(cs);
  411. list<CTransaction> transactionsToRemove;
  412. for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
  413. const CTransaction& tx = it->GetTx();
  414. BOOST_FOREACH(const CTxIn& txin, tx.vin) {
  415. indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
  416. if (it2 != mapTx.end())
  417. continue;
  418. const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
  419. if (fSanityCheck) assert(coins);
  420. if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
  421. transactionsToRemove.push_back(tx);
  422. break;
  423. }
  424. }
  425. }
  426. BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
  427. list<CTransaction> removed;
  428. remove(tx, removed, true);
  429. }
  430. }
  431. void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
  432. {
  433. // Remove transactions which depend on inputs of tx, recursively
  434. list<CTransaction> result;
  435. LOCK(cs);
  436. BOOST_FOREACH(const CTxIn &txin, tx.vin) {
  437. std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
  438. if (it != mapNextTx.end()) {
  439. const CTransaction &txConflict = *it->second.ptx;
  440. if (txConflict != tx)
  441. {
  442. remove(txConflict, removed, true);
  443. }
  444. }
  445. }
  446. }
  447. /**
  448. * Called when a block is connected. Removes from mempool and updates the miner fee estimator.
  449. */
  450. void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
  451. std::list<CTransaction>& conflicts, bool fCurrentEstimate)
  452. {
  453. LOCK(cs);
  454. std::vector<CTxMemPoolEntry> entries;
  455. BOOST_FOREACH(const CTransaction& tx, vtx)
  456. {
  457. uint256 hash = tx.GetHash();
  458. indexed_transaction_set::iterator i = mapTx.find(hash);
  459. if (i != mapTx.end())
  460. entries.push_back(*i);
  461. }
  462. BOOST_FOREACH(const CTransaction& tx, vtx)
  463. {
  464. std::list<CTransaction> dummy;
  465. remove(tx, dummy, false);
  466. removeConflicts(tx, conflicts);
  467. ClearPrioritisation(tx.GetHash());
  468. }
  469. // After the txs in the new block have been removed from the mempool, update policy estimates
  470. minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
  471. }
  472. void CTxMemPool::clear()
  473. {
  474. LOCK(cs);
  475. mapLinks.clear();
  476. mapTx.clear();
  477. mapNextTx.clear();
  478. totalTxSize = 0;
  479. cachedInnerUsage = 0;
  480. ++nTransactionsUpdated;
  481. }
  482. void CTxMemPool::check(const CCoinsViewCache *pcoins) const
  483. {
  484. if (!fSanityCheck)
  485. return;
  486. LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
  487. uint64_t checkTotal = 0;
  488. uint64_t innerUsage = 0;
  489. CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
  490. LOCK(cs);
  491. list<const CTxMemPoolEntry*> waitingOnDependants;
  492. for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
  493. unsigned int i = 0;
  494. checkTotal += it->GetTxSize();
  495. innerUsage += it->DynamicMemoryUsage();
  496. const CTransaction& tx = it->GetTx();
  497. txlinksMap::const_iterator linksiter = mapLinks.find(it);
  498. assert(linksiter != mapLinks.end());
  499. const TxLinks &links = linksiter->second;
  500. innerUsage += memusage::DynamicUsage(links.parents) + memusage::DynamicUsage(links.children);
  501. bool fDependsWait = false;
  502. setEntries setParentCheck;
  503. BOOST_FOREACH(const CTxIn &txin, tx.vin) {
  504. // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
  505. indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
  506. if (it2 != mapTx.end()) {
  507. const CTransaction& tx2 = it2->GetTx();
  508. assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
  509. fDependsWait = true;
  510. setParentCheck.insert(it2);
  511. } else {
  512. const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
  513. assert(coins && coins->IsAvailable(txin.prevout.n));
  514. }
  515. // Check whether its inputs are marked in mapNextTx.
  516. std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
  517. assert(it3 != mapNextTx.end());
  518. assert(it3->second.ptx == &tx);
  519. assert(it3->second.n == i);
  520. i++;
  521. }
  522. assert(setParentCheck == GetMemPoolParents(it));
  523. // Check children against mapNextTx
  524. CTxMemPool::setEntries setChildrenCheck;
  525. std::map<COutPoint, CInPoint>::const_iterator iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0));
  526. int64_t childSizes = 0;
  527. CAmount childFees = 0;
  528. for (; iter != mapNextTx.end() && iter->first.hash == it->GetTx().GetHash(); ++iter) {
  529. txiter childit = mapTx.find(iter->second.ptx->GetHash());
  530. assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions
  531. if (setChildrenCheck.insert(childit).second) {
  532. childSizes += childit->GetTxSize();
  533. childFees += childit->GetFee();
  534. }
  535. }
  536. assert(setChildrenCheck == GetMemPoolChildren(it));
  537. // Also check to make sure size/fees is greater than sum with immediate children.
  538. // just a sanity check, not definitive that this calc is correct...
  539. // also check that the size is less than the size of the entire mempool.
  540. if (!it->IsDirty()) {
  541. assert(it->GetSizeWithDescendants() >= childSizes + it->GetTxSize());
  542. assert(it->GetFeesWithDescendants() >= childFees + it->GetFee());
  543. } else {
  544. assert(it->GetSizeWithDescendants() == it->GetTxSize());
  545. assert(it->GetFeesWithDescendants() == it->GetFee());
  546. }
  547. assert(it->GetFeesWithDescendants() >= 0);
  548. if (fDependsWait)
  549. waitingOnDependants.push_back(&(*it));
  550. else {
  551. CValidationState state;
  552. assert(CheckInputs(tx, state, mempoolDuplicate, false, 0, false, NULL));
  553. UpdateCoins(tx, state, mempoolDuplicate, 1000000);
  554. }
  555. }
  556. unsigned int stepsSinceLastRemove = 0;
  557. while (!waitingOnDependants.empty()) {
  558. const CTxMemPoolEntry* entry = waitingOnDependants.front();
  559. waitingOnDependants.pop_front();
  560. CValidationState state;
  561. if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
  562. waitingOnDependants.push_back(entry);
  563. stepsSinceLastRemove++;
  564. assert(stepsSinceLastRemove < waitingOnDependants.size());
  565. } else {
  566. assert(CheckInputs(entry->GetTx(), state, mempoolDuplicate, false, 0, false, NULL));
  567. UpdateCoins(entry->GetTx(), state, mempoolDuplicate, 1000000);
  568. stepsSinceLastRemove = 0;
  569. }
  570. }
  571. for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
  572. uint256 hash = it->second.ptx->GetHash();
  573. indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
  574. const CTransaction& tx = it2->GetTx();
  575. assert(it2 != mapTx.end());
  576. assert(&tx == it->second.ptx);
  577. assert(tx.vin.size() > it->second.n);
  578. assert(it->first == it->second.ptx->vin[it->second.n].prevout);
  579. }
  580. assert(totalTxSize == checkTotal);
  581. assert(innerUsage == cachedInnerUsage);
  582. }
  583. void CTxMemPool::queryHashes(vector<uint256>& vtxid)
  584. {
  585. vtxid.clear();
  586. LOCK(cs);
  587. vtxid.reserve(mapTx.size());
  588. for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
  589. vtxid.push_back(mi->GetTx().GetHash());
  590. }
  591. bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
  592. {
  593. LOCK(cs);
  594. indexed_transaction_set::const_iterator i = mapTx.find(hash);
  595. if (i == mapTx.end()) return false;
  596. result = i->GetTx();
  597. return true;
  598. }
  599. CFeeRate CTxMemPool::estimateFee(int nBlocks) const
  600. {
  601. LOCK(cs);
  602. return minerPolicyEstimator->estimateFee(nBlocks);
  603. }
  604. double CTxMemPool::estimatePriority(int nBlocks) const
  605. {
  606. LOCK(cs);
  607. return minerPolicyEstimator->estimatePriority(nBlocks);
  608. }
  609. bool
  610. CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
  611. {
  612. try {
  613. LOCK(cs);
  614. fileout << 109900; // version required to read: 0.10.99 or later
  615. fileout << CLIENT_VERSION; // version that wrote the file
  616. minerPolicyEstimator->Write(fileout);
  617. }
  618. catch (const std::exception&) {
  619. LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
  620. return false;
  621. }
  622. return true;
  623. }
  624. bool
  625. CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
  626. {
  627. try {
  628. int nVersionRequired, nVersionThatWrote;
  629. filein >> nVersionRequired >> nVersionThatWrote;
  630. if (nVersionRequired > CLIENT_VERSION)
  631. return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
  632. LOCK(cs);
  633. minerPolicyEstimator->Read(filein);
  634. }
  635. catch (const std::exception&) {
  636. LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
  637. return false;
  638. }
  639. return true;
  640. }
  641. void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
  642. {
  643. {
  644. LOCK(cs);
  645. std::pair<double, CAmount> &deltas = mapDeltas[hash];
  646. deltas.first += dPriorityDelta;
  647. deltas.second += nFeeDelta;
  648. }
  649. LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
  650. }
  651. void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
  652. {
  653. LOCK(cs);
  654. std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
  655. if (pos == mapDeltas.end())
  656. return;
  657. const std::pair<double, CAmount> &deltas = pos->second;
  658. dPriorityDelta += deltas.first;
  659. nFeeDelta += deltas.second;
  660. }
  661. void CTxMemPool::ClearPrioritisation(const uint256 hash)
  662. {
  663. LOCK(cs);
  664. mapDeltas.erase(hash);
  665. }
  666. bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
  667. {
  668. for (unsigned int i = 0; i < tx.vin.size(); i++)
  669. if (exists(tx.vin[i].prevout.hash))
  670. return false;
  671. return true;
  672. }
  673. CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
  674. bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
  675. // If an entry in the mempool exists, always return that one, as it's guaranteed to never
  676. // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
  677. // transactions. First checking the underlying cache risks returning a pruned entry instead.
  678. CTransaction tx;
  679. if (mempool.lookup(txid, tx)) {
  680. coins = CCoins(tx, MEMPOOL_HEIGHT);
  681. return true;
  682. }
  683. return (base->GetCoins(txid, coins) && !coins.IsPruned());
  684. }
  685. bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
  686. return mempool.exists(txid) || base->HaveCoins(txid);
  687. }
  688. size_t CTxMemPool::DynamicMemoryUsage() const {
  689. LOCK(cs);
  690. // Estimate the overhead of mapTx to be 9 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
  691. return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 9 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + cachedInnerUsage;
  692. }
  693. void CTxMemPool::RemoveStaged(setEntries &stage) {
  694. AssertLockHeld(cs);
  695. UpdateForRemoveFromMempool(stage);
  696. BOOST_FOREACH(const txiter& it, stage) {
  697. removeUnchecked(it);
  698. }
  699. }
  700. bool CTxMemPool::addUnchecked(const uint256&hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
  701. {
  702. LOCK(cs);
  703. setEntries setAncestors;
  704. uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
  705. std::string dummy;
  706. CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
  707. return addUnchecked(hash, entry, setAncestors, fCurrentEstimate);
  708. }
  709. void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add)
  710. {
  711. setEntries s;
  712. if (add && mapLinks[entry].children.insert(child).second) {
  713. cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
  714. } else if (!add && mapLinks[entry].children.erase(child)) {
  715. cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
  716. }
  717. }
  718. void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add)
  719. {
  720. setEntries s;
  721. if (add && mapLinks[entry].parents.insert(parent).second) {
  722. cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
  723. } else if (!add && mapLinks[entry].parents.erase(parent)) {
  724. cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
  725. }
  726. }
  727. const CTxMemPool::setEntries & CTxMemPool::GetMemPoolParents(txiter entry) const
  728. {
  729. assert (entry != mapTx.end());
  730. txlinksMap::const_iterator it = mapLinks.find(entry);
  731. assert(it != mapLinks.end());
  732. return it->second.parents;
  733. }
  734. const CTxMemPool::setEntries & CTxMemPool::GetMemPoolChildren(txiter entry) const
  735. {
  736. assert (entry != mapTx.end());
  737. txlinksMap::const_iterator it = mapLinks.find(entry);
  738. assert(it != mapLinks.end());
  739. return it->second.children;
  740. }