Skip to content

Commit

Permalink
Replace CCoins-based CTxMemPool::pruneSpent with isSpent
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and xanimo committed Jul 9, 2024
1 parent 9f733ac commit 7483107
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 1 addition & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 2 additions & 9 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class CTxMemPool
void _clear(); //lock free
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
void queryHashes(std::vector<uint256>& vtxid);
void pruneSpent(const uint256& hash, CCoins &coins);
bool isSpent(const COutPoint& outpoint);
unsigned int GetTransactionsUpdated() const;
void AddTransactionsUpdated(unsigned int n);
/**
Expand Down

0 comments on commit 7483107

Please sign in to comment.