Skip to content

Commit

Permalink
core/vm: charge BLOCKHASH witness cost (ethereum#409)
Browse files Browse the repository at this point in the history
* core/vm: charge BLOCKHASH witness cost

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* remove gas optimization for now

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign authored and gballet committed Apr 10, 2024
1 parent b5e2ba6 commit ccb4692
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
return nil, nil
}

func getBlockHashFromContract(number uint64, statedb StateDB, witness *state.AccessWitness) common.Hash {
func getBlockHashFromContract(number uint64, statedb StateDB, witness *state.AccessWitness) (common.Hash, uint64) {
ringIndex := number % params.Eip2935BlockHashHistorySize
var pnum common.Hash
binary.BigEndian.PutUint64(pnum[24:], ringIndex)
witness.TouchSlotAndChargeGas(params.HistoryStorageAddress[:], pnum, false)
return statedb.GetState(params.HistoryStorageAddress, pnum)
statelessGas := witness.TouchSlotAndChargeGas(params.HistoryStorageAddress[:], pnum, false)
return statedb.GetState(params.HistoryStorageAddress, pnum), statelessGas
}

func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand All @@ -486,7 +486,13 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
if num64 >= lower && num64 < upper {
// if Prague is active, read it from the history contract (EIP 2935).
if evm.chainRules.IsPrague {
num.SetBytes(getBlockHashFromContract(num64, evm.StateDB, evm.Accesses).Bytes())
blockHash, statelessGas := getBlockHashFromContract(num64, evm.StateDB, evm.Accesses)
if interpreter.evm.chainRules.IsEIP4762 {
if !scope.Contract.UseGas(statelessGas) {
return nil, ErrExecutionReverted
}
}
num.SetBytes(blockHash.Bytes())
} else {
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
}
Expand Down

0 comments on commit ccb4692

Please sign in to comment.