Skip to content

Commit

Permalink
Get snapshot on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Nov 22, 2023
1 parent f5ec14a commit 297ff52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
39 changes: 10 additions & 29 deletions src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,35 +202,21 @@ class CStorageLevelDB : public CStorageKV {
return db->IsEmpty();
}
std::unique_ptr<CStorageLevelDB> GetSnapshotDB() {
if (snapshotUpdated.load()) {
if (blockTipChanged.load()) {
// 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 (snapshotUpdated.load()) {
snapshotUpdated.store(false);
if (tipSnapshot) {
tipCopySnapshot = tipSnapshot;
} else {
tipCopySnapshot.reset();
}
if (blockTipChanged.load()) {
tipSnapshot = db->GetSnapshot();
blockTipChanged.store(false);
}
}

if (tipCopySnapshot) {
return std::make_unique<CStorageLevelDB>(db, tipCopySnapshot);
}

auto dbSnapshot = db->GetSnapshot();
return std::make_unique<CStorageLevelDB>(db, dbSnapshot);
}
void GenerateSnapshot() {
snapshotUpdated.store(true);
tipSnapshot = db->GetSnapshot();
return std::make_unique<CStorageLevelDB>(db, tipSnapshot);
}
void ReleaseSnapshot() {
snapshotUpdated.store(true);
tipSnapshot.reset();
void BlockTipChanged() {
blockTipChanged.store(true);
}

private:
Expand All @@ -242,17 +228,12 @@ class CStorageLevelDB : public CStorageKV {
// reading from the DB.
std::shared_ptr<CStorageSnapshot> snapshot;

// Snapshot updated at tip. Accessed under cs_main lock.
std::shared_ptr<CStorageSnapshot> tipSnapshot;

// Used to create another CStorageLevelDB object set
// as the snapshot member. Lock free.
std::shared_ptr<CStorageSnapshot> tipCopySnapshot;
std::shared_ptr<CStorageSnapshot> tipSnapshot;

// Used to determine whether the tipSnapshot has been
// updated and should be copied over to the lock free
// tipCopySnapshot.
std::atomic<bool> snapshotUpdated{};
// Used to determine whether the block tip has changed.
std::atomic<bool> blockTipChanged{true};
};

// Flushable storage
Expand Down
8 changes: 2 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3814,8 +3814,7 @@ bool CChainState::DisconnectTip(CValidationState &state,
assert(flushed);
mnview.GetHistoryWriters().FlushDB();

// Release snapshot
pcustomcsDB->ReleaseSnapshot();
pcustomcsDB->BlockTipChanged();

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

// Generate snapshot
if (!IsInitialBlockDownload()) {
pcustomcsDB->GenerateSnapshot();
}
pcustomcsDB->BlockTipChanged();

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

0 comments on commit 297ff52

Please sign in to comment.