main.go 6.9 KB

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