aboutsummaryrefslogtreecommitdiffstats
path: root/tests/state_test.go
blob: adec4feb2be841a3ceeb7e3fcf55a76fa3d38a8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2015 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 <http://www.gnu.org/licenses/>.

package tests

import (
    "bytes"
    "fmt"
    "reflect"
    "testing"

    "github.com/ethereum/go-ethereum/core/vm"
)

func TestState(t *testing.T) {
    t.Parallel()

    st := new(testMatcher)
    // Long tests:
    st.skipShortMode(`^stQuadraticComplexityTest/`)
    // Broken tests:
    st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
    st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
    // Expected failures:
    st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/EIP158`, "bug in test")
    st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")

    st.walk(t, stateTestDir, 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
            t.Run(key, func(t *testing.T) {
                if subtest.Fork == "Constantinople" {
                    t.Skip("constantinople not supported yet")
                }
                withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
                    _, err := test.Run(subtest, vmconfig)
                    return st.checkFailure(t, name, err)
                })
            })
        }
    })
}

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

func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    err := test(vm.Config{})
    if err == nil {
        return
    }
    t.Error(err)
    if gasLimit > traceErrorLimit {
        t.Log("gas limit too high for EVM trace")
        return
    }
    tracer := vm.NewStructLogger(nil)
    err2 := test(vm.Config{Debug: true, Tracer: tracer})
    if !reflect.DeepEqual(err, err2) {
        t.Errorf("different error for second run: %v", err2)
    }
    buf := new(bytes.Buffer)
    vm.WriteTrace(buf, tracer.StructLogs())
    if buf.Len() == 0 {
        t.Log("no EVM operation logs generated")
    } else {
        t.Log("EVM operation log:\n" + buf.String())
    }
    t.Logf("EVM output: 0x%x", tracer.Output())
    t.Logf("EVM error: %v", tracer.Error())
}