diff options
author | James O'Beirne <james.obeirne@gmail.com> | 2019-01-25 16:33:21 -0500 |
---|---|---|
committer | James O'Beirne <james.obeirne@gmail.com> | 2019-04-26 10:01:45 -0400 |
commit | 5d262052728acdaa2d108a35ba9921a23b3d761a (patch) | |
tree | beb10f26d1a49e9b9a2b79908a008d5e215dd48e | |
parent | ab46fe6ec1b37e88c5a06ee7a06ab31cbd18304f (diff) | |
download | bitcoin-5d262052728acdaa2d108a35ba9921a23b3d761a.tar.xz |
doc: explain AcceptToMemoryPoolWorker's coins_to_uncache
-rw-r--r-- | src/coins.h | 4 | ||||
-rw-r--r-- | src/validation.cpp | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/coins.h b/src/coins.h index 94493453f0..975fbb1945 100644 --- a/src/coins.h +++ b/src/coins.h @@ -294,6 +294,10 @@ public: bool HaveInputs(const CTransaction& tx) const; private: + /** + * @note this is marked const, but may actually append to `cacheCoins`, increasing + * memory usage. + */ CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const; }; diff --git a/src/validation.cpp b/src/validation.cpp index 6a26bf9baa..aec878891d 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -565,6 +565,13 @@ static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, CValidationSt return CheckInputs(tx, state, view, true, flags, cacheSigStore, true, txdata); } +/** + * @param[out] coins_to_uncache Return any outpoints which were not previously present in the + * coins cache, but were added as a result of validating the tx + * for mempool acceptance. This allows the caller to optionally + * remove the cache additions if the associated transaction ends + * up being rejected by the mempool. + */ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx, bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced, bool bypass_limits, const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache, bool test_accept) EXCLUSIVE_LOCKS_REQUIRED(cs_main) @@ -660,6 +667,10 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool if (!pcoinsTip->HaveCoinInCache(txin.prevout)) { coins_to_uncache.push_back(txin.prevout); } + + // Note: this call may add txin.prevout to the coins cache + // (pcoinsTip.cacheCoins) by way of FetchCoin(). It should be removed + // later (via coins_to_uncache) if this tx turns out to be invalid. if (!view.HaveCoin(txin.prevout)) { // Are inputs missing because we already have the tx? for (size_t out = 0; out < tx.vout.size(); out++) { @@ -981,6 +992,11 @@ static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPo std::vector<COutPoint> coins_to_uncache; bool res = AcceptToMemoryPoolWorker(chainparams, pool, state, tx, pfMissingInputs, nAcceptTime, plTxnReplaced, bypass_limits, nAbsurdFee, coins_to_uncache, test_accept); if (!res) { + // Remove coins that were not present in the coins cache before calling ATMPW; + // this is to prevent memory DoS in case we receive a large number of + // invalid transactions that attempt to overrun the in-memory coins cache + // (`CCoinsViewCache::cacheCoins`). + for (const COutPoint& hashTx : coins_to_uncache) pcoinsTip->Uncache(hashTx); } |