main.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/logger/glog"
  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. 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. CreateFlag = cli.BoolFlag{
  85. Name: "create",
  86. Usage: "indicates the action should be create rather than call",
  87. }
  88. )
  89. func init() {
  90. app.Flags = []cli.Flag{
  91. CreateFlag,
  92. DebugFlag,
  93. VerbosityFlag,
  94. ForceJitFlag,
  95. DisableJitFlag,
  96. SysStatFlag,
  97. CodeFlag,
  98. GasFlag,
  99. PriceFlag,
  100. ValueFlag,
  101. DumpFlag,
  102. InputFlag,
  103. }
  104. app.Action = run
  105. }
  106. func run(ctx *cli.Context) error {
  107. glog.SetToStderr(true)
  108. glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
  109. db, _ := ethdb.NewMemDatabase()
  110. statedb, _ := state.New(common.Hash{}, db)
  111. sender := statedb.CreateAccount(common.StringToAddress("sender"))
  112. logger := vm.NewStructLogger(nil)
  113. vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), vm.Config{
  114. Debug: ctx.GlobalBool(DebugFlag.Name),
  115. ForceJit: ctx.GlobalBool(ForceJitFlag.Name),
  116. EnableJit: !ctx.GlobalBool(DisableJitFlag.Name),
  117. Tracer: logger,
  118. })
  119. tstart := time.Now()
  120. var (
  121. ret []byte
  122. err error
  123. )
  124. if ctx.GlobalBool(CreateFlag.Name) {
  125. input := append(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
  126. ret, _, err = vmenv.Create(
  127. sender,
  128. input,
  129. common.Big(ctx.GlobalString(GasFlag.Name)),
  130. common.Big(ctx.GlobalString(PriceFlag.Name)),
  131. common.Big(ctx.GlobalString(ValueFlag.Name)),
  132. )
  133. } else {
  134. receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
  135. receiver.SetCode(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)))
  136. ret, err = vmenv.Call(
  137. sender,
  138. receiver.Address(),
  139. common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)),
  140. common.Big(ctx.GlobalString(GasFlag.Name)),
  141. common.Big(ctx.GlobalString(PriceFlag.Name)),
  142. common.Big(ctx.GlobalString(ValueFlag.Name)),
  143. )
  144. }
  145. vmdone := time.Since(tstart)
  146. if ctx.GlobalBool(DumpFlag.Name) {
  147. statedb.Commit()
  148. fmt.Println(string(statedb.Dump()))
  149. }
  150. vm.StdErrFormat(logger.StructLogs())
  151. if ctx.GlobalBool(SysStatFlag.Name) {
  152. var mem runtime.MemStats
  153. runtime.ReadMemStats(&mem)
  154. fmt.Printf("vm took %v\n", vmdone)
  155. fmt.Printf(`alloc: %d
  156. tot alloc: %d
  157. no. malloc: %d
  158. heap alloc: %d
  159. heap objs: %d
  160. num gc: %d
  161. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  162. }
  163. fmt.Printf("OUT: 0x%x", ret)
  164. if err != nil {
  165. fmt.Printf(" error: %v", err)
  166. }
  167. fmt.Println()
  168. return nil
  169. }
  170. func main() {
  171. if err := app.Run(os.Args); err != nil {
  172. fmt.Fprintln(os.Stderr, err)
  173. os.Exit(1)
  174. }
  175. }
  176. type VMEnv struct {
  177. state *state.StateDB
  178. block *types.Block
  179. transactor *common.Address
  180. value *big.Int
  181. depth int
  182. Gas *big.Int
  183. time *big.Int
  184. logs []vm.StructLog
  185. evm *vm.EVM
  186. }
  187. func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg vm.Config) *VMEnv {
  188. env := &VMEnv{
  189. state: state,
  190. transactor: &transactor,
  191. value: value,
  192. time: big.NewInt(time.Now().Unix()),
  193. }
  194. env.evm = vm.New(env, cfg)
  195. return env
  196. }
  197. // ruleSet implements vm.RuleSet and will always default to the homestead rule set.
  198. type ruleSet struct{}
  199. func (ruleSet) IsHomestead(*big.Int) bool { return true }
  200. func (self *VMEnv) RuleSet() vm.RuleSet { return ruleSet{} }
  201. func (self *VMEnv) Vm() vm.Vm { return self.evm }
  202. func (self *VMEnv) Db() vm.Database { return self.state }
  203. func (self *VMEnv) MakeSnapshot() vm.Database { return self.state.Copy() }
  204. func (self *VMEnv) SetSnapshot(db vm.Database) { self.state.Set(db.(*state.StateDB)) }
  205. func (self *VMEnv) Origin() common.Address { return *self.transactor }
  206. func (self *VMEnv) BlockNumber() *big.Int { return common.Big0 }
  207. func (self *VMEnv) Coinbase() common.Address { return *self.transactor }
  208. func (self *VMEnv) Time() *big.Int { return self.time }
  209. func (self *VMEnv) Difficulty() *big.Int { return common.Big1 }
  210. func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
  211. func (self *VMEnv) Value() *big.Int { return self.value }
  212. func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) }
  213. func (self *VMEnv) VmType() vm.Type { return vm.StdVmTy }
  214. func (self *VMEnv) Depth() int { return 0 }
  215. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  216. func (self *VMEnv) GetHash(n uint64) common.Hash {
  217. if self.block.Number().Cmp(big.NewInt(int64(n))) == 0 {
  218. return self.block.Hash()
  219. }
  220. return common.Hash{}
  221. }
  222. func (self *VMEnv) AddLog(log *vm.Log) {
  223. self.state.AddLog(log)
  224. }
  225. func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
  226. return self.state.GetBalance(from).Cmp(balance) >= 0
  227. }
  228. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
  229. core.Transfer(from, to, amount)
  230. }
  231. func (self *VMEnv) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  232. self.Gas = gas
  233. return core.Call(self, caller, addr, data, gas, price, value)
  234. }
  235. func (self *VMEnv) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  236. return core.CallCode(self, caller, addr, data, gas, price, value)
  237. }
  238. func (self *VMEnv) DelegateCall(caller vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  239. return core.DelegateCall(self, caller, addr, data, gas, price)
  240. }
  241. func (self *VMEnv) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  242. return core.Create(self, caller, data, gas, price, value)
  243. }