dash/src/evo/evodb.h
Alexander Block 7a440d626b Optimize on-disk deterministic masternode storage to reduce size of evodb (#3017)
* Implement CompactFull() in CDBWrapper

This allows to compact the whole DB in one go.

* Implement more compact version of CDeterministicMNListDiff

This introduces CDeterministicMNStateDiff which requires to only store
fields on-disk which actually changed.

* Avoid writing mnUniquePropertyMap to disk when storing snapshots

This map can be rebuilt by simply using AddMN for each deserialized MN.

* Implement Serialize/Unserialize in CScript

This allows us to directly use READWRITE() on scripts and removes the need
for the ugly cast to CScriptBase. This commit also changes all Dash specific
uses of CScript to not use the cast.

* Keep track of registeration counts and introduce internalID for masternodes

The "internalId" is simply the number of MNs registered so far when the
new MN is added. It is deterministic and stays the same forever.

* Use internalId as keys in MN list diffs

This reduces the used size on-disk.

* Two simple speedups in MN list diff handling

1. Avoid full compare if dmn or state pointers match in BuildDiff
2. Use std::move when adding diff to listDiff in GetListForBlock

* Implement upgrade code for old CDeterministicMNListDiff format to new format

* Track tipIndex instead of tipHeight/tipBlockHash

* Store and pass around CBlockIndex* instead of block hash and height

This allows us to switch CDeterministicMNManager::GetListForBlock to work
with CBlockIndex.

* Refactor CDeterministicMNManager::GetListForBlock to require CBlockIndex*

Instead of requiring a block hash. This allows us to remove blockHash and
prevBlockHash from CDeterministicMNListDiff without the use of cs_main
locks in GetListForBlock.

* Remove prevBlockHash, blockHash and nHeight from CDeterministicMNListDiff

* Remove access to determinisitcMNManager in CMasternodeMetaMan::ToString()

The deterministic MN manager is not fully initialized yet at the time this
is called, which results in an empty list being returned everytime.

* Better logic to determine if an upgrade is needed

Reuse the "best block" logic to figure out if an upgrade is needed. Also
use it to ensure that older nodes are unable to start after the upgrade
was performed.

* Return null block hash if it was requested with getmnlistdiff

* bump CGovernanceManager::SERIALIZATION_VERSION_STRING

* Check SERIALIZATION_VERSION_STRING before deserializing anything else

* Invoke Clear() before deserializing just to be sure
2019-07-09 08:59:57 +03:00

92 lines
2.1 KiB
C++

// Copyright (c) 2018-2019 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef DASH_EVODB_H
#define DASH_EVODB_H
#include "dbwrapper.h"
#include "sync.h"
#include "uint256.h"
// "b_b" was used in the initial version of deterministic MN storage
// "b_b2" was used after compact diffs were introduced
static const std::string EVODB_BEST_BLOCK = "b_b2";
class CEvoDB
{
private:
CCriticalSection cs;
CDBWrapper db;
typedef CDBTransaction<CDBWrapper, CDBBatch> RootTransaction;
typedef CDBTransaction<RootTransaction, RootTransaction> CurTransaction;
typedef CScopedDBTransaction<RootTransaction, RootTransaction> ScopedTransaction;
CDBBatch rootBatch;
RootTransaction rootDBTransaction;
CurTransaction curDBTransaction;
public:
CEvoDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
std::unique_ptr<ScopedTransaction> BeginTransaction()
{
LOCK(cs);
auto t = ScopedTransaction::Begin(curDBTransaction);
return t;
}
CurTransaction& GetCurTransaction()
{
return curDBTransaction;
}
template <typename K, typename V>
bool Read(const K& key, V& value)
{
LOCK(cs);
return curDBTransaction.Read(key, value);
}
template <typename K, typename V>
void Write(const K& key, const V& value)
{
LOCK(cs);
curDBTransaction.Write(key, value);
}
template <typename K>
bool Exists(const K& key)
{
LOCK(cs);
return curDBTransaction.Exists(key);
}
template <typename K>
void Erase(const K& key)
{
LOCK(cs);
curDBTransaction.Erase(key);
}
CDBWrapper& GetRawDB()
{
return db;
}
size_t GetMemoryUsage()
{
return rootDBTransaction.GetMemoryUsage();
}
bool CommitRootTransaction();
bool VerifyBestBlock(const uint256& hash);
void WriteBestBlock(const uint256& hash);
};
extern CEvoDB* evoDb;
#endif //DASH_EVODB_H