Skip to content

Commit

Permalink
Prune undo data prior last checkpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Apr 14, 2021
1 parent 1fcfa10 commit 86b05b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/masternodes/undos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
/// @attention make sure that it does not overlap with those in masternodes.cpp/tokens.cpp/undos.cpp/accounts.cpp !!!
const unsigned char CUndosView::ByUndoKey::prefix = 'u';

void CUndosView::ForEachUndo(std::function<bool(UndoKey, CLazySerialize<CUndo>)> callback, UndoKey const & start)
void CUndosView::ForEachUndo(std::function<bool(UndoKey const &, CLazySerialize<CUndo>)> callback, UndoKey const & start)
{
ForEach<ByUndoKey, UndoKey, CUndo>(callback, start);
}

Res CUndosView::SetUndo(UndoKey key, CUndo const & undo)
Res CUndosView::SetUndo(UndoKey const & key, CUndo const & undo)
{
WriteBy<ByUndoKey>(key, undo);
return Res::Ok();
}

Res CUndosView::DelUndo(UndoKey key)
Res CUndosView::DelUndo(UndoKey const & key)
{
EraseBy<ByUndoKey>(key);
return Res::Ok();
}

boost::optional<CUndo> CUndosView::GetUndo(UndoKey key) const
boost::optional<CUndo> CUndosView::GetUndo(UndoKey const & key) const
{
CUndo val;
bool ok = ReadBy<ByUndoKey>(key, val);
Expand Down
8 changes: 4 additions & 4 deletions src/masternodes/undos.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

class CUndosView : public virtual CStorageView {
public:
void ForEachUndo(std::function<bool(UndoKey, CLazySerialize<CUndo>)> callback, UndoKey const & start = {});
void ForEachUndo(std::function<bool(UndoKey const &, CLazySerialize<CUndo>)> callback, UndoKey const & start = {});

boost::optional<CUndo> GetUndo(UndoKey key) const;
Res SetUndo(UndoKey key, CUndo const & undo);
Res DelUndo(UndoKey key);
boost::optional<CUndo> GetUndo(UndoKey const & key) const;
Res SetUndo(UndoKey const & key, CUndo const & undo);
Res DelUndo(UndoKey const & key);

// tags
struct ByUndoKey { static const unsigned char prefix; };
Expand Down
18 changes: 17 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,22 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
}
mnview.SetLastHeight(pindex->nHeight);

if (fCheckpointsEnabled) {
auto &checkpoints = chainparams.Checkpoints().mapCheckpoints;
auto it = checkpoints.lower_bound(pindex->nHeight);
if (it != checkpoints.begin()) {
--it;
CCustomCSView pruned(mnview);
mnview.ForEachUndo([&](UndoKey const & key, CLazySerialize<CUndo>) {
if (key.height >= it->first) { // don't erase checkpoint height
return false;
}
return pruned.DelUndo(key).ok;
});
pruned.Flush();
}
}

int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);

Expand Down Expand Up @@ -3007,7 +3023,7 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
InvalidChainFound(vpindexToConnect.front());
}
state = CValidationState();
if (pindexConnect == pindexMostWork) {
if (fCheckpointsEnabled && pindexConnect == pindexMostWork) {
// NOTE: Invalidate blocks back to last checkpoint
auto &checkpoints = chainparams.Checkpoints().mapCheckpoints;
auto it = checkpoints.lower_bound(pindexConnect->nHeight);
Expand Down

0 comments on commit 86b05b7

Please sign in to comment.