From 22e2484878d7b3faba0b66641e39ffa05b8d9c7f Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Tue, 13 Aug 2019 16:58:06 -0700 Subject: [PATCH] start revert logic --- state/execution.go | 20 ++++++++++++++++++++ store/store.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/state/execution.go b/state/execution.go index 2ac2e968..9268fc28 100644 --- a/state/execution.go +++ b/state/execution.go @@ -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. diff --git a/store/store.go b/store/store.go index c16d5efe..d51a407e 100644 --- a/store/store.go +++ b/store/store.go @@ -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 {