state_test.go 4.03 KB
Newer Older
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// 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.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

17 18
package tests

Taylor Gerring's avatar
Taylor Gerring committed
19
import (
20
	"bufio"
21 22 23
	"bytes"
	"fmt"
	"reflect"
Taylor Gerring's avatar
Taylor Gerring committed
24
	"testing"
25

26
	"github.com/ethereum/go-ethereum/core/vm"
Taylor Gerring's avatar
Taylor Gerring committed
27 28
)

29 30 31 32 33
func TestState(t *testing.T) {
	t.Parallel()

	st := new(testMatcher)
	// Long tests:
34 35 36 37 38 39 40 41
	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`)
42 43 44 45

	// Very time consuming
	st.skipLoad(`^stTimeConsuming/`)

46 47 48
	// Uses 1GB RAM per tested fork
	st.skipLoad(`^stStaticCall/static_Call1MB`)

49 50
	// Broken tests:
	// Expected failures:
51 52 53 54 55 56
	//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")
57

58
	// For Istanbul, older tests were moved into LegacyTests
59 60 61 62 63 64 65 66 67
	for _, dir := range []string{
		stateTestDir,
		legacyStateTestDir,
	} {
		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)
				name := name + "/" + key
68 69 70

				t.Run(key+"/trie", func(t *testing.T) {
					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
71
						_, _, err := test.Run(subtest, vmconfig, false)
72 73 74 75
						return st.checkFailure(t, name+"/trie", err)
					})
				})
				t.Run(key+"/snap", func(t *testing.T) {
76
					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
77 78 79 80
						snaps, statedb, err := test.Run(subtest, vmconfig, true)
						if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
							return err
						}
81
						return st.checkFailure(t, name+"/snap", err)
82
					})
83
				})
84 85 86
			}
		})
	}
87 88
}

89
// Transactions with gasLimit above this value will not get a VM trace on failure.
90
const traceErrorLimit = 400000
91

92
func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
93 94 95
	// Use config from command line arguments.
	config := vm.Config{EVMInterpreter: *testEVM, EWASMInterpreter: *testEWASM}
	err := test(config)
96 97
	if err == nil {
		return
98
	}
99 100

	// Test failed, re-run with tracing enabled.
101 102 103 104
	t.Error(err)
	if gasLimit > traceErrorLimit {
		t.Log("gas limit too high for EVM trace")
		return
105
	}
106 107 108
	buf := new(bytes.Buffer)
	w := bufio.NewWriter(buf)
	tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
109 110
	config.Debug, config.Tracer = true, tracer
	err2 := test(config)
111 112
	if !reflect.DeepEqual(err, err2) {
		t.Errorf("different error for second run: %v", err2)
113
	}
114
	w.Flush()
115 116 117 118
	if buf.Len() == 0 {
		t.Log("no EVM operation logs generated")
	} else {
		t.Log("EVM operation log:\n" + buf.String())
119
	}
120 121
	//t.Logf("EVM output: 0x%x", tracer.Output())
	//t.Logf("EVM error: %v", tracer.Error())
122
}