main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2014 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. // evm executes EVM code snippets.
  17. package main
  18. import (
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. goruntime "runtime"
  23. "time"
  24. "github.com/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/core/vm/runtime"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/logger/glog"
  32. "gopkg.in/urfave/cli.v1"
  33. )
  34. var gitCommit = "" // Git SHA1 commit hash of the release (set via linker flags)
  35. var (
  36. app = utils.NewApp(gitCommit, "the evm command line interface")
  37. DebugFlag = cli.BoolFlag{
  38. Name: "debug",
  39. Usage: "output full trace logs",
  40. }
  41. ForceJitFlag = cli.BoolFlag{
  42. Name: "forcejit",
  43. Usage: "forces jit compilation",
  44. }
  45. DisableJitFlag = cli.BoolFlag{
  46. Name: "nojit",
  47. Usage: "disabled jit compilation",
  48. }
  49. CodeFlag = cli.StringFlag{
  50. Name: "code",
  51. Usage: "EVM code",
  52. }
  53. CodeFileFlag = cli.StringFlag{
  54. Name: "codefile",
  55. Usage: "file containing EVM code",
  56. }
  57. GasFlag = cli.StringFlag{
  58. Name: "gas",
  59. Usage: "gas limit for the evm",
  60. Value: "10000000000",
  61. }
  62. PriceFlag = cli.StringFlag{
  63. Name: "price",
  64. Usage: "price set for the evm",
  65. Value: "0",
  66. }
  67. ValueFlag = cli.StringFlag{
  68. Name: "value",
  69. Usage: "value set for the evm",
  70. Value: "0",
  71. }
  72. DumpFlag = cli.BoolFlag{
  73. Name: "dump",
  74. Usage: "dumps the state after the run",
  75. }
  76. InputFlag = cli.StringFlag{
  77. Name: "input",
  78. Usage: "input for the EVM",
  79. }
  80. SysStatFlag = cli.BoolFlag{
  81. Name: "sysstat",
  82. Usage: "display system stats",
  83. }
  84. VerbosityFlag = cli.IntFlag{
  85. Name: "verbosity",
  86. Usage: "sets the verbosity level",
  87. }
  88. CreateFlag = cli.BoolFlag{
  89. Name: "create",
  90. Usage: "indicates the action should be create rather than call",
  91. }
  92. )
  93. func init() {
  94. app.Flags = []cli.Flag{
  95. CreateFlag,
  96. DebugFlag,
  97. VerbosityFlag,
  98. ForceJitFlag,
  99. DisableJitFlag,
  100. SysStatFlag,
  101. CodeFlag,
  102. CodeFileFlag,
  103. GasFlag,
  104. PriceFlag,
  105. ValueFlag,
  106. DumpFlag,
  107. InputFlag,
  108. }
  109. app.Action = run
  110. }
  111. func run(ctx *cli.Context) error {
  112. glog.SetToStderr(true)
  113. glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
  114. db, _ := ethdb.NewMemDatabase()
  115. statedb, _ := state.New(common.Hash{}, db)
  116. sender := statedb.CreateAccount(common.StringToAddress("sender"))
  117. logger := vm.NewStructLogger(nil)
  118. tstart := time.Now()
  119. var (
  120. code []byte
  121. ret []byte
  122. err error
  123. )
  124. if ctx.GlobalString(CodeFlag.Name) != "" {
  125. code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
  126. } else {
  127. var hexcode []byte
  128. if ctx.GlobalString(CodeFileFlag.Name) != "" {
  129. var err error
  130. hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name))
  131. if err != nil {
  132. fmt.Printf("Could not load code from file: %v\n", err)
  133. os.Exit(1)
  134. }
  135. } else {
  136. var err error
  137. hexcode, err = ioutil.ReadAll(os.Stdin)
  138. if err != nil {
  139. fmt.Printf("Could not load code from stdin: %v\n", err)
  140. os.Exit(1)
  141. }
  142. }
  143. code = common.Hex2Bytes(string(hexcode[:]))
  144. }
  145. if ctx.GlobalBool(CreateFlag.Name) {
  146. input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
  147. ret, _, err = runtime.Create(input, &runtime.Config{
  148. Origin: sender.Address(),
  149. State: statedb,
  150. GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)),
  151. GasPrice: common.Big(ctx.GlobalString(PriceFlag.Name)),
  152. Value: common.Big(ctx.GlobalString(ValueFlag.Name)),
  153. EVMConfig: vm.Config{
  154. Tracer: logger,
  155. },
  156. })
  157. } else {
  158. receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
  159. receiver.SetCode(crypto.Keccak256Hash(code), code)
  160. ret, err = runtime.Call(receiver.Address(), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
  161. Origin: sender.Address(),
  162. State: statedb,
  163. GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)),
  164. GasPrice: common.Big(ctx.GlobalString(PriceFlag.Name)),
  165. Value: common.Big(ctx.GlobalString(ValueFlag.Name)),
  166. EVMConfig: vm.Config{
  167. Tracer: logger,
  168. },
  169. })
  170. }
  171. vmdone := time.Since(tstart)
  172. if ctx.GlobalBool(DumpFlag.Name) {
  173. statedb.Commit(true)
  174. fmt.Println(string(statedb.Dump()))
  175. }
  176. vm.StdErrFormat(logger.StructLogs())
  177. if ctx.GlobalBool(SysStatFlag.Name) {
  178. var mem goruntime.MemStats
  179. goruntime.ReadMemStats(&mem)
  180. fmt.Printf("vm took %v\n", vmdone)
  181. fmt.Printf(`alloc: %d
  182. tot alloc: %d
  183. no. malloc: %d
  184. heap alloc: %d
  185. heap objs: %d
  186. num gc: %d
  187. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  188. }
  189. fmt.Printf("OUT: 0x%x", ret)
  190. if err != nil {
  191. fmt.Printf(" error: %v", err)
  192. }
  193. fmt.Println()
  194. return nil
  195. }
  196. func main() {
  197. if err := app.Run(os.Args); err != nil {
  198. fmt.Fprintln(os.Stderr, err)
  199. os.Exit(1)
  200. }
  201. }