Skip to content

Commit

Permalink
vm: Rename stateTransition gas to gasRemaining (#10025)
Browse files Browse the repository at this point in the history
The `StateTransition` property `gas` actually tracks the remaining gas
in the current context. This PR is to improve code readability.
Geth also uses similar naming.
  • Loading branch information
somnathb1 authored Apr 23, 2024
1 parent 6457ef3 commit de39ac4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
44 changes: 22 additions & 22 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ The state transitioning model does all the necessary work to work out a valid ne
6) Derive new state root
*/
type StateTransition struct {
gp *GasPool
msg Message
gas uint64
gasPrice *uint256.Int
gasFeeCap *uint256.Int
tip *uint256.Int
initialGas uint64
value *uint256.Int
data []byte
state evmtypes.IntraBlockState
evm *vm.EVM
gp *GasPool
msg Message
gasRemaining uint64
gasPrice *uint256.Int
gasFeeCap *uint256.Int
tip *uint256.Int
initialGas uint64
value *uint256.Int
data []byte
state evmtypes.IntraBlockState
evm *vm.EVM

//some pre-allocated intermediate variables
sharedBuyGas *uint256.Int
Expand Down Expand Up @@ -254,7 +254,7 @@ func (st *StateTransition) buyGas(gasBailout bool) error {
return err
}
}
st.gas += st.msg.Gas()
st.gasRemaining += st.msg.Gas()
st.initialGas = st.msg.Gas()

if subBalance {
Expand Down Expand Up @@ -370,7 +370,7 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*Executi
if st.evm.Config().Debug {
st.evm.Config().Tracer.CaptureTxStart(st.initialGas)
defer func() {
st.evm.Config().Tracer.CaptureTxEnd(st.gas)
st.evm.Config().Tracer.CaptureTxEnd(st.gasRemaining)
}()
}

Expand All @@ -386,10 +386,10 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*Executi
if err != nil {
return nil, err
}
if st.gas < gas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gas, gas)
if st.gasRemaining < gas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas)
}
st.gas -= gas
st.gasRemaining -= gas

var bailout bool
// Gas bailout (for trace_call) should only be applied if there is not sufficient balance to perform value transfer
Expand Down Expand Up @@ -418,11 +418,11 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*Executi
// nonce to calculate the address of the contract that is being created
// It does get incremented inside the `Create` call, after the computation
// of the contract's address, but before the execution of the code.
ret, _, st.gas, vmerr = st.evm.Create(sender, st.data, st.gas, st.value)
ret, _, st.gasRemaining, vmerr = st.evm.Create(sender, st.data, st.gasRemaining, st.value)
} else {
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
ret, st.gas, vmerr = st.evm.Call(sender, st.to(), st.data, st.gas, st.value, bailout)
ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), st.data, st.gasRemaining, st.value, bailout)
}
if refunds {
if rules.IsLondon {
Expand Down Expand Up @@ -483,18 +483,18 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
if refund > st.state.GetRefund() {
refund = st.state.GetRefund()
}
st.gas += refund
st.gasRemaining += refund

// Return ETH for remaining gas, exchanged at the original rate.
remaining := new(uint256.Int).Mul(new(uint256.Int).SetUint64(st.gas), st.gasPrice)
remaining := new(uint256.Int).Mul(new(uint256.Int).SetUint64(st.gasRemaining), st.gasPrice)
st.state.AddBalance(st.msg.From(), remaining)

// Also return remaining gas to the block gas counter so it is
// available for the next transaction.
st.gp.AddGas(st.gas)
st.gp.AddGas(st.gasRemaining)
}

// gasUsed returns the amount of gas used up by the state transition.
func (st *StateTransition) gasUsed() uint64 {
return st.initialGas - st.gas
return st.initialGas - st.gasRemaining
}
26 changes: 13 additions & 13 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,20 @@ func (evm *EVM) OverlayCreate(caller ContractRef, codeAndHash *codeAndHash, gas
}

// create creates a new contract using code as deployment code.
func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, value *uint256.Int, address libcommon.Address, typ OpCode, incrementNonce bool) ([]byte, libcommon.Address, uint64, error) {
func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gasRemaining uint64, value *uint256.Int, address libcommon.Address, typ OpCode, incrementNonce bool) ([]byte, libcommon.Address, uint64, error) {
var ret []byte
var err error
var gasConsumption uint64
depth := evm.interpreter.Depth()

if evm.config.Debug {
if depth == 0 {
evm.config.Tracer.CaptureStart(evm, caller.Address(), address, false /* precompile */, true /* create */, codeAndHash.code, gas, value, nil)
evm.config.Tracer.CaptureStart(evm, caller.Address(), address, false /* precompile */, true /* create */, codeAndHash.code, gasRemaining, value, nil)
defer func() {
evm.config.Tracer.CaptureEnd(ret, gasConsumption, err)
}()
} else {
evm.config.Tracer.CaptureEnter(typ, caller.Address(), address, false /* precompile */, true /* create */, codeAndHash.code, gas, value, nil)
evm.config.Tracer.CaptureEnter(typ, caller.Address(), address, false /* precompile */, true /* create */, codeAndHash.code, gasRemaining, value, nil)
defer func() {
evm.config.Tracer.CaptureExit(ret, gasConsumption, err)
}()
Expand All @@ -362,17 +362,17 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// limit.
if depth > int(params.CallCreateDepth) {
err = ErrDepth
return nil, libcommon.Address{}, gas, err
return nil, libcommon.Address{}, gasRemaining, err
}
if !evm.Context.CanTransfer(evm.intraBlockState, caller.Address(), value) {
err = ErrInsufficientBalance
return nil, libcommon.Address{}, gas, err
return nil, libcommon.Address{}, gasRemaining, err
}
if incrementNonce {
nonce := evm.intraBlockState.GetNonce(caller.Address())
if nonce+1 < nonce {
err = ErrNonceUintOverflow
return nil, libcommon.Address{}, gas, err
return nil, libcommon.Address{}, gasRemaining, err
}
evm.intraBlockState.SetNonce(caller.Address(), nonce+1)
}
Expand All @@ -397,11 +397,11 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,

// Initialise a new contract and set the code that is to be used by the EVM.
// The contract is a scoped environment for this execution context only.
contract := NewContract(caller, address, value, gas, evm.config.SkipAnalysis)
contract := NewContract(caller, address, value, gasRemaining, evm.config.SkipAnalysis)
contract.SetCodeOptionalHash(&address, codeAndHash)

if evm.config.NoRecursion && depth > 0 {
return nil, address, gas, nil
return nil, address, gasRemaining, nil
}

ret, err = run(evm, contract, nil, false)
Expand Down Expand Up @@ -443,27 +443,27 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
}

// calculate gasConsumption for deferred captures
gasConsumption = gas - contract.Gas
gasConsumption = gasRemaining - contract.Gas

return ret, address, contract.Gas, err
}

// Create creates a new contract using code as deployment code.
// DESCRIBED: docs/programmers_guide/guide.md#nonce
func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, endowment *uint256.Int) (ret []byte, contractAddr libcommon.Address, leftOverGas uint64, err error) {
func (evm *EVM) Create(caller ContractRef, code []byte, gasRemaining uint64, endowment *uint256.Int) (ret []byte, contractAddr libcommon.Address, leftOverGas uint64, err error) {
contractAddr = crypto.CreateAddress(caller.Address(), evm.intraBlockState.GetNonce(caller.Address()))
return evm.create(caller, &codeAndHash{code: code}, gas, endowment, contractAddr, CREATE, true /* incrementNonce */)
return evm.create(caller, &codeAndHash{code: code}, gasRemaining, endowment, contractAddr, CREATE, true /* incrementNonce */)
}

// Create2 creates a new contract using code as deployment code.
//
// The different between Create2 with Create is Create2 uses keccak256(0xff ++ msg.sender ++ salt ++ keccak256(init_code))[12:]
// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
// DESCRIBED: docs/programmers_guide/guide.md#nonce
func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *uint256.Int, salt *uint256.Int) (ret []byte, contractAddr libcommon.Address, leftOverGas uint64, err error) {
func (evm *EVM) Create2(caller ContractRef, code []byte, gasRemaining uint64, endowment *uint256.Int, salt *uint256.Int) (ret []byte, contractAddr libcommon.Address, leftOverGas uint64, err error) {
codeAndHash := &codeAndHash{code: code}
contractAddr = crypto.CreateAddress2(caller.Address(), salt.Bytes32(), codeAndHash.Hash().Bytes())
return evm.create(caller, codeAndHash, gas, endowment, contractAddr, CREATE2, true /* incrementNonce */)
return evm.create(caller, codeAndHash, gasRemaining, endowment, contractAddr, CREATE2, true /* incrementNonce */)
}

// SysCreate is a special (system) contract creation methods for genesis constructors.
Expand Down

0 comments on commit de39ac4

Please sign in to comment.