diff --git a/core/vm/gas.go b/core/vm/gas.go index 5fe589bce6..dcb20893c5 100644 --- a/core/vm/gas.go +++ b/core/vm/gas.go @@ -49,6 +49,5 @@ func callGas(isEip150 bool, availableGas, base uint64, callCost *uint256.Int) (u if !callCost.IsUint64() { return 0, ErrGasUintOverflow } - return callCost.Uint64(), nil } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 23a2cbbf4d..88490c76c5 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -374,7 +374,32 @@ func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor return gas, nil } -func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +var ( + gasCall = makeCallVariantGasCost(gasCallIntrinsic) + gasCallCode = makeCallVariantGasCost(gasCallCodeIntrinsic) + gasDelegateCall = makeCallVariantGasCost(gasDelegateCallIntrinsic) + gasStaticCall = makeCallVariantGasCost(gasStaticCallIntrinsic) +) + +func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + intrinsic, err := intrinsicFunc(evm, contract, stack, mem, memorySize) + if err != nil { + return 0, err + } + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, intrinsic, stack.Back(0)) + if err != nil { + return 0, err + } + gas, overflow := math.SafeAdd(intrinsic, evm.callGasTemp) + if overflow { + return 0, ErrGasUintOverflow + } + return gas, nil + } +} + +func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { var ( gas uint64 transfersValue = !stack.Back(2).IsZero() @@ -383,38 +408,40 @@ func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize if evm.readOnly && transfersValue { return 0, ErrWriteProtection } - - if evm.chainRules.IsEIP158 { - if transfersValue && evm.StateDB.Empty(address) { - gas += params.CallNewAccountGas - } - } else if !evm.StateDB.Exist(address) { - gas += params.CallNewAccountGas - } - if transfersValue && !evm.chainRules.IsEIP4762 { - gas += params.CallValueTransferGas - } + // Stateless check memoryGas, err := memoryGasCost(mem, memorySize) if err != nil { return 0, err } + var transferGas uint64 + if transfersValue && !evm.chainRules.IsEIP4762 { + transferGas = params.CallValueTransferGas + } var overflow bool - if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { + if gas, overflow = math.SafeAdd(memoryGas, transferGas); overflow { return 0, ErrGasUintOverflow } - - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) - if err != nil { - return 0, err + // Terminate the gas measurement if the leftover gas is not sufficient, + // it can effectively prevent accessing the states in the following steps. + if contract.Gas < gas { + return 0, ErrOutOfGas } - if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { + // Stateful check + var stateGas uint64 + if evm.chainRules.IsEIP158 { + if transfersValue && evm.StateDB.Empty(address) { + stateGas += params.CallNewAccountGas + } + } else if !evm.StateDB.Exist(address) { + stateGas += params.CallNewAccountGas + } + if gas, overflow = math.SafeAdd(gas, stateGas); overflow { return 0, ErrGasUintOverflow } - return gas, nil } -func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { memoryGas, err := memoryGasCost(mem, memorySize) if err != nil { return 0, err @@ -429,46 +456,15 @@ func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memory if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { return 0, ErrGasUintOverflow } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) - if err != nil { - return 0, err - } - if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { - return 0, ErrGasUintOverflow - } return gas, nil } -func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - gas, err := memoryGasCost(mem, memorySize) - if err != nil { - return 0, err - } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) - if err != nil { - return 0, err - } - var overflow bool - if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { - return 0, ErrGasUintOverflow - } - return gas, nil +func gasDelegateCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return memoryGasCost(mem, memorySize) } -func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - gas, err := memoryGasCost(mem, memorySize) - if err != nil { - return 0, err - } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) - if err != nil { - return 0, err - } - var overflow bool - if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { - return 0, ErrGasUintOverflow - } - return gas, nil +func gasStaticCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return memoryGasCost(mem, memorySize) } func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index ce394d9384..addd2b162f 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -256,10 +256,10 @@ func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { } var ( - innerGasCallEIP7702 = makeCallVariantGasCallEIP7702(gasCall) - gasDelegateCallEIP7702 = makeCallVariantGasCallEIP7702(gasDelegateCall) - gasStaticCallEIP7702 = makeCallVariantGasCallEIP7702(gasStaticCall) - gasCallCodeEIP7702 = makeCallVariantGasCallEIP7702(gasCallCode) + innerGasCallEIP7702 = makeCallVariantGasCallEIP7702(gasCallIntrinsic) + gasDelegateCallEIP7702 = makeCallVariantGasCallEIP7702(gasDelegateCallIntrinsic) + gasStaticCallEIP7702 = makeCallVariantGasCallEIP7702(gasStaticCallIntrinsic) + gasCallCodeEIP7702 = makeCallVariantGasCallEIP7702(gasCallCodeIntrinsic) ) func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { @@ -274,62 +274,85 @@ func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem return innerGasCallEIP7702(evm, contract, stack, mem, memorySize) } -func makeCallVariantGasCallEIP7702(oldCalculator gasFunc) gasFunc { +func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { var ( - total uint64 // total dynamic gas used - addr = common.Address(stack.Back(1).Bytes20()) + eip2929Cost uint64 + eip7702Cost uint64 + addr = common.Address(stack.Back(1).Bytes20()) ) - - // Check slot presence in the access list + // Perform EIP-2929 checks (stateless), checking address presence + // in the accessList and charge the cold access accordingly. if !evm.StateDB.AddressInAccessList(addr) { evm.StateDB.AddAddressToAccessList(addr) - // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so - // the cost to charge for cold access, if any, is Cold - Warm - coldCost := params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929 - // Charge the remaining difference here already, to correctly calculate available - // gas for call - if !contract.UseGas(coldCost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { + + // The WarmStorageReadCostEIP2929 (100) is already deducted in the form + // of a constant cost, so the cost to charge for cold access, if any, + // is Cold - Warm + eip2929Cost = params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929 + + // Charge the remaining difference here already, to correctly calculate + // available gas for call + if !contract.UseGas(eip2929Cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { return 0, ErrOutOfGas } - total += coldCost + } + + // Perform the intrinsic cost calculation including: + // + // - transfer value + // - memory expansion + // - create new account + intrinsicCost, err := intrinsicFunc(evm, contract, stack, mem, memorySize) + if err != nil { + return 0, err + } + // Terminate the gas measurement if the leftover gas is not sufficient, + // it can effectively prevent accessing the states in the following steps. + // It's an essential safeguard before any stateful check. + if contract.Gas < intrinsicCost { + return 0, ErrOutOfGas } // Check if code is a delegation and if so, charge for resolution. if target, ok := types.ParseDelegation(evm.StateDB.GetCode(addr)); ok { - var cost uint64 if evm.StateDB.AddressInAccessList(target) { - cost = params.WarmStorageReadCostEIP2929 + eip7702Cost = params.WarmStorageReadCostEIP2929 } else { evm.StateDB.AddAddressToAccessList(target) - cost = params.ColdAccountAccessCostEIP2929 + eip7702Cost = params.ColdAccountAccessCostEIP2929 } - if !contract.UseGas(cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { + if !contract.UseGas(eip7702Cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { return 0, ErrOutOfGas } - total += cost } - - // Now call the old calculator, which takes into account - // - create new account - // - transfer value - // - memory expansion - // - 63/64ths rule - old, err := oldCalculator(evm, contract, stack, mem, memorySize) + // Calculate the gas budget for the nested call. The costs defined by + // EIP-2929 and EIP-7702 have already been applied. + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, intrinsicCost, stack.Back(0)) if err != nil { - return old, err + return 0, err } - // Temporarily add the gas charge back to the contract and return value. By // adding it to the return, it will be charged outside of this function, as // part of the dynamic gas. This will ensure it is correctly reported to // tracers. - contract.Gas += total + contract.Gas += eip2929Cost + eip7702Cost - var overflow bool - if total, overflow = math.SafeAdd(old, total); overflow { + // Aggregate the gas costs from all components, including EIP-2929, EIP-7702, + // the CALL opcode itself, and the cost incurred by nested calls. + var ( + overflow bool + totalCost uint64 + ) + if totalCost, overflow = math.SafeAdd(eip2929Cost, eip7702Cost); overflow { return 0, ErrGasUintOverflow } - return total, nil + if totalCost, overflow = math.SafeAdd(totalCost, intrinsicCost); overflow { + return 0, ErrGasUintOverflow + } + if totalCost, overflow = math.SafeAdd(totalCost, evm.callGasTemp); overflow { + return 0, ErrGasUintOverflow + } + return totalCost, nil } }