Skip to content

Commit 65a1c2d

Browse files
authored
core/vm: make gas cost reporting to tracers correct (#22702)
Previously, the makeCallVariantGasCallEIP2929 charged the cold account access cost directly, leading to an incorrect gas cost passed to the tracer from the main execution loop. This change still temporarily charges the cost (to allow for an accurate calculation of the available gas for the call), but then afterwards refunds it and instead returns the correct total gas cost to be then properly charged in the main loop.
1 parent a0a99e6 commit 65a1c2d

2 files changed

Lines changed: 98 additions & 4 deletions

File tree

‎core/vm/operations_acl.go‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,15 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc {
177177
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
178178
addr := common.Address(stack.Back(1).Bytes20())
179179
// Check slot presence in the access list
180-
if !evm.StateDB.AddressInAccessList(addr) {
180+
warmAccess := evm.StateDB.AddressInAccessList(addr)
181+
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
182+
// the cost to charge for cold access, if any, is Cold - Warm
183+
coldCost := ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929
184+
if !warmAccess {
181185
evm.StateDB.AddAddressToAccessList(addr)
182-
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost
183-
if !contract.UseGas(ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929) {
186+
// Charge the remaining difference here already, to correctly calculate available
187+
// gas for call
188+
if !contract.UseGas(coldCost) {
184189
return 0, ErrOutOfGas
185190
}
186191
}
@@ -189,7 +194,16 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc {
189194
// - transfer value
190195
// - memory expansion
191196
// - 63/64ths rule
192-
return oldCalculator(evm, contract, stack, mem, memorySize)
197+
gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
198+
if warmAccess || err != nil {
199+
return gas, err
200+
}
201+
// In case of a cold access, we temporarily add the cold charge back, and also
202+
// add it to the returned gas. By adding it to the return, it will be charged
203+
// outside of this function, as part of the dynamic gas, and that will make it
204+
// also become correctly reported to tracers.
205+
contract.Gas += coldCost
206+
return gas + coldCost, nil
193207
}
194208
}
195209

‎core/vm/runtime/runtime_test.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,3 +608,83 @@ func TestEip2929Cases(t *testing.T) {
608608
"account (cheap)", code)
609609
}
610610
}
611+
612+
// TestColdAccountAccessCost test that the cold account access cost is reported
613+
// correctly
614+
// see: https://github.com/ethereum/go-ethereum/issues/22649
615+
func TestColdAccountAccessCost(t *testing.T) {
616+
for i, tc := range []struct {
617+
code []byte
618+
step int
619+
want uint64
620+
}{
621+
{ // EXTCODEHASH(0xff)
622+
code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.EXTCODEHASH), byte(vm.POP)},
623+
step: 1,
624+
want: 2600,
625+
},
626+
{ // BALANCE(0xff)
627+
code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.BALANCE), byte(vm.POP)},
628+
step: 1,
629+
want: 2600,
630+
},
631+
{ // CALL(0xff)
632+
code: []byte{
633+
byte(vm.PUSH1), 0x0,
634+
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
635+
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALL), byte(vm.POP),
636+
},
637+
step: 7,
638+
want: 2855,
639+
},
640+
{ // CALLCODE(0xff)
641+
code: []byte{
642+
byte(vm.PUSH1), 0x0,
643+
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
644+
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALLCODE), byte(vm.POP),
645+
},
646+
step: 7,
647+
want: 2855,
648+
},
649+
{ // DELEGATECALL(0xff)
650+
code: []byte{
651+
byte(vm.PUSH1), 0x0,
652+
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
653+
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.DELEGATECALL), byte(vm.POP),
654+
},
655+
step: 6,
656+
want: 2855,
657+
},
658+
{ // STATICCALL(0xff)
659+
code: []byte{
660+
byte(vm.PUSH1), 0x0,
661+
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
662+
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.STATICCALL), byte(vm.POP),
663+
},
664+
step: 6,
665+
want: 2855,
666+
},
667+
{ // SELFDESTRUCT(0xff)
668+
code: []byte{
669+
byte(vm.PUSH1), 0xff, byte(vm.SELFDESTRUCT),
670+
},
671+
step: 1,
672+
want: 7600,
673+
},
674+
} {
675+
tracer := vm.NewStructLogger(nil)
676+
Execute(tc.code, nil, &Config{
677+
EVMConfig: vm.Config{
678+
Debug: true,
679+
Tracer: tracer,
680+
},
681+
})
682+
have := tracer.StructLogs()[tc.step].GasCost
683+
if want := tc.want; have != want {
684+
for ii, op := range tracer.StructLogs() {
685+
t.Logf("%d: %v %d", ii, op.OpName(), op.GasCost)
686+
}
687+
t.Fatalf("tescase %d, gas report wrong, step %d, have %d want %d", i, tc.step, have, want)
688+
}
689+
}
690+
}

0 commit comments

Comments
 (0)