state_test_util.go 5.6 KB

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