main.go 5.5 KB

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