Skip to content

Commit

Permalink
remove 4762 gas call wrappers
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <[email protected]>
  • Loading branch information
jsign committed Sep 19, 2024
1 parent 88307bd commit 5e0aed2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
5 changes: 2 additions & 3 deletions core/state/access_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ func (aw *AccessWitness) TouchFullAccount(addr []byte, isWrite bool, useGasFn Us
return true
}

func (aw *AccessWitness) TouchAndChargeMessageCall(addr []byte, useGasFn UseGasFn) bool {
_, ok := aw.touchAddressAndChargeGas(addr, zeroTreeIndex, utils.BasicDataLeafKey, false, useGasFn)
return ok
func (aw *AccessWitness) TouchAndChargeMessageCall(addr []byte, useGasFn UseGasFn) (uint64, bool) {
return aw.touchAddressAndChargeGas(addr, zeroTreeIndex, utils.BasicDataLeafKey, false, useGasFn)
}

func (aw *AccessWitness) TouchAndChargeValueTransfer(callerAddr, targetAddr []byte, useGasFn UseGasFn) bool {
Expand Down
10 changes: 5 additions & 5 deletions core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,18 @@ func enable4762(jt *JumpTable) {
jt[EXTCODESIZE].constantGas = 0
jt[EXTCODESIZE].dynamicGas = nil
jt[EXTCODEHASH].constantGas = 0
jt[EXTCODEHASH].dynamicGas = gasExtCodeHash4762
jt[EXTCODEHASH].dynamicGas = nil
jt[EXTCODECOPY].constantGas = 0
jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP4762
jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP4762
jt[CREATE].constantGas = params.CreateNGasEip4762
jt[CREATE2].constantGas = params.CreateNGasEip4762
jt[CALL].constantGas = 0
jt[CALL].dynamicGas = gasCallEIP4762
jt[CALL].dynamicGas = gasCall
jt[CALLCODE].constantGas = 0
jt[CALLCODE].dynamicGas = gasCallCodeEIP4762
jt[CALLCODE].dynamicGas = gasCallCode
jt[STATICCALL].constantGas = 0
jt[STATICCALL].dynamicGas = gasStaticCallEIP4762
jt[STATICCALL].dynamicGas = gasStaticCall
jt[DELEGATECALL].constantGas = 0
jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP4762
jt[DELEGATECALL].dynamicGas = gasDelegateCall
}
33 changes: 32 additions & 1 deletion core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
ringIndex := num64 % params.Eip2935BlockHashHistorySize
var pnum common.Hash
binary.BigEndian.PutUint64(pnum[24:], ringIndex)
if !evm.Accesses.TouchSlotAndChargeGas(params.HistoryStorageAddress[:], pnum, false, scope.Contract.UseGas) {
if _, ok := evm.Accesses.TouchSlotAndChargeGas(params.HistoryStorageAddress[:], pnum, false, scope.Contract.UseGas); !ok {
return nil, ErrExecutionReverted
}
blockHash := evm.StateDB.GetState(params.HistoryStorageAddress, pnum)
Expand Down Expand Up @@ -744,7 +744,29 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
return nil, nil
}

func chargeCallVariantEIP4762(evm *EVM, scope *ScopeContext) bool {
target := common.Address(scope.Stack.Back(1).Bytes20())
if _, isPrecompile := evm.precompile(target); isPrecompile {
return true
}
// The charging for the value transfer is done BEFORE subtracting
// the 1/64th gas, as this is considered part of the CALL instruction.
// (so before we get to this point)
// But the message call is part of the subcall, for which only 63/64th
// of the gas should be available.
chargedGas, ok := evm.Accesses.TouchAndChargeMessageCall(target.Bytes(), scope.Contract.UseGas)
if !ok || (chargedGas == 0 && !scope.Contract.UseGas(params.WarmStorageReadCostEIP2929)) {
return false
}
return true

}

func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
if !chargeCallVariantEIP4762(interpreter.evm, scope) {
return nil, ErrExecutionReverted
}

stack := scope.Stack
// Pop gas. The actual gas in interpreter.evm.callGasTemp.
// We can use this as a temporary value
Expand Down Expand Up @@ -786,6 +808,9 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
}

func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
if !chargeCallVariantEIP4762(interpreter.evm, scope) {
return nil, ErrExecutionReverted
}
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
stack := scope.Stack
// We use it as a temporary value
Expand Down Expand Up @@ -821,6 +846,9 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
}

func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
if !chargeCallVariantEIP4762(interpreter.evm, scope) {
return nil, ErrExecutionReverted
}
stack := scope.Stack
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
// We use it as a temporary value
Expand Down Expand Up @@ -849,6 +877,9 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
}

func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
if !chargeCallVariantEIP4762(interpreter.evm, scope) {
return nil, ErrExecutionReverted
}
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
stack := scope.Stack
// We use it as a temporary value
Expand Down
30 changes: 0 additions & 30 deletions core/vm/operations_verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,6 @@ import (
"github.com/ethereum/go-ethereum/params"
)

func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
if err != nil {
return 0, err
}
target := common.Address(stack.Back(1).Bytes20())
if _, isPrecompile := evm.precompile(target); isPrecompile {
return gas, nil
}
// The charging for the value transfer is done BEFORE subtracting
// the 1/64th gas, as this is considered part of the CALL instruction.
// (so before we get to this point)
// But the message call is part of the subcall, for which only 63/64th
// of the gas should be available.
wgas := evm.Accesses.TouchAndChargeMessageCall(target.Bytes())
if wgas == 0 {
wgas = params.WarmStorageReadCostEIP2929
}
return wgas + gas, nil
}
}

var (
gasCallEIP4762 = makeCallVariantGasEIP4762(gasCall)
gasCallCodeEIP4762 = makeCallVariantGasEIP4762(gasCallCode)
gasStaticCallEIP4762 = makeCallVariantGasEIP4762(gasStaticCall)
gasDelegateCallEIP4762 = makeCallVariantGasEIP4762(gasDelegateCall)
)

func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
beneficiaryAddr := common.Address(stack.peek().Bytes20())
contractAddr := contract.Address()
Expand Down

0 comments on commit 5e0aed2

Please sign in to comment.