2018-02-14 06:49:11 -06:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-06-10 11:04:56 -05:00
|
|
|
package tests
|
|
|
|
|
2015-06-10 11:34:38 -05:00
|
|
|
import (
|
2019-01-24 04:36:30 -06:00
|
|
|
"bufio"
|
2017-07-11 06:49:14 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-01-28 01:47:19 -06:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-11 06:49:14 -05:00
|
|
|
"reflect"
|
2022-01-28 01:47:19 -06:00
|
|
|
"strings"
|
2015-06-10 11:34:38 -05:00
|
|
|
"testing"
|
2016-10-20 06:36:29 -05:00
|
|
|
|
2022-01-28 01:47:19 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-07-11 06:49:14 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2021-11-25 06:17:09 -06:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers/logger"
|
2015-06-10 11:34:38 -05:00
|
|
|
)
|
|
|
|
|
2017-07-11 06:49:14 -05:00
|
|
|
func TestState(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
st := new(testMatcher)
|
|
|
|
// Long tests:
|
2018-09-28 15:23:47 -05:00
|
|
|
st.slow(`^stAttackTest/ContractCreationSpam`)
|
|
|
|
st.slow(`^stBadOpcode/badOpcodes`)
|
|
|
|
st.slow(`^stPreCompiledContracts/modexp`)
|
|
|
|
st.slow(`^stQuadraticComplexityTest/`)
|
|
|
|
st.slow(`^stStaticCall/static_Call50000`)
|
|
|
|
st.slow(`^stStaticCall/static_Return50000`)
|
|
|
|
st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
|
|
|
|
st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
|
2019-10-02 03:33:51 -05:00
|
|
|
|
|
|
|
// Very time consuming
|
|
|
|
st.skipLoad(`^stTimeConsuming/`)
|
2021-08-25 08:07:58 -05:00
|
|
|
st.skipLoad(`.*vmPerformance/loop.*`)
|
2019-10-02 03:33:51 -05:00
|
|
|
|
2020-05-05 04:27:09 -05:00
|
|
|
// Uses 1GB RAM per tested fork
|
|
|
|
st.skipLoad(`^stStaticCall/static_Call1MB`)
|
2021-08-17 10:30:21 -05:00
|
|
|
|
2017-07-11 06:49:14 -05:00
|
|
|
// Broken tests:
|
|
|
|
// Expected failures:
|
2019-08-15 07:59:46 -05:00
|
|
|
//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
|
|
|
|
//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
|
|
|
|
//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
|
|
|
|
//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
|
|
|
|
//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
|
|
|
|
//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
|
2018-01-04 06:18:30 -06:00
|
|
|
|
2019-10-02 03:33:51 -05:00
|
|
|
// For Istanbul, older tests were moved into LegacyTests
|
2019-11-20 06:54:18 -06:00
|
|
|
for _, dir := range []string{
|
|
|
|
stateTestDir,
|
2021-08-17 10:30:21 -05:00
|
|
|
legacyStateTestDir,
|
2022-01-28 01:47:19 -06:00
|
|
|
benchmarksDir,
|
2019-11-20 06:54:18 -06:00
|
|
|
} {
|
|
|
|
st.walk(t, dir, func(t *testing.T, name string, test *StateTest) {
|
|
|
|
for _, subtest := range test.Subtests() {
|
|
|
|
subtest := subtest
|
|
|
|
key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
|
2020-03-03 01:10:23 -06:00
|
|
|
|
|
|
|
t.Run(key+"/trie", func(t *testing.T) {
|
|
|
|
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
|
2020-05-04 03:54:24 -05:00
|
|
|
_, _, err := test.Run(subtest, vmconfig, false)
|
2021-07-29 07:05:22 -05:00
|
|
|
if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
|
|
|
|
// Ignore expected errors (TODO MariusVanDerWijden check error string)
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-25 15:47:14 -05:00
|
|
|
return st.checkFailure(t, err)
|
2020-03-03 01:10:23 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run(key+"/snap", func(t *testing.T) {
|
2019-11-20 06:54:18 -06:00
|
|
|
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
|
2020-05-04 03:54:24 -05:00
|
|
|
snaps, statedb, err := test.Run(subtest, vmconfig, true)
|
2021-06-18 05:21:34 -05:00
|
|
|
if snaps != nil && statedb != nil {
|
|
|
|
if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-04 03:54:24 -05:00
|
|
|
}
|
2021-07-29 07:05:22 -05:00
|
|
|
if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
|
|
|
|
// Ignore expected errors (TODO MariusVanDerWijden check error string)
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-25 15:47:14 -05:00
|
|
|
return st.checkFailure(t, err)
|
2019-11-20 06:54:18 -06:00
|
|
|
})
|
2019-10-02 03:33:51 -05:00
|
|
|
})
|
2019-11-20 06:54:18 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-10-31 05:32:05 -05:00
|
|
|
}
|
|
|
|
|
2017-07-11 06:49:14 -05:00
|
|
|
// Transactions with gasLimit above this value will not get a VM trace on failure.
|
2018-01-04 06:18:30 -06:00
|
|
|
const traceErrorLimit = 400000
|
2016-10-31 05:32:05 -05:00
|
|
|
|
2017-07-11 06:49:14 -05:00
|
|
|
func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
|
2019-09-11 07:41:22 -05:00
|
|
|
// Use config from command line arguments.
|
2021-07-06 15:03:09 -05:00
|
|
|
config := vm.Config{}
|
2019-09-11 07:41:22 -05:00
|
|
|
err := test(config)
|
2017-07-11 06:49:14 -05:00
|
|
|
if err == nil {
|
|
|
|
return
|
2016-10-31 05:32:05 -05:00
|
|
|
}
|
2019-09-11 07:41:22 -05:00
|
|
|
|
|
|
|
// Test failed, re-run with tracing enabled.
|
2017-07-11 06:49:14 -05:00
|
|
|
t.Error(err)
|
|
|
|
if gasLimit > traceErrorLimit {
|
|
|
|
t.Log("gas limit too high for EVM trace")
|
|
|
|
return
|
2016-10-31 05:32:05 -05:00
|
|
|
}
|
2019-01-24 04:36:30 -06:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := bufio.NewWriter(buf)
|
2021-11-25 06:17:09 -06:00
|
|
|
tracer := logger.NewJSONLogger(&logger.Config{}, w)
|
2019-09-11 07:41:22 -05:00
|
|
|
config.Debug, config.Tracer = true, tracer
|
|
|
|
err2 := test(config)
|
2017-07-11 06:49:14 -05:00
|
|
|
if !reflect.DeepEqual(err, err2) {
|
|
|
|
t.Errorf("different error for second run: %v", err2)
|
2016-10-31 05:32:05 -05:00
|
|
|
}
|
2019-01-24 04:36:30 -06:00
|
|
|
w.Flush()
|
2017-07-11 06:49:14 -05:00
|
|
|
if buf.Len() == 0 {
|
|
|
|
t.Log("no EVM operation logs generated")
|
|
|
|
} else {
|
|
|
|
t.Log("EVM operation log:\n" + buf.String())
|
2016-10-31 05:32:05 -05:00
|
|
|
}
|
2021-05-25 15:47:14 -05:00
|
|
|
// t.Logf("EVM output: 0x%x", tracer.Output())
|
|
|
|
// t.Logf("EVM error: %v", tracer.Error())
|
2016-10-31 05:32:05 -05:00
|
|
|
}
|
2022-01-28 01:47:19 -06:00
|
|
|
|
|
|
|
func BenchmarkEVM(b *testing.B) {
|
|
|
|
// Walk the directory.
|
|
|
|
dir := benchmarksDir
|
|
|
|
dirinfo, err := os.Stat(dir)
|
|
|
|
if os.IsNotExist(err) || !dirinfo.IsDir() {
|
|
|
|
fmt.Fprintf(os.Stderr, "can't find test files in %s, did you clone the evm-benchmarks submodule?\n", dir)
|
|
|
|
b.Skip("missing test files")
|
|
|
|
}
|
|
|
|
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ext := filepath.Ext(path); ext == ".json" {
|
|
|
|
name := filepath.ToSlash(strings.TrimPrefix(strings.TrimSuffix(path, ext), dir+string(filepath.Separator)))
|
|
|
|
b.Run(name, func(b *testing.B) { runBenchmarkFile(b, path) })
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBenchmarkFile(b *testing.B, path string) {
|
|
|
|
m := make(map[string]StateTest)
|
|
|
|
if err := readJSONFile(path, &m); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(m) != 1 {
|
|
|
|
b.Fatal("expected single benchmark in a file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, t := range m {
|
2022-06-13 09:24:45 -05:00
|
|
|
t := t
|
2022-01-28 01:47:19 -06:00
|
|
|
runBenchmark(b, &t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBenchmark(b *testing.B, t *StateTest) {
|
|
|
|
for _, subtest := range t.Subtests() {
|
|
|
|
subtest := subtest
|
|
|
|
key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
|
|
|
|
|
|
|
|
b.Run(key, func(b *testing.B) {
|
|
|
|
vmconfig := vm.Config{}
|
|
|
|
|
|
|
|
config, eips, err := GetChainConfig(subtest.Fork)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vmconfig.ExtraEips = eips
|
|
|
|
block := t.genesis(config).ToBlock(nil)
|
|
|
|
_, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, false)
|
|
|
|
|
|
|
|
var baseFee *big.Int
|
|
|
|
if config.IsLondon(new(big.Int)) {
|
|
|
|
baseFee = t.json.Env.BaseFee
|
|
|
|
if baseFee == nil {
|
|
|
|
// Retesteth uses `0x10` for genesis baseFee. Therefore, it defaults to
|
|
|
|
// parent - 2 : 0xa as the basefee for 'this' context.
|
|
|
|
baseFee = big.NewInt(0x0a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
post := t.json.Post[subtest.Fork][subtest.Index]
|
|
|
|
msg, err := t.json.Tx.toMessage(post, baseFee)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to recover tx with current signer
|
|
|
|
if len(post.TxBytes) != 0 {
|
|
|
|
var ttx types.Transaction
|
|
|
|
err := ttx.UnmarshalBinary(post.TxBytes)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := types.Sender(types.LatestSigner(config), &ttx); err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the EVM.
|
|
|
|
txContext := core.NewEVMTxContext(msg)
|
|
|
|
context := core.NewEVMBlockContext(block.Header(), nil, &t.json.Env.Coinbase)
|
|
|
|
context.GetHash = vmTestBlockHash
|
|
|
|
context.BaseFee = baseFee
|
|
|
|
evm := vm.NewEVM(context, txContext, statedb, config, vmconfig)
|
|
|
|
|
|
|
|
// Create "contract" for sender to cache code analysis.
|
|
|
|
sender := vm.NewContract(vm.AccountRef(msg.From()), vm.AccountRef(msg.From()),
|
|
|
|
nil, 0)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
// Execute the message.
|
|
|
|
snapshot := statedb.Snapshot()
|
|
|
|
_, _, err = evm.Call(sender, *msg.To(), msg.Data(), msg.Gas(), msg.Value())
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
statedb.RevertToSnapshot(snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|