review feedback

This commit is contained in:
Guillaume Ballet 2026-04-23 15:19:57 +02:00
parent f3f80f222d
commit 20bb7028d3
No known key found for this signature in database
6 changed files with 61 additions and 57 deletions

View file

@ -71,7 +71,7 @@ func memoryCopierGas(stackpos int) gasFunc {
return GasCosts{}, err return GasCosts{}, err
} }
// And gas for copying data, charged per word at param.CopyGas // And gas for copying data, charged per word at param.CopyGas
words, overflow := stack.Back(stackpos).Uint64WithOverflow() words, overflow := stack.back(stackpos).Uint64WithOverflow()
if overflow { if overflow {
return GasCosts{}, ErrGasUintOverflow return GasCosts{}, ErrGasUintOverflow
} }
@ -100,7 +100,7 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi
return GasCosts{}, ErrWriteProtection return GasCosts{}, ErrWriteProtection
} }
var ( var (
y, x = stack.Back(1), stack.Back(0) y, x = stack.back(1), stack.back(0)
current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32()) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32())
) )
// The legacy gas metering only takes into consideration the current state // The legacy gas metering only takes into consideration the current state
@ -192,7 +192,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
} }
// Gas sentry honoured, do the actual gas calculation based on the stored value // Gas sentry honoured, do the actual gas calculation based on the stored value
var ( var (
y, x = stack.Back(1), stack.Back(0) y, x = stack.back(1), stack.back(0)
current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32()) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32())
) )
value := common.Hash(y.Bytes32()) value := common.Hash(y.Bytes32())
@ -228,7 +228,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
func makeGasLog(n uint64) gasFunc { func makeGasLog(n uint64) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) {
requestedSize, overflow := stack.Back(1).Uint64WithOverflow() requestedSize, overflow := stack.back(1).Uint64WithOverflow()
if overflow { if overflow {
return GasCosts{}, ErrGasUintOverflow return GasCosts{}, ErrGasUintOverflow
} }
@ -261,7 +261,7 @@ func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }
wordGas, overflow := stack.Back(1).Uint64WithOverflow() wordGas, overflow := stack.back(1).Uint64WithOverflow()
if overflow { if overflow {
return GasCosts{}, ErrGasUintOverflow return GasCosts{}, ErrGasUintOverflow
} }
@ -299,7 +299,7 @@ func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memoryS
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }
wordGas, overflow := stack.Back(2).Uint64WithOverflow() wordGas, overflow := stack.back(2).Uint64WithOverflow()
if overflow { if overflow {
return GasCosts{}, ErrGasUintOverflow return GasCosts{}, ErrGasUintOverflow
} }
@ -317,7 +317,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }
size, overflow := stack.Back(2).Uint64WithOverflow() size, overflow := stack.back(2).Uint64WithOverflow()
if overflow { if overflow {
return GasCosts{}, ErrGasUintOverflow return GasCosts{}, ErrGasUintOverflow
} }
@ -336,7 +336,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }
size, overflow := stack.Back(2).Uint64WithOverflow() size, overflow := stack.back(2).Uint64WithOverflow()
if overflow { if overflow {
return GasCosts{}, ErrGasUintOverflow return GasCosts{}, ErrGasUintOverflow
} }
@ -352,7 +352,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
} }
func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) {
expByteLen := uint64((stack.Back(1).BitLen() + 7) / 8) expByteLen := uint64((stack.back(1).BitLen() + 7) / 8)
var ( var (
gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas
@ -365,7 +365,7 @@ func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem
} }
func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) {
expByteLen := uint64((stack.Back(1).BitLen() + 7) / 8) expByteLen := uint64((stack.back(1).BitLen() + 7) / 8)
var ( var (
gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas
@ -390,7 +390,7 @@ func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc {
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }
evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.Back(0)) evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.back(0))
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }
@ -405,8 +405,8 @@ func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc {
func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) {
var ( var (
gas uint64 gas uint64
transfersValue = !stack.Back(2).IsZero() transfersValue = !stack.back(2).IsZero()
address = common.Address(stack.Back(1).Bytes20()) address = common.Address(stack.back(1).Bytes20())
) )
if evm.readOnly && transfersValue { if evm.readOnly && transfersValue {
return GasCosts{}, ErrWriteProtection return GasCosts{}, ErrWriteProtection
@ -453,7 +453,7 @@ func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memor
gas uint64 gas uint64
overflow bool overflow bool
) )
if stack.Back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 { if stack.back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 {
gas += params.CallValueTransferGas gas += params.CallValueTransferGas
} }
if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
@ -487,7 +487,7 @@ func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me
// EIP150 homestead gas reprice fork: // EIP150 homestead gas reprice fork:
if evm.chainRules.IsEIP150 { if evm.chainRules.IsEIP150 {
gas = params.SelfdestructGasEIP150 gas = params.SelfdestructGasEIP150
var address = common.Address(stack.Back(0).Bytes20()) var address = common.Address(stack.back(0).Bytes20())
if evm.chainRules.IsEIP158 { if evm.chainRules.IsEIP158 {
// if empty and transfers value // if empty and transfers value

View file

@ -1003,7 +1003,7 @@ func opDupN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
} }
//The nth stack item is duplicated at the top of the stack. //The nth stack item is duplicated at the top of the stack.
scope.Stack.push(scope.Stack.Back(n - 1)) scope.Stack.push(scope.Stack.back(n - 1))
*pc += 1 *pc += 1
return nil, nil return nil, nil
} }
@ -1032,7 +1032,7 @@ func opSwapN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
// The (n+1)th stack item is swapped with the top of the stack. // The (n+1)th stack item is swapped with the top of the stack.
top := scope.Stack.peek() top := scope.Stack.peek()
nth := scope.Stack.Back(n) nth := scope.Stack.back(n)
*top, *nth = *nth, *top *top, *nth = *nth, *top
*pc += 1 *pc += 1
return nil, nil return nil, nil
@ -1064,8 +1064,8 @@ func opExchange(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
} }
// The (n+1)th stack item is swapped with the (m+1)th stack item. // The (n+1)th stack item is swapped with the (m+1)th stack item.
nth := scope.Stack.Back(n) nth := scope.Stack.back(n)
mth := scope.Stack.Back(m) mth := scope.Stack.back(m)
*nth, *mth = *mth, *nth *nth, *mth = *mth, *nth
*pc += 1 *pc += 1
return nil, nil return nil, nil

View file

@ -17,59 +17,59 @@
package vm package vm
func memoryKeccak256(stack *Stack) (uint64, bool) { func memoryKeccak256(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(1)) return calcMemSize64(stack.back(0), stack.back(1))
} }
func memoryCallDataCopy(stack *Stack) (uint64, bool) { func memoryCallDataCopy(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(2)) return calcMemSize64(stack.back(0), stack.back(2))
} }
func memoryReturnDataCopy(stack *Stack) (uint64, bool) { func memoryReturnDataCopy(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(2)) return calcMemSize64(stack.back(0), stack.back(2))
} }
func memoryCodeCopy(stack *Stack) (uint64, bool) { func memoryCodeCopy(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(2)) return calcMemSize64(stack.back(0), stack.back(2))
} }
func memoryExtCodeCopy(stack *Stack) (uint64, bool) { func memoryExtCodeCopy(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(1), stack.Back(3)) return calcMemSize64(stack.back(1), stack.back(3))
} }
func memoryMLoad(stack *Stack) (uint64, bool) { func memoryMLoad(stack *Stack) (uint64, bool) {
return calcMemSize64WithUint(stack.Back(0), 32) return calcMemSize64WithUint(stack.back(0), 32)
} }
func memoryMStore8(stack *Stack) (uint64, bool) { func memoryMStore8(stack *Stack) (uint64, bool) {
return calcMemSize64WithUint(stack.Back(0), 1) return calcMemSize64WithUint(stack.back(0), 1)
} }
func memoryMStore(stack *Stack) (uint64, bool) { func memoryMStore(stack *Stack) (uint64, bool) {
return calcMemSize64WithUint(stack.Back(0), 32) return calcMemSize64WithUint(stack.back(0), 32)
} }
func memoryMcopy(stack *Stack) (uint64, bool) { func memoryMcopy(stack *Stack) (uint64, bool) {
mStart := stack.Back(0) // stack[0]: dest mStart := stack.back(0) // stack[0]: dest
if stack.Back(1).Gt(mStart) { if stack.back(1).Gt(mStart) {
mStart = stack.Back(1) // stack[1]: source mStart = stack.back(1) // stack[1]: source
} }
return calcMemSize64(mStart, stack.Back(2)) // stack[2]: length return calcMemSize64(mStart, stack.back(2)) // stack[2]: length
} }
func memoryCreate(stack *Stack) (uint64, bool) { func memoryCreate(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(1), stack.Back(2)) return calcMemSize64(stack.back(1), stack.back(2))
} }
func memoryCreate2(stack *Stack) (uint64, bool) { func memoryCreate2(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(1), stack.Back(2)) return calcMemSize64(stack.back(1), stack.back(2))
} }
func memoryCall(stack *Stack) (uint64, bool) { func memoryCall(stack *Stack) (uint64, bool) {
x, overflow := calcMemSize64(stack.Back(5), stack.Back(6)) x, overflow := calcMemSize64(stack.back(5), stack.back(6))
if overflow { if overflow {
return 0, true return 0, true
} }
y, overflow := calcMemSize64(stack.Back(3), stack.Back(4)) y, overflow := calcMemSize64(stack.back(3), stack.back(4))
if overflow { if overflow {
return 0, true return 0, true
} }
@ -80,11 +80,11 @@ func memoryCall(stack *Stack) (uint64, bool) {
} }
func memoryDelegateCall(stack *Stack) (uint64, bool) { func memoryDelegateCall(stack *Stack) (uint64, bool) {
x, overflow := calcMemSize64(stack.Back(4), stack.Back(5)) x, overflow := calcMemSize64(stack.back(4), stack.back(5))
if overflow { if overflow {
return 0, true return 0, true
} }
y, overflow := calcMemSize64(stack.Back(2), stack.Back(3)) y, overflow := calcMemSize64(stack.back(2), stack.back(3))
if overflow { if overflow {
return 0, true return 0, true
} }
@ -95,11 +95,11 @@ func memoryDelegateCall(stack *Stack) (uint64, bool) {
} }
func memoryStaticCall(stack *Stack) (uint64, bool) { func memoryStaticCall(stack *Stack) (uint64, bool) {
x, overflow := calcMemSize64(stack.Back(4), stack.Back(5)) x, overflow := calcMemSize64(stack.back(4), stack.back(5))
if overflow { if overflow {
return 0, true return 0, true
} }
y, overflow := calcMemSize64(stack.Back(2), stack.Back(3)) y, overflow := calcMemSize64(stack.back(2), stack.back(3))
if overflow { if overflow {
return 0, true return 0, true
} }
@ -110,13 +110,13 @@ func memoryStaticCall(stack *Stack) (uint64, bool) {
} }
func memoryReturn(stack *Stack) (uint64, bool) { func memoryReturn(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(1)) return calcMemSize64(stack.back(0), stack.back(1))
} }
func memoryRevert(stack *Stack) (uint64, bool) { func memoryRevert(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(1)) return calcMemSize64(stack.back(0), stack.back(1))
} }
func memoryLog(stack *Stack) (uint64, bool) { func memoryLog(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(1)) return calcMemSize64(stack.back(0), stack.back(1))
} }

View file

@ -37,7 +37,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
} }
// Gas sentry honoured, do the actual gas calculation based on the stored value // Gas sentry honoured, do the actual gas calculation based on the stored value
var ( var (
y, x = stack.Back(1), stack.peek() y, x = stack.back(1), stack.peek()
slot = common.Hash(x.Bytes32()) slot = common.Hash(x.Bytes32())
current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), slot) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), slot)
cost = uint64(0) cost = uint64(0)
@ -158,7 +158,7 @@ func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Mem
func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc { func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) {
addr := common.Address(stack.Back(addressPosition).Bytes20()) addr := common.Address(stack.back(addressPosition).Bytes20())
// Check slot presence in the access list // Check slot presence in the access list
warmAccess := evm.StateDB.AddressInAccessList(addr) warmAccess := evm.StateDB.AddressInAccessList(addr)
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
@ -269,7 +269,7 @@ func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem
// Although it's checked in `gasCall`, EIP-7702 loads the target's code before // Although it's checked in `gasCall`, EIP-7702 loads the target's code before
// to determine if it is resolving a delegation. This could incorrectly record // to determine if it is resolving a delegation. This could incorrectly record
// the target in the block access list (BAL) if the call later fails. // the target in the block access list (BAL) if the call later fails.
transfersValue := !stack.Back(2).IsZero() transfersValue := !stack.back(2).IsZero()
if evm.readOnly && transfersValue { if evm.readOnly && transfersValue {
return GasCosts{}, ErrWriteProtection return GasCosts{}, ErrWriteProtection
} }
@ -281,7 +281,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc {
var ( var (
eip2929Cost uint64 eip2929Cost uint64
eip7702Cost uint64 eip7702Cost uint64
addr = common.Address(stack.Back(1).Bytes20()) addr = common.Address(stack.back(1).Bytes20())
) )
// Perform EIP-2929 checks (stateless), checking address presence // Perform EIP-2929 checks (stateless), checking address presence
// in the accessList and charge the cold access accordingly. // in the accessList and charge the cold access accordingly.
@ -330,7 +330,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc {
} }
// Calculate the gas budget for the nested call. The costs defined by // Calculate the gas budget for the nested call. The costs defined by
// EIP-2929 and EIP-7702 have already been applied. // EIP-2929 and EIP-7702 have already been applied.
evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.Back(0)) evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.back(0))
if err != nil { if err != nil {
return GasCosts{}, err return GasCosts{}, err
} }

View file

@ -56,7 +56,7 @@ func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) gasFunc { func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) gasFunc {
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) {
var ( var (
target = common.Address(stack.Back(1).Bytes20()) target = common.Address(stack.back(1).Bytes20())
witnessGas uint64 witnessGas uint64
_, isPrecompile = evm.precompile(target) _, isPrecompile = evm.precompile(target)
isSystemContract = target == params.HistoryStorageAddress isSystemContract = target == params.HistoryStorageAddress
@ -64,7 +64,7 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) ga
// If value is transferred, it is charged before 1/64th // If value is transferred, it is charged before 1/64th
// is subtracted from the available gas pool. // is subtracted from the available gas pool.
if withTransferCosts && !stack.Back(2).IsZero() { if withTransferCosts && !stack.back(2).IsZero() {
wantedValueTransferWitnessGas := evm.AccessEvents.ValueTransferGas(contract.Address(), target, contract.Gas.RegularGas) wantedValueTransferWitnessGas := evm.AccessEvents.ValueTransferGas(contract.Address(), target, contract.Gas.RegularGas)
if wantedValueTransferWitnessGas > contract.Gas.RegularGas { if wantedValueTransferWitnessGas > contract.Gas.RegularGas {
return GasCosts{RegularGas: wantedValueTransferWitnessGas}, nil return GasCosts{RegularGas: wantedValueTransferWitnessGas}, nil
@ -168,8 +168,8 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
gas := gasCost.RegularGas gas := gasCost.RegularGas
if !contract.IsDeployment && !contract.IsSystemCall { if !contract.IsDeployment && !contract.IsSystemCall {
var ( var (
codeOffset = stack.Back(1) codeOffset = stack.back(1)
length = stack.Back(2) length = stack.back(2)
) )
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
if overflow { if overflow {

View file

@ -33,10 +33,15 @@ func newArena() *stackArena {
return stackPool.New().(*stackArena) return stackPool.New().(*stackArena)
} }
// 1025, because in stack() there is a condition check
// for the stack size that would fail if it was set to
// 1024.
const initialStackSize = 1025
var stackPool = sync.Pool{ var stackPool = sync.Pool{
New: func() any { New: func() any {
return &stackArena{ return &stackArena{
data: make([]uint256.Int, 1025), data: make([]uint256.Int, initialStackSize),
} }
}, },
} }
@ -93,9 +98,8 @@ func (s *Stack) Data() []uint256.Int {
} }
func (s *Stack) push(d *uint256.Int) { func (s *Stack) push(d *uint256.Int) {
s.inner.data[s.inner.top] = *d elem := s.get()
s.inner.top++ *elem = *d
s.size++
} }
// get returns a pointer to a newly created element // get returns a pointer to a newly created element
@ -176,7 +180,7 @@ func (s *Stack) peek() *uint256.Int {
return &s.inner.data[s.bottom+s.size-1] return &s.inner.data[s.bottom+s.size-1]
} }
// Back returns the n'th item in stack // back returns the n'th item in stack
func (s *Stack) Back(n int) *uint256.Int { func (s *Stack) back(n int) *uint256.Int {
return &s.inner.data[s.bottom+s.size-n-1] return &s.inner.data[s.bottom+s.size-n-1]
} }