Skip to content

Commit e40362f

Browse files
committed
fix(store): publish initial head via heightSub
1 parent 0a58e20 commit e40362f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

store/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ func newStore[H header.Header](ds datastore.Batching, opts ...Option) (*Store[H]
108108
}
109109

110110
func (s *Store[H]) Init(ctx context.Context, initial H) error {
111+
if s.heightSub.Height() != 0 {
112+
return fmt.Errorf("store already initialized")
113+
}
111114
// trust the given header as the initial head
112115
err := s.flush(ctx, initial)
113116
if err != nil {
114117
return err
115118
}
116119

117120
log.Infow("initialized head", "height", initial.Height(), "hash", initial.Hash())
121+
s.heightSub.Pub(initial)
118122
return nil
119123
}
120124

store/store_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,25 @@ func TestStorePendingCacheMiss(t *testing.T) {
112112
_, err = store.GetRangeByHeight(ctx, 101, 151)
113113
require.NoError(t, err)
114114
}
115+
116+
func TestBatch_GetByHeightBeforeInit(t *testing.T) {
117+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
118+
t.Cleanup(cancel)
119+
120+
suite := test.NewTestSuite(t)
121+
122+
ds := sync.MutexWrap(datastore.NewMapDatastore())
123+
store, err := NewStore[*test.DummyHeader](ds)
124+
require.NoError(t, err)
125+
126+
err = store.Start(ctx)
127+
require.NoError(t, err)
128+
129+
go func() {
130+
_ = store.Init(ctx, suite.Head())
131+
}()
132+
133+
h, err := store.GetByHeight(ctx, 1)
134+
require.NoError(t, err)
135+
require.NotNil(t, h)
136+
}

0 commit comments

Comments
 (0)