Skip to content

Commit 6333855

Browse files
core: turn gas into a vector <regularGas, stateGas> (#34691)
Pre-refactor PR to get 8037 upstreamed in chunks --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com>
1 parent deda47f commit 6333855

File tree

12 files changed

+278
-225
lines changed

12 files changed

+278
-225
lines changed

core/vm/contract.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Contract struct {
4242
IsDeployment bool
4343
IsSystemCall bool
4444

45-
Gas uint64
45+
Gas GasCosts
4646
value *uint256.Int
4747
}
4848

@@ -56,7 +56,7 @@ func NewContract(caller common.Address, address common.Address, value *uint256.I
5656
caller: caller,
5757
address: address,
5858
jumpDests: jumpDests,
59-
Gas: gas,
59+
Gas: GasCosts{RegularGas: gas},
6060
value: value,
6161
}
6262
}
@@ -127,13 +127,13 @@ func (c *Contract) Caller() common.Address {
127127

128128
// UseGas attempts the use gas and subtracts it and returns true on success
129129
func (c *Contract) UseGas(gas uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) (ok bool) {
130-
if c.Gas < gas {
130+
if c.Gas.RegularGas < gas {
131131
return false
132132
}
133133
if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored {
134-
logger.OnGasChange(c.Gas, c.Gas-gas, reason)
134+
logger.OnGasChange(c.Gas.RegularGas, c.Gas.RegularGas-gas, reason)
135135
}
136-
c.Gas -= gas
136+
c.Gas.RegularGas -= gas
137137
return true
138138
}
139139

@@ -143,9 +143,9 @@ func (c *Contract) RefundGas(gas uint64, logger *tracing.Hooks, reason tracing.G
143143
return
144144
}
145145
if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored {
146-
logger.OnGasChange(c.Gas, c.Gas+gas, reason)
146+
logger.OnGasChange(c.Gas.RegularGas, c.Gas.RegularGas+gas, reason)
147147
}
148-
c.Gas += gas
148+
c.Gas.RegularGas += gas
149149
}
150150

151151
// Address returns the contracts address

core/vm/eips.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func opExtCodeCopyEIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, er
381381
addr := common.Address(a.Bytes20())
382382
code := evm.StateDB.GetCode(addr)
383383
paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64())
384-
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false, scope.Contract.Gas)
384+
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false, scope.Contract.Gas.RegularGas)
385385
scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified)
386386
if consumed < wanted {
387387
return nil, ErrOutOfGas
@@ -407,7 +407,7 @@ func opPush1EIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
407407
// touch next chunk if PUSH1 is at the boundary. if so, *pc has
408408
// advanced past this boundary.
409409
contractAddr := scope.Contract.Address()
410-
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, *pc+1, uint64(1), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas)
410+
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, *pc+1, uint64(1), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas.RegularGas)
411411
scope.Contract.UseGas(wanted, evm.Config.Tracer, tracing.GasChangeUnspecified)
412412
if consumed < wanted {
413413
return nil, ErrOutOfGas
@@ -435,7 +435,7 @@ func makePushEIP4762(size uint64, pushByteSize int) executionFunc {
435435

436436
if !scope.Contract.IsDeployment && !scope.Contract.IsSystemCall {
437437
contractAddr := scope.Contract.Address()
438-
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, uint64(start), uint64(pushByteSize), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas)
438+
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, uint64(start), uint64(pushByteSize), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas.RegularGas)
439439
scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified)
440440
if consumed < wanted {
441441
return nil, ErrOutOfGas

core/vm/evm.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g
303303
contract.IsSystemCall = isSystemCall(caller)
304304
contract.SetCallCode(evm.resolveCodeHash(addr), code)
305305
ret, err = evm.Run(contract, input, false)
306-
gas = contract.Gas
306+
gas = contract.Gas.RegularGas
307307
}
308308
}
309309
// When an error was returned by the EVM or when setting the creation code
@@ -365,7 +365,7 @@ func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byt
365365
contract := NewContract(caller, caller, value, gas, evm.jumpDests)
366366
contract.SetCallCode(evm.resolveCodeHash(addr), evm.resolveCode(addr))
367367
ret, err = evm.Run(contract, input, false)
368-
gas = contract.Gas
368+
gas = contract.Gas.RegularGas
369369
}
370370
if err != nil {
371371
evm.StateDB.RevertToSnapshot(snapshot)
@@ -413,7 +413,7 @@ func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address,
413413
contract := NewContract(originCaller, caller, value, gas, evm.jumpDests)
414414
contract.SetCallCode(evm.resolveCodeHash(addr), evm.resolveCode(addr))
415415
ret, err = evm.Run(contract, input, false)
416-
gas = contract.Gas
416+
gas = contract.Gas.RegularGas
417417
}
418418
if err != nil {
419419
evm.StateDB.RevertToSnapshot(snapshot)
@@ -472,7 +472,7 @@ func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []b
472472
// above we revert to the snapshot and consume any gas remaining. Additionally
473473
// when we're in Homestead this also counts for code storage gas errors.
474474
ret, err = evm.Run(contract, input, true)
475-
gas = contract.Gas
475+
gas = contract.Gas.RegularGas
476476
}
477477
if err != nil {
478478
evm.StateDB.RevertToSnapshot(snapshot)
@@ -583,10 +583,10 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui
583583
if err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas) {
584584
evm.StateDB.RevertToSnapshot(snapshot)
585585
if err != ErrExecutionReverted {
586-
contract.UseGas(contract.Gas, evm.Config.Tracer, tracing.GasChangeCallFailedExecution)
586+
contract.UseGas(contract.Gas.RegularGas, evm.Config.Tracer, tracing.GasChangeCallFailedExecution)
587587
}
588588
}
589-
return ret, address, contract.Gas, err
589+
return ret, address, contract.Gas.RegularGas, err
590590
}
591591

592592
// initNewContract runs a new contract's creation code, performs checks on the
@@ -613,7 +613,7 @@ func (evm *EVM) initNewContract(contract *Contract, address common.Address) ([]b
613613
return ret, ErrCodeStoreOutOfGas
614614
}
615615
} else {
616-
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(address, 0, uint64(len(ret)), uint64(len(ret)), true, contract.Gas)
616+
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(address, 0, uint64(len(ret)), uint64(len(ret)), true, contract.Gas.RegularGas)
617617
contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk)
618618
if len(ret) > 0 && (consumed < wanted) {
619619
return ret, ErrCodeStoreOutOfGas

0 commit comments

Comments
 (0)