From fcf950d6f478d6674ae071b4c69bfff69e89fc9f Mon Sep 17 00:00:00 2001 From: Stan Kladko <13399135+kladkogex@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:55:39 +0100 Subject: [PATCH] #1545 state locking --- libethereum/Client.cpp | 3 +-- libethereum/SkaleHost.cpp | 6 +++--- libskale/State.cpp | 18 ++---------------- libskale/State.h | 7 ------- 4 files changed, 6 insertions(+), 28 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 03efbb03f..8a8193745 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -1076,8 +1076,7 @@ Block Client::blockByNumber( BlockNumber _h ) const { } // blockByNumber is only used for reads - - auto readState = m_state.createStateCopyWithReadLock(); + auto readState = m_state.createStateCopyAndClearCaches(); readState.mutableHistoricState().setRootByBlockNumber( _h ); // removed m_blockImportMutex here // this function doesn't interact with latest block so the mutex isn't needed diff --git a/libethereum/SkaleHost.cpp b/libethereum/SkaleHost.cpp index d3c985524..ca2c21e66 100644 --- a/libethereum/SkaleHost.cpp +++ b/libethereum/SkaleHost.cpp @@ -449,9 +449,9 @@ ConsensusExtFace::transactions_vector SkaleHost::pendingTransactions( if ( tx.verifiedOn < m_lastBlockWithBornTransactions ) try { bool isMtmEnabled = m_client.chainParams().sChain.multiTransactionMode; - Executive::verifyTransaction(tx, latestInfo.timestamp(), latestInfo, - m_client.state().createStateCopyWithReadLock(), m_client.chainParams(), 0, - getGasPrice(), isMtmEnabled ); + Executive::verifyTransaction( tx, latestInfo.timestamp(), latestInfo, + m_client.state().createReadOnlySnapBasedCopy(), m_client.chainParams(), 0, + getGasPrice(), isMtmEnabled ); } catch ( const exception& ex ) { if ( to_delete.count( tx.sha3() ) == 0 ) clog( VerbosityInfo, "skale-host" ) diff --git a/libskale/State.cpp b/libskale/State.cpp index 454e04443..fb47ee6fc 100644 --- a/libskale/State.cpp +++ b/libskale/State.cpp @@ -91,7 +91,7 @@ State::State( dev::u256 const& _accountStartNonce, boost::filesystem::path const m_db_ptr = make_shared< OverlayDB >( openDB( _dbPath, _genesis, _bs == BaseState::PreExisting ? dev::WithExisting::Trust : dev::WithExisting::Kill ) ); - auto state = createStateCopyWithReadLock(); + auto state = createStateCopyAndClearCaches(); totalStorageUsed_ = state.storageUsedTotal(); #ifdef HISTORIC_STATE m_historicState.setRootFromDB(); @@ -123,7 +123,7 @@ State::State( u256 const& _accountStartNonce, OverlayDB const& _db, m_historicState( _accountStartNonce, _historicDb, _historicBlockToStateRootDb, _bs ) #endif { - auto state = createStateCopyWithReadLock(); + auto state = createStateCopyAndClearCaches(); totalStorageUsed_ = state.storageUsedTotal(); #ifdef HISTORIC_STATE m_historicState.setRootFromDB(); @@ -246,9 +246,6 @@ State::State( const State& _s ) #endif { x_db_ptr = _s.x_db_ptr; - if ( _s.m_db_read_lock ) { - m_db_read_lock.emplace( *x_db_ptr ); - } m_db_ptr = _s.m_db_ptr; m_orig_db = _s.m_orig_db; m_cache = _s.m_cache; @@ -266,9 +263,6 @@ State::State( const State& _s ) State& State::operator=( const State& _s ) { x_db_ptr = _s.x_db_ptr; - if ( _s.m_db_read_lock ) { - m_db_read_lock.emplace( *x_db_ptr ); - } m_db_ptr = _s.m_db_ptr; m_orig_db = _s.m_orig_db; m_cache = _s.m_cache; @@ -901,13 +895,6 @@ void State::clearAllCaches() { } -State State::createStateCopyWithReadLock() const { - LDB_CHECK(!m_isReadOnlySnapBasedState); - State stateCopy = State( *this ); - stateCopy.m_db_read_lock.emplace( *stateCopy.x_db_ptr ); - stateCopy.clearCaches(); - return stateCopy; -} State State::createStateCopyAndClearCaches() const { LDB_CHECK(!m_isReadOnlySnapBasedState); @@ -928,7 +915,6 @@ State State::createReadOnlySnapBasedCopy() const { stateCopy.m_snap = m_orig_db->getLastBlockSnap(); LDB_CHECK( stateCopy.m_snap ) // the state does not use any locking since it is based on db snapshot - stateCopy.m_db_read_lock = boost::none; stateCopy.x_db_ptr = nullptr; stateCopy.m_db_ptr = make_shared< OverlayDB >( make_unique< batched_io::read_only_snap_based_batched_db >( stateCopy.m_orig_db, stateCopy.m_snap ) ); diff --git a/libskale/State.h b/libskale/State.h index 7a93c0359..46ed65e9d 100644 --- a/libskale/State.h +++ b/libskale/State.h @@ -380,11 +380,6 @@ class State { ChangeLog const& changeLog() const { return m_changeLog; } - /// Create State copy with read locked state db - /// since the lock the db is unlocked - - State createStateCopyWithReadLock() const; - /// Create State copy to modify data. State createStateCopyAndClearCaches() const; @@ -482,8 +477,6 @@ class State { private: enum Auxiliary { CODE = 1 }; - boost::optional< boost::shared_lock< boost::shared_mutex > > m_db_read_lock; - std::shared_ptr< boost::shared_mutex > x_db_ptr; std::shared_ptr< OverlayDB > m_db_ptr; ///< Our overlay for the state. std::shared_ptr< OverlayFS > m_fs_ptr; ///< Our overlay for the file system operations.