From 748310701247fb916305218aa049556676ccad62 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 25 Apr 2017 11:29:37 -0700 Subject: [PATCH] Replace CCoins-based CTxMemPool::pruneSpent with isSpent --- src/rest.cpp | 6 +----- src/rpc/blockchain.cpp | 3 +-- src/txmempool.cpp | 11 ++--------- src/txmempool.h | 2 +- 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/rest.cpp b/src/rest.cpp index 2f7138993ca..e8776485cc4 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -524,11 +524,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart) uint256 hash = vOutPoints[i].hash; bool hit = false; if (view.GetCoins(hash, coins)) { - if (fCheckMemPool) { - mempool.pruneSpent(hash, coins); - } - - if (coins.IsAvailable(vOutPoints[i].n)) { + if (coins.IsAvailable(vOutPoints[i].n) && !mempool.isSpent(vOutPoints[i])) { hit = true; // Safe to index into vout here because IsAvailable checked if it's off the end of the array, or if // n is valid but points to an already spent output (IsNull). diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 5b821f8c5a3..f4878cf9288 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1055,9 +1055,8 @@ UniValue gettxout(const JSONRPCRequest& request) if (fMempool) { LOCK(mempool.cs); CCoinsViewMemPool view(pcoinsTip, mempool); - if (!view.GetCoins(hash, coins)) + if (!view.GetCoins(hash, coins) || mempool.isSpent(COutPoint(hash, n))) // TODO: this should be done by the CCoinsViewMemPool return NullUniValue; - mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool } else { if (!pcoinsTip->GetCoins(hash, coins)) return NullUniValue; diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 49de7befa9e..b0bad00aa54 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -363,17 +363,10 @@ CTxMemPool::~CTxMemPool() delete minerPolicyEstimator; } -void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins) +bool CTxMemPool::isSpent(const COutPoint& outpoint) { LOCK(cs); - - auto it = mapNextTx.lower_bound(COutPoint(hashTx, 0)); - - // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx - while (it != mapNextTx.end() && it->first->hash == hashTx) { - coins.Spend(it->first->n); // and remove those outputs from coins - it++; - } + return mapNextTx.count(outpoint); } unsigned int CTxMemPool::GetTransactionsUpdated() const diff --git a/src/txmempool.h b/src/txmempool.h index 850d8382ee7..822e5619f52 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -557,7 +557,7 @@ class CTxMemPool void _clear(); //lock free bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb); void queryHashes(std::vector& vtxid); - void pruneSpent(const uint256& hash, CCoins &coins); + bool isSpent(const COutPoint& outpoint); unsigned int GetTransactionsUpdated() const; void AddTransactionsUpdated(unsigned int n); /**