main.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. VerbosityFlag = cli.IntFlag{
  80. Name: "verbosity",
  81. Usage: "sets the verbosity level",
  82. }
  83. )
  84. func init() {
  85. app = utils.NewApp("0.2", "the evm command line interface")
  86. app.Flags = []cli.Flag{
  87. DebugFlag,
  88. VerbosityFlag,
  89. ForceJitFlag,
  90. DisableJitFlag,
  91. SysStatFlag,
  92. CodeFlag,
  93. GasFlag,
  94. PriceFlag,
  95. ValueFlag,
  96. DumpFlag,
  97. InputFlag,
  98. }
  99. app.Action = run
  100. }
  101. func run(ctx *cli.Context) {
  102. glog.SetToStderr(true)
  103. glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
  104. db, _ := ethdb.NewMemDatabase()
  105. statedb, _ := state.New(common.Hash{}, db)
  106. sender := statedb.CreateAccount(common.StringToAddress("sender"))
  107. receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
  108. receiver.SetCode(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)))
  109. vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), vm.Config{
  110. Debug: ctx.GlobalBool(DebugFlag.Name),
  111. ForceJit: ctx.GlobalBool(ForceJitFlag.Name),
  112. EnableJit: !ctx.GlobalBool(DisableJitFlag.Name),
  113. })
  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.EVM
  162. }
  163. func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg vm.Config) *VMEnv {
  164. env := &VMEnv{
  165. state: state,
  166. transactor: &transactor,
  167. value: value,
  168. time: big.NewInt(time.Now().Unix()),
  169. }
  170. cfg.Logger.Collector = env
  171. env.evm = vm.New(env, cfg)
  172. return env
  173. }
  174. // ruleSet implements vm.RuleSet and will always default to the homestead rule set.
  175. type ruleSet struct{}
  176. func (ruleSet) IsHomestead(*big.Int) bool { return true }
  177. func (self *VMEnv) RuleSet() vm.RuleSet { return ruleSet{} }
  178. func (self *VMEnv) Vm() vm.Vm { return self.evm }
  179. func (self *VMEnv) Db() vm.Database { return self.state }
  180. func (self *VMEnv) MakeSnapshot() vm.Database { return self.state.Copy() }
  181. func (self *VMEnv) SetSnapshot(db vm.Database) { self.state.Set(db.(*state.StateDB)) }
  182. func (self *VMEnv) Origin() common.Address { return *self.transactor }
  183. func (self *VMEnv) BlockNumber() *big.Int { return common.Big0 }
  184. func (self *VMEnv) Coinbase() common.Address { return *self.transactor }
  185. func (self *VMEnv) Time() *big.Int { return self.time }
  186. func (self *VMEnv) Difficulty() *big.Int { return common.Big1 }
  187. func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
  188. func (self *VMEnv) Value() *big.Int { return self.value }
  189. func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) }
  190. func (self *VMEnv) VmType() vm.Type { return vm.StdVmTy }
  191. func (self *VMEnv) Depth() int { return 0 }
  192. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  193. func (self *VMEnv) GetHash(n uint64) common.Hash {
  194. if self.block.Number().Cmp(big.NewInt(int64(n))) == 0 {
  195. return self.block.Hash()
  196. }
  197. return common.Hash{}
  198. }
  199. func (self *VMEnv) AddStructLog(log vm.StructLog) {
  200. self.logs = append(self.logs, log)
  201. }
  202. func (self *VMEnv) StructLogs() []vm.StructLog {
  203. return self.logs
  204. }
  205. func (self *VMEnv) AddLog(log *vm.Log) {
  206. self.state.AddLog(log)
  207. }
  208. func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
  209. return self.state.GetBalance(from).Cmp(balance) >= 0
  210. }
  211. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
  212. core.Transfer(from, to, amount)
  213. }
  214. func (self *VMEnv) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  215. self.Gas = gas
  216. return core.Call(self, caller, addr, data, gas, price, value)
  217. }
  218. func (self *VMEnv) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  219. return core.CallCode(self, caller, addr, data, gas, price, value)
  220. }
  221. func (self *VMEnv) DelegateCall(caller vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  222. return core.DelegateCall(self, caller, addr, data, gas, price)
  223. }
  224. func (self *VMEnv) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  225. return core.Create(self, caller, data, gas, price, value)
  226. }