forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 6
/
evodb.cpp
79 lines (66 loc) · 1.83 KB
/
evodb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2018-2019 The Dash Core developers
// Copyright (c) 2018-2020 The Ion Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "evodb.h"
CEvoDB* evoDb;
CEvoDBScopedCommitter::CEvoDBScopedCommitter(CEvoDB &_evoDB) :
evoDB(_evoDB)
{
}
CEvoDBScopedCommitter::~CEvoDBScopedCommitter()
{
if (!didCommitOrRollback)
Rollback();
}
void CEvoDBScopedCommitter::Commit()
{
assert(!didCommitOrRollback);
didCommitOrRollback = true;
evoDB.CommitCurTransaction();
}
void CEvoDBScopedCommitter::Rollback()
{
assert(!didCommitOrRollback);
didCommitOrRollback = true;
evoDB.RollbackCurTransaction();
}
CEvoDB::CEvoDB(size_t nCacheSize, bool fMemory, bool fWipe) :
db(fMemory ? "" : (GetDataDir() / "evodb"), nCacheSize, fMemory, fWipe),
rootBatch(db),
rootDBTransaction(db, rootBatch),
curDBTransaction(rootDBTransaction, rootDBTransaction)
{
}
void CEvoDB::CommitCurTransaction()
{
LOCK(cs);
curDBTransaction.Commit();
}
void CEvoDB::RollbackCurTransaction()
{
LOCK(cs);
curDBTransaction.Clear();
}
bool CEvoDB::CommitRootTransaction()
{
assert(curDBTransaction.IsClean());
rootDBTransaction.Commit();
bool ret = db.WriteBatch(rootBatch);
rootBatch.Clear();
return ret;
}
bool CEvoDB::VerifyBestBlock(const uint256& hash)
{
// Make sure evodb is consistent.
// If we already have best block hash saved, the previous block should match it.
uint256 hashBestBlock;
bool fHasBestBlock = Read(EVODB_BEST_BLOCK, hashBestBlock);
uint256 hashBlockIndex = fHasBestBlock ? hash : uint256();
assert(hashBestBlock == hashBlockIndex);
return fHasBestBlock;
}
void CEvoDB::WriteBestBlock(const uint256& hash)
{
Write(EVODB_BEST_BLOCK, hash);
}