Skip to content

Commit d6c56d1

Browse files
codablockrandom-zebra
authored andcommitted
Introduce CEvoDB for all evo related things, e.g. DIP3
>>> backports dash@c9a72e8880fe88f07b64aecfc059f08ec14440fc Also add transaction handling to ConnectTip and DisconnectTip and a few other places where blocks are processed.
1 parent 2496933 commit d6c56d1

File tree

7 files changed

+111
-2
lines changed

7 files changed

+111
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ set(COMMON_SOURCES
385385
./src/coins.cpp
386386
./src/key_io.cpp
387387
./src/compressor.cpp
388+
./src/evo/evodb.cpp
388389
./src/evo/specialtx.cpp
389390
./src/consensus/merkle.cpp
390391
./src/consensus/zerocoin_verify.cpp

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ BITCOIN_CORE_H = \
181181
cuckoocache.h \
182182
crypter.h \
183183
cyclingvector.h \
184+
evo/evodb.h \
184185
evo/specialtx.h \
185186
pairresult.h \
186187
addressbook.h \
@@ -322,6 +323,7 @@ libbitcoin_server_a_SOURCES = \
322323
consensus/params.cpp \
323324
consensus/tx_verify.cpp \
324325
consensus/zerocoin_verify.cpp \
326+
evo/evodb.cpp \
325327
evo/specialtx.cpp \
326328
httprpc.cpp \
327329
httpserver.cpp \

src/evo/evodb.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2018-2021 The Dash Core developers
2+
// Copyright (c) 2021 The PIVX Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include "evodb.h"
7+
8+
CEvoDB* evoDb;
9+
10+
CEvoDB::CEvoDB(size_t nCacheSize, bool fMemory, bool fWipe) :
11+
db(GetDataDir() / "evodb", nCacheSize, fMemory, fWipe),
12+
dbTransaction(db)
13+
{
14+
}

src/evo/evodb.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2018-2021 The Dash Core developers
2+
// Copyright (c) 2021 The PIVX Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef PIVX_EVODB_H
7+
#define PIVX_EVODB_H
8+
9+
#include "dbwrapper.h"
10+
#include "sync.h"
11+
12+
class CEvoDB
13+
{
14+
private:
15+
RecursiveMutex cs;
16+
CDBWrapper db;
17+
CDBTransaction dbTransaction;
18+
19+
public:
20+
CEvoDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
21+
22+
std::unique_ptr<CScopedDBTransaction> BeginTransaction()
23+
{
24+
LOCK(cs);
25+
auto t = CScopedDBTransaction::Begin(dbTransaction);
26+
return t;
27+
}
28+
29+
template<typename K, typename V>
30+
bool Read(const K& key, V& value)
31+
{
32+
LOCK(cs);
33+
return dbTransaction.Read(key, value);
34+
}
35+
36+
template<typename K, typename V>
37+
void Write(const K& key, const V& value)
38+
{
39+
LOCK(cs);
40+
dbTransaction.Write(key, value);
41+
}
42+
43+
template <typename K>
44+
bool Exists(const K& key)
45+
{
46+
LOCK(cs);
47+
return dbTransaction.Exists(key);
48+
}
49+
50+
template <typename K>
51+
void Erase(const K& key)
52+
{
53+
LOCK(cs);
54+
dbTransaction.Erase(key);
55+
}
56+
57+
CDBWrapper& GetRawDB()
58+
{
59+
return db;
60+
}
61+
};
62+
63+
extern CEvoDB* evoDb;
64+
65+
#endif//PIVX_EVODB_H

src/init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "scheduler.h"
4545
#include "spork.h"
4646
#include "sporkdb.h"
47+
#include "evo/evodb.h"
4748
#include "txdb.h"
4849
#include "torcontrol.h"
4950
#include "guiinterface.h"
@@ -297,6 +298,8 @@ void PrepareShutdown()
297298
zerocoinDB = NULL;
298299
delete pSporkDB;
299300
pSporkDB = NULL;
301+
delete evoDb;
302+
evoDb = nullptr;
300303
}
301304
#ifdef ENABLE_WALLET
302305
if (pwalletMain)
@@ -1547,6 +1550,7 @@ bool AppInitMain()
15471550
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
15481551
nTotalCache -= nCoinDBCache;
15491552
nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
1553+
int64_t nEvoDbCache = 1024 * 1024 * 16; // TODO
15501554
LogPrintf("Cache configuration:\n");
15511555
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
15521556
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
@@ -1571,10 +1575,12 @@ bool AppInitMain()
15711575
delete pblocktree;
15721576
delete zerocoinDB;
15731577
delete pSporkDB;
1578+
delete evoDb;
15741579

15751580
//PIVX specific: zerocoin and spork DB's
15761581
zerocoinDB = new CZerocoinDB(0, false, fReindex);
15771582
pSporkDB = new CSporkDB(0, false, false);
1583+
evoDb = new CEvoDB(nEvoDbCache, false, fReindex);
15781584

15791585
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
15801586

src/test/test_pivx.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "blockassembler.h"
1111
#include "guiinterface.h"
12+
#include "evo/evodb.h"
1213
#include "miner.h"
1314
#include "net_processing.h"
1415
#include "random.h"
@@ -75,6 +76,7 @@ TestingSetup::TestingSetup()
7576
RegisterAllCoreRPCCommands(tableRPC);
7677
zerocoinDB = new CZerocoinDB(0, true);
7778
pSporkDB = new CSporkDB(0, true);
79+
evoDb = new CEvoDB(1 << 20, true, true);
7880
pblocktree = new CBlockTreeDB(1 << 20, true);
7981
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
8082
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
@@ -109,6 +111,7 @@ TestingSetup::~TestingSetup()
109111
delete pblocktree;
110112
delete zerocoinDB;
111113
delete pSporkDB;
114+
delete evoDb;
112115
fs::remove_all(pathTemp);
113116
}
114117

src/validation.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "script/sigcache.h"
4444
#include "spork.h"
4545
#include "sporkdb.h"
46+
#include "evo/evodb.h"
4647
#include "txdb.h"
4748
#include "txmempool.h"
4849
#include "undo.h"
@@ -1954,11 +1955,16 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara
19541955
const uint256& saplingAnchorBeforeDisconnect = pcoinsTip->GetBestAnchor();
19551956
int64_t nStart = GetTimeMicros();
19561957
{
1958+
auto dbTx = evoDb->BeginTransaction();
1959+
19571960
CCoinsViewCache view(pcoinsTip);
19581961
assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
19591962
if (DisconnectBlock(block, pindexDelete, view) != DISCONNECT_OK)
19601963
return error("DisconnectTip() : DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
1961-
assert(view.Flush());
1964+
bool flushed = view.Flush();
1965+
assert(flushed);
1966+
bool committed = dbTx->Commit();
1967+
assert(committed);
19621968
}
19631969
LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
19641970
const uint256& saplingAnchorAfterDisconnect = pcoinsTip->GetBestAnchor();
@@ -2103,6 +2109,8 @@ bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const st
21032109
int64_t nTime3;
21042110
LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
21052111
{
2112+
auto dbTx = evoDb->BeginTransaction();
2113+
21062114
CCoinsViewCache view(pcoinsTip);
21072115
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, false);
21082116
GetMainSignals().BlockChecked(blockConnecting, state);
@@ -2114,7 +2122,10 @@ bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const st
21142122
nTime3 = GetTimeMicros();
21152123
nTimeConnectTotal += nTime3 - nTime2;
21162124
LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
2117-
assert(view.Flush());
2125+
bool flushed = view.Flush();
2126+
assert(flushed);
2127+
bool committed = dbTx->Commit();
2128+
assert(committed);
21182129
}
21192130
int64_t nTime4 = GetTimeMicros();
21202131
nTimeFlush += nTime4 - nTime3;
@@ -3415,6 +3426,9 @@ bool TestBlockValidity(CValidationState& state, const CBlock& block, CBlockIndex
34153426
indexDummy.pprev = pindexPrev;
34163427
indexDummy.nHeight = pindexPrev->nHeight + 1;
34173428

3429+
// begin tx and let it rollback
3430+
auto dbTx = evoDb->BeginTransaction();
3431+
34183432
// NOTE: CheckBlockHeader is called by CheckBlock
34193433
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
34203434
return error("%s: ContextualCheckBlockHeader failed: %s", __func__, FormatStateMessage(state));
@@ -3654,6 +3668,10 @@ bool CVerifyDB::VerifyDB(CCoinsView* coinsview, int nCheckLevel, int nCheckDepth
36543668
return true;
36553669

36563670
const int chainHeight = chainActive.Height();
3671+
3672+
// begin tx and let it rollback
3673+
auto dbTx = evoDb->BeginTransaction();
3674+
36573675
// Verify blocks in the best chain
36583676
if (nCheckDepth <= 0)
36593677
nCheckDepth = 1000000000; // suffices until the year 19000

0 commit comments

Comments
 (0)