main.go 5.7 KB

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