main.go 5.1 KB

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