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.

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