core/vm: implement the remaining gas changes for 7904

This commit is contained in:
MariusVanDerWijden 2025-09-22 15:30:17 +02:00
parent 7949a928e9
commit 98d653cd31
5 changed files with 188 additions and 23 deletions

View file

@ -569,7 +569,7 @@ func enable7904(jt *JumpTable) {
jt[SMOD].constantGas = GasBaseCost jt[SMOD].constantGas = GasBaseCost
jt[ADDMOD].constantGas = GasFastCost jt[ADDMOD].constantGas = GasFastCost
jt[MULMOD].constantGas = GasMidCost jt[MULMOD].constantGas = GasMidCost
//jt[EXP].constantGas = GasMidCost jt[EXP].dynamicGas = gasExpEIP7904
jt[SIGNEXTEND].constantGas = GasBaseCost jt[SIGNEXTEND].constantGas = GasBaseCost
jt[LT].constantGas = GasBaseCost jt[LT].constantGas = GasBaseCost
jt[GT].constantGas = GasBaseCost jt[GT].constantGas = GasBaseCost
@ -586,22 +586,34 @@ func enable7904(jt *JumpTable) {
jt[SHR].constantGas = GasBaseCost jt[SHR].constantGas = GasBaseCost
jt[SAR].constantGas = GasBaseCost jt[SAR].constantGas = GasBaseCost
jt[ADDRESS].constantGas = GasBaseCost jt[ADDRESS].constantGas = GasBaseCost
//jt[BALANCE].constantGas = GasBaseCost
jt[BALANCE].constantGas = params.WarmStorageReadCostEIP7904
jt[BALANCE].dynamicGas = gasEip7904AccountCheck
jt[ORIGIN].constantGas = GasBaseCost jt[ORIGIN].constantGas = GasBaseCost
jt[CALLER].constantGas = GasBaseCost jt[CALLER].constantGas = GasBaseCost
jt[CALLVALUE].constantGas = GasBaseCost jt[CALLVALUE].constantGas = GasBaseCost
jt[CALLDATALOAD].constantGas = GasBaseCost jt[CALLDATALOAD].constantGas = GasBaseCost
jt[CALLDATASIZE].constantGas = GasBaseCost jt[CALLDATASIZE].constantGas = GasBaseCost
//jt[CALLDATACOPY].constantGas = GasBaseCost
jt[CALLDATACOPY].constantGas = GasBaseCost
jt[CALLDATACOPY].dynamicGas = gasCallDataCopy7904
jt[CODESIZE].constantGas = GasBaseCost jt[CODESIZE].constantGas = GasBaseCost
//jt[CODECOPY].constantGas = GasBaseCost
jt[CODECOPY].constantGas = GasBaseCost
jt[CODECOPY].dynamicGas = gasCodeCopy7904
jt[GASPRICE].constantGas = GasBaseCost jt[GASPRICE].constantGas = GasBaseCost
//jt[EXTCODESIZE].constantGas = GasBaseCost jt[EXTCODESIZE].dynamicGas = gasEip7904AccountCheck
//jt[EXTCODECOPY].constantGas = GasBaseCost
jt[EXTCODECOPY].dynamicGas = gasExtCodeCopy7904
jt[RETURNDATASIZE].constantGas = GasBaseCost jt[RETURNDATASIZE].constantGas = GasBaseCost
//jt[RETURNDATACOPY].constantGas = GasBaseCost jt[RETURNDATACOPY].dynamicGas = gasReturnDataCopy7904
//jt[EXTCODEHASH].constantGas = GasBaseCost
jt[EXTCODEHASH].dynamicGas = gasEip7904AccountCheck
jt[COINBASE].constantGas = GasBaseCost jt[COINBASE].constantGas = GasBaseCost
jt[TIMESTAMP].constantGas = GasBaseCost jt[TIMESTAMP].constantGas = GasBaseCost
jt[NUMBER].constantGas = GasBaseCost jt[NUMBER].constantGas = GasBaseCost
@ -610,8 +622,8 @@ func enable7904(jt *JumpTable) {
jt[SELFBALANCE].constantGas = GasBaseCost jt[SELFBALANCE].constantGas = GasBaseCost
jt[POP].constantGas = GasBaseCost jt[POP].constantGas = GasBaseCost
jt[MLOAD].constantGas = GasBaseCost jt[MLOAD].constantGas = GasBaseCost
//jt[MSTORE].constantGas = GasBaseCost jt[MSTORE].constantGas = GasBaseCost
//jt[MSTORE8].constantGas = GasBaseCost jt[MSTORE8].constantGas = GasBaseCost
jt[JUMP].constantGas = GasBaseCost jt[JUMP].constantGas = GasBaseCost
jt[JUMPI].constantGas = GasBaseCost jt[JUMPI].constantGas = GasBaseCost
jt[PC].constantGas = GasBaseCost jt[PC].constantGas = GasBaseCost
@ -619,7 +631,9 @@ func enable7904(jt *JumpTable) {
jt[TLOAD].constantGas = GasWarmStorageCost jt[TLOAD].constantGas = GasWarmStorageCost
jt[TSTORE].constantGas = GasWarmStorageCost jt[TSTORE].constantGas = GasWarmStorageCost
jt[JUMPDEST].constantGas = GasBaseCost jt[JUMPDEST].constantGas = GasBaseCost
//jt[MCOPY].constantGas = GasBaseCost
jt[MCOPY].dynamicGas = gasMcopy7904
jt[PUSH0].constantGas = GasBaseCost jt[PUSH0].constantGas = GasBaseCost
for i := PUSH1; i < PUSH32; i++ { for i := PUSH1; i < PUSH32; i++ {
jt[i].constantGas = GasBaseCost jt[i].constantGas = GasBaseCost
@ -633,4 +647,12 @@ func enable7904(jt *JumpTable) {
for i := SWAP1; i < SWAP16; i++ { for i := SWAP1; i < SWAP16; i++ {
jt[i].constantGas = GasBaseCost jt[i].constantGas = GasBaseCost
} }
jt[SLOAD].dynamicGas = gasSLoadEIP7904
jt[SSTORE].dynamicGas = gasSStoreEIP7904
jt[CALL].dynamicGas = gasCallEIP7904
jt[STATICCALL].dynamicGas = gasStaticCallEIP7904
jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP7904
jt[CALLCODE].dynamicGas = gasCallCodeEIP7904
} }

View file

@ -33,6 +33,8 @@ const (
GasFastCost uint64 = 2 GasFastCost uint64 = 2
GasMidCost uint64 = 3 GasMidCost uint64 = 3
GasWarmStorageCost uint64 = 5 GasWarmStorageCost uint64 = 5
GasExpBaseCost uint64 = 2
GasExpPerByteCost uint64 = 4
) )
// callGas returns the actual gas cost of the call. // callGas returns the actual gas cost of the call.

View file

@ -88,12 +88,49 @@ func memoryCopierGas(stackpos int) gasFunc {
} }
} }
// memoryCopierGas7904 creates the gas functions for the following opcodes, and takes
// the stack position of the operand which determines the size of the data to copy
// as argument:
// CALLDATACOPY (stack position 2)
// CODECOPY (stack position 2)
// MCOPY (stack position 2)
// EXTCODECOPY (stack position 3)
// RETURNDATACOPY (stack position 2)
func memoryCopierGas7904(stackpos int) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
// Gas for expanding the memory
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
// And gas for copying data, charged per word at param.CopyGas
words, overflow := stack.Back(stackpos).Uint64WithOverflow()
if overflow {
return 0, ErrGasUintOverflow
}
if words, overflow = math.SafeMul(toWordSize(words), params.CopyGasEIP7904); overflow {
return 0, ErrGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, words); overflow {
return 0, ErrGasUintOverflow
}
return gas, nil
}
}
var ( var (
gasCallDataCopy = memoryCopierGas(2) gasCallDataCopy = memoryCopierGas(2)
gasCodeCopy = memoryCopierGas(2) gasCodeCopy = memoryCopierGas(2)
gasMcopy = memoryCopierGas(2) gasMcopy = memoryCopierGas(2)
gasExtCodeCopy = memoryCopierGas(3) gasExtCodeCopy = memoryCopierGas(3)
gasReturnDataCopy = memoryCopierGas(2) gasReturnDataCopy = memoryCopierGas(2)
gasCallDataCopy7904 = memoryCopierGas7904(2)
gasCodeCopy7904 = memoryCopierGas7904(2)
gasMcopy7904 = memoryCopierGas7904(2)
gasExtCodeCopy7904 = memoryCopierGas7904(3)
gasReturnDataCopy7904 = memoryCopierGas7904(2)
) )
func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
@ -368,6 +405,19 @@ func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor
return gas, nil return gas, nil
} }
func gasExpEIP7904(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
var (
gas = expByteLen * params.ExpByteEIP7904 // no overflow check required. Max is 256 * ExpByte gas
overflow bool
)
if gas, overflow = math.SafeAdd(gas, params.ExpGasEIP7904); overflow {
return 0, ErrGasUintOverflow
}
return gas, nil
}
func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var ( var (
gas uint64 gas uint64

View file

@ -26,7 +26,7 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
func makeGasSStoreFunc(clearingRefund uint64) gasFunc { func makeGasSStoreFunc(clearingRefund uint64, warmLoadCosts uint64) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
// If we fail the minimum gas availability invariant, fail (0) // If we fail the minimum gas availability invariant, fail (0)
if contract.Gas <= params.SstoreSentryGasEIP2200 { if contract.Gas <= params.SstoreSentryGasEIP2200 {
@ -50,7 +50,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
if current == value { // noop (1) if current == value { // noop (1)
// EIP 2200 original clause: // EIP 2200 original clause:
// return params.SloadGasEIP2200, nil // return params.SloadGasEIP2200, nil
return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS return cost + warmLoadCosts, nil // SLOAD_GAS
} }
if original == current { if original == current {
if original == (common.Hash{}) { // create slot (2.1.1) if original == (common.Hash{}) { // create slot (2.1.1)
@ -74,19 +74,19 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
// EIP 2200 Original clause: // EIP 2200 Original clause:
//evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200) //evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929) evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - warmLoadCosts)
} else { // reset to original existing slot (2.2.2.2) } else { // reset to original existing slot (2.2.2.2)
// EIP 2200 Original clause: // EIP 2200 Original clause:
// evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200) // evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
// - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST) // - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST)
// - SLOAD_GAS redefined as WARM_STORAGE_READ_COST // - SLOAD_GAS redefined as WARM_STORAGE_READ_COST
// Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST // Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST
evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929) evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - warmLoadCosts)
} }
} }
// EIP-2200 original clause: // EIP-2200 original clause:
//return params.SloadGasEIP2200, nil // dirty update (2.2) //return params.SloadGasEIP2200, nil // dirty update (2.2)
return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2) return cost + warmLoadCosts, nil // dirty update (2.2)
} }
} }
@ -108,6 +108,24 @@ func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me
return params.WarmStorageReadCostEIP2929, nil return params.WarmStorageReadCostEIP2929, nil
} }
// gasSLoadEIP7904 calculates dynamic gas for SLOAD according to EIP-7904
// For SLOAD, if the (address, storage_key) pair (where address is the address of the contract
// whose storage is being read) is not yet in accessed_storage_keys,
// charge 2100 gas and add the pair to accessed_storage_keys.
// If the pair is already in accessed_storage_keys, charge 5 gas.
func gasSLoadEIP7904(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
loc := stack.peek()
slot := common.Hash(loc.Bytes32())
// Check slot presence in the access list
if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
// If the caller cannot afford the cost, this change will be rolled back
// If he does afford it, we can skip checking the same thing later on, during execution
evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
return params.ColdSloadCostEIP2929, nil
}
return params.WarmStorageReadCostEIP7904, nil
}
// gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929 // gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
// EIP spec: // EIP spec:
// > If the target is not in accessed_addresses, // > If the target is not in accessed_addresses,
@ -152,6 +170,25 @@ func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Mem
return 0, nil return 0, nil
} }
// gasEip7904AccountCheck checks whether the first stack item (as address) is present in the access list.
// If it is, this method returns '0', otherwise 'cold-warm' gas, presuming that the opcode using it
// is also using 'warm' as constant factor.
// This method is used by:
// - extcodehash,
// - extcodesize,
// - (ext) balance
func gasEip7904AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
addr := common.Address(stack.peek().Bytes20())
// Check slot presence in the access list
if !evm.StateDB.AddressInAccessList(addr) {
// If the caller cannot afford the cost, this change will be rolled back
evm.StateDB.AddAddressToAccessList(addr)
// The warm storage read cost is already charged as constantGas
return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP7904, nil
}
return 0, nil
}
func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc { func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
addr := common.Address(stack.Back(addressPosition).Bytes20()) addr := common.Address(stack.Back(addressPosition).Bytes20())
@ -191,11 +228,55 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) g
} }
} }
func makeCallVariantGasCallEIP7904(oldCalculator gasFunc, addressPosition int) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
addr := common.Address(stack.Back(addressPosition).Bytes20())
// Check slot presence in the access list
warmAccess := evm.StateDB.AddressInAccessList(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.WarmStorageReadCostEIP7904
if !warmAccess {
evm.StateDB.AddAddressToAccessList(addr)
// Charge the remaining difference here already, to correctly calculate available
// gas for call
if !contract.UseGas(coldCost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) {
return 0, ErrOutOfGas
}
}
// Now call the old calculator, which takes into account
// - create new account
// - transfer value
// - memory expansion
// - 63/64ths rule
gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
if warmAccess || err != nil {
return gas, err
}
// In case of a cold access, we temporarily add the cold charge back, and also
// add it to the returned gas. By adding it to the return, it will be charged
// outside of this function, as part of the dynamic gas, and that will make it
// also become correctly reported to tracers.
contract.Gas += coldCost
var overflow bool
if gas, overflow = math.SafeAdd(gas, coldCost); overflow {
return 0, ErrGasUintOverflow
}
return gas, nil
}
}
var ( var (
gasCallEIP2929 = makeCallVariantGasCallEIP2929(gasCall, 1) gasCallEIP2929 = makeCallVariantGasCallEIP2929(gasCall, 1)
gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall, 1) gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall, 1)
gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall, 1) gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall, 1)
gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode, 1) gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode, 1)
gasCallEIP7904 = makeCallVariantGasCallEIP7904(gasCall, 1)
gasDelegateCallEIP7904 = makeCallVariantGasCallEIP7904(gasDelegateCall, 1)
gasStaticCallEIP7904 = makeCallVariantGasCallEIP7904(gasStaticCall, 1)
gasCallCodeEIP7904 = makeCallVariantGasCallEIP7904(gasCallCode, 1)
gasSelfdestructEIP2929 = makeSelfdestructGasFn(true) gasSelfdestructEIP2929 = makeSelfdestructGasFn(true)
// gasSelfdestructEIP3529 implements the changes in EIP-3529 (no refunds) // gasSelfdestructEIP3529 implements the changes in EIP-3529 (no refunds)
gasSelfdestructEIP3529 = makeSelfdestructGasFn(false) gasSelfdestructEIP3529 = makeSelfdestructGasFn(false)
@ -212,11 +293,15 @@ var (
// //
//The other parameters defined in EIP 2200 are unchanged. //The other parameters defined in EIP 2200 are unchanged.
// see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified // see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200) gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200, params.WarmStorageReadCostEIP2929)
// gasSStoreEIP3529 implements gas cost for SSTORE according to EIP-3529 // gasSStoreEIP3529 implements gas cost for SSTORE according to EIP-3529
// Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800) // Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800)
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529) gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529, params.WarmStorageReadCostEIP2929)
// gasSStoreEIP7904 implements gas cost for SSTORE according to EIP-7904
// Lowers `WARM_STORAGE_READ_COST`.
gasSStoreEIP7904 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529, params.WarmStorageReadCostEIP7904)
) )
// makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-3529 // makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-3529

View file

@ -69,6 +69,8 @@ const (
ColdSloadCostEIP2929 = uint64(2100) // COLD_SLOAD_COST ColdSloadCostEIP2929 = uint64(2100) // COLD_SLOAD_COST
WarmStorageReadCostEIP2929 = uint64(100) // WARM_STORAGE_READ_COST WarmStorageReadCostEIP2929 = uint64(100) // WARM_STORAGE_READ_COST
WarmStorageReadCostEIP7904 = uint64(5) // WARM_STORAGE_READ_COST
// In EIP-2200: SstoreResetGas was 5000. // In EIP-2200: SstoreResetGas was 5000.
// In EIP-2929: SstoreResetGas was changed to '5000 - COLD_SLOAD_COST'. // In EIP-2929: SstoreResetGas was changed to '5000 - COLD_SLOAD_COST'.
// In EIP-3529: SSTORE_CLEARS_SCHEDULE is defined as SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST // In EIP-3529: SSTORE_CLEARS_SCHEDULE is defined as SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST
@ -119,6 +121,10 @@ const (
// EXP has a dynamic portion depending on the size of the exponent // EXP has a dynamic portion depending on the size of the exponent
ExpByteFrontier uint64 = 10 // was set to 10 in Frontier ExpByteFrontier uint64 = 10 // was set to 10 in Frontier
ExpByteEIP158 uint64 = 50 // was raised to 50 during Eip158 (Spurious Dragon) ExpByteEIP158 uint64 = 50 // was raised to 50 during Eip158 (Spurious Dragon)
ExpGasEIP7904 uint64 = 2 // lowered to 2 during EIP7904
ExpByteEIP7904 uint64 = 4 // was lowered to 4 during EIP7904
CopyGasEIP7904 uint64 = 1 // lowered
// Extcodecopy has a dynamic AND a static cost. This represents only the // Extcodecopy has a dynamic AND a static cost. This represents only the
// static portion of the gas. It was changed during EIP 150 (Tangerine) // static portion of the gas. It was changed during EIP 150 (Tangerine)