cmd: add integration test for rollback functionality (backport #7315) (#7369)

This commit is contained in:
mergify[bot]
2022-01-05 18:07:06 +01:00
committed by GitHub
parent 7770ab0efd
commit c7d338f712
12 changed files with 133 additions and 47 deletions
+4
View File
@@ -261,6 +261,10 @@ func (evpool *Pool) State() sm.State {
return evpool.state
}
func (evpool *Pool) Close() error {
return evpool.evidenceStore.Close()
}
// IsExpired checks whether evidence or a polc is expired by checking whether a height and time is older
// than set by the evidence consensus parameters
func (evpool *Pool) isExpired(height int64, time time.Time) bool {
+3
View File
@@ -120,6 +120,9 @@ func (r *Reactor) OnStop() {
// panics will occur.
<-r.evidenceCh.Done()
<-r.peerUpdates.Done()
// Close the evidence db
r.evpool.Close()
}
// handleEvidenceMessage handles envelopes sent from peers on the EvidenceChannel.
+14
View File
@@ -29,6 +29,20 @@ func (_m *Store) Bootstrap(_a0 state.State) error {
return r0
}
// Close provides a mock function with given fields:
func (_m *Store) Close() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Load provides a mock function with given fields:
func (_m *Store) Load() (state.State, error) {
ret := _m.Called()
+8 -8
View File
@@ -36,18 +36,18 @@ func Rollback(bs BlockStore, ss Store) (int64, []byte, error) {
}
// state store height is equal to blockstore height. We're good to proceed with rolling back state
rollbackHeight := invalidState.LastBlockHeight
rollbackHeight := invalidState.LastBlockHeight - 1
rollbackBlock := bs.LoadBlockMeta(rollbackHeight)
if rollbackBlock == nil {
return -1, nil, fmt.Errorf("block at height %d not found", rollbackHeight)
}
previousValidatorSet, err := ss.LoadValidators(rollbackHeight - 1)
previousLastValidatorSet, err := ss.LoadValidators(rollbackHeight)
if err != nil {
return -1, nil, err
}
previousParams, err := ss.LoadConsensusParams(rollbackHeight)
previousParams, err := ss.LoadConsensusParams(rollbackHeight + 1)
if err != nil {
return -1, nil, err
}
@@ -55,13 +55,13 @@ func Rollback(bs BlockStore, ss Store) (int64, []byte, error) {
valChangeHeight := invalidState.LastHeightValidatorsChanged
// this can only happen if the validator set changed since the last block
if valChangeHeight > rollbackHeight {
valChangeHeight = rollbackHeight
valChangeHeight = rollbackHeight + 1
}
paramsChangeHeight := invalidState.LastHeightConsensusParamsChanged
// this can only happen if params changed from the last block
if paramsChangeHeight > rollbackHeight {
paramsChangeHeight = rollbackHeight
paramsChangeHeight = rollbackHeight + 1
}
// build the new state from the old state and the prior block
@@ -77,13 +77,13 @@ func Rollback(bs BlockStore, ss Store) (int64, []byte, error) {
ChainID: invalidState.ChainID,
InitialHeight: invalidState.InitialHeight,
LastBlockHeight: invalidState.LastBlockHeight - 1,
LastBlockID: rollbackBlock.Header.LastBlockID,
LastBlockHeight: rollbackBlock.Header.Height,
LastBlockID: rollbackBlock.BlockID,
LastBlockTime: rollbackBlock.Header.Time,
NextValidators: invalidState.Validators,
Validators: invalidState.LastValidators,
LastValidators: previousValidatorSet,
LastValidators: previousLastValidatorSet,
LastHeightValidatorsChanged: valChangeHeight,
ConsensusParams: previousParams,
+22 -23
View File
@@ -15,50 +15,49 @@ import (
func TestRollback(t *testing.T) {
var (
height int64 = 100
appVersion uint64 = 10
height int64 = 100
nextHeight int64 = 101
)
blockStore := &mocks.BlockStore{}
stateStore := setupStateStore(t, height)
initialState, err := stateStore.Load()
require.NoError(t, err)
height++
block := &types.BlockMeta{
Header: types.Header{
Height: height,
AppHash: initialState.AppHash,
LastBlockID: initialState.LastBlockID,
LastResultsHash: initialState.LastResultsHash,
},
}
blockStore.On("LoadBlockMeta", height).Return(block)
blockStore.On("Height").Return(height)
// perform the rollback over a version bump
appVersion++
newParams := types.DefaultConsensusParams()
newParams.Version.AppVersion = appVersion
newParams.Version.AppVersion = 11
newParams.Block.MaxBytes = 1000
nextState := initialState.Copy()
nextState.LastBlockHeight = height
nextState.Version.Consensus.App = appVersion
nextState.LastBlockHeight = nextHeight
nextState.Version.Consensus.App = 11
nextState.LastBlockID = factory.MakeBlockID()
nextState.AppHash = factory.RandomHash()
nextState.LastValidators = initialState.Validators
nextState.Validators = initialState.NextValidators
nextState.NextValidators = initialState.NextValidators.CopyIncrementProposerPriority(1)
nextState.ConsensusParams = *newParams
nextState.LastHeightConsensusParamsChanged = height + 1
nextState.LastHeightValidatorsChanged = height + 1
nextState.LastHeightConsensusParamsChanged = nextHeight + 1
nextState.LastHeightValidatorsChanged = nextHeight + 1
// update the state
require.NoError(t, stateStore.Save(nextState))
block := &types.BlockMeta{
BlockID: initialState.LastBlockID,
Header: types.Header{
Height: initialState.LastBlockHeight,
AppHash: initialState.AppHash,
LastBlockID: factory.MakeBlockID(),
LastResultsHash: initialState.LastResultsHash,
},
}
blockStore.On("LoadBlockMeta", initialState.LastBlockHeight).Return(block)
blockStore.On("Height").Return(nextHeight)
// rollback the state
rollbackHeight, rollbackHash, err := state.Rollback(blockStore, stateStore)
require.NoError(t, err)
require.EqualValues(t, int64(100), rollbackHeight)
require.EqualValues(t, height, rollbackHeight)
require.EqualValues(t, initialState.AppHash, rollbackHash)
blockStore.AssertExpectations(t)
@@ -82,11 +81,11 @@ func TestRollbackNoBlocks(t *testing.T) {
stateStore := setupStateStore(t, height)
blockStore := &mocks.BlockStore{}
blockStore.On("Height").Return(height)
blockStore.On("LoadBlockMeta", height).Return(nil)
blockStore.On("LoadBlockMeta", height-1).Return(nil)
_, _, err := state.Rollback(blockStore, stateStore)
require.Error(t, err)
require.Contains(t, err.Error(), "block at height 100 not found")
require.Contains(t, err.Error(), "block at height 99 not found")
}
func TestRollbackDifferentStateHeight(t *testing.T) {
+6
View File
@@ -93,6 +93,8 @@ type Store interface {
Bootstrap(State) error
// PruneStates takes the height from which to prune up to (exclusive)
PruneStates(int64) error
// Close closes the connection with the database
Close() error
}
// dbStore wraps a db (github.com/tendermint/tm-db)
@@ -663,3 +665,7 @@ func (store dbStore) saveConsensusParamsInfo(
return batch.Set(consensusParamsKey(nextHeight), bz)
}
func (store dbStore) Close() error {
return store.db.Close()
}
+4
View File
@@ -572,6 +572,10 @@ func (bs *BlockStore) SaveSignedHeader(sh *types.SignedHeader, blockID types.Blo
return batch.Close()
}
func (bs *BlockStore) Close() error {
return bs.db.Close()
}
//---------------------------------- KEY ENCODING -----------------------------------------
// key prefixes