start revert logic

This commit is contained in:
Aditya Sripal
2019-08-13 16:58:06 -07:00
parent 7fe02a04db
commit 22e2484878
2 changed files with 59 additions and 0 deletions

View File

@ -442,6 +442,26 @@ func updateState(
}, nil
}
func revertState(
state State,
reverted *types.Block,
newHead *types.Block,
) State {
var lastValidators *types.ValidatorSet
lastValidators, _ := LoadValidators(blockExec.db, lastValidatorsInfo.LastHeightChanged)
return State{
Version: state.Version,
ChainID: state.ChainID,
LastBlockHeight: state.LastBlockHeight - 1,
LastBlockTotalTx: state.LastBlockTotalTx - len(reverted.Data.Txs),
LastBlockID: reverted.Header.LastBlockID,
LastBlockTime: newHead.Header.LastBlockTime,
NextValidators: state.Validators.Copy(),
Validators: state.LastValidators.Copy(),
LastValidators: lastValidators.Copy(),
}
}
// Fire NewBlock, NewBlockHeader.
// Fire TxEvent for every tx.
// NOTE: if Tendermint crashes before commit, some or all of these events may be published again.

View File

@ -195,6 +195,45 @@ func (bs *BlockStore) saveBlockPart(height int64, index int, part *types.Part) {
bs.db.Set(calcBlockPartKey(height, index), partBytes)
}
// RevertBlock reverts the store changes from the latest SaveBlock.
// Only modifies blockstore, does not change TM state
// For now, seenCommit for reverted block remains in db
// Returns the block that got reverted and the newest lastBlock
func (bs *BlockStore) RevertBlock() (reverted *types.Block, newHead *types.Block) {
bs.mtx.RLock()
latest := bs.height
bs.mtx.RUnlock()
// Load block to return later
reverted = bs.LoadBlock(latest)
newHead = bs.LoadBlock(latest - 1)
// Delete block meta
blockMeta := bs.LoadBlockMeta(latest)
blockID = blockMeta.BlockID
bs.db.Delete(calcBlockMetaKey(latest))
// Delete block parts
for i := 0; i < blockID.PartsHeader.Total; i++ {
bs.db.Delete(calcBlockPartKey(latest, i))
}
// Delete block commit
bs.db.Delete(calcBlockCommitKey(latest - 1))
// Update BlockStateStateJSON descriptor
BlockStateStateJSON{Height: latest - 1}.Save(bs.db)
// Update height
bs.mtx.Lock()
bs.height = latest - 1
bs.mtx.Unlock()
// Flush
bs.db.SetSync(nil, nil)
}
//-----------------------------------------------------------------------------
func calcBlockMetaKey(height int64) []byte {