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 (
|
2023-05-23 10:23:17 +00:00
|
|
|
"bufio"
|
2017-09-05 09:24:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-12-05 17:58:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-09-05 09:24:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state/snapshot"
|
2017-09-05 09:24:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2021-11-25 12:17:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers/logger"
|
2017-09-05 09:24:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
2022-06-27 16:22:36 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2017-09-05 09:24:26 +00:00
|
|
|
)
|
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
var stateTestCommand = &cli.Command{
|
2017-09-05 09:24:26 +00:00
|
|
|
Action: stateTestCmd,
|
|
|
|
Name: "statetest",
|
2023-05-23 10:23:17 +00:00
|
|
|
Usage: "Executes the given state tests. Filenames can be fed via standard input (batch mode) or as an argument (one-off execution).",
|
2017-09-05 09:24:26 +00:00
|
|
|
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 {
|
2022-12-05 17:58:32 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Pass bool `json:"pass"`
|
|
|
|
Root *common.Hash `json:"stateRoot,omitempty"`
|
|
|
|
Fork string `json:"fork"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
State *state.Dump `json:"state,omitempty"`
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stateTestCmd(ctx *cli.Context) error {
|
|
|
|
// Configure the EVM logger
|
2021-11-25 12:17:09 +00:00
|
|
|
config := &logger.Config{
|
2022-06-27 16:22:36 +00:00
|
|
|
EnableMemory: !ctx.Bool(DisableMemoryFlag.Name),
|
|
|
|
DisableStack: ctx.Bool(DisableStackFlag.Name),
|
|
|
|
DisableStorage: ctx.Bool(DisableStorageFlag.Name),
|
|
|
|
EnableReturnData: !ctx.Bool(DisableReturnDataFlag.Name),
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
2023-05-23 10:23:17 +00:00
|
|
|
var cfg vm.Config
|
2017-09-05 09:24:26 +00:00
|
|
|
switch {
|
2022-06-27 16:22:36 +00:00
|
|
|
case ctx.Bool(MachineFlag.Name):
|
2023-05-23 10:23:17 +00:00
|
|
|
cfg.Tracer = logger.NewJSONLogger(config, os.Stderr)
|
2017-09-05 09:24:26 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
case ctx.Bool(DebugFlag.Name):
|
2023-05-23 10:23:17 +00:00
|
|
|
cfg.Tracer = logger.NewStructLogger(config)
|
2017-09-05 09:24:26 +00:00
|
|
|
}
|
|
|
|
// Load the test content from the input file
|
2023-05-23 10:23:17 +00:00
|
|
|
if len(ctx.Args().First()) != 0 {
|
|
|
|
return runStateTest(ctx.Args().First(), cfg, ctx.Bool(MachineFlag.Name), ctx.Bool(DumpFlag.Name))
|
|
|
|
}
|
|
|
|
// Read filenames from stdin and execute back-to-back
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
fname := scanner.Text()
|
|
|
|
if len(fname) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := runStateTest(fname, cfg, ctx.Bool(MachineFlag.Name), ctx.Bool(DumpFlag.Name)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runStateTest loads the state-test given by fname, and executes the test.
|
|
|
|
func runStateTest(fname string, cfg vm.Config, jsonOut, dump bool) error {
|
|
|
|
src, err := os.ReadFile(fname)
|
2017-09-05 09:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tests map[string]tests.StateTest
|
2023-05-23 10:23:17 +00:00
|
|
|
if err := json.Unmarshal(src, &tests); err != nil {
|
2017-09-05 09:24:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Iterate over all the tests, run them and aggregate the results
|
|
|
|
results := make([]StatetestResult, 0, len(tests))
|
|
|
|
for key, test := range tests {
|
|
|
|
for _, st := range test.Subtests() {
|
|
|
|
// Run the test and aggregate the result
|
|
|
|
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true}
|
2023-12-08 10:06:01 +00:00
|
|
|
test.Run(st, cfg, false, rawdb.HashScheme, func(err error, snaps *snapshot.Tree, statedb *state.StateDB) {
|
|
|
|
var root common.Hash
|
|
|
|
if statedb != nil {
|
|
|
|
root = statedb.IntermediateRoot(false)
|
2023-08-22 13:37:04 +00:00
|
|
|
result.Root = &root
|
|
|
|
if jsonOut {
|
|
|
|
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
|
|
|
|
}
|
2023-12-08 10:06:01 +00:00
|
|
|
if dump { // Dump any state to aid debugging
|
|
|
|
cpy, _ := state.New(root, statedb.Database(), nil)
|
|
|
|
dump := cpy.RawDump(nil)
|
|
|
|
result.State = &dump
|
|
|
|
}
|
2023-11-10 11:21:51 +00:00
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
if err != nil {
|
2023-11-10 11:21:51 +00:00
|
|
|
// Test failed, mark as so
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
result.Pass, result.Error = false, err.Error()
|
2022-12-05 17:58:32 +00:00
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
})
|
2017-09-05 09:24:26 +00:00
|
|
|
results = append(results, *result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|