Browse Source

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
release/v0.33.4
Erik Grinaker 4 years ago
committed by GitHub
parent
commit
82b585cd5e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 11 deletions
  1. +3
    -6
      store/store.go
  2. +6
    -5
      store/store_test.go

+ 3
- 6
store/store.go View File

@ -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)
}


+ 6
- 5
store/store_test.go View File

@ -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()),
},
{


Loading…
Cancel
Save