main.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  17. * @authors
  18. * Jeffrey Wilcke <i@jev.io>
  19. */
  20. package main
  21. import (
  22. "flag"
  23. "fmt"
  24. "log"
  25. "math/big"
  26. "os"
  27. "runtime"
  28. "time"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/core"
  31. "github.com/ethereum/go-ethereum/core/state"
  32. "github.com/ethereum/go-ethereum/core/types"
  33. "github.com/ethereum/go-ethereum/core/vm"
  34. "github.com/ethereum/go-ethereum/ethdb"
  35. "github.com/ethereum/go-ethereum/logger"
  36. )
  37. var (
  38. code = flag.String("code", "", "evm code")
  39. loglevel = flag.Int("log", 4, "log level")
  40. gas = flag.String("gas", "1000000000", "gas amount")
  41. price = flag.String("price", "0", "gas price")
  42. value = flag.String("value", "0", "tx value")
  43. dump = flag.Bool("dump", false, "dump state after run")
  44. data = flag.String("data", "", "data")
  45. )
  46. func perr(v ...interface{}) {
  47. fmt.Println(v...)
  48. //os.Exit(1)
  49. }
  50. func main() {
  51. flag.Parse()
  52. logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.LogLevel(*loglevel)))
  53. vm.Debug = true
  54. db, _ := ethdb.NewMemDatabase()
  55. statedb := state.New(common.Hash{}, db)
  56. sender := statedb.CreateAccount(common.StringToAddress("sender"))
  57. receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
  58. receiver.SetCode(common.Hex2Bytes(*code))
  59. vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(*value))
  60. tstart := time.Now()
  61. ret, e := vmenv.Call(sender, receiver.Address(), common.Hex2Bytes(*data), common.Big(*gas), common.Big(*price), common.Big(*value))
  62. logger.Flush()
  63. if e != nil {
  64. perr(e)
  65. }
  66. if *dump {
  67. fmt.Println(string(statedb.Dump()))
  68. }
  69. vm.StdErrFormat(vmenv.StructLogs())
  70. var mem runtime.MemStats
  71. runtime.ReadMemStats(&mem)
  72. fmt.Printf("vm took %v\n", time.Since(tstart))
  73. fmt.Printf(`alloc: %d
  74. tot alloc: %d
  75. no. malloc: %d
  76. heap alloc: %d
  77. heap objs: %d
  78. num gc: %d
  79. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  80. fmt.Printf("%x\n", ret)
  81. }
  82. type VMEnv struct {
  83. state *state.StateDB
  84. block *types.Block
  85. transactor *common.Address
  86. value *big.Int
  87. depth int
  88. Gas *big.Int
  89. time uint64
  90. logs []vm.StructLog
  91. }
  92. func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VMEnv {
  93. return &VMEnv{
  94. state: state,
  95. transactor: &transactor,
  96. value: value,
  97. time: uint64(time.Now().Unix()),
  98. }
  99. }
  100. func (self *VMEnv) State() *state.StateDB { return self.state }
  101. func (self *VMEnv) Origin() common.Address { return *self.transactor }
  102. func (self *VMEnv) BlockNumber() *big.Int { return common.Big0 }
  103. func (self *VMEnv) Coinbase() common.Address { return *self.transactor }
  104. func (self *VMEnv) Time() uint64 { return self.time }
  105. func (self *VMEnv) Difficulty() *big.Int { return common.Big1 }
  106. func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
  107. func (self *VMEnv) Value() *big.Int { return self.value }
  108. func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) }
  109. func (self *VMEnv) VmType() vm.Type { return vm.StdVmTy }
  110. func (self *VMEnv) Depth() int { return 0 }
  111. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  112. func (self *VMEnv) GetHash(n uint64) common.Hash {
  113. if self.block.Number().Cmp(big.NewInt(int64(n))) == 0 {
  114. return self.block.Hash()
  115. }
  116. return common.Hash{}
  117. }
  118. func (self *VMEnv) AddStructLog(log vm.StructLog) {
  119. self.logs = append(self.logs, log)
  120. }
  121. func (self *VMEnv) StructLogs() []vm.StructLog {
  122. return self.logs
  123. }
  124. func (self *VMEnv) AddLog(log *state.Log) {
  125. self.state.AddLog(log)
  126. }
  127. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
  128. return vm.Transfer(from, to, amount)
  129. }
  130. func (self *VMEnv) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution {
  131. return core.NewExecution(self, addr, data, gas, price, value)
  132. }
  133. func (self *VMEnv) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  134. exe := self.vm(&addr, data, gas, price, value)
  135. ret, err := exe.Call(addr, caller)
  136. self.Gas = exe.Gas
  137. return ret, err
  138. }
  139. func (self *VMEnv) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  140. a := caller.Address()
  141. exe := self.vm(&a, data, gas, price, value)
  142. return exe.Call(addr, caller)
  143. }
  144. func (self *VMEnv) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
  145. exe := self.vm(nil, data, gas, price, value)
  146. return exe.Create(caller)
  147. }