state_test_util.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "fmt"
  20. "io"
  21. "math/big"
  22. "strconv"
  23. "strings"
  24. "testing"
  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/ethdb"
  30. "github.com/ethereum/go-ethereum/logger/glog"
  31. "github.com/ethereum/go-ethereum/params"
  32. )
  33. func RunStateTestWithReader(chainConfig *params.ChainConfig, r io.Reader, skipTests []string) error {
  34. tests := make(map[string]VmTest)
  35. if err := readJson(r, &tests); err != nil {
  36. return err
  37. }
  38. if err := runStateTests(chainConfig, tests, skipTests); err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func RunStateTest(chainConfig *params.ChainConfig, p string, skipTests []string) error {
  44. tests := make(map[string]VmTest)
  45. if err := readJsonFile(p, &tests); err != nil {
  46. return err
  47. }
  48. if err := runStateTests(chainConfig, tests, skipTests); err != nil {
  49. return err
  50. }
  51. return nil
  52. }
  53. func BenchStateTest(chainConfig *params.ChainConfig, p string, conf bconf, b *testing.B) error {
  54. tests := make(map[string]VmTest)
  55. if err := readJsonFile(p, &tests); err != nil {
  56. return err
  57. }
  58. test, ok := tests[conf.name]
  59. if !ok {
  60. return fmt.Errorf("test not found: %s", conf.name)
  61. }
  62. // XXX Yeah, yeah...
  63. env := make(map[string]string)
  64. env["currentCoinbase"] = test.Env.CurrentCoinbase
  65. env["currentDifficulty"] = test.Env.CurrentDifficulty
  66. env["currentGasLimit"] = test.Env.CurrentGasLimit
  67. env["currentNumber"] = test.Env.CurrentNumber
  68. env["previousHash"] = test.Env.PreviousHash
  69. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  70. env["currentTimestamp"] = strconv.Itoa(int(n))
  71. } else {
  72. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  73. }
  74. b.ResetTimer()
  75. for i := 0; i < b.N; i++ {
  76. benchStateTest(chainConfig, test, env, b)
  77. }
  78. return nil
  79. }
  80. func benchStateTest(chainConfig *params.ChainConfig, test VmTest, env map[string]string, b *testing.B) {
  81. b.StopTimer()
  82. db, _ := ethdb.NewMemDatabase()
  83. statedb := makePreState(db, test.Pre)
  84. b.StartTimer()
  85. RunState(chainConfig, statedb, env, test.Exec)
  86. }
  87. func runStateTests(chainConfig *params.ChainConfig, tests map[string]VmTest, skipTests []string) error {
  88. skipTest := make(map[string]bool, len(skipTests))
  89. for _, name := range skipTests {
  90. skipTest[name] = true
  91. }
  92. for name, test := range tests {
  93. if skipTest[name] || name != "loop_stacklimit_1021" {
  94. glog.Infoln("Skipping state test", name)
  95. continue
  96. }
  97. //fmt.Println("StateTest:", name)
  98. if err := runStateTest(chainConfig, test); err != nil {
  99. return fmt.Errorf("%s: %s\n", name, err.Error())
  100. }
  101. //glog.Infoln("State test passed: ", name)
  102. //fmt.Println(string(statedb.Dump()))
  103. }
  104. return nil
  105. }
  106. func runStateTest(chainConfig *params.ChainConfig, test VmTest) error {
  107. db, _ := ethdb.NewMemDatabase()
  108. statedb := makePreState(db, test.Pre)
  109. // XXX Yeah, yeah...
  110. env := make(map[string]string)
  111. env["currentCoinbase"] = test.Env.CurrentCoinbase
  112. env["currentDifficulty"] = test.Env.CurrentDifficulty
  113. env["currentGasLimit"] = test.Env.CurrentGasLimit
  114. env["currentNumber"] = test.Env.CurrentNumber
  115. env["previousHash"] = test.Env.PreviousHash
  116. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  117. env["currentTimestamp"] = strconv.Itoa(int(n))
  118. } else {
  119. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  120. }
  121. var (
  122. ret []byte
  123. // gas *big.Int
  124. // err error
  125. logs []*types.Log
  126. )
  127. ret, logs, _, _ = RunState(chainConfig, statedb, env, test.Transaction)
  128. // Compare expected and actual return
  129. var rexp []byte
  130. if strings.HasPrefix(test.Out, "#") {
  131. n, _ := strconv.Atoi(test.Out[1:])
  132. rexp = make([]byte, n)
  133. } else {
  134. rexp = common.FromHex(test.Out)
  135. }
  136. if bytes.Compare(rexp, ret) != 0 {
  137. return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret)
  138. }
  139. // check post state
  140. for addr, account := range test.Post {
  141. obj := statedb.GetStateObject(common.HexToAddress(addr))
  142. if obj == nil {
  143. return fmt.Errorf("did not find expected post-state account: %s", addr)
  144. }
  145. if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
  146. return fmt.Errorf("(%x) balance failed. Expected: %v have: %v\n", obj.Address().Bytes()[:4], common.String2Big(account.Balance), obj.Balance())
  147. }
  148. if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
  149. return fmt.Errorf("(%x) nonce failed. Expected: %v have: %v\n", obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
  150. }
  151. for addr, value := range account.Storage {
  152. v := statedb.GetState(obj.Address(), common.HexToHash(addr))
  153. vexp := common.HexToHash(value)
  154. if v != vexp {
  155. return fmt.Errorf("storage failed:\n%x: %s:\nexpected: %x\nhave: %x\n(%v %v)\n", obj.Address().Bytes(), addr, vexp, v, vexp.Big(), v.Big())
  156. }
  157. }
  158. }
  159. root, _ := statedb.Commit(false)
  160. if common.HexToHash(test.PostStateRoot) != root {
  161. return fmt.Errorf("Post state root error. Expected: %s have: %x", test.PostStateRoot, root)
  162. }
  163. // check logs
  164. if len(test.Logs) > 0 {
  165. if err := checkLogs(test.Logs, logs); err != nil {
  166. return err
  167. }
  168. }
  169. return nil
  170. }
  171. func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, env, tx map[string]string) ([]byte, []*types.Log, *big.Int, error) {
  172. environment, msg := NewEVMEnvironment(false, chainConfig, statedb, env, tx)
  173. gaspool := new(core.GasPool).AddGas(common.Big(env["currentGasLimit"]))
  174. root, _ := statedb.Commit(false)
  175. statedb.Reset(root)
  176. snapshot := statedb.Snapshot()
  177. ret, gasUsed, err := core.ApplyMessage(environment, msg, gaspool)
  178. if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || core.IsGasLimitErr(err) {
  179. statedb.RevertToSnapshot(snapshot)
  180. }
  181. statedb.Commit(chainConfig.IsEIP158(environment.Context.BlockNumber))
  182. return ret, statedb.Logs(), gasUsed, err
  183. }