Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service/header): lazily load Store head #458

Merged
merged 4 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Month, DD, YYYY

### BUG FIXES

- [fix(service/header): lazily load Store head #458](https://github.com/celestiaorg/celestia-node/pull/458) [@Wondertan](https://github.com/Wondertan)
- [fix(service/header): allow some clock drift during verification #435](https://github.com/celestiaorg/celestia-node/pull/435) [@Wondertan](https://github.com/Wondertan)
- [service/header: fix ExtendedHeader message duplicates on the network #409](https://github.com/celestiaorg/celestia-node/pull/409) [@Wondertan](https://github.com/Wondertan)
- [fix(header/service): #339 race](https://github.com/celestiaorg/celestia-node/pull/343) [@Wondertan](https://github.com/Wondertan)
Expand Down
29 changes: 15 additions & 14 deletions service/header/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,11 @@ func (s *store) Init(_ context.Context, initial *ExtendedHeader) error {
return err
}

log.Infow("initial head", "height", initial.Height, "hash", initial.Hash())
log.Infow("initialized head", "height", initial.Height, "hash", initial.Hash())
return nil
}

func (s *store) Start(ctx context.Context) error {
head, err := s.readHead(ctx)
switch err {
default:
return err
case datastore.ErrNotFound:
log.Warnf("starting uninitialized store")
case nil:
s.heightSub.SetHeight(uint64(head.Height))
}

func (s *store) Start(context.Context) error {
go s.flushLoop()
return nil
}
Expand All @@ -140,10 +130,21 @@ func (s *store) Stop(ctx context.Context) error {

func (s *store) Head(ctx context.Context) (*ExtendedHeader, error) {
head, err := s.GetByHeight(ctx, s.heightSub.Height())
if err == ErrNotFound {
if err == nil {
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
return head, err
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
}

head, err = s.readHead(ctx)
switch err {
default:
return nil, err
case datastore.ErrNotFound, ErrNotFound:
return nil, ErrNoHead
case nil:
s.heightSub.SetHeight(uint64(head.Height))
log.Infow("loaded head", "height", head.Height, "hash", head.Hash())
return head, nil
}
return head, err
}

func (s *store) Get(_ context.Context, hash tmbytes.HexBytes) (*ExtendedHeader, error) {
Expand Down
56 changes: 56 additions & 0 deletions service/header/store_init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package header

import (
"context"
"testing"
"time"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInitStore_NoReinit(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
t.Cleanup(cancel)

suite := NewTestSuite(t, 3)
head := suite.Head()
exchange := NewLocalExchange(NewTestStore(ctx, t, head))

ds := sync.MutexWrap(datastore.NewMapDatastore())
store, err := NewStore(ds)
require.NoError(t, err)

err = InitStore(ctx, store, exchange, head.Hash())
assert.NoError(t, err)

err = store.Start(ctx)
require.NoError(t, err)

_, err = store.Append(ctx, suite.GenExtendedHeaders(10)...)
require.NoError(t, err)

err = store.Stop(ctx)
require.NoError(t, err)

reopenedStore, err := NewStore(ds)
require.NoError(t, err)

err = InitStore(ctx, reopenedStore, exchange, head.Hash())
assert.NoError(t, err)

err = reopenedStore.Start(ctx)
require.NoError(t, err)

reopenedHead, err := reopenedStore.Head(ctx)
require.NoError(t, err)

// check that reopened head changed and the store wasn't reinitialized
assert.Equal(t, suite.Head().Height, reopenedHead.Height)
assert.NotEqual(t, head.Height, reopenedHead.Height)

err = reopenedStore.Stop(ctx)
require.NoError(t, err)
}