github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/cmd/evm/main.go (about)

     1  // Copyright 2014 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  // evm executes EVM code snippets.
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"math/big"
    23  	"os"
    24  
    25  	"github.com/tacshi/go-ethereum/cmd/evm/internal/t8ntool"
    26  	"github.com/tacshi/go-ethereum/internal/flags"
    27  	"github.com/urfave/cli/v2"
    28  )
    29  
    30  var (
    31  	DebugFlag = &cli.BoolFlag{
    32  		Name:  "debug",
    33  		Usage: "output full trace logs",
    34  	}
    35  	MemProfileFlag = &cli.StringFlag{
    36  		Name:  "memprofile",
    37  		Usage: "creates a memory profile at the given path",
    38  	}
    39  	CPUProfileFlag = &cli.StringFlag{
    40  		Name:  "cpuprofile",
    41  		Usage: "creates a CPU profile at the given path",
    42  	}
    43  	StatDumpFlag = &cli.BoolFlag{
    44  		Name:  "statdump",
    45  		Usage: "displays stack and heap memory information",
    46  	}
    47  	CodeFlag = &cli.StringFlag{
    48  		Name:  "code",
    49  		Usage: "EVM code",
    50  	}
    51  	CodeFileFlag = &cli.StringFlag{
    52  		Name:  "codefile",
    53  		Usage: "File containing EVM code. If '-' is specified, code is read from stdin ",
    54  	}
    55  	GasFlag = &cli.Uint64Flag{
    56  		Name:  "gas",
    57  		Usage: "gas limit for the evm",
    58  		Value: 10000000000,
    59  	}
    60  	PriceFlag = &flags.BigFlag{
    61  		Name:  "price",
    62  		Usage: "price set for the evm",
    63  		Value: new(big.Int),
    64  	}
    65  	ValueFlag = &flags.BigFlag{
    66  		Name:  "value",
    67  		Usage: "value set for the evm",
    68  		Value: new(big.Int),
    69  	}
    70  	DumpFlag = &cli.BoolFlag{
    71  		Name:  "dump",
    72  		Usage: "dumps the state after the run",
    73  	}
    74  	InputFlag = &cli.StringFlag{
    75  		Name:  "input",
    76  		Usage: "input for the EVM",
    77  	}
    78  	InputFileFlag = &cli.StringFlag{
    79  		Name:  "inputfile",
    80  		Usage: "file containing input for the EVM",
    81  	}
    82  	VerbosityFlag = &cli.IntFlag{
    83  		Name:  "verbosity",
    84  		Usage: "sets the verbosity level",
    85  	}
    86  	BenchFlag = &cli.BoolFlag{
    87  		Name:  "bench",
    88  		Usage: "benchmark the execution",
    89  	}
    90  	CreateFlag = &cli.BoolFlag{
    91  		Name:  "create",
    92  		Usage: "indicates the action should be create rather than call",
    93  	}
    94  	GenesisFlag = &cli.StringFlag{
    95  		Name:  "prestate",
    96  		Usage: "JSON file with prestate (genesis) config",
    97  	}
    98  	MachineFlag = &cli.BoolFlag{
    99  		Name:  "json",
   100  		Usage: "output trace logs in machine readable format (json)",
   101  	}
   102  	SenderFlag = &cli.StringFlag{
   103  		Name:  "sender",
   104  		Usage: "The transaction origin",
   105  	}
   106  	ReceiverFlag = &cli.StringFlag{
   107  		Name:  "receiver",
   108  		Usage: "The transaction receiver (execution context)",
   109  	}
   110  	DisableMemoryFlag = &cli.BoolFlag{
   111  		Name:  "nomemory",
   112  		Value: true,
   113  		Usage: "disable memory output",
   114  	}
   115  	DisableStackFlag = &cli.BoolFlag{
   116  		Name:  "nostack",
   117  		Usage: "disable stack output",
   118  	}
   119  	DisableStorageFlag = &cli.BoolFlag{
   120  		Name:  "nostorage",
   121  		Usage: "disable storage output",
   122  	}
   123  	DisableReturnDataFlag = &cli.BoolFlag{
   124  		Name:  "noreturndata",
   125  		Value: true,
   126  		Usage: "enable return data output",
   127  	}
   128  )
   129  
   130  var stateTransitionCommand = &cli.Command{
   131  	Name:    "transition",
   132  	Aliases: []string{"t8n"},
   133  	Usage:   "executes a full state transition",
   134  	Action:  t8ntool.Transition,
   135  	Flags: []cli.Flag{
   136  		t8ntool.TraceFlag,
   137  		t8ntool.TraceDisableMemoryFlag,
   138  		t8ntool.TraceEnableMemoryFlag,
   139  		t8ntool.TraceDisableStackFlag,
   140  		t8ntool.TraceDisableReturnDataFlag,
   141  		t8ntool.TraceEnableReturnDataFlag,
   142  		t8ntool.OutputBasedir,
   143  		t8ntool.OutputAllocFlag,
   144  		t8ntool.OutputResultFlag,
   145  		t8ntool.OutputBodyFlag,
   146  		t8ntool.InputAllocFlag,
   147  		t8ntool.InputEnvFlag,
   148  		t8ntool.InputTxsFlag,
   149  		t8ntool.ForknameFlag,
   150  		t8ntool.ChainIDFlag,
   151  		t8ntool.RewardFlag,
   152  		t8ntool.VerbosityFlag,
   153  	},
   154  }
   155  
   156  var transactionCommand = &cli.Command{
   157  	Name:    "transaction",
   158  	Aliases: []string{"t9n"},
   159  	Usage:   "performs transaction validation",
   160  	Action:  t8ntool.Transaction,
   161  	Flags: []cli.Flag{
   162  		t8ntool.InputTxsFlag,
   163  		t8ntool.ChainIDFlag,
   164  		t8ntool.ForknameFlag,
   165  		t8ntool.VerbosityFlag,
   166  	},
   167  }
   168  
   169  var blockBuilderCommand = &cli.Command{
   170  	Name:    "block-builder",
   171  	Aliases: []string{"b11r"},
   172  	Usage:   "builds a block",
   173  	Action:  t8ntool.BuildBlock,
   174  	Flags: []cli.Flag{
   175  		t8ntool.OutputBasedir,
   176  		t8ntool.OutputBlockFlag,
   177  		t8ntool.InputHeaderFlag,
   178  		t8ntool.InputOmmersFlag,
   179  		t8ntool.InputWithdrawalsFlag,
   180  		t8ntool.InputTxsRlpFlag,
   181  		t8ntool.SealCliqueFlag,
   182  		t8ntool.SealEthashFlag,
   183  		t8ntool.SealEthashDirFlag,
   184  		t8ntool.SealEthashModeFlag,
   185  		t8ntool.VerbosityFlag,
   186  	},
   187  }
   188  
   189  var app = flags.NewApp("the evm command line interface")
   190  
   191  func init() {
   192  	app.Flags = []cli.Flag{
   193  		BenchFlag,
   194  		CreateFlag,
   195  		DebugFlag,
   196  		VerbosityFlag,
   197  		CodeFlag,
   198  		CodeFileFlag,
   199  		GasFlag,
   200  		PriceFlag,
   201  		ValueFlag,
   202  		DumpFlag,
   203  		InputFlag,
   204  		InputFileFlag,
   205  		MemProfileFlag,
   206  		CPUProfileFlag,
   207  		StatDumpFlag,
   208  		GenesisFlag,
   209  		MachineFlag,
   210  		SenderFlag,
   211  		ReceiverFlag,
   212  		DisableMemoryFlag,
   213  		DisableStackFlag,
   214  		DisableStorageFlag,
   215  		DisableReturnDataFlag,
   216  	}
   217  	app.Commands = []*cli.Command{
   218  		compileCommand,
   219  		disasmCommand,
   220  		runCommand,
   221  		blockTestCommand,
   222  		stateTestCommand,
   223  		stateTransitionCommand,
   224  		transactionCommand,
   225  		blockBuilderCommand,
   226  	}
   227  }
   228  
   229  func main() {
   230  	if err := app.Run(os.Args); err != nil {
   231  		code := 1
   232  		if ec, ok := err.(*t8ntool.NumberedError); ok {
   233  			code = ec.ExitCode()
   234  		}
   235  		fmt.Fprintln(os.Stderr, err)
   236  		os.Exit(code)
   237  	}
   238  }