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.

132 lines
4.3 KiB

  1. package state_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. dbm "github.com/tendermint/tm-db"
  6. "github.com/tendermint/tendermint/internal/state"
  7. "github.com/tendermint/tendermint/internal/state/mocks"
  8. "github.com/tendermint/tendermint/internal/test/factory"
  9. "github.com/tendermint/tendermint/types"
  10. "github.com/tendermint/tendermint/version"
  11. )
  12. func TestRollback(t *testing.T) {
  13. var (
  14. height int64 = 100
  15. nextHeight int64 = 101
  16. )
  17. blockStore := &mocks.BlockStore{}
  18. stateStore := setupStateStore(t, height)
  19. initialState, err := stateStore.Load()
  20. require.NoError(t, err)
  21. // perform the rollback over a version bump
  22. newParams := types.DefaultConsensusParams()
  23. newParams.Version.AppVersion = 11
  24. newParams.Block.MaxBytes = 1000
  25. nextState := initialState.Copy()
  26. nextState.LastBlockHeight = nextHeight
  27. nextState.Version.Consensus.App = 11
  28. nextState.LastBlockID = factory.MakeBlockID()
  29. nextState.AppHash = factory.RandomHash()
  30. nextState.LastValidators = initialState.Validators
  31. nextState.Validators = initialState.NextValidators
  32. nextState.NextValidators = initialState.NextValidators.CopyIncrementProposerPriority(1)
  33. nextState.ConsensusParams = *newParams
  34. nextState.LastHeightConsensusParamsChanged = nextHeight + 1
  35. nextState.LastHeightValidatorsChanged = nextHeight + 1
  36. // update the state
  37. require.NoError(t, stateStore.Save(nextState))
  38. block := &types.BlockMeta{
  39. BlockID: initialState.LastBlockID,
  40. Header: types.Header{
  41. Height: initialState.LastBlockHeight,
  42. AppHash: initialState.AppHash,
  43. LastBlockID: factory.MakeBlockID(),
  44. LastResultsHash: initialState.LastResultsHash,
  45. },
  46. }
  47. blockStore.On("LoadBlockMeta", initialState.LastBlockHeight).Return(block)
  48. blockStore.On("Height").Return(nextHeight)
  49. // rollback the state
  50. rollbackHeight, rollbackHash, err := state.Rollback(blockStore, stateStore)
  51. require.NoError(t, err)
  52. require.EqualValues(t, height, rollbackHeight)
  53. require.EqualValues(t, initialState.AppHash, rollbackHash)
  54. blockStore.AssertExpectations(t)
  55. // assert that we've recovered the prior state
  56. loadedState, err := stateStore.Load()
  57. require.NoError(t, err)
  58. require.EqualValues(t, initialState, loadedState)
  59. }
  60. func TestRollbackNoState(t *testing.T) {
  61. stateStore := state.NewStore(dbm.NewMemDB())
  62. blockStore := &mocks.BlockStore{}
  63. _, _, err := state.Rollback(blockStore, stateStore)
  64. require.Error(t, err)
  65. require.Contains(t, err.Error(), "no state found")
  66. }
  67. func TestRollbackNoBlocks(t *testing.T) {
  68. const height = int64(100)
  69. stateStore := setupStateStore(t, height)
  70. blockStore := &mocks.BlockStore{}
  71. blockStore.On("Height").Return(height)
  72. blockStore.On("LoadBlockMeta", height-1).Return(nil)
  73. _, _, err := state.Rollback(blockStore, stateStore)
  74. require.Error(t, err)
  75. require.Contains(t, err.Error(), "block at height 99 not found")
  76. }
  77. func TestRollbackDifferentStateHeight(t *testing.T) {
  78. const height = int64(100)
  79. stateStore := setupStateStore(t, height)
  80. blockStore := &mocks.BlockStore{}
  81. blockStore.On("Height").Return(height + 2)
  82. _, _, err := state.Rollback(blockStore, stateStore)
  83. require.Error(t, err)
  84. require.Equal(t, err.Error(), "statestore height (100) is not one below or equal to blockstore height (102)")
  85. }
  86. func setupStateStore(t *testing.T, height int64) state.Store {
  87. stateStore := state.NewStore(dbm.NewMemDB())
  88. valSet, _ := factory.RandValidatorSet(5, 10)
  89. params := types.DefaultConsensusParams()
  90. params.Version.AppVersion = 10
  91. initialState := state.State{
  92. Version: state.Version{
  93. Consensus: version.Consensus{
  94. Block: version.BlockProtocol,
  95. App: 10,
  96. },
  97. Software: version.TMVersion,
  98. },
  99. ChainID: factory.DefaultTestChainID,
  100. InitialHeight: 10,
  101. LastBlockID: factory.MakeBlockID(),
  102. AppHash: factory.RandomHash(),
  103. LastResultsHash: factory.RandomHash(),
  104. LastBlockHeight: height,
  105. LastValidators: valSet,
  106. Validators: valSet.CopyIncrementProposerPriority(1),
  107. NextValidators: valSet.CopyIncrementProposerPriority(2),
  108. LastHeightValidatorsChanged: height + 1,
  109. ConsensusParams: *params,
  110. LastHeightConsensusParamsChanged: height + 1,
  111. }
  112. require.NoError(t, stateStore.Bootstrap(initialState))
  113. return stateStore
  114. }