Skip to content

Commit 3533fb4

Browse files
committed
Return a bool in SpendCoin to restore pre-per-utxo assert semantics
Since its free to do so, assert that Spends succeeded when we expect them to.
1 parent ec1271f commit 3533fb4

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/coins.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight) {
9191
}
9292
}
9393

94-
void CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
94+
bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
9595
CCoinsMap::iterator it = FetchCoin(outpoint);
96-
if (it == cacheCoins.end()) return;
96+
if (it == cacheCoins.end()) return false;
9797
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
9898
if (moveout) {
9999
*moveout = std::move(it->second.coin);
@@ -104,6 +104,7 @@ void CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
104104
it->second.flags |= CCoinsCacheEntry::DIRTY;
105105
it->second.coin.Clear();
106106
}
107+
return true;
107108
}
108109

109110
static const Coin coinEmpty;

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class CCoinsViewCache : public CCoinsViewBacked
238238
* If no unspent output exists for the passed outpoint, this call
239239
* has no effect.
240240
*/
241-
void SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
241+
bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
242242

243243
/**
244244
* Push the modifications applied to this cache to its base.

src/validation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,8 @@ void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txund
11181118
txundo.vprevout.reserve(tx.vin.size());
11191119
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
11201120
txundo.vprevout.emplace_back();
1121-
inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
1121+
bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
1122+
assert(is_spent);
11221123
}
11231124
}
11241125
// add outputs
@@ -1358,8 +1359,8 @@ static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex*
13581359
if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
13591360
COutPoint out(hash, o);
13601361
Coin coin;
1361-
view.SpendCoin(out, &coin);
1362-
if (tx.vout[o] != coin.out) {
1362+
bool is_spent = view.SpendCoin(out, &coin);
1363+
if (!is_spent || tx.vout[o] != coin.out) {
13631364
fClean = false; // transaction output mismatch
13641365
}
13651366
}

0 commit comments

Comments
 (0)