state_test_util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package tests
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. "strconv"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core"
  9. "github.com/ethereum/go-ethereum/core/state"
  10. "github.com/ethereum/go-ethereum/core/vm"
  11. "github.com/ethereum/go-ethereum/crypto"
  12. "github.com/ethereum/go-ethereum/ethdb"
  13. )
  14. func RunStateTest(p string) error {
  15. skipTest := make(map[string]bool, len(stateSkipTests))
  16. for _, name := range stateSkipTests {
  17. skipTest[name] = true
  18. }
  19. tests := make(map[string]VmTest)
  20. CreateFileTests(p, &tests)
  21. for name, test := range tests {
  22. if skipTest[name] {
  23. fmt.Println("Skipping state test", name)
  24. return nil
  25. }
  26. db, _ := ethdb.NewMemDatabase()
  27. statedb := state.New(common.Hash{}, db)
  28. for addr, account := range test.Pre {
  29. obj := StateObjectFromAccount(db, addr, account)
  30. statedb.SetStateObject(obj)
  31. for a, v := range account.Storage {
  32. obj.SetState(common.HexToHash(a), common.HexToHash(s))
  33. }
  34. }
  35. // XXX Yeah, yeah...
  36. env := make(map[string]string)
  37. env["currentCoinbase"] = test.Env.CurrentCoinbase
  38. env["currentDifficulty"] = test.Env.CurrentDifficulty
  39. env["currentGasLimit"] = test.Env.CurrentGasLimit
  40. env["currentNumber"] = test.Env.CurrentNumber
  41. env["previousHash"] = test.Env.PreviousHash
  42. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  43. env["currentTimestamp"] = strconv.Itoa(int(n))
  44. } else {
  45. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  46. }
  47. var (
  48. ret []byte
  49. // gas *big.Int
  50. // err error
  51. logs state.Logs
  52. )
  53. ret, logs, _, _ = RunState(statedb, env, test.Transaction)
  54. // // Compare expected and actual return
  55. // switch name {
  56. // // the memory required for these tests (4294967297 bytes) would take too much time.
  57. // // on 19 May 2015 decided to skip these tests their output.
  58. // case "mload32bitBound_return", "mload32bitBound_return2":
  59. // default:
  60. rexp := common.FromHex(test.Out)
  61. if bytes.Compare(rexp, ret) != 0 {
  62. return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
  63. }
  64. // }
  65. // check post state
  66. for addr, account := range test.Post {
  67. obj := statedb.GetStateObject(common.HexToAddress(addr))
  68. if obj == nil {
  69. continue
  70. }
  71. if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
  72. return fmt.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance()))
  73. }
  74. if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
  75. return fmt.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
  76. }
  77. for addr, value := range account.Storage {
  78. v := obj.GetState(common.HexToHash(addr)).Bytes()
  79. vexp := common.FromHex(value)
  80. if bytes.Compare(v, vexp) != 0 {
  81. return fmt.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
  82. }
  83. }
  84. }
  85. statedb.Sync()
  86. //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
  87. if common.HexToHash(test.PostStateRoot) != statedb.Root() {
  88. return fmt.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
  89. }
  90. // check logs
  91. if len(test.Logs) > 0 {
  92. lerr := checkLogs(test.Logs, logs)
  93. if lerr != nil {
  94. return fmt.Errorf("'%s' ", name, lerr.Error())
  95. }
  96. }
  97. fmt.Println("State test passed: ", name)
  98. //fmt.Println(string(statedb.Dump()))
  99. }
  100. return nil
  101. }
  102. func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) {
  103. var (
  104. keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"])))
  105. data = common.FromHex(tx["data"])
  106. gas = common.Big(tx["gasLimit"])
  107. price = common.Big(tx["gasPrice"])
  108. value = common.Big(tx["value"])
  109. nonce = common.Big(tx["nonce"]).Uint64()
  110. caddr = common.HexToAddress(env["currentCoinbase"])
  111. )
  112. var to *common.Address
  113. if len(tx["to"]) > 2 {
  114. t := common.HexToAddress(tx["to"])
  115. to = &t
  116. }
  117. // Set pre compiled contracts
  118. vm.Precompiled = vm.PrecompiledContracts()
  119. snapshot := statedb.Copy()
  120. coinbase := statedb.GetOrNewStateObject(caddr)
  121. coinbase.SetGasPool(common.Big(env["currentGasLimit"]))
  122. message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce)
  123. vmenv := NewEnvFromMap(statedb, env, tx)
  124. vmenv.origin = common.BytesToAddress(keyPair.Address())
  125. ret, _, err := core.ApplyMessage(vmenv, message, coinbase)
  126. if core.IsNonceErr(err) || core.IsInvalidTxErr(err) {
  127. statedb.Set(snapshot)
  128. }
  129. statedb.Update()
  130. return ret, vmenv.state.Logs(), vmenv.Gas, err
  131. }