From 356ad96d796513cc4290dcbe93dd35487b7cc11b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gamenik?= Date: Sat, 21 Jun 2025 18:03:00 +0200 Subject: [PATCH] more tests --- core/vm/contract_test.go | 1 - core/vm/eips_test.go | 99 ++++++++++++++++++++++++++++++++++++++++ core/vm/gas_test.go | 37 +++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 core/vm/eips_test.go create mode 100644 core/vm/gas_test.go diff --git a/core/vm/contract_test.go b/core/vm/contract_test.go index e58f7700cc..2544db657f 100644 --- a/core/vm/contract_test.go +++ b/core/vm/contract_test.go @@ -75,7 +75,6 @@ func TestIsCode(t *testing.T) { } destField = [4]uint64{1, 0, 0, 0} - //aField := []byte{0,0,0,0,0} jd := make(map[common.Hash]bitvec) c = Contract{CodeHash: common.Hash{}, jumpdests: jd} c.CodeHash[0] = 1 diff --git a/core/vm/eips_test.go b/core/vm/eips_test.go new file mode 100644 index 0000000000..cfd4427cd3 --- /dev/null +++ b/core/vm/eips_test.go @@ -0,0 +1,99 @@ +// Copyright 2017 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 . + +package vm + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/params" + "github.com/holiman/uint256" +) + +func TestOpChainID(t *testing.T) { + pc := uint64(1) + evm := EVM{} + chainID := big.NewInt(1) + chainConfig := params.ChainConfig{ChainID: chainID} + evm.chainConfig = &chainConfig + interpreter := EVMInterpreter{evm: &evm} + stack := Stack{} + scope := ScopeContext{Stack: &stack} + _, ret2 := opChainID(&pc, &interpreter, &scope) + if ret2 != nil || len(stack.data) != 1 || stack.data[0] != *uint256.NewInt(1) { + t.Errorf("opChainID not successful") + } +} + +func TestOpBaseFee(t *testing.T) { + pc := uint64(1) + baseFee := big.NewInt(1) + context := BlockContext{BaseFee: baseFee} + evm := EVM{Context: context} + interpreter := EVMInterpreter{evm: &evm} + stack := Stack{} + scope := ScopeContext{Stack: &stack} + _, ret2 := opBaseFee(&pc, &interpreter, &scope) + if ret2 != nil || len(stack.data) != 1 || stack.data[0] != *uint256.NewInt(1) { + t.Errorf("opBaseFee not successful") + } +} + +func TestOpBlobBaseFee(t *testing.T) { + pc := uint64(1) + blobBaseFee := big.NewInt(1) + context := BlockContext{BlobBaseFee: blobBaseFee} + evm := EVM{Context: context} + interpreter := EVMInterpreter{evm: &evm} + stack := Stack{} + scope := ScopeContext{Stack: &stack} + _, ret2 := opBlobBaseFee(&pc, &interpreter, &scope) + if ret2 != nil || len(stack.data) != 1 || stack.data[0] != *uint256.NewInt(1) { + t.Errorf("opBlobBaseFee not successful") + } +} + +func TestOpPush1EIP4762(t *testing.T) { + pc := uint64(1) + context := BlockContext{} + evm := EVM{Context: context} + interpreter := EVMInterpreter{evm: &evm} + contract := Contract{Code: []byte{0, 1, 2}} + stack := Stack{} + scope := ScopeContext{Contract: &contract, Stack: &stack} + _, ret2 := opPush1EIP4762(&pc, &interpreter, &scope) + if ret2 != nil || len(stack.data) != 1 || stack.data[0] != *uint256.NewInt(2) { + t.Errorf("opPush1EIP4762 not successful") + } +} + +func TestMakePushEIP4762(t *testing.T) { + ef := makePushEIP4762(4, 8) + + pc := uint64(1) + context := BlockContext{} + evm := EVM{Context: context} + interpreter := EVMInterpreter{evm: &evm} + contract := Contract{Code: []byte{0, 1, 2}, IsDeployment: true} + stack := Stack{} + scope := ScopeContext{Contract: &contract, Stack: &stack} + + _, ret2 := ef(&pc, &interpreter, &scope) + if ret2 != nil || len(stack.data) != 1 || pc != uint64(5) { + t.Errorf("makePushEIP4762 not successful") + } +} diff --git a/core/vm/gas_test.go b/core/vm/gas_test.go new file mode 100644 index 0000000000..30caf072dc --- /dev/null +++ b/core/vm/gas_test.go @@ -0,0 +1,37 @@ +// Copyright 2017 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 . + +package vm + +import ( + "testing" + + "github.com/holiman/uint256" +) + +func TestCallGas(t *testing.T) { + availableGas := uint64(100) + base := uint64(50) + callCost := uint256.NewInt(70) + ret1, ret2 := callGas(false, availableGas, base, callCost) + if ret2 != nil || ret1 != uint64(70) { + t.Errorf("callGas not successful") + } + ret1, ret2 = callGas(true, availableGas, base, callCost) + if ret2 != nil || ret1 != uint64(50) { + t.Errorf("callGas not successful") + } +}