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
This commit is contained in:
		
				
					committed by
					
						
						Péter Szilágyi
					
				
			
			
				
	
			
			
			
						parent
						
							b59c8399fb
						
					
				
				
					commit
					00665a0b72
				
			@@ -19,6 +19,7 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io/ioutil"
 | 
				
			||||||
	"math/big"
 | 
						"math/big"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
@@ -58,6 +59,10 @@ var (
 | 
				
			|||||||
		Name:  "code",
 | 
							Name:  "code",
 | 
				
			||||||
		Usage: "EVM code",
 | 
							Usage: "EVM code",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						CodeFileFlag = cli.StringFlag{
 | 
				
			||||||
 | 
							Name:  "codefile",
 | 
				
			||||||
 | 
							Usage: "file containing EVM code",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	GasFlag = cli.StringFlag{
 | 
						GasFlag = cli.StringFlag{
 | 
				
			||||||
		Name:  "gas",
 | 
							Name:  "gas",
 | 
				
			||||||
		Usage: "gas limit for the evm",
 | 
							Usage: "gas limit for the evm",
 | 
				
			||||||
@@ -104,6 +109,7 @@ func init() {
 | 
				
			|||||||
		DisableJitFlag,
 | 
							DisableJitFlag,
 | 
				
			||||||
		SysStatFlag,
 | 
							SysStatFlag,
 | 
				
			||||||
		CodeFlag,
 | 
							CodeFlag,
 | 
				
			||||||
 | 
							CodeFileFlag,
 | 
				
			||||||
		GasFlag,
 | 
							GasFlag,
 | 
				
			||||||
		PriceFlag,
 | 
							PriceFlag,
 | 
				
			||||||
		ValueFlag,
 | 
							ValueFlag,
 | 
				
			||||||
@@ -133,12 +139,35 @@ func run(ctx *cli.Context) error {
 | 
				
			|||||||
	tstart := time.Now()
 | 
						tstart := time.Now()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
 | 
							code []byte
 | 
				
			||||||
		ret  []byte
 | 
							ret  []byte
 | 
				
			||||||
		err  error
 | 
							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) {
 | 
						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(
 | 
							ret, _, err = vmenv.Create(
 | 
				
			||||||
			sender,
 | 
								sender,
 | 
				
			||||||
			input,
 | 
								input,
 | 
				
			||||||
@@ -149,7 +178,6 @@ func run(ctx *cli.Context) error {
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
 | 
							receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		code := common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
 | 
					 | 
				
			||||||
		receiver.SetCode(crypto.Keccak256Hash(code), code)
 | 
							receiver.SetCode(crypto.Keccak256Hash(code), code)
 | 
				
			||||||
		ret, err = vmenv.Call(
 | 
							ret, err = vmenv.Call(
 | 
				
			||||||
			sender,
 | 
								sender,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user