2024-05-10 13:13:11 -05:00
|
|
|
// Copyright 2024 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2024-10-20 06:41:51 -05:00
|
|
|
gomath "math"
|
|
|
|
|
2024-05-10 13:13:11 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func gasSStore4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
gas := evm.AccessEvents.SlotGas(contract.Address(), stack.peek().Bytes32(), true)
|
|
|
|
if gas == 0 {
|
|
|
|
gas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gasSLoad4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
gas := evm.AccessEvents.SlotGas(contract.Address(), stack.peek().Bytes32(), false)
|
|
|
|
if gas == 0 {
|
|
|
|
gas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gasBalance4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
address := stack.peek().Bytes20()
|
2024-08-30 07:13:02 -05:00
|
|
|
gas := evm.AccessEvents.BasicDataGas(address, false)
|
2024-05-10 13:13:11 -05:00
|
|
|
if gas == 0 {
|
|
|
|
gas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gasExtCodeSize4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
address := stack.peek().Bytes20()
|
|
|
|
if _, isPrecompile := evm.precompile(address); isPrecompile {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2024-08-30 07:13:02 -05:00
|
|
|
gas := evm.AccessEvents.BasicDataGas(address, false)
|
2024-05-10 13:13:11 -05:00
|
|
|
if gas == 0 {
|
|
|
|
gas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
address := stack.peek().Bytes20()
|
|
|
|
if _, isPrecompile := evm.precompile(address); isPrecompile {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
gas := evm.AccessEvents.CodeHashGas(address, false)
|
|
|
|
if gas == 0 {
|
|
|
|
gas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc {
|
|
|
|
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if _, isPrecompile := evm.precompile(contract.Address()); isPrecompile {
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
witnessGas := evm.AccessEvents.MessageCallGas(contract.Address())
|
|
|
|
if witnessGas == 0 {
|
|
|
|
witnessGas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
return witnessGas + gas, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
gasCallEIP4762 = makeCallVariantGasEIP4762(gasCall)
|
|
|
|
gasCallCodeEIP4762 = makeCallVariantGasEIP4762(gasCallCode)
|
|
|
|
gasStaticCallEIP4762 = makeCallVariantGasEIP4762(gasStaticCall)
|
|
|
|
gasDelegateCallEIP4762 = makeCallVariantGasEIP4762(gasDelegateCall)
|
|
|
|
)
|
|
|
|
|
|
|
|
func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
beneficiaryAddr := common.Address(stack.peek().Bytes20())
|
|
|
|
if _, isPrecompile := evm.precompile(beneficiaryAddr); isPrecompile {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
contractAddr := contract.Address()
|
2024-08-30 07:13:02 -05:00
|
|
|
statelessGas := evm.AccessEvents.BasicDataGas(contractAddr, false)
|
2024-05-10 13:13:11 -05:00
|
|
|
if contractAddr != beneficiaryAddr {
|
2024-08-30 07:13:02 -05:00
|
|
|
statelessGas += evm.AccessEvents.BasicDataGas(beneficiaryAddr, false)
|
2024-05-10 13:13:11 -05:00
|
|
|
}
|
|
|
|
// Charge write costs if it transfers value
|
|
|
|
if evm.StateDB.GetBalance(contractAddr).Sign() != 0 {
|
2024-08-30 07:13:02 -05:00
|
|
|
statelessGas += evm.AccessEvents.BasicDataGas(contractAddr, true)
|
2024-05-10 13:13:11 -05:00
|
|
|
if contractAddr != beneficiaryAddr {
|
2024-08-30 07:13:02 -05:00
|
|
|
statelessGas += evm.AccessEvents.BasicDataGas(beneficiaryAddr, true)
|
2024-05-10 13:13:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return statelessGas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
gas, err := gasCodeCopy(evm, contract, stack, mem, memorySize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
codeOffset = stack.Back(1)
|
|
|
|
length = stack.Back(2)
|
|
|
|
)
|
|
|
|
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
|
|
|
|
if overflow {
|
2024-10-20 06:41:51 -05:00
|
|
|
uint64CodeOffset = gomath.MaxUint64
|
2024-05-10 13:13:11 -05:00
|
|
|
}
|
|
|
|
_, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(contract.Code, uint64CodeOffset, length.Uint64())
|
|
|
|
if !contract.IsDeployment {
|
|
|
|
gas += evm.AccessEvents.CodeChunksRangeGas(contract.Address(), copyOffset, nonPaddedCopyLength, uint64(len(contract.Code)), false)
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gasExtCodeCopyEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
|
|
|
// memory expansion first (dynamic part of pre-2929 implementation)
|
|
|
|
gas, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
addr := common.Address(stack.peek().Bytes20())
|
2024-08-30 07:13:02 -05:00
|
|
|
wgas := evm.AccessEvents.BasicDataGas(addr, false)
|
2024-05-10 13:13:11 -05:00
|
|
|
if wgas == 0 {
|
|
|
|
wgas = params.WarmStorageReadCostEIP2929
|
|
|
|
}
|
|
|
|
var overflow bool
|
|
|
|
// We charge (cold-warm), since 'warm' is already charged as constantGas
|
|
|
|
if gas, overflow = math.SafeAdd(gas, wgas); overflow {
|
|
|
|
return 0, ErrGasUintOverflow
|
|
|
|
}
|
|
|
|
return gas, nil
|
|
|
|
}
|