github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/cmd/evm/blockrunner.go (about)

     1  // Copyright 2023 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  	"os"
    24  	"regexp"
    25  	"sort"
    26  
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/core/rawdb"
    29  	"github.com/ethereum/go-ethereum/core/tracing"
    30  	"github.com/ethereum/go-ethereum/eth/tracers/logger"
    31  	"github.com/ethereum/go-ethereum/tests"
    32  	"github.com/urfave/cli/v2"
    33  )
    34  
    35  var RunFlag = &cli.StringFlag{
    36  	Name:  "run",
    37  	Value: ".*",
    38  	Usage: "Run only those tests matching the regular expression.",
    39  }
    40  
    41  var blockTestCommand = &cli.Command{
    42  	Action:    blockTestCmd,
    43  	Name:      "blocktest",
    44  	Usage:     "Executes the given blockchain tests",
    45  	ArgsUsage: "<file>",
    46  	Flags:     []cli.Flag{RunFlag},
    47  }
    48  
    49  func blockTestCmd(ctx *cli.Context) error {
    50  	if len(ctx.Args().First()) == 0 {
    51  		return errors.New("path-to-test argument required")
    52  	}
    53  
    54  	var tracer *tracing.Hooks
    55  	// Configure the EVM logger
    56  	if ctx.Bool(MachineFlag.Name) {
    57  		tracer = logger.NewJSONLogger(&logger.Config{
    58  			EnableMemory:     !ctx.Bool(DisableMemoryFlag.Name),
    59  			DisableStack:     ctx.Bool(DisableStackFlag.Name),
    60  			DisableStorage:   ctx.Bool(DisableStorageFlag.Name),
    61  			EnableReturnData: !ctx.Bool(DisableReturnDataFlag.Name),
    62  		}, os.Stderr)
    63  	}
    64  	// Load the test content from the input file
    65  	src, err := os.ReadFile(ctx.Args().First())
    66  	if err != nil {
    67  		return err
    68  	}
    69  	var tests map[string]tests.BlockTest
    70  	if err = json.Unmarshal(src, &tests); err != nil {
    71  		return err
    72  	}
    73  	re, err := regexp.Compile(ctx.String(RunFlag.Name))
    74  	if err != nil {
    75  		return fmt.Errorf("invalid regex -%s: %v", RunFlag.Name, err)
    76  	}
    77  
    78  	// Run them in order
    79  	var keys []string
    80  	for key := range tests {
    81  		keys = append(keys, key)
    82  	}
    83  	sort.Strings(keys)
    84  	for _, name := range keys {
    85  		if !re.MatchString(name) {
    86  			continue
    87  		}
    88  		test := tests[name]
    89  		if err := test.Run(false, rawdb.HashScheme, tracer, func(res error, chain *core.BlockChain) {
    90  			if ctx.Bool(DumpFlag.Name) {
    91  				if state, _ := chain.State(); state != nil {
    92  					fmt.Println(string(state.Dump(nil)))
    93  				}
    94  			}
    95  		}); err != nil {
    96  			return fmt.Errorf("test %v: %w", name, err)
    97  		}
    98  	}
    99  	return nil
   100  }