From 7fa2327931d4662d9872db2485235842ad44d951 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Mon, 6 May 2024 14:22:32 +0200 Subject: [PATCH] a few bug fixes and feedback from Gary Co-authored-by: Gary Rong --- core/state/access_events.go | 10 ++++----- core/state_processor.go | 3 --- core/vm/eips.go | 41 ++----------------------------------- core/vm/gas_table.go | 29 +++++++++++++------------- core/vm/interpreter.go | 2 +- core/vm/jump_table.go | 2 +- 6 files changed, 23 insertions(+), 64 deletions(-) diff --git a/core/state/access_events.go b/core/state/access_events.go index e8f58318af..10b533636f 100644 --- a/core/state/access_events.go +++ b/core/state/access_events.go @@ -130,7 +130,7 @@ func (ae *AccessEvents) ContractCreateInitGas(addr []byte, createSendsValue bool return gas } -// AddTxOrigin adds the member fields of the sender account to the witness, +// AddTxOrigin adds the member fields of the sender account to the access event list, // so that cold accesses are not charged, since they are covered by the 21000 gas. func (ae *AccessEvents) AddTxOrigin(originAddr []byte) { for i := utils.VersionLeafKey; i <= utils.CodeSizeLeafKey; i++ { @@ -138,11 +138,11 @@ func (ae *AccessEvents) AddTxOrigin(originAddr []byte) { } } -// AddTxDestination adds the member fields of the sender account to the witness, +// AddTxDestination adds the member fields of the sender account to the access event list, // so that cold accesses are not charged, since they are covered by the 21000 gas. func (ae *AccessEvents) AddTxDestination(targetAddr []byte, sendsValue bool) { for i := utils.VersionLeafKey; i <= utils.CodeSizeLeafKey; i++ { - ae.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, byte(i), i == utils.VersionLeafKey && sendsValue) + ae.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, byte(i), i == utils.BalanceLeafKey && sendsValue) } } @@ -152,7 +152,7 @@ func (ae *AccessEvents) SlotGas(addr []byte, slot common.Hash, isWrite bool) uin return ae.touchAddressAndChargeGas(addr, *treeIndex, subIndex, isWrite) } -// touchAddressAndChargeGas adds any missing access event to the witness, and returns the cold +// touchAddressAndChargeGas adds any missing access event to the access event list, and returns the cold // access cost to be charged, if need be. func (ae *AccessEvents) touchAddressAndChargeGas(addr []byte, treeIndex uint256.Int, subIndex byte, isWrite bool) uint64 { stemRead, selectorRead, stemWrite, selectorWrite, selectorFill := ae.touchAddress(addr, treeIndex, subIndex, isWrite) @@ -176,7 +176,7 @@ func (ae *AccessEvents) touchAddressAndChargeGas(addr []byte, treeIndex uint256. return gas } -// touchAddress adds any missing access event to the witness. +// touchAddress adds any missing access event to the access event list. func (ae *AccessEvents) touchAddress(addr []byte, treeIndex uint256.Int, subIndex byte, isWrite bool) (bool, bool, bool, bool, bool) { branchKey := newBranchAccessKey(addr, treeIndex) chunkKey := newChunkAccessKey(branchKey, subIndex) diff --git a/core/state_processor.go b/core/state_processor.go index 19389f0797..114844c0a8 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -199,9 +199,6 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *stat } txctx := NewEVMTxContext(msg) vmenv.Reset(txctx, statedb) - if vmenv.ChainConfig().Rules(vmenv.Context.BlockNumber, true, vmenv.Context.Time).IsEIP2929 { - statedb.AddAddressToAccessList(params.BeaconRootsAddress) - } _, _, _ = vmenv.Call(vm.AccountRef(msg.From), *msg.To, msg.Data, 30_000_000, common.U2560) statedb.Finalise(true) } diff --git a/core/vm/eips.go b/core/vm/eips.go index 2af1b17672..a18af46ab7 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -23,7 +23,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/tracing" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" ) @@ -324,42 +323,6 @@ func enable6780(jt *JumpTable) { } } -func opCreateEIP4762(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - if interpreter.readOnly { - return nil, ErrWriteProtection - } - var endowment = scope.Stack.peek() - - contractAddress := crypto.CreateAddress(scope.Contract.Address(), interpreter.evm.StateDB.GetNonce(scope.Contract.Address())) - statelessGas := interpreter.evm.AccessEvents.ContractCreateInitGas(contractAddress.Bytes()[:], endowment.Sign() != 0) - if !scope.Contract.UseGas(statelessGas, interpreter.evm.Config.Tracer, tracing.GasChangeUnspecified) { - return nil, ErrExecutionReverted - } - - return opCreate(pc, interpreter, scope) -} - -func opCreate2EIP4762(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - if interpreter.readOnly { - return nil, ErrWriteProtection - } - var ( - endowment = scope.Stack.Back(0) - offset, size = scope.Stack.Back(1), scope.Stack.Back(2) - salt = scope.Stack.Back(3) - input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) - ) - - codeAndHash := &codeAndHash{code: input} - contractAddress := crypto.CreateAddress2(scope.Contract.Address(), salt.Bytes32(), codeAndHash.Hash().Bytes()) - statelessGas := interpreter.evm.AccessEvents.ContractCreateInitGas(contractAddress.Bytes()[:], endowment.Sign() != 0) - if !scope.Contract.UseGas(statelessGas, interpreter.evm.Config.Tracer, tracing.GasChangeUnspecified) { - return nil, ErrExecutionReverted - } - - return opCreate2(pc, interpreter, scope) -} - func opExtCodeCopyEIP4762(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { var ( stack = scope.Stack @@ -506,7 +469,7 @@ func enable4762(jt *JumpTable) { } jt[CREATE] = &operation{ - execute: opCreateEIP4762, + execute: opCreate, constantGas: params.CreateNGasEip4762, dynamicGas: gasCreateEip3860, minStack: minStack(3, 1), @@ -515,7 +478,7 @@ func enable4762(jt *JumpTable) { } jt[CREATE2] = &operation{ - execute: opCreate2EIP4762, + execute: opCreate2, constantGas: params.CreateNGasEip4762, dynamicGas: gasCreate2Eip3860, minStack: minStack(4, 1), diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index b3cedfd09e..65a2c8826d 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -394,14 +394,6 @@ func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { return 0, ErrGasUintOverflow } - - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) - if err != nil { - return 0, err - } - if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { - return 0, ErrGasUintOverflow - } if evm.chainRules.IsEIP4762 { if transfersValue { gas, overflow = math.SafeAdd(gas, evm.AccessEvents.ValueTransferGas(contract.Address().Bytes()[:], address.Bytes()[:])) @@ -410,6 +402,13 @@ func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize } } } + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) + if err != nil { + return 0, err + } + if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { + return 0, ErrGasUintOverflow + } return gas, nil } @@ -429,13 +428,6 @@ func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memory if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { return 0, ErrGasUintOverflow } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) - if err != nil { - return 0, err - } - if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { - return 0, ErrGasUintOverflow - } if evm.chainRules.IsEIP4762 { address := common.Address(stack.Back(1).Bytes20()) transfersValue := !stack.Back(2).IsZero() @@ -446,6 +438,13 @@ func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memory } } } + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) + if err != nil { + return 0, err + } + if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { + return 0, ErrGasUintOverflow + } return gas, nil } diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 7761790f43..e12362110b 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -100,7 +100,7 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { var table *JumpTable switch { case evm.chainRules.IsVerkle: - // TODO replace with prooper instruction set when fork is specified + // TODO replace with proper instruction set when fork is specified table = &verkleInstructionSet case evm.chainRules.IsCancun: table = &cancunInstructionSet diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index 37e63eaab6..e0e86418ad 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -82,7 +82,7 @@ func validate(jt JumpTable) JumpTable { } func newVerkleInstructionSet() JumpTable { - instructionSet := newShanghaiInstructionSet() + instructionSet := newCancunInstructionSet() enable6780(&instructionSet) enable4762(&instructionSet) return validate(instructionSet)