github.com/calmw/ethereum@v0.1.1/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  
    25  	"github.com/calmw/ethereum/log"
    26  	"github.com/calmw/ethereum/tests"
    27  	"github.com/urfave/cli/v2"
    28  )
    29  
    30  var blockTestCommand = &cli.Command{
    31  	Action:    blockTestCmd,
    32  	Name:      "blocktest",
    33  	Usage:     "executes the given blockchain tests",
    34  	ArgsUsage: "<file>",
    35  }
    36  
    37  func blockTestCmd(ctx *cli.Context) error {
    38  	if len(ctx.Args().First()) == 0 {
    39  		return errors.New("path-to-test argument required")
    40  	}
    41  	// Configure the go-ethereum logger
    42  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
    43  	glogger.Verbosity(log.Lvl(ctx.Int(VerbosityFlag.Name)))
    44  	log.Root().SetHandler(glogger)
    45  
    46  	// Load the test content from the input file
    47  	src, err := os.ReadFile(ctx.Args().First())
    48  	if err != nil {
    49  		return err
    50  	}
    51  	var tests map[string]tests.BlockTest
    52  	if err = json.Unmarshal(src, &tests); err != nil {
    53  		return err
    54  	}
    55  	for i, test := range tests {
    56  		if err := test.Run(false); err != nil {
    57  			return fmt.Errorf("test %v: %w", i, err)
    58  		}
    59  	}
    60  	return nil
    61  }