Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snapshot support tested in poolpair RPC #2698

Merged
merged 17 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ static const std::string DEFAULT_LEVELDB_CHECKSUM = "auto";

extern bool levelDBChecksum;

class CStorageSnapshot;

class dbwrapper_error : public std::runtime_error
{
public:
Expand Down Expand Up @@ -263,15 +265,20 @@ class CDBWrapper

template <typename K, typename V>
bool Read(const K& key, V& value) const
{
return Read(key, value, readoptions);
}

template <typename K, typename V>
bool Read(const K& key, V& value, const leveldb::ReadOptions &otherOptions) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
// leveldb::Slice slKey(SliceKey(key));

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
leveldb::Status status = pdb->Get(otherOptions, slKey, &strValue);
if (!status.ok()) {
if (status.IsNotFound())
return false;
Expand All @@ -297,16 +304,20 @@ class CDBWrapper
}

template <typename K>
bool Exists(const K& key) const
bool Exists(const K& key) const {
return Exists(key, readoptions);
}

template <typename K>
bool Exists(const K& key, const leveldb::ReadOptions &otherOptions) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
// leveldb::Slice slKey(SliceKey(key));

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
leveldb::Status status = pdb->Get(otherOptions, slKey, &strValue);
if (!status.ok()) {
if (status.IsNotFound())
return false;
Expand Down Expand Up @@ -343,7 +354,16 @@ class CDBWrapper

CDBIterator *NewIterator()
{
return new CDBIterator(*this, pdb->NewIterator(iteroptions));
return NewIterator(readoptions);
}

CDBIterator *NewIterator(const leveldb::ReadOptions &readOptions)
{
return new CDBIterator(*this, pdb->NewIterator(readOptions));
}

[[nodiscard]] std::shared_ptr<CStorageSnapshot> GetStorageSnapshot() const {
return std::make_shared<CStorageSnapshot>(pdb);
}

/**
Expand Down Expand Up @@ -384,4 +404,30 @@ class CDBWrapper
}
};


class CStorageSnapshot {
public:
explicit CStorageSnapshot(leveldb::DB* db) : db(db) {
snapshot = db->GetSnapshot();
}

~CStorageSnapshot() {
if (db && snapshot) {
db->ReleaseSnapshot(snapshot);
}
}

[[nodiscard]] const leveldb::Snapshot* GetLevelDBSnapshot() const {
return snapshot;
}

CStorageSnapshot(const CStorageSnapshot&) = delete;
CStorageSnapshot& operator=(const CStorageSnapshot&) = delete;

private:
leveldb::DB* db; // Owned by pcustomcsDB
const leveldb::Snapshot* snapshot;
};


#endif // DEFI_DBWRAPPER_H
13 changes: 13 additions & 0 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ CCustomCSView::CCustomCSView(CStorageKV &st)
CheckPrefixes();
}

CCustomCSView::CCustomCSView(std::unique_ptr<CStorageLevelDB> &st, const MapKV &changed)
: CStorageView(new CFlushableStorageKV(st, changed)) {
CheckPrefixes();
}

// cache-upon-a-cache (not a copy!) constructor
CCustomCSView::CCustomCSView(CCustomCSView &other)
: CStorageView(new CFlushableStorageKV(other.DB())),
Expand Down Expand Up @@ -1394,3 +1399,11 @@ void CalcMissingRewardTempFix(CCustomCSView &mnview, const uint32_t targetHeight
}
}
}

std::unique_ptr<CCustomCSView> GetViewSnapshot() {
// Get database snapshot and flushable storage changed map
auto [changed, snapshotDB] = pcustomcsview->GetStorage().GetSnapshotPair();

// Create new view using snapshot and change map
return std::make_unique<CCustomCSView>(snapshotDB, changed);
}
8 changes: 7 additions & 1 deletion src/dfi/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,14 @@ class CCustomCSView : public CMasternodesView,
// Increase version when underlaying tables are changed
static constexpr const int DbVersion = 1;

// Normal constructors
CCustomCSView();
explicit CCustomCSView(CStorageKV &st);

// cache-upon-a-cache (not a copy!) constructor
// Snapshot constructor
explicit CCustomCSView(std::unique_ptr<CStorageLevelDB> &st, const MapKV &changed);

// Cache-upon-a-cache constructors
CCustomCSView(CCustomCSView &other);
CCustomCSView(CCustomCSView &other,
CAccountHistoryStorage *historyView,
Expand Down Expand Up @@ -624,6 +628,8 @@ class CCustomCSView : public CMasternodesView,

std::map<CKeyID, CKey> AmISignerNow(int height, const CAnchorData::CTeam &team);

std::unique_ptr<CCustomCSView> GetViewSnapshot();

/** Global DB and view that holds enhanced chainstate data (should be protected by cs_main) */
extern std::unique_ptr<CStorageLevelDB> pcustomcsDB;
extern std::unique_ptr<CCustomCSView> pcustomcsview;
Expand Down
Loading
Loading