main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "flag"
  21. "fmt"
  22. "log"
  23. "math/big"
  24. "os"
  25. "runtime"
  26. "time"
  27. "github.com/ethereum/go-ethereum/core"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/ethutil"
  31. "github.com/ethereum/go-ethereum/logger"
  32. "github.com/ethereum/go-ethereum/ptrie"
  33. "github.com/ethereum/go-ethereum/state"
  34. "github.com/ethereum/go-ethereum/vm"
  35. )
  36. var (
  37. code = flag.String("code", "", "evm code")
  38. loglevel = flag.Int("log", 4, "log level")
  39. gas = flag.String("gas", "1000000000", "gas amount")
  40. price = flag.String("price", "0", "gas price")
  41. value = flag.String("value", "0", "tx value")
  42. dump = flag.Bool("dump", false, "dump state after run")
  43. data = flag.String("data", "", "data")
  44. )
  45. func perr(v ...interface{}) {
  46. fmt.Println(v...)
  47. //os.Exit(1)
  48. }
  49. func main() {
  50. flag.Parse()
  51. logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.LogLevel(*loglevel)))
  52. ethutil.ReadConfig("/tmp/evmtest", "/tmp/evm", "")
  53. db, _ := ethdb.NewMemDatabase()
  54. statedb := state.New(ptrie.New(nil, db))
  55. sender := statedb.NewStateObject([]byte("sender"))
  56. receiver := statedb.NewStateObject([]byte("receiver"))
  57. //receiver.SetCode([]byte(*code))
  58. receiver.SetCode(ethutil.Hex2Bytes(*code))
  59. vmenv := NewEnv(statedb, []byte("evmuser"), ethutil.Big(*value))
  60. tstart := time.Now()
  61. ret, e := vmenv.Call(sender, receiver.Address(), ethutil.Hex2Bytes(*data), ethutil.Big(*gas), ethutil.Big(*price), ethutil.Big(*value))
  62. logger.Flush()
  63. if e != nil {
  64. perr(e)
  65. }
  66. if *dump {
  67. fmt.Println(string(statedb.Dump()))
  68. }
  69. var mem runtime.MemStats
  70. runtime.ReadMemStats(&mem)
  71. fmt.Printf("vm took %v\n", time.Since(tstart))
  72. fmt.Printf(`alloc: %d
  73. tot alloc: %d
  74. no. malloc: %d
  75. heap alloc: %d
  76. heap objs: %d
  77. num gc: %d
  78. `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
  79. fmt.Printf("%x\n", ret)
  80. }
  81. type VMEnv struct {
  82. state *state.StateDB
  83. block *types.Block
  84. transactor []byte
  85. value *big.Int
  86. depth int
  87. Gas *big.Int
  88. time int64
  89. }
  90. func NewEnv(state *state.StateDB, transactor []byte, value *big.Int) *VMEnv {
  91. return &VMEnv{
  92. state: state,
  93. transactor: transactor,
  94. value: value,
  95. time: time.Now().Unix(),
  96. }
  97. }
  98. func (self *VMEnv) State() *state.StateDB { return self.state }
  99. func (self *VMEnv) Origin() []byte { return self.transactor }
  100. func (self *VMEnv) BlockNumber() *big.Int { return ethutil.Big0 }
  101. func (self *VMEnv) PrevHash() []byte { return make([]byte, 32) }
  102. func (self *VMEnv) Coinbase() []byte { return self.transactor }
  103. func (self *VMEnv) Time() int64 { return self.time }
  104. func (self *VMEnv) Difficulty() *big.Int { return ethutil.Big1 }
  105. func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
  106. func (self *VMEnv) Value() *big.Int { return self.value }
  107. func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) }
  108. func (self *VMEnv) Depth() int { return 0 }
  109. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  110. func (self *VMEnv) GetHash(n uint64) []byte {
  111. if self.block.Number().Cmp(big.NewInt(int64(n))) == 0 {
  112. return self.block.Hash()
  113. }
  114. return nil
  115. }
  116. func (self *VMEnv) AddLog(log state.Log) {
  117. self.state.AddLog(log)
  118. }
  119. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
  120. return vm.Transfer(from, to, amount)
  121. }
  122. func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution {
  123. return core.NewExecution(self, addr, data, gas, price, value)
  124. }
  125. func (self *VMEnv) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
  126. exe := self.vm(addr, data, gas, price, value)
  127. ret, err := exe.Call(addr, caller)
  128. self.Gas = exe.Gas
  129. return ret, err
  130. }
  131. func (self *VMEnv) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
  132. exe := self.vm(caller.Address(), data, gas, price, value)
  133. return exe.Call(addr, caller)
  134. }
  135. func (self *VMEnv) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
  136. exe := self.vm(addr, data, gas, price, value)
  137. return exe.Create(caller)
  138. }