From ccb4692ceb386d2cfe1270672b9fbb9b4efa1e5b Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 5 Apr 2024 12:23:40 -0300 Subject: [PATCH] core/vm: charge BLOCKHASH witness cost (#409) * core/vm: charge BLOCKHASH witness cost Signed-off-by: Ignacio Hagopian * remove gas optimization for now Signed-off-by: Ignacio Hagopian --------- Signed-off-by: Ignacio Hagopian --- core/vm/instructions.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 7760916e1635..ef3472d8acd9 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -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) { @@ -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()) }