Skip to content

Commit 8cab3ab

Browse files
holimankaralabe
authored andcommitted
cmd/evm: adds ability to run individual state test file (#14998)
* cmd/evm: adds ability to run individual state test file * cmd/evm: Fix statetest runner to be more json friendly * cmd/evm, tests: minor polishes, dump state on fail
1 parent 504278e commit 8cab3ab

4 files changed

Lines changed: 128 additions & 7 deletions

File tree

‎cmd/evm/main.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func init() {
143143
compileCommand,
144144
disasmCommand,
145145
runCommand,
146+
stateTestCommand,
146147
}
147148
}
148149

‎cmd/evm/staterunner.go‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum 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+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"encoding/json"
21+
"errors"
22+
"fmt"
23+
"io/ioutil"
24+
"os"
25+
26+
"github.com/ethereum/go-ethereum/core/state"
27+
"github.com/ethereum/go-ethereum/core/vm"
28+
"github.com/ethereum/go-ethereum/log"
29+
"github.com/ethereum/go-ethereum/tests"
30+
31+
cli "gopkg.in/urfave/cli.v1"
32+
)
33+
34+
var stateTestCommand = cli.Command{
35+
Action: stateTestCmd,
36+
Name: "statetest",
37+
Usage: "executes the given state tests",
38+
ArgsUsage: "<file>",
39+
}
40+
41+
type StatetestResult struct {
42+
Name string `json:"name"`
43+
Pass bool `json:"pass"`
44+
Fork string `json:"fork"`
45+
Error string `json:"error,omitempty"`
46+
State *state.Dump `json:"state,omitempty"`
47+
}
48+
49+
func stateTestCmd(ctx *cli.Context) error {
50+
if len(ctx.Args().First()) == 0 {
51+
return errors.New("path-to-test argument required")
52+
}
53+
// Configure the go-ethereum logger
54+
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
55+
glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
56+
log.Root().SetHandler(glogger)
57+
58+
// Configure the EVM logger
59+
config := &vm.LogConfig{
60+
DisableMemory: ctx.GlobalBool(DisableMemoryFlag.Name),
61+
DisableStack: ctx.GlobalBool(DisableStackFlag.Name),
62+
}
63+
var (
64+
tracer vm.Tracer
65+
debugger *vm.StructLogger
66+
)
67+
switch {
68+
case ctx.GlobalBool(MachineFlag.Name):
69+
tracer = NewJSONLogger(config, os.Stderr)
70+
71+
case ctx.GlobalBool(DebugFlag.Name):
72+
debugger = vm.NewStructLogger(config)
73+
tracer = debugger
74+
75+
default:
76+
debugger = vm.NewStructLogger(config)
77+
}
78+
// Load the test content from the input file
79+
src, err := ioutil.ReadFile(ctx.Args().First())
80+
if err != nil {
81+
return err
82+
}
83+
var tests map[string]tests.StateTest
84+
if err = json.Unmarshal(src, &tests); err != nil {
85+
return err
86+
}
87+
// Iterate over all the tests, run them and aggregate the results
88+
cfg := vm.Config{
89+
Tracer: tracer,
90+
Debug: ctx.GlobalBool(DebugFlag.Name) || ctx.GlobalBool(MachineFlag.Name),
91+
}
92+
results := make([]StatetestResult, 0, len(tests))
93+
for key, test := range tests {
94+
for _, st := range test.Subtests() {
95+
// Run the test and aggregate the result
96+
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true}
97+
if state, err := test.Run(st, cfg); err != nil {
98+
// Test failed, mark as so and dump any state to aid debugging
99+
result.Pass, result.Error = false, err.Error()
100+
if ctx.GlobalBool(DumpFlag.Name) && state != nil {
101+
dump := state.RawDump()
102+
result.State = &dump
103+
}
104+
}
105+
results = append(results, *result)
106+
107+
// Print any structured logs collected
108+
if ctx.GlobalBool(DebugFlag.Name) {
109+
if debugger != nil {
110+
fmt.Fprintln(os.Stderr, "#### TRACE ####")
111+
vm.WriteTrace(os.Stderr, debugger.StructLogs())
112+
}
113+
}
114+
}
115+
}
116+
out, _ := json.MarshalIndent(results, "", " ")
117+
fmt.Println(string(out))
118+
return nil
119+
}

‎tests/state_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func TestState(t *testing.T) {
5050
t.Skip("constantinople not supported yet")
5151
}
5252
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
53-
return st.checkFailure(t, name, test.Run(subtest, vmconfig))
53+
_, err := test.Run(subtest, vmconfig)
54+
return st.checkFailure(t, name, err)
5455
})
5556
})
5657
}

‎tests/state_test_util.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ func (t *StateTest) Subtests() []StateSubtest {
120120
}
121121

122122
// Run executes a specific subtest.
123-
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) error {
123+
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) (*state.StateDB, error) {
124124
config, ok := Forks[subtest.Fork]
125125
if !ok {
126-
return UnsupportedForkError{subtest.Fork}
126+
return nil, UnsupportedForkError{subtest.Fork}
127127
}
128128
block, _ := t.genesis(config).ToBlock()
129129
db, _ := ethdb.NewMemDatabase()
@@ -132,7 +132,7 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) error {
132132
post := t.json.Post[subtest.Fork][subtest.Index]
133133
msg, err := t.json.Tx.toMessage(post)
134134
if err != nil {
135-
return err
135+
return nil, err
136136
}
137137
context := core.NewEVMContext(msg, block.Header(), nil, &t.json.Env.Coinbase)
138138
context.GetHash = vmTestBlockHash
@@ -145,13 +145,13 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) error {
145145
statedb.RevertToSnapshot(snapshot)
146146
}
147147
if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) {
148-
return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
148+
return statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
149149
}
150150
root, _ := statedb.CommitTo(db, config.IsEIP158(block.Number()))
151151
if root != common.Hash(post.Root) {
152-
return fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
152+
return statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
153153
}
154-
return nil
154+
return statedb, nil
155155
}
156156

157157
func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {

0 commit comments

Comments
 (0)