Skip to content

Commit 00665a0

Browse files
Arachnidkaralabe
authored andcommitted
cmd/evm: Allow stdin and files as sources of bytecode (bnb-chain#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
1 parent b59c839 commit 00665a0

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

‎cmd/evm/main.go‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"fmt"
22+
"io/ioutil"
2223
"math/big"
2324
"os"
2425
"runtime"
@@ -58,6 +59,10 @@ var (
5859
Name: "code",
5960
Usage: "EVM code",
6061
}
62+
CodeFileFlag = cli.StringFlag{
63+
Name: "codefile",
64+
Usage: "file containing EVM code",
65+
}
6166
GasFlag = cli.StringFlag{
6267
Name: "gas",
6368
Usage: "gas limit for the evm",
@@ -104,6 +109,7 @@ func init() {
104109
DisableJitFlag,
105110
SysStatFlag,
106111
CodeFlag,
112+
CodeFileFlag,
107113
GasFlag,
108114
PriceFlag,
109115
ValueFlag,
@@ -133,12 +139,35 @@ func run(ctx *cli.Context) error {
133139
tstart := time.Now()
134140

135141
var (
136-
ret []byte
137-
err error
142+
code []byte
143+
ret []byte
144+
err error
138145
)
139146

147+
if ctx.GlobalString(CodeFlag.Name) != "" {
148+
code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
149+
} else {
150+
var hexcode []byte
151+
if ctx.GlobalString(CodeFileFlag.Name) != "" {
152+
var err error
153+
hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name))
154+
if err != nil {
155+
fmt.Printf("Could not load code from file: %v\n", err)
156+
os.Exit(1)
157+
}
158+
} else {
159+
var err error
160+
hexcode, err = ioutil.ReadAll(os.Stdin)
161+
if err != nil {
162+
fmt.Printf("Could not load code from stdin: %v\n", err)
163+
os.Exit(1)
164+
}
165+
}
166+
code = common.Hex2Bytes(string(hexcode[:]))
167+
}
168+
140169
if ctx.GlobalBool(CreateFlag.Name) {
141-
input := append(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
170+
input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
142171
ret, _, err = vmenv.Create(
143172
sender,
144173
input,
@@ -149,7 +178,6 @@ func run(ctx *cli.Context) error {
149178
} else {
150179
receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
151180

152-
code := common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
153181
receiver.SetCode(crypto.Keccak256Hash(code), code)
154182
ret, err = vmenv.Call(
155183
sender,

0 commit comments

Comments
 (0)