From 00665a0b72ed93692daec21bbd79931828653228 Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Sat, 29 Oct 2016 10:07:38 +0100 Subject: cmd/evm: Allow stdin and files as sources of bytecode (#3172) * cmd/evm: Allow stdin and files as sources of bytecode * cmd/evm: Print and exit instead of panicing * cmd/evm: fix compile and vet errors --- cmd/evm/main.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/evm/main.go b/cmd/evm/main.go index 9ca761b7b..704469432 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -19,6 +19,7 @@ package main import ( "fmt" + "io/ioutil" "math/big" "os" "runtime" @@ -58,6 +59,10 @@ var ( Name: "code", Usage: "EVM code", } + CodeFileFlag = cli.StringFlag{ + Name: "codefile", + Usage: "file containing EVM code", + } GasFlag = cli.StringFlag{ Name: "gas", Usage: "gas limit for the evm", @@ -104,6 +109,7 @@ func init() { DisableJitFlag, SysStatFlag, CodeFlag, + CodeFileFlag, GasFlag, PriceFlag, ValueFlag, @@ -133,12 +139,35 @@ func run(ctx *cli.Context) error { tstart := time.Now() var ( - ret []byte - err error + code []byte + ret []byte + err error ) + if ctx.GlobalString(CodeFlag.Name) != "" { + code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)) + } else { + var hexcode []byte + if ctx.GlobalString(CodeFileFlag.Name) != "" { + var err error + hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name)) + if err != nil { + fmt.Printf("Could not load code from file: %v\n", err) + os.Exit(1) + } + } else { + var err error + hexcode, err = ioutil.ReadAll(os.Stdin) + if err != nil { + fmt.Printf("Could not load code from stdin: %v\n", err) + os.Exit(1) + } + } + code = common.Hex2Bytes(string(hexcode[:])) + } + if ctx.GlobalBool(CreateFlag.Name) { - input := append(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...) + input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...) ret, _, err = vmenv.Create( sender, input, @@ -149,7 +178,6 @@ func run(ctx *cli.Context) error { } else { receiver := statedb.CreateAccount(common.StringToAddress("receiver")) - code := common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)) receiver.SetCode(crypto.Keccak256Hash(code), code) ret, err = vmenv.Call( sender, -- cgit v1.2.3