|
| 1 | +// Copyright 2017 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 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "time" |
| 25 | + |
| 26 | + goruntime "runtime" |
| 27 | + |
| 28 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 29 | + "github.com/ethereum/go-ethereum/common" |
| 30 | + "github.com/ethereum/go-ethereum/core/state" |
| 31 | + "github.com/ethereum/go-ethereum/core/vm" |
| 32 | + "github.com/ethereum/go-ethereum/core/vm/runtime" |
| 33 | + "github.com/ethereum/go-ethereum/ethdb" |
| 34 | + "github.com/ethereum/go-ethereum/log" |
| 35 | + cli "gopkg.in/urfave/cli.v1" |
| 36 | +) |
| 37 | + |
| 38 | +var runCommand = cli.Command{ |
| 39 | + Action: runCmd, |
| 40 | + Name: "run", |
| 41 | + Usage: "run arbitrary evm binary", |
| 42 | + ArgsUsage: "<code>", |
| 43 | + Description: `The run command runs arbitrary EVM code.`, |
| 44 | +} |
| 45 | + |
| 46 | +func runCmd(ctx *cli.Context) error { |
| 47 | + glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) |
| 48 | + glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name))) |
| 49 | + log.Root().SetHandler(glogger) |
| 50 | + |
| 51 | + var ( |
| 52 | + db, _ = ethdb.NewMemDatabase() |
| 53 | + statedb, _ = state.New(common.Hash{}, db) |
| 54 | + sender = common.StringToAddress("sender") |
| 55 | + logger = vm.NewStructLogger(nil) |
| 56 | + tstart = time.Now() |
| 57 | + ) |
| 58 | + statedb.CreateAccount(sender) |
| 59 | + |
| 60 | + var ( |
| 61 | + code []byte |
| 62 | + ret []byte |
| 63 | + err error |
| 64 | + ) |
| 65 | + if ctx.GlobalString(CodeFlag.Name) != "" { |
| 66 | + code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)) |
| 67 | + } else { |
| 68 | + var hexcode []byte |
| 69 | + if ctx.GlobalString(CodeFileFlag.Name) != "" { |
| 70 | + var err error |
| 71 | + hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name)) |
| 72 | + if err != nil { |
| 73 | + fmt.Printf("Could not load code from file: %v\n", err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + } else { |
| 77 | + var err error |
| 78 | + hexcode, err = ioutil.ReadAll(os.Stdin) |
| 79 | + if err != nil { |
| 80 | + fmt.Printf("Could not load code from stdin: %v\n", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + } |
| 84 | + code = common.Hex2Bytes(string(bytes.TrimRight(hexcode, "\n"))) |
| 85 | + } |
| 86 | + |
| 87 | + if ctx.GlobalBool(CreateFlag.Name) { |
| 88 | + input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...) |
| 89 | + ret, _, err = runtime.Create(input, &runtime.Config{ |
| 90 | + Origin: sender, |
| 91 | + State: statedb, |
| 92 | + GasLimit: ctx.GlobalUint64(GasFlag.Name), |
| 93 | + GasPrice: utils.GlobalBig(ctx, PriceFlag.Name), |
| 94 | + Value: utils.GlobalBig(ctx, ValueFlag.Name), |
| 95 | + EVMConfig: vm.Config{ |
| 96 | + Tracer: logger, |
| 97 | + Debug: ctx.GlobalBool(DebugFlag.Name), |
| 98 | + DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name), |
| 99 | + }, |
| 100 | + }) |
| 101 | + } else { |
| 102 | + receiver := common.StringToAddress("receiver") |
| 103 | + statedb.SetCode(receiver, code) |
| 104 | + |
| 105 | + ret, err = runtime.Call(receiver, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{ |
| 106 | + Origin: sender, |
| 107 | + State: statedb, |
| 108 | + GasLimit: ctx.GlobalUint64(GasFlag.Name), |
| 109 | + GasPrice: utils.GlobalBig(ctx, PriceFlag.Name), |
| 110 | + Value: utils.GlobalBig(ctx, ValueFlag.Name), |
| 111 | + EVMConfig: vm.Config{ |
| 112 | + Tracer: logger, |
| 113 | + Debug: ctx.GlobalBool(DebugFlag.Name), |
| 114 | + DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name), |
| 115 | + }, |
| 116 | + }) |
| 117 | + } |
| 118 | + vmdone := time.Since(tstart) |
| 119 | + |
| 120 | + if ctx.GlobalBool(DumpFlag.Name) { |
| 121 | + statedb.Commit(true) |
| 122 | + fmt.Println(string(statedb.Dump())) |
| 123 | + } |
| 124 | + vm.StdErrFormat(logger.StructLogs()) |
| 125 | + |
| 126 | + if ctx.GlobalBool(SysStatFlag.Name) { |
| 127 | + var mem goruntime.MemStats |
| 128 | + goruntime.ReadMemStats(&mem) |
| 129 | + fmt.Printf("vm took %v\n", vmdone) |
| 130 | + fmt.Printf(`alloc: %d |
| 131 | +tot alloc: %d |
| 132 | +no. malloc: %d |
| 133 | +heap alloc: %d |
| 134 | +heap objs: %d |
| 135 | +num gc: %d |
| 136 | +`, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC) |
| 137 | + } |
| 138 | + |
| 139 | + fmt.Printf("OUT: 0x%x", ret) |
| 140 | + if err != nil { |
| 141 | + fmt.Printf(" error: %v", err) |
| 142 | + } |
| 143 | + fmt.Println() |
| 144 | + return nil |
| 145 | +} |
0 commit comments