2017-09-05 09:24:26 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-27 13:51:49 +00:00
|
|
|
"context"
|
2017-09-05 09:24:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
2020-05-27 16:24:34 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/tests"
|
2017-09-05 09:24:26 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-09-05 09:24:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var stateTestCommand = cli.Command{
|
|
|
|
Action: stateTestCmd,
|
|
|
|
Name: "statetest",
|
|
|
|
Usage: "executes the given state tests",
|
|
|
|
ArgsUsage: "<file>",
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:38:03 +00:00
|
|
|
// StatetestResult contains the execution status after running a state test, any
|
|
|
|
// error that might have occurred and a dump of the final state if requested.
|
2017-09-05 09:24:26 +00:00
|
|
|
type StatetestResult struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Pass bool `json:"pass"`
|
|
|
|
Fork string `json:"fork"`
|
2019-05-27 13:51:49 +00:00
|
|
|
Error *string `json:"error,omitempty"`
|
2017-09-05 09:24:26 +00:00
|
|
|
State *state.Dump `json:"state,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func stateTestCmd(ctx *cli.Context) error {
|
|
|
|
if len(ctx.Args().First()) == 0 {
|
|
|
|
return errors.New("path-to-test argument required")
|
|
|
|
}
|
|
|
|
// Configure the go-ethereum logger
|
|
|
|
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
|
|
|
|
glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
|
|
|
|
log.Root().SetHandler(glogger)
|
|
|
|
|
|
|
|
// Configure the EVM logger
|
|
|
|
config := &vm.LogConfig{
|
2020-07-16 12:06:19 +00:00
|
|
|
DisableMemory: ctx.GlobalBool(DisableMemoryFlag.Name),
|
|
|
|
DisableStack: ctx.GlobalBool(DisableStackFlag.Name),
|
|
|
|
DisableStorage: ctx.GlobalBool(DisableStorageFlag.Name),
|
|
|
|
DisableReturnData: ctx.GlobalBool(DisableReturnDataFlag.Name),
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
tracer vm.Tracer
|
|
|
|
debugger *vm.StructLogger
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case ctx.GlobalBool(MachineFlag.Name):
|
2018-10-15 10:28:44 +00:00
|
|
|
tracer = vm.NewJSONLogger(config, os.Stderr)
|
2017-09-05 09:24:26 +00:00
|
|
|
|
|
|
|
case ctx.GlobalBool(DebugFlag.Name):
|
|
|
|
debugger = vm.NewStructLogger(config)
|
|
|
|
tracer = debugger
|
|
|
|
|
|
|
|
default:
|
|
|
|
debugger = vm.NewStructLogger(config)
|
|
|
|
}
|
|
|
|
// Load the test content from the input file
|
|
|
|
src, err := ioutil.ReadFile(ctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tests map[string]tests.StateTest
|
|
|
|
if err = json.Unmarshal(src, &tests); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Iterate over all the tests, run them and aggregate the results
|
|
|
|
cfg := vm.Config{
|
|
|
|
Tracer: tracer,
|
|
|
|
Debug: ctx.GlobalBool(DebugFlag.Name) || ctx.GlobalBool(MachineFlag.Name),
|
|
|
|
}
|
|
|
|
results := make([]StatetestResult, 0, len(tests))
|
|
|
|
for key, test := range tests {
|
|
|
|
for _, st := range test.Subtests() {
|
|
|
|
// Run the test and aggregate the result
|
2019-05-27 13:51:49 +00:00
|
|
|
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true, Error: new(string)}
|
2020-05-19 10:28:20 +00:00
|
|
|
statedb, tds, err := test.Run(context.Background(), st, cfg)
|
2018-10-04 08:22:41 +00:00
|
|
|
// print state root for evmlab tracing
|
2019-05-27 13:51:49 +00:00
|
|
|
if ctx.GlobalBool(MachineFlag.Name) && statedb != nil {
|
2020-05-19 10:28:20 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", tds.Trie().Root())
|
2018-10-04 08:22:41 +00:00
|
|
|
}
|
2017-10-20 10:22:06 +00:00
|
|
|
if err != nil {
|
2017-09-05 09:24:26 +00:00
|
|
|
// Test failed, mark as so and dump any state to aid debugging
|
2019-05-27 13:51:49 +00:00
|
|
|
result.Pass, *result.Error = false, err.Error()
|
2020-04-23 09:35:43 +00:00
|
|
|
if ctx.GlobalBool(DumpFlag.Name) && statedb != nil {
|
2021-02-10 17:04:22 +00:00
|
|
|
tx, err1 := tds.Database().(ethdb.HasKV).KV().Begin(context.Background(), ethdb.RO)
|
2020-10-12 08:39:04 +00:00
|
|
|
if err1 != nil {
|
|
|
|
return fmt.Errorf("transition cannot open tx: %v", err1)
|
2020-05-27 16:24:34 +00:00
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
dump := state.NewDumper(tx, tds.GetBlockNr()).DefaultRawDump()
|
|
|
|
tx.Rollback()
|
|
|
|
result.State = &dump
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-20 10:22:06 +00:00
|
|
|
|
2017-09-05 09:24:26 +00:00
|
|
|
results = append(results, *result)
|
|
|
|
|
|
|
|
// Print any structured logs collected
|
|
|
|
if ctx.GlobalBool(DebugFlag.Name) {
|
|
|
|
if debugger != nil {
|
2018-08-01 16:09:08 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "#### TRACE ####")
|
|
|
|
vm.WriteTrace(os.Stderr, debugger.StructLogs())
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 09:25:33 +00:00
|
|
|
tds.Database().Close()
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out, _ := json.MarshalIndent(results, "", " ")
|
2018-08-01 16:09:08 +00:00
|
|
|
fmt.Println(string(out))
|
2017-09-05 09:24:26 +00:00
|
|
|
return nil
|
|
|
|
}
|