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 f569004 commit a6538f6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
7 changes: 2 additions & 5 deletions libethereum/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ Block& Block::operator=( Block const& _s ) {
}


// make a lightweight copy for eth_call.
// make a lightweight read only copy
// we only copy the fields we need for eth_call
// in particular we do not copy receipts and transactions
// as well as raw bytes
Block Block::getCopyForEthCalls() const {
Block Block::getReadOnlyCopy() const {
Block copy(Null);
copy.m_state = m_state.createReadOnlySnapBasedCopy();
copy.m_author = m_author;
Expand Down Expand Up @@ -1097,9 +1097,6 @@ bool Block::sealBlock( bytesConstRef _header ) {
return true;
}

void Block::startReadState() {
m_state = m_state.createStateCopyWithReadLock();
}

h256 Block::stateRootBeforeTx( unsigned _i ) const {
_i = min< unsigned >( _i, m_transactions.size() );
Expand Down
2 changes: 1 addition & 1 deletion libethereum/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Block {
/// Copy state object.
Block& operator=( Block const& _s );

Block getCopyForEthCalls() const;
Block getReadOnlyCopy() const;

/// Get the author address for any transactions we do and rewards we get.
Address author() const { return m_author; }
Expand Down
19 changes: 9 additions & 10 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,8 +1114,7 @@ TransactionSkeleton Client::populateTransactionWithDefaults( TransactionSkeleton
// value used by geth and testrpc.
const u256 defaultTransactionGas = 90000;
if ( ret.nonce == Invalid256 ) {
Block block = postSeal();
block.startReadState();
Block block = getReadOnlyLatestBlockCopy();
ret.nonce = max< u256 >( block.transactionsFrom( ret.from ), m_tq.maxNonce( ret.from ) );
}
if ( ret.gasPrice == Invalid256 )
Expand Down Expand Up @@ -1246,7 +1245,7 @@ ExecutionResult Client::call( Address const& _from, u256 _value, Address _dest,
}
#endif

Block temp = getLatestBlockCopyForEthCall();
Block temp = getReadOnlyLatestBlockCopy();

u256 nonce = max< u256 >( temp.transactionsFrom( _from ), m_tq.maxNonce( _from ) );
// if the user did not specify transaction gas limit, we give transaction block gas
Expand Down Expand Up @@ -1524,7 +1523,7 @@ std::pair< u256, ExecutionResult > Client::estimateGas( Address const& _from, u2
else
lowerBound = Transaction::baseGasRequired( !_dest, &_data, EVMSchedule() );

Block latest = getLatestBlockCopyForEthCall();
Block latest = getReadOnlyLatestBlockCopy();
Block pending = latest;

if ( upperBound > pending.info().gasLimit() ) {
Expand Down Expand Up @@ -1606,25 +1605,25 @@ std::pair< bool, ExecutionResult > Client::estimateGasStep( int64_t _gas, Block&
}

u256 Client::countAt( Address _a ) const {
return getLatestBlockCopyForEthCall().state().getNonce( _a );
return getReadOnlyLatestBlockCopy().state().getNonce( _a );
}

u256 Client::balanceAt( Address _a ) const {
return getLatestBlockCopyForEthCall().state().balance( _a );
return getReadOnlyLatestBlockCopy().state().balance( _a );
}

u256 Client::stateAt( Address _a, u256 _l ) const {
return getLatestBlockCopyForEthCall().state().storage( _a, _l );
return getReadOnlyLatestBlockCopy().state().storage( _a, _l );
}

bytes Client::codeAt( Address _a ) const {
return getLatestBlockCopyForEthCall().state().code( _a );
return getReadOnlyLatestBlockCopy().state().code( _a );
}

h256 Client::codeHashAt( Address _a ) const {
return getLatestBlockCopyForEthCall().state().codeHash( _a );
return getReadOnlyLatestBlockCopy().state().codeHash( _a );
}

map< h256, pair< u256, u256 > > Client::storageAt( Address _a ) const {
return getLatestBlockCopyForEthCall().state().storage( _a );
return getReadOnlyLatestBlockCopy().state().storage( _a );
}
5 changes: 3 additions & 2 deletions libethereum/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,10 @@ namespace dev {
return m_preSeal;
}

Block getLatestBlockCopyForEthCall() const {
// get read only latest block copy
Block getReadOnlyLatestBlockCopy() const {
ReadGuard l(x_postSeal);
return m_postSeal.getCopyForEthCalls();
return m_postSeal.getReadOnlyCopy();
}

Block postSeal() const override {
Expand Down
1 change: 0 additions & 1 deletion libethereum/ClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ bool ClientBase::isKnownTransaction( h256 const& _blockHash, unsigned _i ) const

Block ClientBase::latestBlock() const {
Block res = postSeal();
res.startReadState();
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion libethereum/SkaleHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ ConsensusExtFace::transactions_vector SkaleHost::pendingTransactions(
MICROPROFILE_SCOPEI( "SkaleHost", "pendingTransactions", MP_LAWNGREEN );


_stateRoot = dev::h256::Arith( this->m_client.latestBlock().info().stateRoot() );
_stateRoot = dev::h256::Arith( m_client.getReadOnlyLatestBlockCopy().info().stateRoot() );

h256Hash to_delete;

Expand Down

0 comments on commit a6538f6

Please sign in to comment.