Skip to content

Commit 487493b

Browse files
Instrument makeCallVariantGasEIP4762 with multi gas
1 parent cce7f84 commit 487493b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

core/vm/operations_gas_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ type GasCallFuncTestCase struct {
460460
targetEmpty bool // whether the target account is empty (no balance/code)
461461
isEIP158 bool // whether EIP-158 rules apply (empty account handling)
462462
isEIP4762 bool // whether EIP-4762 rules apply (Verkle trees)
463+
addWitnessGas bool // whether to add witness gas for EIP-4762
463464
isEIP2929 bool // whether EIP-2929 rules apply (access lists)
464465
isSystemCall bool // whether this is a system call (bypasses some gas costs)
465466
memorySize uint64 // memory size for the operation (triggers memory expansion)
@@ -576,6 +577,12 @@ func testGasCallFuncFuncWithCases(t *testing.T, config *params.ChainConfig, gasC
576577
}
577578
}
578579

580+
// For EIP-4762 (Witnesses gas)
581+
if tc.addWitnessGas && !contract.IsSystemCall {
582+
// Calculated in `touchAddressAndChargeGas` WitnessBranchReadCost + WitnessChunkReadCost = 2100
583+
expectedMultiGas.SafeIncrement(multigas.ResourceKindStorageAccess, 2100)
584+
}
585+
579586
// Call the function
580587
multiGas, _, err := gasCallFunc(evm, contract, stack, mem, tc.memorySize)
581588
if err != nil {
@@ -760,7 +767,7 @@ func TestGasCallCode(t *testing.T) {
760767
}
761768

762769
// CALL decorated with makeCallVariantGasCallEIP2929 gas function test
763-
func TestGasCallEIP2929(t *testing.T) {
770+
func TestCallVariantGasCallEIP2929(t *testing.T) {
764771
testCases := []GasCallFuncTestCase{
765772
{
766773
name: "Cold access to existing account",
@@ -793,3 +800,35 @@ func TestGasCallEIP2929(t *testing.T) {
793800
gasCallEIP2929 = makeCallVariantGasCallEIP2929(gasCall, 1)
794801
testGasCallFuncFuncWithCases(t, params.TestChainConfig, gasCallEIP2929, testCases, false)
795802
}
803+
804+
func TestVariantGasEIP4762(t *testing.T) {
805+
testCases := []GasCallFuncTestCase{
806+
{
807+
name: "EIP4762 non-system call with witness cost",
808+
transfersValue: false,
809+
valueTransferGas: 50000,
810+
targetExists: true,
811+
targetEmpty: false,
812+
isEIP158: true,
813+
isEIP4762: true,
814+
addWitnessGas: true,
815+
isSystemCall: false,
816+
memorySize: 64,
817+
},
818+
{
819+
name: "EIP4762 system call skips witness cost",
820+
transfersValue: false,
821+
valueTransferGas: 50000,
822+
targetExists: true,
823+
targetEmpty: false,
824+
isEIP158: true,
825+
isEIP4762: true,
826+
addWitnessGas: true,
827+
isSystemCall: true,
828+
memorySize: 64,
829+
},
830+
}
831+
832+
gasCallEIP4762 = makeCallVariantGasEIP4762(gasCallCode)
833+
testGasCallFuncFuncWithCases(t, params.TestChainConfig, gasCallEIP4762, testCases, true)
834+
}

core/vm/operations_verkle.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,18 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc {
9393
return multigas.ZeroGas(), 0, err
9494
}
9595
if contract.IsSystemCall {
96-
return multigas.ZeroGas(), gas, nil
96+
return multiGas, gas, nil
9797
}
9898
if _, isPrecompile := evm.precompile(contract.Address()); isPrecompile {
99-
return multigas.ZeroGas(), gas, nil
99+
return multiGas, gas, nil
100100
}
101101
witnessGas := evm.AccessEvents.MessageCallGas(contract.Address())
102102
if witnessGas == 0 {
103103
witnessGas = params.WarmStorageReadCostEIP2929
104104
}
105-
return multiGas, witnessGas + gas, nil
105+
multiGas.SafeIncrement(multigas.ResourceKindStorageAccess, witnessGas)
106+
singleGas, _ := multiGas.SingleGas()
107+
return multiGas, singleGas, nil
106108
}
107109
}
108110

0 commit comments

Comments
 (0)