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.

103 lines
2.6 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
  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 randAccountBalance(id uint64, status byte) *AccountBalance {
  12. return &AccountBalance{
  13. Account: Account{
  14. Id: id,
  15. PubKey: CRandBytes(32),
  16. },
  17. Balance: RandUInt64(),
  18. Status: status,
  19. }
  20. }
  21. // The first numValidators accounts are validators.
  22. func randGenesisState(numAccounts int, numValidators int) *State {
  23. db := NewMemDB()
  24. accountBalances := make([]*AccountBalance, numAccounts)
  25. for i := 0; i < numAccounts; i++ {
  26. if i < numValidators {
  27. accountBalances[i] = randAccountBalance(uint64(i), AccountBalanceStatusNominal)
  28. } else {
  29. accountBalances[i] = randAccountBalance(uint64(i), AccountBalanceStatusBonded)
  30. }
  31. }
  32. s0 := GenesisState(db, time.Now(), accountBalances)
  33. return s0
  34. }
  35. func TestGenesisSaveLoad(t *testing.T) {
  36. // Generate a state, save & load it.
  37. s0 := randGenesisState(10, 5)
  38. // Figure out what the next state hashes should be.
  39. s0ValsCopy := s0.Validators.Copy()
  40. s0ValsCopy.IncrementAccum()
  41. nextValidationStateHash := s0ValsCopy.Hash()
  42. nextAccountStateHash := s0.AccountBalances.Tree.Hash()
  43. // Mutate the state to append one empty block.
  44. block := &Block{
  45. Header: Header{
  46. Network: Config.Network,
  47. Height: 1,
  48. ValidationStateHash: nextValidationStateHash,
  49. AccountStateHash: nextAccountStateHash,
  50. },
  51. Data: Data{
  52. Txs: []Tx{},
  53. },
  54. }
  55. err := s0.AppendBlock(block)
  56. if err != nil {
  57. t.Error("Error appending initial block:", err)
  58. }
  59. // Save s0
  60. commitTime := time.Now()
  61. s0.Save(commitTime)
  62. // Sanity check s0
  63. //s0.DB.(*MemDB).Print()
  64. if s0.Validators.TotalVotingPower() == 0 {
  65. t.Error("s0 Validators TotalVotingPower should not be 0")
  66. }
  67. if s0.Height != 1 {
  68. t.Error("s0 Height should be 1, got", s0.Height)
  69. }
  70. // Load s1
  71. s1 := LoadState(s0.DB)
  72. // Compare CommitTime
  73. if !s0.CommitTime.Equal(s1.CommitTime) {
  74. t.Error("CommitTime was not the same", s0.CommitTime, s1.CommitTime)
  75. }
  76. // Compare height & blockHash
  77. if s0.Height != s1.Height {
  78. t.Error("Height mismatch")
  79. }
  80. if !bytes.Equal(s0.BlockHash, s1.BlockHash) {
  81. t.Error("BlockHash mismatch")
  82. }
  83. // Compare Validators
  84. if s0.Validators.Size() != s1.Validators.Size() {
  85. t.Error("Validators Size mismatch")
  86. }
  87. if s0.Validators.TotalVotingPower() != s1.Validators.TotalVotingPower() {
  88. t.Error("Validators TotalVotingPower mismatch")
  89. }
  90. if !bytes.Equal(s0.AccountBalances.Tree.Hash(), s1.AccountBalances.Tree.Hash()) {
  91. t.Error("AccountBalance mismatch")
  92. }
  93. }