state_test.go 4.39 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

	// Very time consuming
	st.skipLoad(`^stTimeConsuming/`)
45
	st.skipLoad(`.*vmPerformance/loop.*`)
46

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

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
		legacyStateTestDir,
63 64 65 66 67
	} {
		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)
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
						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
							// Ignore expected errors (TODO MariusVanDerWijden check error string)
							return nil
						}
76
						return st.checkFailure(t, err)
77 78 79
					})
				})
				t.Run(key+"/snap", func(t *testing.T) {
80
					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
81
						snaps, statedb, err := test.Run(subtest, vmconfig, true)
82 83 84 85
						if snaps != nil && statedb != nil {
							if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
								return err
							}
86
						}
87 88 89 90
						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
							// Ignore expected errors (TODO MariusVanDerWijden check error string)
							return nil
						}
91
						return st.checkFailure(t, err)
92
					})
93
				})
94 95 96
			}
		})
	}
97 98
}

99
// Transactions with gasLimit above this value will not get a VM trace on failure.
100
const traceErrorLimit = 400000
101

102
func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
103
	// Use config from command line arguments.
104
	config := vm.Config{}
105
	err := test(config)
106 107
	if err == nil {
		return
108
	}
109 110

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