Skip to content

Commit cb71de0

Browse files
committed
add pruningwindow
1 parent d83e15d commit cb71de0

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

sync/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Parameters struct {
3838
//
3939
// SyncFromHeight has lower priority than SyncFromHash.
4040
SyncFromHeight uint64
41+
// PruningWindow defines the duration within which headers will be retained before being pruned.
42+
PruningWindow time.Duration
4143
// blockTime provides a reference point for the Syncer to determine
4244
// whether its subjective head is outdated.
4345
// Keeping it private to disable serialization for it.
@@ -54,6 +56,7 @@ type Parameters struct {
5456
func DefaultParameters() Parameters {
5557
return Parameters{
5658
TrustingPeriod: 336 * time.Hour, // tendermint's default trusting period
59+
PruningWindow: 337 * time.Hour,
5760
}
5861
}
5962

sync/sync.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ func (s *Syncer[H]) Start(ctx context.Context) error {
110110
// Stop stops Syncer.
111111
func (s *Syncer[H]) Stop(context.Context) error {
112112
s.cancel()
113-
s.store.Reset()
114113
return s.metrics.Close()
115114
}
116115

sync/sync_head.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
5050
// NOTE: We could trust the netHead like we do during 'automatic subjective initialization'
5151
// but in this case our subjective head is not expired, so we should verify netHead
5252
// and only if it is valid, set it as new head
53-
_ = s.incomingNetworkHead(ctx, netHead)
53+
err = s.incomingNetworkHead(ctx, netHead)
54+
if err != nil {
55+
log.Errorw("incoming network head failed",
56+
"height", netHead.Height(),
57+
"hash", netHead.Hash().String(),
58+
"err", err)
59+
}
5460
// netHead was either accepted or rejected as the new subjective
5561
// anyway return most current known subjective head
5662
return s.subjectiveHead(ctx)

sync/sync_store.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ type syncStore[H header.Header[H]] struct {
2828
head atomic.Pointer[H]
2929
}
3030

31-
func (s *syncStore[H]) Reset() {
32-
s.head.Store(nil)
33-
}
34-
3531
func (s *syncStore[H]) Head(ctx context.Context) (H, error) {
3632
if headPtr := s.head.Load(); headPtr != nil {
3733
return *headPtr, nil

sync/sync_tail.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
"github.com/celestiaorg/go-header"
1111
)
1212

13+
// TODO:
14+
// * Refactor tests
15+
// * Write tests for estimation
16+
// * Ensure sync always happen on start
17+
1318
// subjectiveTail returns the current Tail header.
1419
// Lazily fetching it if it doesn't exist locally or moving it to a different height.
1520
// Moving is done if either parameters are changed or tail moved outside a pruning window.
@@ -30,11 +35,10 @@ func (s *Syncer[H]) subjectiveTail(ctx context.Context, head H) (H, error) {
3035
fetched = true
3136
}
3237
} else if tailHeight, outdated := s.isTailHeightOutdated(tail); outdated {
33-
// hack for the case with tailHeight > store.Height avoiding heightSub
34-
storeCtx, cancel := context.WithTimeout(ctx, time.Second)
35-
tail, err = s.store.GetByHeight(storeCtx, tailHeight)
36-
cancel()
37-
if err != nil {
38+
if tailHeight <= s.store.Height() {
39+
tail, err = s.store.GetByHeight(ctx, tailHeight)
40+
}
41+
if err != nil || tailHeight != tail.Height() {
3842
tail, err = s.getter.GetByHeight(ctx, tailHeight)
3943
if err != nil {
4044
return tail, fmt.Errorf("getting SyncFromHeight tail(%d): %w", tailHeight, err)
@@ -52,7 +56,7 @@ func (s *Syncer[H]) subjectiveTail(ctx context.Context, head H) (H, error) {
5256
fetched = true
5357
} else {
5458
// have a known Tail - estimate basing on it.
55-
cutoffTime := head.Time().UTC().Add(-s.Params.TrustingPeriod)
59+
cutoffTime := head.Time().UTC().Add(-s.Params.PruningWindow)
5660
diff := cutoffTime.Sub(tail.Time().UTC())
5761
if diff <= 0 {
5862
// current tail is relevant as is

sync/sync_tail_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func TestSyncer_TailReconfiguration(t *testing.T) {
4040

4141
err = syncer.Start(ctx)
4242
require.NoError(t, err)
43+
time.Sleep(time.Millisecond * 10)
44+
syncer.SyncWait(ctx)
45+
4346
err = syncer.Stop(ctx)
4447
require.NoError(t, err)
4548
time.Sleep(time.Millisecond * 10)
@@ -48,7 +51,6 @@ func TestSyncer_TailReconfiguration(t *testing.T) {
4851

4952
err = syncer.Start(ctx)
5053
require.NoError(t, err)
51-
time.Sleep(time.Millisecond * 10)
5254

5355
storeTail, err := localStore.Tail(ctx)
5456
require.NoError(t, err)

0 commit comments

Comments
 (0)