From 82b585cd5ecfeaa59b916ecff9b6f6412241e377 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Mon, 20 Apr 2020 12:18:03 +0200 Subject: [PATCH] blockstore: allow initial SaveBlock() at any height Followup from #4588. Allow the first `SaveBlock()` call in an empty block store to be at any height, to start from a truncated block history. Subsequent `SaveBlock()` calls must be for contiguous blocks. ______ For contributor use: - [x] Wrote tests - [ ] ~Updated CHANGELOG_PENDING.md~ - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [x] Updated relevant documentation (`docs/`) and code comments - [x] Re-reviewed `Files changed` in the Github PR explorer --- store/store.go | 9 +++------ store/store_test.go | 11 ++++++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/store/store.go b/store/store.go index 38a53d590..c971a9a15 100644 --- a/store/store.go +++ b/store/store.go @@ -275,7 +275,7 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s height := block.Height hash := block.Hash() - if g, w := height, bs.Height()+1; g != w { + if g, w := height, bs.Height()+1; bs.Base() > 0 && g != w { panic(fmt.Sprintf("BlockStore can only save contiguous blocks. Wanted %v, got %v", w, g)) } if !blockParts.IsComplete() { @@ -306,8 +306,8 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s // Done! bs.mtx.Lock() bs.height = height - if bs.base == 0 && height == 1 { - bs.base = 1 + if bs.base == 0 { + bs.base = height } bs.mtx.Unlock() @@ -319,9 +319,6 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s } func (bs *BlockStore) saveBlockPart(height int64, index int, part *types.Part) { - if height != bs.Height()+1 { - panic(fmt.Sprintf("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height)) - } partBytes := cdc.MustMarshalBinaryBare(part) bs.db.Set(calcBlockPartKey(height, index), partBytes) } diff --git a/store/store_test.go b/store/store_test.go index 16f52aa88..3b61604e1 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -187,8 +187,6 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { ChainID: "block_test", Time: tmtime.Now(), } - header2 := header1 - header2.Height = 4 // End of setup, test data @@ -218,9 +216,12 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }, { - block: newBlock(header2, commitAtH10), - parts: uncontiguousPartSet, - wantPanic: "only save contiguous blocks", // and incomplete and uncontiguous parts + block: newBlock( // New block at height 5 in empty block store is fine + types.Header{Height: 5, ChainID: "block_test", Time: tmtime.Now()}, + makeTestCommit(5, tmtime.Now()), + ), + parts: validPartSet, + seenCommit: makeTestCommit(5, tmtime.Now()), }, {