Skip to content

Commit

Permalink
Delete old confirms to prevent ballooning memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Aug 2, 2022
1 parent 3313a76 commit a4d6272
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 18 additions & 13 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,7 @@ bool StopOrInterruptConnect(const CBlockIndex *pIndex, CValidationState& state)
* Validity checks that depend on the UTXO set are also done; ConnectBlock ()
* can fail if those validity checks fail (among other reasons). */
bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
CCoinsViewCache& view, CCustomCSView& mnview, const CChainParams& chainparams, std::vector<uint256> & rewardedAnchors, bool fJustCheck)
CCoinsViewCache& view, CCustomCSView& mnview, const CChainParams& chainparams, bool & rewardedAnchors, bool fJustCheck)
{
AssertLockHeld(cs_main);
assert(pindex);
Expand Down Expand Up @@ -2846,7 +2846,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
error("%s: %s", __func__, res.msg),
REJECT_INVALID, res.dbgMsg);
}
rewardedAnchors.push_back(*res.val);
rewardedAnchors = true;
if (!fJustCheck) {
LogPrint(BCLog::ANCHORING, "%s: connected finalization tx: %s block: %d\n", __func__, tx.GetHash().GetHex(), pindex->nHeight);
}
Expand All @@ -2860,7 +2860,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
error("%s: %s", __func__, res.msg),
REJECT_INVALID, res.dbgMsg);
}
rewardedAnchors.push_back(*res.val);
rewardedAnchors = true;
if (!fJustCheck) {
LogPrint(BCLog::ANCHORING, "%s: connected finalization tx: %s block: %d\n", __func__, tx.GetHash().GetHex(), pindex->nHeight);
}
Expand Down Expand Up @@ -5181,8 +5181,8 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * MILLI, nTimeReadFromDisk * MICRO);
{
CCoinsViewCache view(&CoinsTip());
CCustomCSView mnview(*pcustomcsview.get());
std::vector<uint256> rewardedAnchors;
CCustomCSView mnview(*pcustomcsview);
bool rewardedAnchors{};
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, mnview, chainparams, rewardedAnchors);
GetMainSignals().BlockChecked(blockConnecting, state);
if (!rv) {
Expand Down Expand Up @@ -5217,11 +5217,16 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
pvaultHistoryDB->Flush();
}

// anchor rewards re-voting etc...
if (!rewardedAnchors.empty()) {
// we do not clear ALL votes (even they are stale) for the case of rapid tip changing. At least, they'll be deleted after their rewards
for (auto const & btcTxHash : rewardedAnchors) {
panchorAwaitingConfirms->EraseAnchor(btcTxHash);
// Delete all other confirms from memory
if (rewardedAnchors) {
std::vector<uint256> oldConfirms;
panchorAwaitingConfirms->ForEachConfirm([&oldConfirms](const CAnchorConfirmMessage &confirm) {
oldConfirms.push_back(confirm.btcTxHash);
return true;
});

for (const auto &confirm: oldConfirms) {
panchorAwaitingConfirms->EraseAnchor(confirm);
}
}
}
Expand Down Expand Up @@ -6721,8 +6726,8 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
AssertLockHeld(cs_main);
assert(pindexPrev && pindexPrev == ::ChainActive().Tip());
CCoinsViewCache viewNew(&::ChainstateActive().CoinsTip());
std::vector<uint256> dummyRewardedAnchors;
CCustomCSView mnview(*pcustomcsview.get());
bool dummyRewardedAnchors{};
CCustomCSView mnview(*pcustomcsview);
uint256 block_hash(block.GetHash());
CBlockIndex indexDummy(block);
indexDummy.pprev = pindexPrev;
Expand Down Expand Up @@ -7199,7 +7204,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
CBlock block;
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
std::vector<uint256> dummyRewardedAnchors;
bool dummyRewardedAnchors{};
if (!::ChainstateActive().ConnectBlock(block, state, pindex, coins, mnview, chainparams, dummyRewardedAnchors))
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
if (ShutdownRequested()) return true;
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ class CChainState {
// Block (dis)connection on a given view:
DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CCustomCSView& cache, std::vector<CAnchorConfirmMessage> & disconnectedAnchorConfirms);
bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
CCoinsViewCache& view, CCustomCSView& cache, const CChainParams& chainparams, std::vector<uint256> & rewardedAnchors, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
CCoinsViewCache& view, CCustomCSView& cache, const CChainParams& chainparams, bool & rewardedAnchors, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

// Apply the effects of a block disconnection on the UTXO set.
bool DisconnectTip(CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);
Expand Down

0 comments on commit a4d6272

Please sign in to comment.