cmd/evm: Allow stdin and files as sources of bytecode

This commit is contained in:
Nick Johnson 2016-10-19 12:07:28 +01:00
parent 5b262ff5ab
commit 5418d7267a

View file

@ -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,33 @@ 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 {
panic(err)
}
} else {
var err error
hexcode, err = ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
}
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 +176,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,