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.

199 lines
6.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. abci "github.com/tendermint/abci/types"
  8. crypto "github.com/tendermint/go-crypto"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. dbm "github.com/tendermint/tmlibs/db"
  11. "github.com/tendermint/tmlibs/log"
  12. cfg "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. func TestStateCopyEquals(t *testing.T) {
  16. assert := assert.New(t)
  17. config := cfg.ResetTestRoot("state_")
  18. // Get State db
  19. stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
  20. state := GetState(stateDB, config.GenesisFile())
  21. state.SetLogger(log.TestingLogger())
  22. stateCopy := state.Copy()
  23. assert.True(state.Equals(stateCopy), cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state))
  24. stateCopy.LastBlockHeight += 1
  25. assert.False(state.Equals(stateCopy), cmn.Fmt("expected states to be different. got same %v", state))
  26. }
  27. func TestStateSaveLoad(t *testing.T) {
  28. assert := assert.New(t)
  29. config := cfg.ResetTestRoot("state_")
  30. // Get State db
  31. stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
  32. state := GetState(stateDB, config.GenesisFile())
  33. state.SetLogger(log.TestingLogger())
  34. state.LastBlockHeight += 1
  35. state.Save()
  36. loadedState := LoadState(stateDB)
  37. assert.True(state.Equals(loadedState), cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", loadedState, state))
  38. }
  39. func TestABCIResponsesSaveLoad(t *testing.T) {
  40. assert := assert.New(t)
  41. config := cfg.ResetTestRoot("state_")
  42. stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
  43. state := GetState(stateDB, config.GenesisFile())
  44. state.SetLogger(log.TestingLogger())
  45. state.LastBlockHeight += 1
  46. // build mock responses
  47. block := makeBlock(2, state)
  48. abciResponses := NewABCIResponses(block)
  49. abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo")}
  50. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok"}
  51. abciResponses.EndBlock = abci.ResponseEndBlock{Diffs: []*abci.Validator{
  52. {
  53. PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
  54. Power: 10,
  55. },
  56. }}
  57. abciResponses.txs = nil
  58. state.SaveABCIResponses(abciResponses)
  59. abciResponses2 := state.LoadABCIResponses()
  60. assert.Equal(abciResponses, abciResponses2, cmn.Fmt("ABCIResponses don't match: Got %v, Expected %v", abciResponses2, abciResponses))
  61. }
  62. func TestValidatorSimpleSaveLoad(t *testing.T) {
  63. assert := assert.New(t)
  64. config := cfg.ResetTestRoot("state_")
  65. // Get State db
  66. stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
  67. state := GetState(stateDB, config.GenesisFile())
  68. state.SetLogger(log.TestingLogger())
  69. // cant load anything for height 0
  70. v, err := state.LoadValidators(0)
  71. assert.IsType(ErrNoValSetForHeight{}, err, "expected err at height 0")
  72. // should be able to load for height 1
  73. v, err = state.LoadValidators(1)
  74. assert.Nil(err, "expected no err at height 1")
  75. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  76. // increment height, save; should be able to load for next height
  77. state.LastBlockHeight += 1
  78. state.saveValidatorsInfo()
  79. v, err = state.LoadValidators(state.LastBlockHeight + 1)
  80. assert.Nil(err, "expected no err")
  81. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  82. // increment height, save; should be able to load for next height
  83. state.LastBlockHeight += 10
  84. state.saveValidatorsInfo()
  85. v, err = state.LoadValidators(state.LastBlockHeight + 1)
  86. assert.Nil(err, "expected no err")
  87. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  88. // should be able to load for next next height
  89. _, err = state.LoadValidators(state.LastBlockHeight + 2)
  90. assert.IsType(ErrNoValSetForHeight{}, err, "expected err at unknown height")
  91. }
  92. func TestValidatorChangesSaveLoad(t *testing.T) {
  93. assert := assert.New(t)
  94. config := cfg.ResetTestRoot("state_")
  95. // Get State db
  96. stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
  97. state := GetState(stateDB, config.GenesisFile())
  98. state.SetLogger(log.TestingLogger())
  99. // change vals at these heights
  100. changeHeights := []int{1, 2, 4, 5, 10, 15, 16, 17, 20}
  101. N := len(changeHeights)
  102. // each valset is just one validator.
  103. // create list of them
  104. pubkeys := make([]crypto.PubKey, N+1)
  105. pubkeys[0] = state.GenesisDoc.Validators[0].PubKey
  106. for i := 1; i < N+1; i++ {
  107. pubkeys[i] = crypto.GenPrivKeyEd25519().PubKey()
  108. }
  109. // build the validator history by running SetBlockAndValidators
  110. // with the right validator set for each height
  111. highestHeight := changeHeights[N-1] + 5
  112. changeIndex := 0
  113. pubkey := pubkeys[changeIndex]
  114. for i := 1; i < highestHeight; i++ {
  115. // when we get to a change height,
  116. // use the next pubkey
  117. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  118. changeIndex += 1
  119. pubkey = pubkeys[changeIndex]
  120. }
  121. header, parts, responses := makeHeaderPartsResponses(state, i, pubkey)
  122. state.SetBlockAndValidators(header, parts, responses)
  123. state.saveValidatorsInfo()
  124. }
  125. // make all the test cases by using the same validator until after the change
  126. testCases := make([]valChangeTestCase, highestHeight)
  127. changeIndex = 0
  128. pubkey = pubkeys[changeIndex]
  129. for i := 1; i < highestHeight+1; i++ {
  130. // we we get to the height after a change height
  131. // use the next pubkey (note our counter starts at 0 this time)
  132. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  133. changeIndex += 1
  134. pubkey = pubkeys[changeIndex]
  135. }
  136. testCases[i-1] = valChangeTestCase{i, pubkey}
  137. }
  138. for _, testCase := range testCases {
  139. v, err := state.LoadValidators(testCase.height)
  140. assert.Nil(err, fmt.Sprintf("expected no err at height %d", testCase.height))
  141. assert.Equal(v.Size(), 1, "validator set size is greater than 1: %d", v.Size())
  142. addr, _ := v.GetByIndex(0)
  143. assert.Equal(addr, testCase.vals.Address(), fmt.Sprintf("unexpected pubkey at height %d", testCase.height))
  144. }
  145. }
  146. func makeHeaderPartsResponses(state *State, height int, pubkey crypto.PubKey) (*types.Header, types.PartSetHeader, *ABCIResponses) {
  147. block := makeBlock(height, state)
  148. _, val := state.Validators.GetByIndex(0)
  149. abciResponses := &ABCIResponses{
  150. Height: height,
  151. }
  152. // if the pubkey is new, remove the old and add the new
  153. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  154. abciResponses.EndBlock = abci.ResponseEndBlock{
  155. Diffs: []*abci.Validator{
  156. {val.PubKey.Bytes(), 0},
  157. {pubkey.Bytes(), 10},
  158. },
  159. }
  160. }
  161. return block.Header, types.PartSetHeader{}, abciResponses
  162. }
  163. type valChangeTestCase struct {
  164. height int
  165. vals crypto.PubKey
  166. }