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.

201 lines
6.1 KiB

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. // 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 := GetState(stateDB, config.GenesisFile())
  20. state.SetLogger(log.TestingLogger())
  21. tearDown := func(t *testing.T) {}
  22. return tearDown, stateDB, state
  23. }
  24. func TestStateCopy(t *testing.T) {
  25. tearDown, _, state := setupTestCase(t)
  26. defer tearDown(t)
  27. assert := assert.New(t)
  28. stateCopy := state.Copy()
  29. assert.True(state.Equals(stateCopy),
  30. cmn.Fmt("exppppppected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state))
  31. stateCopy.LastBlockHeight++
  32. assert.False(state.Equals(stateCopy), cmn.Fmt("expected states to be different. got same %v", state))
  33. }
  34. func TestStateSaveLoad(t *testing.T) {
  35. tearDown, stateDB, state := setupTestCase(t)
  36. defer tearDown(t)
  37. state.LastBlockHeight++
  38. state.Save()
  39. loadedState := LoadState(stateDB)
  40. assert.True(state.Equals(loadedState),
  41. cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", loadedState, state))
  42. }
  43. func TestABCIResponsesSaveLoad(t *testing.T) {
  44. tearDown, _, state := setupTestCase(t)
  45. defer tearDown(t)
  46. assert := assert.New(t)
  47. state.LastBlockHeight++
  48. // build mock responses
  49. block := makeBlock(2, state)
  50. abciResponses := NewABCIResponses(block)
  51. abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo")}
  52. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok"}
  53. abciResponses.EndBlock = abci.ResponseEndBlock{Diffs: []*abci.Validator{
  54. {
  55. PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
  56. Power: 10,
  57. },
  58. }}
  59. abciResponses.txs = nil
  60. state.SaveABCIResponses(abciResponses)
  61. abciResponses2 := state.LoadABCIResponses()
  62. assert.Equal(abciResponses, abciResponses2,
  63. cmn.Fmt("ABCIResponses don't match: Got %v, Expected %v", abciResponses2, abciResponses))
  64. }
  65. func TestValidatorSimpleSaveLoad(t *testing.T) {
  66. tearDown, _, state := setupTestCase(t)
  67. defer tearDown(t)
  68. assert := assert.New(t)
  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++
  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. tearDown, _, state := setupTestCase(t)
  94. defer tearDown(t)
  95. assert := assert.New(t)
  96. // change vals at these heights
  97. changeHeights := []int{1, 2, 4, 5, 10, 15, 16, 17, 20}
  98. N := len(changeHeights)
  99. // each valset is just one validator.
  100. // create list of them
  101. pubkeys := make([]crypto.PubKey, N+1)
  102. pubkeys[0] = state.GenesisDoc.Validators[0].PubKey
  103. for i := 1; i < N+1; i++ {
  104. pubkeys[i] = crypto.GenPrivKeyEd25519().PubKey()
  105. }
  106. // build the validator history by running SetBlockAndValidators
  107. // with the right validator set for each height
  108. highestHeight := changeHeights[N-1] + 5
  109. changeIndex := 0
  110. pubkey := pubkeys[changeIndex]
  111. for i := 1; i < highestHeight; i++ {
  112. // when we get to a change height,
  113. // use the next pubkey
  114. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  115. changeIndex++
  116. pubkey = pubkeys[changeIndex]
  117. }
  118. header, parts, responses := makeHeaderPartsResponses(state, i, pubkey)
  119. state.SetBlockAndValidators(header, parts, responses)
  120. state.saveValidatorsInfo()
  121. }
  122. // make all the test cases by using the same validator until after the change
  123. testCases := make([]valChangeTestCase, highestHeight)
  124. changeIndex = 0
  125. pubkey = pubkeys[changeIndex]
  126. for i := 1; i < highestHeight+1; i++ {
  127. // we we get to the height after a change height
  128. // use the next pubkey (note our counter starts at 0 this time)
  129. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  130. changeIndex++
  131. pubkey = pubkeys[changeIndex]
  132. }
  133. testCases[i-1] = valChangeTestCase{i, pubkey}
  134. }
  135. for _, testCase := range testCases {
  136. v, err := state.LoadValidators(testCase.height)
  137. assert.Nil(err, fmt.Sprintf("expected no err at height %d", testCase.height))
  138. assert.Equal(v.Size(), 1, "validator set size is greater than 1: %d", v.Size())
  139. addr, _ := v.GetByIndex(0)
  140. assert.Equal(addr, testCase.vals.Address(), fmt.Sprintf("unexpected pubkey at height %d", testCase.height))
  141. }
  142. }
  143. func makeHeaderPartsResponses(state *State, height int,
  144. pubkey crypto.PubKey) (*types.Header, types.PartSetHeader, *ABCIResponses) {
  145. block := makeBlock(height, state)
  146. _, val := state.Validators.GetByIndex(0)
  147. abciResponses := &ABCIResponses{
  148. Height: height,
  149. }
  150. // if the pubkey is new, remove the old and add the new
  151. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  152. abciResponses.EndBlock = abci.ResponseEndBlock{
  153. Diffs: []*abci.Validator{
  154. {val.PubKey.Bytes(), 0},
  155. {pubkey.Bytes(), 10},
  156. },
  157. }
  158. }
  159. return block.Header, types.PartSetHeader{}, abciResponses
  160. }
  161. type valChangeTestCase struct {
  162. height int
  163. vals crypto.PubKey
  164. }