2017-09-05 04:24:26 -05: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 05:23:17 -05:00
|
|
|
"bufio"
|
2017-09-05 04:24:26 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-12-02 08:18:02 -06:00
|
|
|
"regexp"
|
|
|
|
"slices"
|
2017-09-05 04:24:26 -05:00
|
|
|
|
2022-12-05 11:58:32 -06: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 14:21:36 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-09-05 04:24:26 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2024-11-08 08:18:42 -06:00
|
|
|
"github.com/ethereum/go-ethereum/internal/flags"
|
2017-09-05 04:24:26 -05:00
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
2022-06-27 11:22:36 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2017-09-05 04:24:26 -05:00
|
|
|
)
|
|
|
|
|
2024-11-08 08:18:42 -06:00
|
|
|
var (
|
|
|
|
forkFlag = &cli.StringFlag{
|
|
|
|
Name: "statetest.fork",
|
2024-12-02 08:18:02 -06:00
|
|
|
Usage: "Only run tests for the specified fork.",
|
2024-11-08 08:18:42 -06:00
|
|
|
Category: flags.VMCategory,
|
|
|
|
}
|
|
|
|
idxFlag = &cli.IntFlag{
|
|
|
|
Name: "statetest.index",
|
2024-12-02 08:18:02 -06:00
|
|
|
Usage: "The index of the subtest to run.",
|
2024-11-08 08:18:42 -06:00
|
|
|
Category: flags.VMCategory,
|
|
|
|
Value: -1, // default to select all subtest indices
|
|
|
|
}
|
|
|
|
)
|
2022-06-27 11:22:36 -05:00
|
|
|
var stateTestCommand = &cli.Command{
|
2017-09-05 04:24:26 -05:00
|
|
|
Action: stateTestCmd,
|
|
|
|
Name: "statetest",
|
2023-05-23 05:23:17 -05: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 04:24:26 -05:00
|
|
|
ArgsUsage: "<file>",
|
2024-12-02 08:18:02 -06:00
|
|
|
Flags: slices.Concat([]cli.Flag{
|
|
|
|
DumpFlag,
|
|
|
|
HumanReadableFlag,
|
|
|
|
RunFlag,
|
|
|
|
}, traceFlags),
|
2017-09-05 04:24:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func stateTestCmd(ctx *cli.Context) error {
|
2024-12-02 08:18:02 -06:00
|
|
|
path := ctx.Args().First()
|
2017-09-05 04:24:26 -05:00
|
|
|
|
2024-12-02 08:18:02 -06:00
|
|
|
// If path is provided, run the tests at that path.
|
|
|
|
if len(path) != 0 {
|
|
|
|
var (
|
|
|
|
collected = collectJSONFiles(path)
|
|
|
|
results []testResult
|
|
|
|
)
|
|
|
|
for _, fname := range collected {
|
|
|
|
r, err := runStateTest(ctx, fname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
results = append(results, r...)
|
|
|
|
}
|
|
|
|
report(ctx, results)
|
|
|
|
return nil
|
2023-05-23 05:23:17 -05:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
// Otherwise, read filenames from stdin and execute back-to-back.
|
2023-05-23 05:23:17 -05:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
fname := scanner.Text()
|
|
|
|
if len(fname) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
results, err := runStateTest(ctx, fname)
|
|
|
|
if err != nil {
|
2023-05-23 05:23:17 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
report(ctx, results)
|
2023-05-23 05:23:17 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runStateTest loads the state-test given by fname, and executes the test.
|
2024-12-02 08:18:02 -06:00
|
|
|
func runStateTest(ctx *cli.Context, fname string) ([]testResult, error) {
|
2023-05-23 05:23:17 -05:00
|
|
|
src, err := os.ReadFile(fname)
|
2017-09-05 04:24:26 -05:00
|
|
|
if err != nil {
|
2024-12-02 08:18:02 -06:00
|
|
|
return nil, err
|
2017-09-05 04:24:26 -05:00
|
|
|
}
|
2024-02-14 10:02:56 -06:00
|
|
|
var testsByName map[string]tests.StateTest
|
|
|
|
if err := json.Unmarshal(src, &testsByName); err != nil {
|
2024-12-02 08:18:02 -06:00
|
|
|
return nil, fmt.Errorf("unable to read test file %s: %w", fname, err)
|
2017-09-05 04:24:26 -05:00
|
|
|
}
|
2024-02-14 10:02:56 -06:00
|
|
|
|
2024-12-02 08:18:02 -06:00
|
|
|
cfg := vm.Config{Tracer: tracerFromFlags(ctx)}
|
|
|
|
re, err := regexp.Compile(ctx.String(RunFlag.Name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid regex -%s: %v", RunFlag.Name, err)
|
|
|
|
}
|
2024-11-08 08:18:42 -06:00
|
|
|
|
2017-09-05 04:24:26 -05:00
|
|
|
// Iterate over all the tests, run them and aggregate the results
|
2024-12-02 08:18:02 -06:00
|
|
|
results := make([]testResult, 0, len(testsByName))
|
|
|
|
for key, test := range testsByName {
|
|
|
|
if !re.MatchString(key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, st := range test.Subtests() {
|
|
|
|
if idx := ctx.Int(idxFlag.Name); idx != -1 && idx != i {
|
|
|
|
// If specific index requested, skip all tests that do not match.
|
|
|
|
continue
|
2024-11-08 08:18:42 -06:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
if fork := ctx.String(forkFlag.Name); fork != "" && st.Fork != fork {
|
|
|
|
// If specific fork requested, skip all tests that do not match.
|
|
|
|
continue
|
2024-11-08 08:18:42 -06:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
// Run the test and aggregate the result
|
|
|
|
result := &testResult{Name: key, Fork: st.Fork, Pass: true}
|
|
|
|
test.Run(st, cfg, false, rawdb.HashScheme, func(err error, state *tests.StateTestState) {
|
|
|
|
var root common.Hash
|
|
|
|
if state.StateDB != nil {
|
|
|
|
root = state.StateDB.IntermediateRoot(false)
|
|
|
|
result.Root = &root
|
|
|
|
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
|
|
|
|
// Dump any state to aid debugging.
|
|
|
|
if ctx.Bool(DumpFlag.Name) {
|
|
|
|
result.State = dump(state.StateDB)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Collect bench stats if requested.
|
|
|
|
if ctx.Bool(BenchFlag.Name) {
|
|
|
|
_, stats, _ := timedExec(true, func() ([]byte, uint64, error) {
|
|
|
|
_, _, gasUsed, _ := test.RunNoVerify(st, cfg, false, rawdb.HashScheme)
|
|
|
|
return nil, gasUsed, nil
|
|
|
|
})
|
|
|
|
result.Stats = &stats
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// Test failed, mark as so.
|
|
|
|
result.Pass, result.Error = false, err.Error()
|
|
|
|
return
|
|
|
|
}
|
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 14:21:36 -05:00
|
|
|
})
|
2024-12-02 08:18:02 -06:00
|
|
|
results = append(results, *result)
|
2017-09-05 04:24:26 -05:00
|
|
|
}
|
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
return results, nil
|
2017-09-05 04:24:26 -05:00
|
|
|
}
|