Skip to content

Commit

Permalink
remove stake commitment when disconnecting block
Browse files Browse the repository at this point in the history
  • Loading branch information
alex v committed Aug 6, 2024
1 parent 01b4f32 commit 77f4383
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>(scriptPubKey.begin() + 4, scriptPubKey.end());

Expand Down
3 changes: 3 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 77f4383

Please sign in to comment.