You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package state
  2. import (
  3. . "github.com/tendermint/tendermint/blocks"
  4. . "github.com/tendermint/tendermint/common"
  5. . "github.com/tendermint/tendermint/config"
  6. . "github.com/tendermint/tendermint/db"
  7. "bytes"
  8. "testing"
  9. "time"
  10. )
  11. func randAccountDetail(id uint64, status byte) *AccountDetail {
  12. return &AccountDetail{
  13. Account: Account{
  14. Id: id,
  15. PubKey: CRandBytes(32),
  16. },
  17. Sequence: RandUInt(),
  18. Balance: RandUInt64(),
  19. Status: status,
  20. }
  21. }
  22. // The first numValidators accounts are validators.
  23. func randGenesisState(numAccounts int, numValidators int) *State {
  24. db := NewMemDB()
  25. accountDetails := make([]*AccountDetail, numAccounts)
  26. for i := 0; i < numAccounts; i++ {
  27. if i < numValidators {
  28. accountDetails[i] = randAccountDetail(uint64(i), AccountStatusNominal)
  29. } else {
  30. accountDetails[i] = randAccountDetail(uint64(i), AccountStatusBonded)
  31. }
  32. }
  33. s0 := GenesisState(db, time.Now(), accountDetails)
  34. s0.Save(time.Now())
  35. return s0
  36. }
  37. func TestCopyState(t *testing.T) {
  38. // Generate a state
  39. s0 := randGenesisState(10, 5)
  40. s0Hash := s0.Hash()
  41. if len(s0Hash) == 0 {
  42. t.Error("Expected state hash")
  43. }
  44. // Check hash of copy
  45. s0Copy := s0.Copy()
  46. if !bytes.Equal(s0Hash, s0Copy.Hash()) {
  47. t.Error("Expected state copy hash to be the same")
  48. }
  49. // Mutate the original.
  50. _, accDet_ := s0.AccountDetails.GetByIndex(0)
  51. accDet := accDet_.(*AccountDetail)
  52. if accDet == nil {
  53. t.Error("Expected state to have an account")
  54. }
  55. accDet.Balance += 1
  56. s0.AccountDetails.Set(accDet.Id, accDet)
  57. if bytes.Equal(s0Hash, s0.Hash()) {
  58. t.Error("Expected state hash to have changed")
  59. }
  60. if !bytes.Equal(s0Hash, s0Copy.Hash()) {
  61. t.Error("Expected state copy hash to have not changed")
  62. }
  63. }
  64. func TestGenesisSaveLoad(t *testing.T) {
  65. // Generate a state, save & load it.
  66. s0 := randGenesisState(10, 5)
  67. // Mutate the state to append one empty block.
  68. block := &Block{
  69. Header: Header{
  70. Network: Config.Network,
  71. Height: 1,
  72. StateHash: nil,
  73. },
  74. Data: Data{
  75. Txs: []Tx{},
  76. },
  77. }
  78. // The second argument to AppendBlock() is false,
  79. // which sets Block.Header.StateHash.
  80. err := s0.Copy().AppendBlock(block, false)
  81. if err != nil {
  82. t.Error("Error appending initial block:", err)
  83. }
  84. if len(block.Header.StateHash) == 0 {
  85. t.Error("Expected StateHash but got nothing.")
  86. }
  87. // Now append the block to s0.
  88. // This time we also check the StateHash (as computed above).
  89. err = s0.AppendBlock(block, true)
  90. if err != nil {
  91. t.Error("Error appending initial block:", err)
  92. }
  93. // Save s0
  94. commitTime := time.Now()
  95. s0.Save(commitTime)
  96. // Sanity check s0
  97. //s0.DB.(*MemDB).Print()
  98. if s0.BondedValidators.TotalVotingPower() == 0 {
  99. t.Error("s0 BondedValidators TotalVotingPower should not be 0")
  100. }
  101. if s0.Height != 1 {
  102. t.Error("s0 Height should be 1, got", s0.Height)
  103. }
  104. // Load s1
  105. s1 := LoadState(s0.DB)
  106. // Compare CommitTime
  107. if !s0.CommitTime.Equal(s1.CommitTime) {
  108. t.Error("CommitTime was not the same", s0.CommitTime, s1.CommitTime)
  109. }
  110. // Compare height & blockHash
  111. if s0.Height != s1.Height {
  112. t.Error("Height mismatch")
  113. }
  114. if !bytes.Equal(s0.BlockHash, s1.BlockHash) {
  115. t.Error("BlockHash mismatch")
  116. }
  117. // Compare state merkle trees
  118. if s0.BondedValidators.Size() != s1.BondedValidators.Size() {
  119. t.Error("BondedValidators Size mismatch")
  120. }
  121. if s0.BondedValidators.TotalVotingPower() != s1.BondedValidators.TotalVotingPower() {
  122. t.Error("BondedValidators TotalVotingPower mismatch")
  123. }
  124. if bytes.Equal(s0.BondedValidators.Hash(), s1.BondedValidators.Hash()) {
  125. // The BondedValidators hash should have changed because
  126. // each AppendBlock() calls IncrementAccum(),
  127. // changing each validator's Accum.
  128. t.Error("BondedValidators hash should have changed")
  129. }
  130. if s0.UnbondingValidators.Size() != s1.UnbondingValidators.Size() {
  131. t.Error("UnbondingValidators Size mismatch")
  132. }
  133. if s0.UnbondingValidators.TotalVotingPower() != s1.UnbondingValidators.TotalVotingPower() {
  134. t.Error("UnbondingValidators TotalVotingPower mismatch")
  135. }
  136. if !bytes.Equal(s0.UnbondingValidators.Hash(), s1.UnbondingValidators.Hash()) {
  137. t.Error("UnbondingValidators hash mismatch")
  138. }
  139. if !bytes.Equal(s0.AccountDetails.Hash(), s1.AccountDetails.Hash()) {
  140. t.Error("AccountDetail mismatch")
  141. }
  142. }