state_test.go 4.6 KB
Newer Older
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
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,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
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/>.
F
Felix Lange 已提交
16

T
Taylor Gerring 已提交
17 18
package tests

T
Taylor Gerring 已提交
19
import (
20
	"bufio"
21 22 23
	"bytes"
	"fmt"
	"reflect"
T
Taylor Gerring 已提交
24
	"testing"
25

26
	"github.com/ethereum/go-ethereum/core/vm"
T
Taylor Gerring 已提交
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
	// Uses 1GB RAM per tested fork
	st.skipLoad(`^stStaticCall/static_Call1MB`)
48 49
	// Un-skip this when https://github.com/ethereum/tests/issues/908 is closed
	st.skipLoad(`^stQuadraticComplexityTest/QuadraticComplexitySolidity_CallDataCopy`)
50 51
	// Broken tests:
	// Expected failures:
52 53 54 55 56 57
	//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")
58

59
	// For Istanbul, older tests were moved into LegacyTests
60 61
	for _, dir := range []string{
		stateTestDir,
62 63 64
		// legacy state tests are disabled, due to them not being
		// regenerated for the no-sender-eoa change.
		//legacyStateTestDir,
65 66 67 68 69
	} {
		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)
70 71 72

				t.Run(key+"/trie", func(t *testing.T) {
					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
73
						_, _, err := test.Run(subtest, vmconfig, false)
74 75 76 77
						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
							// Ignore expected errors (TODO MariusVanDerWijden check error string)
							return nil
						}
78
						return st.checkFailure(t, err)
79 80 81
					})
				})
				t.Run(key+"/snap", func(t *testing.T) {
82
					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
83
						snaps, statedb, err := test.Run(subtest, vmconfig, true)
84 85 86 87
						if snaps != nil && statedb != nil {
							if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
								return err
							}
88
						}
89 90 91 92
						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
							// Ignore expected errors (TODO MariusVanDerWijden check error string)
							return nil
						}
93
						return st.checkFailure(t, err)
94
					})
95
				})
96 97 98
			}
		})
	}
J
Jeffrey Wilcke 已提交
99 100
}

101
// Transactions with gasLimit above this value will not get a VM trace on failure.
102
const traceErrorLimit = 400000
J
Jeffrey Wilcke 已提交
103

104
func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
105
	// Use config from command line arguments.
106
	config := vm.Config{}
107
	err := test(config)
108 109
	if err == nil {
		return
J
Jeffrey Wilcke 已提交
110
	}
111 112

	// Test failed, re-run with tracing enabled.
113 114 115 116
	t.Error(err)
	if gasLimit > traceErrorLimit {
		t.Log("gas limit too high for EVM trace")
		return
J
Jeffrey Wilcke 已提交
117
	}
118 119 120
	buf := new(bytes.Buffer)
	w := bufio.NewWriter(buf)
	tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
121 122
	config.Debug, config.Tracer = true, tracer
	err2 := test(config)
123 124
	if !reflect.DeepEqual(err, err2) {
		t.Errorf("different error for second run: %v", err2)
J
Jeffrey Wilcke 已提交
125
	}
126
	w.Flush()
127 128 129 130
	if buf.Len() == 0 {
		t.Log("no EVM operation logs generated")
	} else {
		t.Log("EVM operation log:\n" + buf.String())
J
Jeffrey Wilcke 已提交
131
	}
132 133
	// t.Logf("EVM output: 0x%x", tracer.Output())
	// t.Logf("EVM error: %v", tracer.Error())
J
Jeffrey Wilcke 已提交
134
}