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.

137 lines
4.4 KiB

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