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 35368 : CEvoDBScopedCommitter::CEvoDBScopedCommitter(CEvoDB &_evoDB) : 13 35368 : evoDB(_evoDB) 14 : { 15 35368 : } 16 : 17 70736 : CEvoDBScopedCommitter::~CEvoDBScopedCommitter() 18 : { 19 35368 : if (!didCommitOrRollback) 20 10242 : Rollback(); 21 35368 : } 22 : 23 25126 : void CEvoDBScopedCommitter::Commit() 24 : { 25 25126 : assert(!didCommitOrRollback); 26 25126 : didCommitOrRollback = true; 27 25126 : evoDB.CommitCurTransaction(); 28 25126 : } 29 : 30 10242 : void CEvoDBScopedCommitter::Rollback() 31 : { 32 10242 : assert(!didCommitOrRollback); 33 10242 : didCommitOrRollback = true; 34 10242 : evoDB.RollbackCurTransaction(); 35 10242 : } 36 : 37 1005 : 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 710 : rootDBTransaction(db, rootBatch, CLIENT_VERSION | ADDRV2_FORMAT), 40 2130 : curDBTransaction(rootDBTransaction, rootDBTransaction, CLIENT_VERSION | ADDRV2_FORMAT) 41 : { 42 710 : } 43 : 44 25126 : void CEvoDB::CommitCurTransaction() 45 : { 46 25126 : LOCK(cs); 47 25126 : curDBTransaction.Commit(); 48 25126 : } 49 : 50 10242 : void CEvoDB::RollbackCurTransaction() 51 : { 52 10242 : LOCK(cs); 53 10242 : curDBTransaction.Clear(); 54 10242 : } 55 : 56 632 : bool CEvoDB::CommitRootTransaction() 57 : { 58 632 : LOCK(cs); 59 632 : assert(curDBTransaction.IsClean()); 60 632 : rootDBTransaction.Commit(); 61 632 : bool ret = db.WriteBatch(rootBatch); 62 632 : rootBatch.Clear(); 63 1264 : return ret; 64 : } 65 : 66 36079 : bool CEvoDB::VerifyBestBlock(const uint256& hash) 67 : { 68 36079 : uint256 hashBestBlock; 69 36079 : return Read(EVODB_BEST_BLOCK, hashBestBlock) && hashBestBlock == hash; 70 : } 71 : 72 25977 : void CEvoDB::WriteBestBlock(const uint256& hash) 73 : { 74 25977 : Write(EVODB_BEST_BLOCK, hash); 75 25977 : }