From 10bfbae516c64226d284c8af929844e070fe7f1a Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Tue, 13 Nov 2018 19:43:30 +0800 Subject: [PATCH] Add RetreatLastBlock --- blockchain/store.go | 13 +++++++++++++ consensus/replay_test.go | 2 ++ state/services.go | 1 + 3 files changed, 16 insertions(+) diff --git a/blockchain/store.go b/blockchain/store.go index f02d4facbcf..e8e2a4f7c78 100644 --- a/blockchain/store.go +++ b/blockchain/store.go @@ -186,6 +186,19 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s bs.db.SetSync(nil, nil) } +func (bs *BlockStore) RetreatLastBlock() { + height := bs.height + + bs.db.Delete(calcBlockMetaKey(height)) + bs.db.Delete(calcBlockCommitKey(height-1)) + bs.db.Delete(calcSeenCommitKey(height)) + + BlockStoreStateJSON{Height: height-1 }.Save(bs.db) + + // Flush + bs.db.SetSync(nil, nil) +} + func (bs *BlockStore) saveBlockPart(height int64, index int, part *types.Part) { if height != bs.Height()+1 { cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height)) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index fa0ec040897..b7821ee1c6a 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -629,6 +629,8 @@ func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil } func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { } +func (bs *mockBlockStore) RetreatLastBlock() { +} func (bs *mockBlockStore) LoadBlockCommit(height int64) *types.Commit { return bs.commits[height-1] } diff --git a/state/services.go b/state/services.go index c51fa97521a..aeba9fd2fbd 100644 --- a/state/services.go +++ b/state/services.go @@ -65,6 +65,7 @@ type BlockStoreRPC interface { type BlockStore interface { BlockStoreRPC SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) + RetreatLastBlock() } //-----------------------------------------------------------------------------------------------------