txdb: assert `CCoinsViewDB::GetCoin` only returns unspent coins
What changed, and why it matters
This change adds a safety check inside Bitcoin Core's database lookup for coins (unspent transaction outputs). It ensures that if the database somehow returns a spent coin, the program will stop immediately rather than continue with bad data. It is a defensive hardening patch, not a fix for a known active attack.
Treat as a low-risk hardening commit. No immediate incident response required. Reviewers may want to confirm that all callers handle std::nullopt correctly and that no legitimate path can trigger the assertion.
Security signals we found
Defensive assertion added to enforce an invariant
Prevents propagation of inconsistent UTXO state
Could crash node on database corruption or misuse rather than allow invalid coin usage
Evidence from the diff
The commit inserts an assertion in CCoinsViewDB::GetCoin() that the returned Coin is not marked spent. The chainstate UTXO database is supposed to delete spent entries, so this should never happen. The assertion turns a potential data-corruption or misuse scenario into an immediate crash, preventing a spent coin from propagating through the coin-view interface.
Changed components
src/txdb.cppCCoinsViewDB::GetCoinchainstate UTXO database layerInspect captured patch +4 / −1
diff --git a/src/txdb.cpp b/src/txdb.cpp
index 5d61388b..141c31c5 100644
--- a/src/txdb.cpp
+++ b/src/txdb.cpp
@@ -67,7 +67,10 @@ void CCoinsViewDB::ResizeCache(size_t new_cache_size)
std::optional<Coin> CCoinsViewDB::GetCoin(const COutPoint& outpoint) const
{
- if (Coin coin; m_db->Read(CoinEntry(&outpoint), coin)) return coin;
+ if (Coin coin; m_db->Read(CoinEntry(&outpoint), coin)) {
+ Assert(!coin.IsSpent()); // The UTXO database should never contain spent coins
+ return coin;
+ }
return std::nullopt;
}
Why this scored 31/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.