util.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package tests
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "math/big"
  22. "os"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/state"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/logger/glog"
  31. "github.com/ethereum/go-ethereum/params"
  32. )
  33. var (
  34. ForceJit bool
  35. EnableJit bool
  36. )
  37. func init() {
  38. glog.SetV(0)
  39. if os.Getenv("JITVM") == "true" {
  40. ForceJit = true
  41. EnableJit = true
  42. }
  43. }
  44. func checkLogs(tlog []Log, logs []*types.Log) error {
  45. if len(tlog) != len(logs) {
  46. return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
  47. } else {
  48. for i, log := range tlog {
  49. if common.HexToAddress(log.AddressF) != logs[i].Address {
  50. return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
  51. }
  52. if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
  53. return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
  54. }
  55. if len(log.TopicsF) != len(logs[i].Topics) {
  56. return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
  57. } else {
  58. for j, topic := range log.TopicsF {
  59. if common.HexToHash(topic) != logs[i].Topics[j] {
  60. return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
  61. }
  62. }
  63. }
  64. genBloom := common.LeftPadBytes(types.LogsBloom([]*types.Log{logs[i]}).Bytes(), 256)
  65. if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
  66. return fmt.Errorf("bloom mismatch")
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. type Account struct {
  73. Balance string
  74. Code string
  75. Nonce string
  76. Storage map[string]string
  77. }
  78. type Log struct {
  79. AddressF string `json:"address"`
  80. DataF string `json:"data"`
  81. TopicsF []string `json:"topics"`
  82. BloomF string `json:"bloom"`
  83. }
  84. func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) }
  85. func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) }
  86. func (self Log) RlpData() interface{} { return nil }
  87. func (self Log) Topics() [][]byte {
  88. t := make([][]byte, len(self.TopicsF))
  89. for i, topic := range self.TopicsF {
  90. t[i] = common.Hex2Bytes(topic)
  91. }
  92. return t
  93. }
  94. func makePreState(db ethdb.Database, accounts map[string]Account) *state.StateDB {
  95. statedb, _ := state.New(common.Hash{}, db)
  96. for addr, account := range accounts {
  97. insertAccount(statedb, addr, account)
  98. }
  99. return statedb
  100. }
  101. func insertAccount(state *state.StateDB, saddr string, account Account) {
  102. if common.IsHex(account.Code) {
  103. account.Code = account.Code[2:]
  104. }
  105. addr := common.HexToAddress(saddr)
  106. state.SetCode(addr, common.Hex2Bytes(account.Code))
  107. state.SetNonce(addr, common.Big(account.Nonce).Uint64())
  108. state.SetBalance(addr, common.Big(account.Balance))
  109. for a, v := range account.Storage {
  110. state.SetState(addr, common.HexToHash(a), common.HexToHash(v))
  111. }
  112. }
  113. type VmEnv struct {
  114. CurrentCoinbase string
  115. CurrentDifficulty string
  116. CurrentGasLimit string
  117. CurrentNumber string
  118. CurrentTimestamp interface{}
  119. PreviousHash string
  120. }
  121. type VmTest struct {
  122. Callcreates interface{}
  123. //Env map[string]string
  124. Env VmEnv
  125. Exec map[string]string
  126. Transaction map[string]string
  127. Logs []Log
  128. Gas string
  129. Out string
  130. Post map[string]Account
  131. Pre map[string]Account
  132. PostStateRoot string
  133. }
  134. func NewEVMEnvironment(vmTest bool, chainConfig *params.ChainConfig, statedb *state.StateDB, envValues map[string]string, tx map[string]string) (*vm.EVM, core.Message) {
  135. var (
  136. data = common.FromHex(tx["data"])
  137. gas = common.Big(tx["gasLimit"])
  138. price = common.Big(tx["gasPrice"])
  139. value = common.Big(tx["value"])
  140. nonce = common.Big(tx["nonce"]).Uint64()
  141. )
  142. origin := common.HexToAddress(tx["caller"])
  143. if len(tx["secretKey"]) > 0 {
  144. key, _ := hex.DecodeString(tx["secretKey"])
  145. origin = crypto.PubkeyToAddress(crypto.ToECDSA(key).PublicKey)
  146. }
  147. var to *common.Address
  148. if len(tx["to"]) > 2 {
  149. t := common.HexToAddress(tx["to"])
  150. to = &t
  151. }
  152. msg := types.NewMessage(origin, to, nonce, value, gas, price, data, true)
  153. initialCall := true
  154. canTransfer := func(db vm.StateDB, address common.Address, amount *big.Int) bool {
  155. if vmTest {
  156. if initialCall {
  157. initialCall = false
  158. return true
  159. }
  160. }
  161. return core.CanTransfer(db, address, amount)
  162. }
  163. transfer := func(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
  164. if vmTest {
  165. return
  166. }
  167. core.Transfer(db, sender, recipient, amount)
  168. }
  169. context := vm.Context{
  170. CanTransfer: canTransfer,
  171. Transfer: transfer,
  172. GetHash: func(n uint64) common.Hash {
  173. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  174. },
  175. Origin: origin,
  176. Coinbase: common.HexToAddress(envValues["currentCoinbase"]),
  177. BlockNumber: common.Big(envValues["currentNumber"]),
  178. Time: common.Big(envValues["currentTimestamp"]),
  179. GasLimit: common.Big(envValues["currentGasLimit"]),
  180. Difficulty: common.Big(envValues["currentDifficulty"]),
  181. GasPrice: price,
  182. }
  183. if context.GasPrice == nil {
  184. context.GasPrice = new(big.Int)
  185. }
  186. return vm.NewEVM(context, statedb, chainConfig, vm.Config{NoRecursion: vmTest}), msg
  187. }