From 77f438368e5748f881d1be6caa83c126de22d999 Mon Sep 17 00:00:00 2001 From: alex v Date: Tue, 6 Aug 2024 20:05:03 +0200 Subject: [PATCH] remove stake commitment when disconnecting block --- src/coins.cpp | 9 +++++++-- src/coins.h | 2 ++ src/primitives/transaction.h | 3 +-- src/validation.cpp | 3 +++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/coins.cpp b/src/coins.cpp index 3f1e8f993a9ee..a1cc093804f0e 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -143,6 +143,7 @@ bool CCoinsViewCache::SpendCoin(const COutPoint& outpoint, Coin* moveout) { CCoinsMap::iterator it = FetchCoin(outpoint); if (it == cacheCoins.end()) return false; + cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage(); TRACE5(utxocache, spent, outpoint.hash.data(), @@ -154,8 +155,7 @@ bool CCoinsViewCache::SpendCoin(const COutPoint& outpoint, Coin* moveout) *moveout = std::move(it->second.coin); } if (it->second.coin.out.IsStakedCommitment()) { - LogPrint(BCLog::POPS, "%s: Removing staked commitment %s at height %d\n", __func__, HexStr(it->second.coin.out.blsctData.rangeProof.Vs[0].GetVch()), (uint32_t)it->second.coin.nHeight); - cacheStakedCommitments.Remove(it->second.coin.out.blsctData.rangeProof.Vs[0]); + RemoveStakedCommitment(it->second.coin.out.blsctData.rangeProof.Vs[0]); } if (it->second.flags & CCoinsCacheEntry::FRESH) { cacheCoins.erase(it); @@ -166,6 +166,11 @@ bool CCoinsViewCache::SpendCoin(const COutPoint& outpoint, Coin* moveout) return true; } +void CCoinsViewCache::RemoveStakedCommitment(const MclG1Point& commitment) { + LogPrint(BCLog::POPS, "%s: Removing staked commitment %s\n", __func__, HexStr(commitment.GetVch())); + cacheStakedCommitments.Remove(commitment); +} + const Coin& CCoinsViewCache::AccessCoin(const COutPoint& outpoint) const { CCoinsMap::const_iterator it = FetchCoin(outpoint); diff --git a/src/coins.h b/src/coins.h index 7cbfcdedf2f2d..dfab113636e2f 100644 --- a/src/coins.h +++ b/src/coins.h @@ -275,6 +275,8 @@ class CCoinsViewCache : public CCoinsViewBacked throw std::logic_error("CCoinsViewCache cursor iteration not supported."); } + void RemoveStakedCommitment(const MclG1Point& commitment); + /** * Check if we have the given utxo already loaded in this cache. * The semantics are the same as HaveCoin(), but no calls to diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index 71077729bfac0..6ff2fdf7a0f88 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -307,8 +307,7 @@ class CTxOut return false; if (!tokenId.IsNull()) return false; - if (!(*(scriptPubKey.begin()) == OP_STAKED_COMMITMENT && *(scriptPubKey.begin() + 1) == OP_PUSHDATA2 && *(scriptPubKey.end() - 1) == OP_TRUE)) - return false; + if (!(*(scriptPubKey.begin()) == OP_STAKED_COMMITMENT && *(scriptPubKey.begin() + 1) == OP_PUSHDATA2 && *(scriptPubKey.end() - 1) == OP_TRUE)) return false; try { auto commitment = std::vector(scriptPubKey.begin() + 4, scriptPubKey.end()); diff --git a/src/validation.cpp b/src/validation.cpp index 9d46fac1bcbb0..c24a4b6fbf0bc 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2067,6 +2067,9 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn // Check that all outputs are available and match the outputs in the block itself // exactly. for (size_t o = 0; o < tx.vout.size(); o++) { + if (tx.vout[o].IsStakedCommitment()) { + view.RemoveStakedCommitment(tx.vout[o].blsctData.rangeProof.Vs[0]); + } if (!tx.vout[o].scriptPubKey.IsUnspendable()) { COutPoint out(hash, o); Coin coin;