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.

203 lines
6.2 KiB

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