@@ -253,55 +253,61 @@ var (
253253
254254func makeCallVariantGasCallEIP7702 (oldCalculator gasFunc ) gasFunc {
255255 return func (evm * EVM , contract * Contract , stack * Stack , mem * Memory , memorySize uint64 ) (uint64 , error ) {
256- addr := common .Address (stack .Back (1 ).Bytes20 ())
256+ var (
257+ total uint64 // total dynamic gas used
258+ addr = common .Address (stack .Back (1 ).Bytes20 ())
259+ )
260+
257261 // Check slot presence in the access list
258- warmAccess := evm .StateDB .AddressInAccessList (addr )
259- // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
260- // the cost to charge for cold access, if any, is Cold - Warm
261- coldCost := params .ColdAccountAccessCostEIP2929 - params .WarmStorageReadCostEIP2929
262- if ! warmAccess {
262+ if ! evm .StateDB .AddressInAccessList (addr ) {
263263 evm .StateDB .AddAddressToAccessList (addr )
264+ // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
265+ // the cost to charge for cold access, if any, is Cold - Warm
266+ coldCost := params .ColdAccountAccessCostEIP2929 - params .WarmStorageReadCostEIP2929
264267 // Charge the remaining difference here already, to correctly calculate available
265268 // gas for call
266269 if ! contract .UseGas (coldCost , evm .Config .Tracer , tracing .GasChangeCallStorageColdAccess ) {
267270 return 0 , ErrOutOfGas
268271 }
272+ total += coldCost
269273 }
270274
271275 // Check if code is a delegation and if so, charge for resolution.
272- if addr , ok := types .ParseDelegation (evm .StateDB .GetCode (addr )); ok {
276+ if target , ok := types .ParseDelegation (evm .StateDB .GetCode (addr )); ok {
273277 var cost uint64
274- if evm .StateDB .AddressInAccessList (addr ) {
275- cost + = params .WarmStorageReadCostEIP2929
278+ if evm .StateDB .AddressInAccessList (target ) {
279+ cost = params .WarmStorageReadCostEIP2929
276280 } else {
277- evm .StateDB .AddAddressToAccessList (addr )
278- cost + = params .ColdAccountAccessCostEIP2929
281+ evm .StateDB .AddAddressToAccessList (target )
282+ cost = params .ColdAccountAccessCostEIP2929
279283 }
280284 if ! contract .UseGas (cost , evm .Config .Tracer , tracing .GasChangeCallStorageColdAccess ) {
281285 return 0 , ErrOutOfGas
282286 }
283- coldCost += cost
287+ total += cost
284288 }
289+
285290 // Now call the old calculator, which takes into account
286291 // - create new account
287292 // - transfer value
288293 // - memory expansion
289294 // - 63/64ths rule
290- gas , err := oldCalculator (evm , contract , stack , mem , memorySize )
291- if warmAccess || err != nil {
292- return gas , err
295+ old , err := oldCalculator (evm , contract , stack , mem , memorySize )
296+ if err != nil {
297+ return old , err
293298 }
294- // In case of a cold access, we temporarily add the cold charge back, and also
295- // add it to the returned gas. By adding it to the return, it will be charged
296- // outside of this function, as part of the dynamic gas, and that will make it
297- // also become correctly reported to tracers.
298- contract .Gas += coldCost
299+
300+ // Temporarily add the gas charge back to the contract and return value. By
301+ // adding it to the return, it will be charged outside of this function, as
302+ // part of the dynamic gas. This will ensure it is correctly reported to
303+ // tracers.
304+ contract .Gas += total
299305
300306 var overflow bool
301- if gas , overflow = math .SafeAdd (gas , coldCost ); overflow {
307+ if total , overflow = math .SafeAdd (old , total ); overflow {
302308 return 0 , ErrGasUintOverflow
303309 }
304- return gas , nil
310+ return total , nil
305311 }
306312}
307313
0 commit comments