Skip to content

Commit

Permalink
Merge pull request #6133 from filecoin-project/fix/head-change-panic-…
Browse files Browse the repository at this point in the history
…splitstore

fix(splitstore): fix a panic on revert-only head changes
  • Loading branch information
Stebalien authored Apr 29, 2021
2 parents b23837a + f353a79 commit 83ec2b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions blockstore/splitstore/splitstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ func (s *SplitStore) Close() error {
}

func (s *SplitStore) HeadChange(_, apply []*types.TipSet) error {
// Revert only.
if len(apply) == 0 {
return nil
}

s.mx.Lock()
curTs := apply[len(apply)-1]
epoch := curTs.Height()
Expand Down
28 changes: 26 additions & 2 deletions blockstore/splitstore/splitstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
}

func testSplitStore(t *testing.T, cfg *Config) {
chain := &mockChain{}
chain := &mockChain{t: t}
// genesis
genBlock := mock.MkBlock(nil, 0, 0)
genTs := mock.TipSet(genBlock)
Expand Down Expand Up @@ -169,6 +169,9 @@ func testSplitStore(t *testing.T, cfg *Config) {
t.Errorf("expected %d hot blocks, but got %d", 7, hotCnt)
}
}

// Make sure we can revert without panicking.
chain.revert(2)
}

func TestSplitStoreSimpleCompaction(t *testing.T) {
Expand All @@ -191,6 +194,8 @@ func TestSplitStoreFullCompactionWithGC(t *testing.T) {
}

type mockChain struct {
t testing.TB

sync.Mutex
tipsets []*types.TipSet
listener func(revert []*types.TipSet, apply []*types.TipSet) error
Expand All @@ -204,7 +209,26 @@ func (c *mockChain) push(ts *types.TipSet) {
if c.listener != nil {
err := c.listener(nil, []*types.TipSet{ts})
if err != nil {
log.Errorf("mockchain: error dispatching listener: %s", err)
c.t.Errorf("mockchain: error dispatching listener: %s", err)
}
}
}

func (c *mockChain) revert(count int) {
c.Lock()
revert := make([]*types.TipSet, count)
if count > len(c.tipsets) {
c.Unlock()
c.t.Fatalf("not enough tipsets to revert")
}
copy(revert, c.tipsets[len(c.tipsets)-count:])
c.tipsets = c.tipsets[:len(c.tipsets)-count]
c.Unlock()

if c.listener != nil {
err := c.listener(revert, nil)
if err != nil {
c.t.Errorf("mockchain: error dispatching listener: %s", err)
}
}
}
Expand Down

0 comments on commit 83ec2b5

Please sign in to comment.