How SLOAD opcode works in ethereum virtual machine and why it is one of the most computationally expensive and time consuming process. vm stats for different opcodes of evm
1 Answer
It has to access the contract state to retrieve the value stored at the given key. Typically Ethereum clients will access a database to retrieve the data requested.
The difficult part is hidden in the database access. The database has to be organized such that data can be accessed quickly while minimizing IOPs, and at the same time it has support heavy usage since one block includes hundreds of transactions each 12 seconds. All of that supporting chain reorganizations and minimizing space.
For example here you will find go-ethereum implementation for v1.10.1
func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
loc := scope.Stack.peek()
hash := common.Hash(loc.Bytes32())
val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash)
loc.SetBytes(val.Bytes())
return nil, nil
}
The database access is done in the GetState function.