2023-01-27 07:30:13 -06:00
|
|
|
// Copyright 2023 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 (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-10-31 09:23:51 -05:00
|
|
|
"regexp"
|
2024-12-02 08:18:02 -06:00
|
|
|
"slices"
|
2023-01-27 07:30:13 -06:00
|
|
|
|
2023-11-28 06:54:17 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
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"
|
2023-01-27 07:30:13 -06:00
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
|
|
|
"github.com/urfave/cli/v2"
|
2024-12-02 08:18:02 -06:00
|
|
|
"golang.org/x/exp/maps"
|
2023-01-27 07:30:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var blockTestCommand = &cli.Command{
|
|
|
|
Action: blockTestCmd,
|
|
|
|
Name: "blocktest",
|
2023-11-21 01:56:23 -06:00
|
|
|
Usage: "Executes the given blockchain tests",
|
2024-12-02 08:18:02 -06:00
|
|
|
ArgsUsage: "<path>",
|
|
|
|
Flags: slices.Concat([]cli.Flag{
|
|
|
|
DumpFlag,
|
|
|
|
HumanReadableFlag,
|
|
|
|
RunFlag,
|
|
|
|
WitnessCrossCheckFlag,
|
|
|
|
}, traceFlags),
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func blockTestCmd(ctx *cli.Context) error {
|
2024-12-02 08:18:02 -06:00
|
|
|
path := ctx.Args().First()
|
|
|
|
if len(path) == 0 {
|
|
|
|
return errors.New("path argument required")
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
var (
|
|
|
|
collected = collectJSONFiles(path)
|
|
|
|
results []testResult
|
|
|
|
)
|
|
|
|
for _, fname := range collected {
|
|
|
|
r, err := runBlockTest(ctx, fname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
results = append(results, r...)
|
2023-06-06 03:27:40 -05:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
report(ctx, results)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBlockTest(ctx *cli.Context, fname string) ([]testResult, error) {
|
|
|
|
src, err := os.ReadFile(fname)
|
2023-01-27 07:30:13 -06:00
|
|
|
if err != nil {
|
2024-12-02 08:18:02 -06:00
|
|
|
return nil, err
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
var tests map[string]*tests.BlockTest
|
2023-01-27 07:30:13 -06:00
|
|
|
if err = json.Unmarshal(src, &tests); err != nil {
|
2024-12-02 08:18:02 -06:00
|
|
|
return nil, err
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|
2023-10-31 09:23:51 -05:00
|
|
|
re, err := regexp.Compile(ctx.String(RunFlag.Name))
|
|
|
|
if err != nil {
|
2024-12-02 08:18:02 -06:00
|
|
|
return nil, fmt.Errorf("invalid regex -%s: %v", RunFlag.Name, err)
|
2023-10-31 09:23:51 -05:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
tracer := tracerFromFlags(ctx)
|
2023-10-31 09:23:51 -05:00
|
|
|
|
2024-12-02 08:18:02 -06:00
|
|
|
// Pull out keys to sort and ensure tests are run in order.
|
|
|
|
keys := maps.Keys(tests)
|
|
|
|
slices.Sort(keys)
|
|
|
|
|
|
|
|
// Run all the tests.
|
|
|
|
var results []testResult
|
2023-10-16 13:10:05 -05:00
|
|
|
for _, name := range keys {
|
2023-10-31 09:23:51 -05:00
|
|
|
if !re.MatchString(name) {
|
|
|
|
continue
|
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
result := &testResult{Name: name, Pass: true}
|
|
|
|
if err := tests[name].Run(false, rawdb.HashScheme, ctx.Bool(WitnessCrossCheckFlag.Name), tracer, func(res error, chain *core.BlockChain) {
|
2023-11-28 06:54:17 -06:00
|
|
|
if ctx.Bool(DumpFlag.Name) {
|
2024-12-02 08:18:02 -06:00
|
|
|
if s, _ := chain.State(); s != nil {
|
|
|
|
result.State = dump(s)
|
2023-11-28 06:54:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}); err != nil {
|
2024-12-02 08:18:02 -06:00
|
|
|
result.Pass, result.Error = false, err.Error()
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
results = append(results, *result)
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|
2024-12-02 08:18:02 -06:00
|
|
|
return results, nil
|
2023-01-27 07:30:13 -06:00
|
|
|
}
|