Line data Source code
1 : // Copyright (c) 2018-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <evo/evodb.h> 6 : 7 : #include <uint256.h> 8 : 9 722716 : CEvoDBScopedCommitter::CEvoDBScopedCommitter(CEvoDB &_evoDB) : 10 361358 : evoDB(_evoDB) 11 361358 : { 12 361358 : } 13 : 14 722716 : CEvoDBScopedCommitter::~CEvoDBScopedCommitter() 15 361358 : { 16 361358 : if (!didCommitOrRollback) 17 90033 : Rollback(); 18 722716 : } 19 : 20 271325 : void CEvoDBScopedCommitter::Commit() 21 : { 22 271325 : assert(!didCommitOrRollback); 23 271325 : didCommitOrRollback = true; 24 271325 : evoDB.CommitCurTransaction(); 25 271325 : } 26 : 27 90033 : void CEvoDBScopedCommitter::Rollback() 28 : { 29 90033 : assert(!didCommitOrRollback); 30 90033 : didCommitOrRollback = true; 31 90033 : evoDB.RollbackCurTransaction(); 32 90033 : } 33 : 34 7380 : CEvoDB::CEvoDB(const util::DbWrapperParams& db_params) : 35 3690 : db{util::MakeDbWrapper({db_params.path / "evodb", db_params.memory, db_params.wipe, /*cache_size=*/64 << 20})}, 36 3690 : rootBatch{*db}, 37 3690 : rootDBTransaction{*db, rootBatch}, 38 3690 : curDBTransaction{rootDBTransaction, rootDBTransaction} 39 3690 : { 40 7380 : } 41 : 42 7378 : CEvoDB::~CEvoDB() = default; 43 : 44 271325 : void CEvoDB::CommitCurTransaction() 45 : { 46 271325 : LOCK(cs); 47 271325 : curDBTransaction.Commit(); 48 271325 : } 49 : 50 90033 : void CEvoDB::RollbackCurTransaction() 51 : { 52 90033 : LOCK(cs); 53 90033 : curDBTransaction.Clear(); 54 90033 : } 55 : 56 16974 : bool CEvoDB::CommitRootTransaction() 57 : { 58 16974 : LOCK(cs); 59 16974 : assert(curDBTransaction.IsClean()); 60 16974 : rootDBTransaction.Commit(); 61 16974 : bool ret = db->WriteBatch(rootBatch); 62 16974 : rootBatch.Clear(); 63 16974 : return ret; 64 16974 : } 65 : 66 176200 : bool CEvoDB::VerifyBestBlock(const uint256& hash) 67 : { 68 : // Make sure evodb is consistent. 69 : // If we already have best block hash saved, the previous block should match it. 70 176200 : uint256 hashBestBlock; 71 176200 : if (!Read(EVODB_BEST_BLOCK, hashBestBlock)) { 72 0 : return false; 73 : } 74 176200 : return hashBestBlock == hash; 75 176200 : } 76 : 77 279546 : void CEvoDB::WriteBestBlock(const uint256& hash) 78 : { 79 279546 : Write(EVODB_BEST_BLOCK, hash); 80 279546 : }