main.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. "math/big"
  22. "os"
  23. goruntime "runtime"
  24. "time"
  25. "github.com/ethereum/go-ethereum/cmd/utils"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/core/vm/runtime"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/log"
  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.Uint64Flag{
  50. Name: "gas",
  51. Usage: "gas limit for the evm",
  52. Value: 10000000000,
  53. }
  54. PriceFlag = utils.BigFlag{
  55. Name: "price",
  56. Usage: "price set for the evm",
  57. Value: new(big.Int),
  58. }
  59. ValueFlag = utils.BigFlag{
  60. Name: "value",
  61. Usage: "value set for the evm",
  62. Value: new(big.Int),
  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. glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
  108. glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
  109. log.Root().SetHandler(glogger)
  110. var (
  111. db, _ = ethdb.NewMemDatabase()
  112. statedb, _ = state.New(common.Hash{}, db)
  113. address = common.StringToAddress("sender")
  114. sender = vm.AccountRef(address)
  115. )
  116. 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: ctx.GlobalUint64(GasFlag.Name),
  151. GasPrice: utils.GlobalBig(ctx, PriceFlag.Name),
  152. Value: utils.GlobalBig(ctx, ValueFlag.Name),
  153. EVMConfig: vm.Config{
  154. Tracer: logger,
  155. Debug: ctx.GlobalBool(DebugFlag.Name),
  156. DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
  157. },
  158. })
  159. } else {
  160. receiverAddress := common.StringToAddress("receiver")
  161. statedb.CreateAccount(receiverAddress)
  162. statedb.SetCode(receiverAddress, code)
  163. ret, err = runtime.Call(receiverAddress, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
  164. Origin: sender.Address(),
  165. State: statedb,
  166. GasLimit: ctx.GlobalUint64(GasFlag.Name),
  167. GasPrice: utils.GlobalBig(ctx, PriceFlag.Name),
  168. Value: utils.GlobalBig(ctx, ValueFlag.Name),
  169. EVMConfig: vm.Config{
  170. Tracer: logger,
  171. Debug: ctx.GlobalBool(DebugFlag.Name),
  172. DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
  173. },
  174. })
  175. }
  176. vmdone := time.Since(tstart)
  177. if ctx.GlobalBool(DumpFlag.Name) {
  178. statedb.Commit(true)
  179. fmt.Println(string(statedb.Dump()))
  180. }
  181. vm.StdErrFormat(logger.StructLogs())
  182. if ctx.GlobalBool(SysStatFlag.Name) {
  183. var mem goruntime.MemStats
  184. goruntime.ReadMemStats(&mem)
  185. fmt.Printf("vm took %v\n", vmdone)
  186. fmt.Printf(`alloc: %d
  187. tot alloc: %d
  188. no. malloc: %d
  189. heap alloc: %d
  190. heap objs: %d
  191. num gc: %d
  192. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  193. }
  194. fmt.Printf("OUT: 0x%x", ret)
  195. if err != nil {
  196. fmt.Printf(" error: %v", err)
  197. }
  198. fmt.Println()
  199. return nil
  200. }
  201. func main() {
  202. if err := app.Run(os.Args); err != nil {
  203. fmt.Fprintln(os.Stderr, err)
  204. os.Exit(1)
  205. }
  206. }