aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/coins.cpp5
-rw-r--r--src/coins.h11
-rw-r--r--src/consensus/tx_verify.cpp8
-rw-r--r--src/txmempool.cpp4
-rw-r--r--src/validation.cpp7
5 files changed, 22 insertions, 13 deletions
diff --git a/src/coins.cpp b/src/coins.cpp
index b45fc76338..2dceaf09b6 100644
--- a/src/coins.cpp
+++ b/src/coins.cpp
@@ -91,9 +91,9 @@ void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight) {
}
}
-void CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
+bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
CCoinsMap::iterator it = FetchCoin(outpoint);
- if (it == cacheCoins.end()) return;
+ if (it == cacheCoins.end()) return false;
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
if (moveout) {
*moveout = std::move(it->second.coin);
@@ -104,6 +104,7 @@ void CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
it->second.flags |= CCoinsCacheEntry::DIRTY;
it->second.coin.Clear();
}
+ return true;
}
static const Coin coinEmpty;
diff --git a/src/coins.h b/src/coins.h
index dc3210b8ac..4774c9f6a6 100644
--- a/src/coins.h
+++ b/src/coins.h
@@ -224,8 +224,13 @@ public:
/**
* Return a reference to Coin in the cache, or a pruned one if not found. This is
- * more efficient than GetCoin. Modifications to other cache entries are
- * allowed while accessing the returned pointer.
+ * more efficient than GetCoin.
+ *
+ * Generally, do not hold the reference returned for more than a short scope.
+ * While the current implementation allows for modifications to the contents
+ * of the cache while holding the reference, this behavior should not be relied
+ * on! To be safe, best to not hold the returned reference through any other
+ * calls to this cache.
*/
const Coin& AccessCoin(const COutPoint &output) const;
@@ -240,7 +245,7 @@ public:
* If no unspent output exists for the passed outpoint, this call
* has no effect.
*/
- void SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
+ bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
/**
* Push the modifications applied to this cache to its base.
diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp
index bf68f8754b..0671cbc132 100644
--- a/src/consensus/tx_verify.cpp
+++ b/src/consensus/tx_verify.cpp
@@ -126,7 +126,9 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& in
unsigned int nSigOps = 0;
for (unsigned int i = 0; i < tx.vin.size(); i++)
{
- const CTxOut &prevout = inputs.AccessCoin(tx.vin[i].prevout).out;
+ const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
+ assert(!coin.IsSpent());
+ const CTxOut &prevout = coin.out;
if (prevout.scriptPubKey.IsPayToScriptHash())
nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
}
@@ -146,7 +148,9 @@ int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& i
for (unsigned int i = 0; i < tx.vin.size(); i++)
{
- const CTxOut &prevout = inputs.AccessCoin(tx.vin[i].prevout).out;
+ const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
+ assert(!coin.IsSpent());
+ const CTxOut &prevout = coin.out;
nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, &tx.vin[i].scriptWitness, flags);
}
return nSigOps;
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
index afafc695f4..304300239c 100644
--- a/src/txmempool.cpp
+++ b/src/txmempool.cpp
@@ -1050,9 +1050,7 @@ void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpends
for (const CTransaction& tx : txn) {
for (const CTxIn& txin : tx.vin) {
if (exists(txin.prevout.hash)) continue;
- if (!mapNextTx.count(txin.prevout)) {
- pvNoSpendsRemaining->push_back(txin.prevout);
- }
+ pvNoSpendsRemaining->push_back(txin.prevout);
}
}
}
diff --git a/src/validation.cpp b/src/validation.cpp
index 4df5234df4..cba3b9e4ad 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -1125,7 +1125,8 @@ void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txund
txundo.vprevout.reserve(tx.vin.size());
for (const CTxIn &txin : tx.vin) {
txundo.vprevout.emplace_back();
- inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
+ bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
+ assert(is_spent);
}
}
// add outputs
@@ -1370,8 +1371,8 @@ static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex*
if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
COutPoint out(hash, o);
Coin coin;
- view.SpendCoin(out, &coin);
- if (tx.vout[o] != coin.out) {
+ bool is_spent = view.SpendCoin(out, &coin);
+ if (!is_spent || tx.vout[o] != coin.out) {
fClean = false; // transaction output mismatch
}
}