main.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "math/big"
  21. "os"
  22. "runtime"
  23. "time"
  24. "github.com/codegangsta/cli"
  25. "github.com/ethereum/go-ethereum/cmd/utils"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core"
  28. "github.com/ethereum/go-ethereum/core/state"
  29. "github.com/ethereum/go-ethereum/core/types"
  30. "github.com/ethereum/go-ethereum/core/vm"
  31. "github.com/ethereum/go-ethereum/ethdb"
  32. "github.com/ethereum/go-ethereum/logger/glog"
  33. )
  34. var (
  35. app *cli.App
  36. DebugFlag = cli.BoolFlag{
  37. Name: "debug",
  38. Usage: "output full trace logs",
  39. }
  40. ForceJitFlag = cli.BoolFlag{
  41. Name: "forcejit",
  42. Usage: "forces jit compilation",
  43. }
  44. DisableJitFlag = cli.BoolFlag{
  45. Name: "nojit",
  46. Usage: "disabled jit compilation",
  47. }
  48. CodeFlag = cli.StringFlag{
  49. Name: "code",
  50. Usage: "EVM code",
  51. }
  52. GasFlag = cli.StringFlag{
  53. Name: "gas",
  54. Usage: "gas limit for the evm",
  55. Value: "10000000000",
  56. }
  57. PriceFlag = cli.StringFlag{
  58. Name: "price",
  59. Usage: "price set for the evm",
  60. Value: "0",
  61. }
  62. ValueFlag = cli.StringFlag{
  63. Name: "value",
  64. Usage: "value set for the evm",
  65. Value: "0",
  66. }
  67. DumpFlag = cli.BoolFlag{
  68. Name: "dump",
  69. Usage: "dumps the state after the run",
  70. }
  71. InputFlag = cli.StringFlag{
  72. Name: "input",
  73. Usage: "input for the EVM",
  74. }
  75. SysStatFlag = cli.BoolFlag{
  76. Name: "sysstat",
  77. Usage: "display system stats",
  78. }
  79. )
  80. func init() {
  81. app = utils.NewApp("0.2", "the evm command line interface")
  82. app.Flags = []cli.Flag{
  83. DebugFlag,
  84. ForceJitFlag,
  85. DisableJitFlag,
  86. SysStatFlag,
  87. CodeFlag,
  88. GasFlag,
  89. PriceFlag,
  90. ValueFlag,
  91. DumpFlag,
  92. InputFlag,
  93. }
  94. app.Action = run
  95. }
  96. func run(ctx *cli.Context) {
  97. vm.Debug = ctx.GlobalBool(DebugFlag.Name)
  98. vm.ForceJit = ctx.GlobalBool(ForceJitFlag.Name)
  99. vm.EnableJit = !ctx.GlobalBool(DisableJitFlag.Name)
  100. glog.SetToStderr(true)
  101. db, _ := ethdb.NewMemDatabase()
  102. statedb := state.New(common.Hash{}, db)
  103. sender := statedb.CreateAccount(common.StringToAddress("sender"))
  104. receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
  105. receiver.SetCode(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)))
  106. vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)))
  107. tstart := time.Now()
  108. ret, e := vmenv.Call(
  109. sender,
  110. receiver.Address(),
  111. common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)),
  112. common.Big(ctx.GlobalString(GasFlag.Name)),
  113. common.Big(ctx.GlobalString(PriceFlag.Name)),
  114. common.Big(ctx.GlobalString(ValueFlag.Name)),
  115. )
  116. vmdone := time.Since(tstart)
  117. if ctx.GlobalBool(DumpFlag.Name) {
  118. fmt.Println(string(statedb.Dump()))
  119. }
  120. vm.StdErrFormat(vmenv.StructLogs())
  121. if ctx.GlobalBool(SysStatFlag.Name) {
  122. var mem runtime.MemStats
  123. runtime.ReadMemStats(&mem)
  124. fmt.Printf("vm took %v\n", vmdone)
  125. fmt.Printf(`alloc: %d
  126. tot alloc: %d
  127. no. malloc: %d
  128. heap alloc: %d
  129. heap objs: %d
  130. num gc: %d
  131. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  132. }
  133. fmt.Printf("OUT: 0x%x", ret)
  134. if e != nil {
  135. fmt.Printf(" error: %v", e)
  136. }
  137. fmt.Println()
  138. }
  139. func main() {
  140. if err := app.Run(os.Args); err != nil {
  141. fmt.Fprintln(os.Stderr, err)
  142. os.Exit(1)
  143. }
  144. }
  145. type VMEnv struct {
  146. state *state.StateDB
  147. block *types.Block
  148. transactor *common.Address
  149. value *big.Int
  150. depth int
  151. Gas *big.Int
  152. time *big.Int
  153. logs []vm.StructLog
  154. }
  155. func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VMEnv {
  156. return &VMEnv{
  157. state: state,
  158. transactor: &transactor,
  159. value: value,
  160. time: big.NewInt(time.Now().Unix()),
  161. }
  162. }
  163. func (self *VMEnv) State() *state.StateDB { return self.state }
  164. func (self *VMEnv) Origin() common.Address { return *self.transactor }
  165. func (self *VMEnv) BlockNumber() *big.Int { return common.Big0 }
  166. func (self *VMEnv) Coinbase() common.Address { return *self.transactor }
  167. func (self *VMEnv) Time() *big.Int { return self.time }
  168. func (self *VMEnv) Difficulty() *big.Int { return common.Big1 }
  169. func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
  170. func (self *VMEnv) Value() *big.Int { return self.value }
  171. func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) }
  172. func (self *VMEnv) VmType() vm.Type { return vm.StdVmTy }
  173. func (self *VMEnv) Depth() int { return 0 }
  174. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  175. func (self *VMEnv) GetHash(n uint64) common.Hash {
  176. if self.block.Number().Cmp(big.NewInt(int64(n))) == 0 {
  177. return self.block.Hash()
  178. }
  179. return common.Hash{}
  180. }
  181. func (self *VMEnv) AddStructLog(log vm.StructLog) {
  182. self.logs = append(self.logs, log)
  183. }
  184. func (self *VMEnv) StructLogs() []vm.StructLog {
  185. return self.logs
  186. }
  187. func (self *VMEnv) AddLog(log *state.Log) {
  188. self.state.AddLog(log)
  189. }
  190. func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
  191. return from.Balance().Cmp(balance) >= 0
  192. }
  193. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
  194. return vm.Transfer(from, to, amount)
  195. }
  196. func (self *VMEnv) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution {
  197. return core.NewExecution(self, addr, data, gas, price, value)
  198. }
  199. func (self *VMEnv) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  200. exe := self.vm(&addr, data, gas, price, value)
  201. ret, err := exe.Call(addr, caller)
  202. self.Gas = exe.Gas
  203. return ret, err
  204. }
  205. func (self *VMEnv) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  206. a := caller.Address()
  207. exe := self.vm(&a, data, gas, price, value)
  208. return exe.Call(addr, caller)
  209. }
  210. func (self *VMEnv) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
  211. exe := self.vm(nil, data, gas, price, value)
  212. return exe.Create(caller)
  213. }