main.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/logger/glog"
  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. glog.SetToStderr(true)
  107. glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
  108. var (
  109. db, _ = ethdb.NewMemDatabase()
  110. statedb, _ = state.New(common.Hash{}, db)
  111. address = common.StringToAddress("sender")
  112. sender = vm.AccountRef(address)
  113. )
  114. statedb.CreateAccount(common.StringToAddress("sender"))
  115. logger := vm.NewStructLogger(nil)
  116. tstart := time.Now()
  117. var (
  118. code []byte
  119. ret []byte
  120. err error
  121. )
  122. if ctx.GlobalString(CodeFlag.Name) != "" {
  123. code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
  124. } else {
  125. var hexcode []byte
  126. if ctx.GlobalString(CodeFileFlag.Name) != "" {
  127. var err error
  128. hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name))
  129. if err != nil {
  130. fmt.Printf("Could not load code from file: %v\n", err)
  131. os.Exit(1)
  132. }
  133. } else {
  134. var err error
  135. hexcode, err = ioutil.ReadAll(os.Stdin)
  136. if err != nil {
  137. fmt.Printf("Could not load code from stdin: %v\n", err)
  138. os.Exit(1)
  139. }
  140. }
  141. code = common.Hex2Bytes(string(hexcode[:]))
  142. }
  143. if ctx.GlobalBool(CreateFlag.Name) {
  144. input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
  145. ret, _, err = runtime.Create(input, &runtime.Config{
  146. Origin: sender.Address(),
  147. State: statedb,
  148. GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)).Uint64(),
  149. GasPrice: common.Big(ctx.GlobalString(PriceFlag.Name)),
  150. Value: common.Big(ctx.GlobalString(ValueFlag.Name)),
  151. EVMConfig: vm.Config{
  152. Tracer: logger,
  153. Debug: ctx.GlobalBool(DebugFlag.Name),
  154. DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
  155. },
  156. })
  157. } else {
  158. receiverAddress := common.StringToAddress("receiver")
  159. statedb.CreateAccount(receiverAddress)
  160. statedb.SetCode(receiverAddress, code)
  161. ret, err = runtime.Call(receiverAddress, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
  162. Origin: sender.Address(),
  163. State: statedb,
  164. GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)).Uint64(),
  165. GasPrice: common.Big(ctx.GlobalString(PriceFlag.Name)),
  166. Value: common.Big(ctx.GlobalString(ValueFlag.Name)),
  167. EVMConfig: vm.Config{
  168. Tracer: logger,
  169. Debug: ctx.GlobalBool(DebugFlag.Name),
  170. DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
  171. },
  172. })
  173. }
  174. vmdone := time.Since(tstart)
  175. if ctx.GlobalBool(DumpFlag.Name) {
  176. statedb.Commit(true)
  177. fmt.Println(string(statedb.Dump()))
  178. }
  179. vm.StdErrFormat(logger.StructLogs())
  180. if ctx.GlobalBool(SysStatFlag.Name) {
  181. var mem goruntime.MemStats
  182. goruntime.ReadMemStats(&mem)
  183. fmt.Printf("vm took %v\n", vmdone)
  184. fmt.Printf(`alloc: %d
  185. tot alloc: %d
  186. no. malloc: %d
  187. heap alloc: %d
  188. heap objs: %d
  189. num gc: %d
  190. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  191. }
  192. fmt.Printf("OUT: 0x%x", ret)
  193. if err != nil {
  194. fmt.Printf(" error: %v", err)
  195. }
  196. fmt.Println()
  197. return nil
  198. }
  199. func main() {
  200. if err := app.Run(os.Args); err != nil {
  201. fmt.Fprintln(os.Stderr, err)
  202. os.Exit(1)
  203. }
  204. }