Skip to content

Commit

Permalink
#1545 state locking
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Jul 22, 2024
1 parent d729d01 commit fcf950d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 28 deletions.
3 changes: 1 addition & 2 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions libethereum/SkaleHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
Expand Down
18 changes: 2 additions & 16 deletions libskale/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 ) );
Expand Down
7 changes: 0 additions & 7 deletions libskale/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fcf950d

Please sign in to comment.