Skip to content

Commit

Permalink
Update snapshot based on height
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Nov 23, 2023
1 parent 53d44ec commit adc22c6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
16 changes: 7 additions & 9 deletions src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <functional>
#include <map>
#include <memusage.h>
#include <rpc/resultcache.h>

#include <optional>

Expand Down Expand Up @@ -370,25 +371,22 @@ class CFlushableStorageKV : public CStorageKV {
std::pair<MapKV, std::unique_ptr<CStorageLevelDB>> GetSnapshotPair() {
const auto storageLevelDB = dynamic_cast<CStorageLevelDB*>(&db);
assert(storageLevelDB);
if (blockTipChanged.load()) {
const auto currentHeight = GetLastValidatedHeight();
if (currentHeight != snapshotHeight) {
// Lock cs_main when updating from tipSnapshot to avoid
// race with Dis/ConnectTip.
LOCK(cs_main);
// Double check bool for safety now we are under lock.
if (blockTipChanged.load()) {
// Double check for safety now we are under lock.
if (currentHeight != snapshotHeight) {
tipSnapshot = storageLevelDB->GetStorageSnapshot();
changedCopy = changed;
blockTipChanged.store(false);
snapshotHeight = currentHeight;
}
}

return {changedCopy, std::make_unique<CStorageLevelDB>(storageLevelDB->GetDB(), tipSnapshot)};
}

void BlockTipChanged() {
blockTipChanged.store(true);
}

private:
std::unique_ptr<CStorageLevelDB> snapshotDB;
CStorageKV& db;
Expand All @@ -402,7 +400,7 @@ class CFlushableStorageKV : public CStorageKV {
MapKV changedCopy;

// Used to determine whether the block tip has changed.
std::atomic<bool> blockTipChanged{true};
int snapshotHeight{std::numeric_limits<int>::max()};
};

template<typename T>
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/resultcache.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <rpc/resultcache.h>
#include <rpc/util.h>
#include <logging.h>
#include <validation.h>

void RPCResultCache::Init(RPCCacheMode mode) {
std::unique_lock l{aMutex};
Expand Down
3 changes: 2 additions & 1 deletion src/rpc/resultcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include <optional>
#include <memory>
#include <univalue.h>
#include <dfi/mn_rpc.h>
#include <dfi/balances.h>
#include <rpc/request.h>
#include <uint256.h>

struct CGetBurnInfoResult {
CAmount burntDFI{};
Expand Down
6 changes: 2 additions & 4 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,6 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock &block,
auto prevHeight = pindex->pprev->nHeight;

mnview.SetLastHeight(prevHeight);
SetLastValidatedHeight(prevHeight);

return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
}
Expand Down Expand Up @@ -3466,7 +3465,6 @@ bool CChainState::ConnectBlock(const CBlock &block,
}
}
mnview.SetLastHeight(pindex->nHeight);
SetLastValidatedHeight(pindex->nHeight);

auto &checkpoints = chainparams.Checkpoints().mapCheckpoints;
auto it = checkpoints.lower_bound(pindex->nHeight);
Expand Down Expand Up @@ -3818,7 +3816,7 @@ bool CChainState::DisconnectTip(CValidationState &state,
assert(flushed);
mnview.GetHistoryWriters().FlushDB();

pcustomcsview->GetStorage().BlockTipChanged();
SetLastValidatedHeight(pindexDelete->pprev->nHeight);

if (!disconnectedConfirms.empty()) {
for (const auto &confirm : disconnectedConfirms) {
Expand Down Expand Up @@ -3997,7 +3995,7 @@ bool CChainState::ConnectTip(CValidationState &state,
assert(flushed);
mnview.GetHistoryWriters().FlushDB();

pcustomcsview->GetStorage().BlockTipChanged();
SetLastValidatedHeight(pindexNew->nHeight);

// Delete all other confirms from memory
if (rewardedAnchors) {
Expand Down

0 comments on commit adc22c6

Please sign in to comment.