Line data Source code
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 : #include "clientversion.h" 8 : #include "netaddress.h" 9 : 10 : std::unique_ptr<CEvoDB> evoDb; 11 : 12 56666 : CEvoDBScopedCommitter::CEvoDBScopedCommitter(CEvoDB &_evoDB) : 13 56666 : evoDB(_evoDB) 14 : { 15 56666 : } 16 : 17 113332 : CEvoDBScopedCommitter::~CEvoDBScopedCommitter() 18 : { 19 56666 : if (!didCommitOrRollback) 20 15137 : Rollback(); 21 56666 : } 22 : 23 41529 : void CEvoDBScopedCommitter::Commit() 24 : { 25 41529 : assert(!didCommitOrRollback); 26 41529 : didCommitOrRollback = true; 27 41529 : evoDB.CommitCurTransaction(); 28 41529 : } 29 : 30 15137 : void CEvoDBScopedCommitter::Rollback() 31 : { 32 15137 : assert(!didCommitOrRollback); 33 15137 : didCommitOrRollback = true; 34 15137 : evoDB.RollbackCurTransaction(); 35 15137 : } 36 : 37 1129 : CEvoDB::CEvoDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(fMemory ? "" : (GetDataDir() / "evodb"), nCacheSize, fMemory, fWipe, CLIENT_VERSION | ADDRV2_FORMAT), 38 : rootBatch(CLIENT_VERSION | ADDRV2_FORMAT), 39 772 : rootDBTransaction(db, rootBatch, CLIENT_VERSION | ADDRV2_FORMAT), 40 2316 : curDBTransaction(rootDBTransaction, rootDBTransaction, CLIENT_VERSION | ADDRV2_FORMAT) 41 : { 42 772 : } 43 : 44 41529 : void CEvoDB::CommitCurTransaction() 45 : { 46 41529 : LOCK(cs); 47 41529 : curDBTransaction.Commit(); 48 41529 : } 49 : 50 15137 : void CEvoDB::RollbackCurTransaction() 51 : { 52 15137 : LOCK(cs); 53 15137 : curDBTransaction.Clear(); 54 15137 : } 55 : 56 802 : bool CEvoDB::CommitRootTransaction() 57 : { 58 802 : LOCK(cs); 59 802 : assert(curDBTransaction.IsClean()); 60 802 : rootDBTransaction.Commit(); 61 802 : bool ret = db.WriteBatch(rootBatch); 62 802 : rootBatch.Clear(); 63 1604 : return ret; 64 : } 65 : 66 57329 : bool CEvoDB::VerifyBestBlock(const uint256& hash) 67 : { 68 57329 : uint256 hashBestBlock; 69 57329 : return Read(EVODB_BEST_BLOCK, hashBestBlock) && hashBestBlock == hash; 70 : } 71 : 72 42336 : void CEvoDB::WriteBestBlock(const uint256& hash) 73 : { 74 42336 : Write(EVODB_BEST_BLOCK, hash); 75 42336 : }