vm_test_util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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. // 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 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package tests
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io"
  21. "math/big"
  22. "strconv"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/logger/glog"
  28. )
  29. func RunVmTestWithReader(r io.Reader, skipTests []string) error {
  30. tests := make(map[string]VmTest)
  31. err := readJson(r, &tests)
  32. if err != nil {
  33. return err
  34. }
  35. if err != nil {
  36. return err
  37. }
  38. if err := runVmTests(tests, skipTests); err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func RunVmTest(p string, skipTests []string) error {
  44. tests := make(map[string]VmTest)
  45. err := readJsonFile(p, &tests)
  46. if err != nil {
  47. return err
  48. }
  49. if err := runVmTests(tests, skipTests); err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func runVmTests(tests map[string]VmTest, skipTests []string) error {
  55. skipTest := make(map[string]bool, len(skipTests))
  56. for _, name := range skipTests {
  57. skipTest[name] = true
  58. }
  59. for name, test := range tests {
  60. if skipTest[name] {
  61. glog.Infoln("Skipping VM test", name)
  62. return nil
  63. }
  64. if err := runVmTest(test); err != nil {
  65. return fmt.Errorf("%s %s", name, err.Error())
  66. }
  67. glog.Infoln("VM test passed: ", name)
  68. //fmt.Println(string(statedb.Dump()))
  69. }
  70. return nil
  71. }
  72. func runVmTest(test VmTest) error {
  73. db, _ := ethdb.NewMemDatabase()
  74. statedb := state.New(common.Hash{}, db)
  75. for addr, account := range test.Pre {
  76. obj := StateObjectFromAccount(db, addr, account)
  77. statedb.SetStateObject(obj)
  78. for a, v := range account.Storage {
  79. obj.SetState(common.HexToHash(a), common.HexToHash(v))
  80. }
  81. }
  82. // XXX Yeah, yeah...
  83. env := make(map[string]string)
  84. env["currentCoinbase"] = test.Env.CurrentCoinbase
  85. env["currentDifficulty"] = test.Env.CurrentDifficulty
  86. env["currentGasLimit"] = test.Env.CurrentGasLimit
  87. env["currentNumber"] = test.Env.CurrentNumber
  88. env["previousHash"] = test.Env.PreviousHash
  89. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  90. env["currentTimestamp"] = strconv.Itoa(int(n))
  91. } else {
  92. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  93. }
  94. var (
  95. ret []byte
  96. gas *big.Int
  97. err error
  98. logs state.Logs
  99. )
  100. ret, logs, gas, err = RunVm(statedb, env, test.Exec)
  101. // Compare expected and actual return
  102. rexp := common.FromHex(test.Out)
  103. if bytes.Compare(rexp, ret) != 0 {
  104. return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret)
  105. }
  106. // Check gas usage
  107. if len(test.Gas) == 0 && err == nil {
  108. return fmt.Errorf("gas unspecified, indicating an error. VM returned (incorrectly) successfull")
  109. } else {
  110. gexp := common.Big(test.Gas)
  111. if gexp.Cmp(gas) != 0 {
  112. return fmt.Errorf("gas failed. Expected %v, got %v\n", gexp, gas)
  113. }
  114. }
  115. // check post state
  116. for addr, account := range test.Post {
  117. obj := statedb.GetStateObject(common.HexToAddress(addr))
  118. if obj == nil {
  119. continue
  120. }
  121. for addr, value := range account.Storage {
  122. v := obj.GetState(common.HexToHash(addr))
  123. vexp := common.HexToHash(value)
  124. if v != vexp {
  125. return fmt.Errorf("(%x: %s) storage failed. Expected %x, got %x (%v %v)\n", obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big())
  126. }
  127. }
  128. }
  129. // check logs
  130. if len(test.Logs) > 0 {
  131. lerr := checkLogs(test.Logs, logs)
  132. if lerr != nil {
  133. return lerr
  134. }
  135. }
  136. return nil
  137. }
  138. func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) {
  139. var (
  140. to = common.HexToAddress(exec["address"])
  141. from = common.HexToAddress(exec["caller"])
  142. data = common.FromHex(exec["data"])
  143. gas = common.Big(exec["gas"])
  144. price = common.Big(exec["gasPrice"])
  145. value = common.Big(exec["value"])
  146. )
  147. // Reset the pre-compiled contracts for VM tests.
  148. vm.Precompiled = make(map[string]*vm.PrecompiledAccount)
  149. caller := state.GetOrNewStateObject(from)
  150. vmenv := NewEnvFromMap(state, env, exec)
  151. vmenv.vmTest = true
  152. vmenv.skipTransfer = true
  153. vmenv.initial = true
  154. ret, err := vmenv.Call(caller, to, data, gas, price, value)
  155. return ret, vmenv.state.Logs(), vmenv.Gas, err
  156. }