From 1de524bbfa5db898e900b6327a40148f642c5585 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 4 Dec 2026 12:56:37 +0100 Subject: [PATCH 1/4] fix(da): retry rolled-back inline heights and leave split namespaces to catch-up - When inline processing failed, the subscriber rolled the claimed height back without waking catch-up. If catch-up had run while the height was claimed, it saw the head reached and stopped, so the height waited for the next blob in the chain's namespaces: indefinitely on an idle chain. The rollback now signals catch-up. - With separate header and data namespaces, each subscription event carries one namespace's blobs. A height could be counted as done when its header event completed one block, and the data event for another block at that height was then ignored. Events are now processed inline only when there is one distinct namespace; otherwise catch-up reads every namespace for the height. Closes #3526 Closes #3527 --- block/internal/da/subscriber.go | 14 ++++- block/internal/da/subscriber_test.go | 88 +++++++++++++++++++++++++++ block/internal/syncing/da_follower.go | 3 +- 3 files changed, 102 insertions(+), 3 deletions(-) diff --git a/block/internal/da/subscriber.go b/block/internal/da/subscriber.go index 9c3c28bc57..4bde771600 100644 --- a/block/internal/da/subscriber.go +++ b/block/internal/da/subscriber.go @@ -63,6 +63,11 @@ type Subscriber struct { // namespaces to subscribe on. When multiple, they are merged. namespaces [][]byte + // inline is whether an event can be processed as its height's only + // notification. With distinct namespaces each event carries one + // namespace's blobs, so heights are left to catch-up, which reads them all. + inline bool + // localDAHeight is only written by catchupLoop (via CAS) and read by // followLoop to determine whether inline processing is possible. localDAHeight atomic.Uint64 @@ -102,10 +107,14 @@ func NewSubscriber(cfg SubscriberConfig) *Subscriber { logger: cfg.Logger, handler: cfg.Handler, namespaces: cfg.Namespaces, + inline: true, catchupSignal: make(chan struct{}, 1), daBlockTime: cfg.DABlockTime, fetchBlockTimestamp: cfg.FetchBlockTimestamp, } + for _, ns := range s.namespaces { + s.inline = s.inline && bytes.Equal(ns, s.namespaces[0]) + } s.localDAHeight.Store(cfg.StartHeight) s.highestSeenDAHeight.Store(cfg.StartHeight) if cfg.StartHeight != 0 { @@ -313,7 +322,7 @@ func (s *Subscriber) runSubscription(ctx context.Context) error { s.updateHighest(ev.Height) local := s.localDAHeight.Load() - isInline := ev.Height == local && s.localDAHeight.CompareAndSwap(local, local+1) + isInline := s.inline && ev.Height == local && s.localDAHeight.CompareAndSwap(local, local+1) err = s.handler.HandleEvent(subCtx, ev, isInline) if isInline { @@ -326,6 +335,9 @@ func (s *Subscriber) runSubscription(ctx context.Context) error { } } else { s.localDAHeight.Store(local) + // Catch-up may have run while the height was claimed and + // stopped at it; nothing else wakes it on an idle chain. + s.signalCatchup() s.logger.Debug().Err(err).Uint64("da_height", ev.Height). Msg("inline processing skipped/failed, rolling back") } diff --git a/block/internal/da/subscriber_test.go b/block/internal/da/subscriber_test.go index c396091edd..20d70d2bf8 100644 --- a/block/internal/da/subscriber_test.go +++ b/block/internal/da/subscriber_test.go @@ -297,3 +297,91 @@ func TestSubscriber_RunSubscription_InlineDoesNotPrematurelyReachHead(t *testing assert.Equal(t, uint64(101), sub.LocalDAHeight()) assert.Equal(t, uint64(101), sub.HighestSeenDAHeight()) } + +// Catch-up running while an inline height is claimed sees local past the head +// and stops. When inline processing then fails and rolls back, catch-up must be +// woken to retry the height: on an idle chain no new event would wake it. +func TestSubscriber_RunSubscription_RollbackWakesCatchup(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + + mockHandler := new(MockSubscriberHandler) + mockClient := testmocks.NewMockClient(t) + + sub := NewSubscriber(SubscriberConfig{ + Client: mockClient, + Logger: zerolog.Nop(), + Handler: mockHandler, + Namespaces: [][]byte{[]byte("ns")}, + StartHeight: 100, + DABlockTime: time.Hour, + }) + <-sub.catchupSignal // drain the startup signal + + subCh := make(chan datypes.SubscriptionEvent, 1) + mockClient.EXPECT(). + Subscribe(mock.Anything, []byte("ns"), false). + Return((<-chan datypes.SubscriptionEvent)(subCh), nil). + Once() + + ev := datypes.SubscriptionEvent{Height: 100, Blobs: [][]byte{[]byte("h100")}} + mockHandler.On("HandleEvent", mock.Anything, ev, true). + Run(func(mock.Arguments) { sub.runCatchup(ctx) }). + Return(errors.New("skip inline")).Once() + + subCh <- ev + close(subCh) + assert.Error(t, sub.runSubscription(ctx)) + assert.Equal(t, uint64(100), sub.LocalDAHeight()) + + select { + case <-sub.catchupSignal: + default: + t.Fatal("rollback did not wake catch-up") + } + mockHandler.On("HandleCatchup", mock.Anything, uint64(100)).Return(nil).Once() + sub.runCatchup(ctx) + mockHandler.AssertExpectations(t) + assert.Equal(t, uint64(101), sub.LocalDAHeight()) +} + +// With distinct namespaces each event carries one namespace's blobs, so no +// event is processed inline: catch-up reads every namespace for the height. +func TestSubscriber_RunSubscription_SplitNamespacesNotInline(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + + mockHandler := new(MockSubscriberHandler) + mockClient := testmocks.NewMockClient(t) + + sub := NewSubscriber(SubscriberConfig{ + Client: mockClient, + Logger: zerolog.Nop(), + Handler: mockHandler, + Namespaces: [][]byte{[]byte("header"), []byte("data")}, + StartHeight: 100, + DABlockTime: time.Hour, + }) + + headerCh := make(chan datypes.SubscriptionEvent, 1) + dataCh := make(chan datypes.SubscriptionEvent, 1) + mockClient.EXPECT().Subscribe(mock.Anything, []byte("header"), false). + Return((<-chan datypes.SubscriptionEvent)(headerCh), nil).Once() + mockClient.EXPECT().Subscribe(mock.Anything, []byte("data"), false). + Return((<-chan datypes.SubscriptionEvent)(dataCh), nil).Once() + + headerEv := datypes.SubscriptionEvent{Height: 100, Blobs: [][]byte{[]byte("header")}} + dataEv := datypes.SubscriptionEvent{Height: 100, Blobs: [][]byte{[]byte("data")}} + mockHandler.On("HandleEvent", mock.Anything, headerEv, false).Return(nil).Once() + mockHandler.On("HandleEvent", mock.Anything, dataEv, false).Return(nil).Once() + + headerCh <- headerEv + dataCh <- dataEv + close(headerCh) + close(dataCh) + assert.Error(t, sub.runSubscription(ctx)) + + mockHandler.AssertExpectations(t) + assert.Equal(t, uint64(100), sub.LocalDAHeight()) + assert.Equal(t, uint64(100), sub.HighestSeenDAHeight()) +} diff --git a/block/internal/syncing/da_follower.go b/block/internal/syncing/da_follower.go index 30ddc47606..435212c369 100644 --- a/block/internal/syncing/da_follower.go +++ b/block/internal/syncing/da_follower.go @@ -114,8 +114,7 @@ func (f *daFollower) HandleEvent(ctx context.Context, ev datypes.SubscriptionEve completed = completed || event.DaHeight == ev.Height } // Events from earlier DA heights, e.g. held for a proposer rotation, say - // nothing about this height: with split namespaces its other half may still - // be on its way. + // nothing about this height, so leave it to catch-up. if !completed { return errors.New("skip inline: no complete events at this height") // subscriber rolls back } From 886fc94a94d619cbcace40cfb9697bbf53e2f44a Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 4 Dec 2026 13:30:58 +0100 Subject: [PATCH 2/4] refactor(da): name the subscriber's namespace check for what it tests --- block/internal/da/subscriber.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/block/internal/da/subscriber.go b/block/internal/da/subscriber.go index 4bde771600..bff09d2713 100644 --- a/block/internal/da/subscriber.go +++ b/block/internal/da/subscriber.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "slices" "sync" "sync/atomic" "time" @@ -63,10 +64,10 @@ type Subscriber struct { // namespaces to subscribe on. When multiple, they are merged. namespaces [][]byte - // inline is whether an event can be processed as its height's only - // notification. With distinct namespaces each event carries one - // namespace's blobs, so heights are left to catch-up, which reads them all. - inline bool + // singleNamespace is false when header and data use separate namespaces: + // each event then carries one namespace's blobs, so heights are left to + // catch-up, which reads them all. + singleNamespace bool // localDAHeight is only written by catchupLoop (via CAS) and read by // followLoop to determine whether inline processing is possible. @@ -102,19 +103,19 @@ type Subscriber struct { // NewSubscriber creates a new Subscriber. func NewSubscriber(cfg SubscriberConfig) *Subscriber { + singleNamespace := !slices.ContainsFunc(cfg.Namespaces, func(ns []byte) bool { + return !bytes.Equal(ns, cfg.Namespaces[0]) + }) s := &Subscriber{ client: cfg.Client, logger: cfg.Logger, handler: cfg.Handler, namespaces: cfg.Namespaces, - inline: true, + singleNamespace: singleNamespace, catchupSignal: make(chan struct{}, 1), daBlockTime: cfg.DABlockTime, fetchBlockTimestamp: cfg.FetchBlockTimestamp, } - for _, ns := range s.namespaces { - s.inline = s.inline && bytes.Equal(ns, s.namespaces[0]) - } s.localDAHeight.Store(cfg.StartHeight) s.highestSeenDAHeight.Store(cfg.StartHeight) if cfg.StartHeight != 0 { @@ -322,7 +323,7 @@ func (s *Subscriber) runSubscription(ctx context.Context) error { s.updateHighest(ev.Height) local := s.localDAHeight.Load() - isInline := s.inline && ev.Height == local && s.localDAHeight.CompareAndSwap(local, local+1) + isInline := s.singleNamespace && ev.Height == local && s.localDAHeight.CompareAndSwap(local, local+1) err = s.handler.HandleEvent(subCtx, ev, isInline) if isInline { From 831c403761f3d445bb3173a17d01f01fa139ea08 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 4 Dec 2026 13:51:35 +0100 Subject: [PATCH 3/4] fix(da): wake catch-up for deferred equal-height events --- block/internal/da/subscriber.go | 3 ++ block/internal/da/subscriber_test.go | 47 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/block/internal/da/subscriber.go b/block/internal/da/subscriber.go index bff09d2713..ebd133a535 100644 --- a/block/internal/da/subscriber.go +++ b/block/internal/da/subscriber.go @@ -342,6 +342,9 @@ func (s *Subscriber) runSubscription(ctx context.Context) error { s.logger.Debug().Err(err).Uint64("da_height", ev.Height). Msg("inline processing skipped/failed, rolling back") } + } else if ev.Height >= local { + // Equal-height events do not wake catch-up through updateHighest. + s.signalCatchup() } watchdog.Reset(watchdogTimeout) diff --git a/block/internal/da/subscriber_test.go b/block/internal/da/subscriber_test.go index 20d70d2bf8..68ba3aa0f5 100644 --- a/block/internal/da/subscriber_test.go +++ b/block/internal/da/subscriber_test.go @@ -385,3 +385,50 @@ func TestSubscriber_RunSubscription_SplitNamespacesNotInline(t *testing.T) { assert.Equal(t, uint64(100), sub.LocalDAHeight()) assert.Equal(t, uint64(100), sub.HighestSeenDAHeight()) } + +// An event at the starting height must wake catch-up even when it does not +// increase highestSeenDAHeight and split namespaces prevent inline processing. +func TestSubscriber_RunSubscription_SplitNamespacesAtStartHeightWakeCatchup(t *testing.T) { + ctx := t.Context() + mockHandler := new(MockSubscriberHandler) + mockClient := testmocks.NewMockClient(t) + sub := NewSubscriber(SubscriberConfig{ + Client: mockClient, + Logger: zerolog.Nop(), + Handler: mockHandler, + Namespaces: [][]byte{[]byte("header"), []byte("data")}, + StartHeight: 100, + DABlockTime: time.Hour, + }) + + // Startup catch-up stops before the starting height is available. + <-sub.catchupSignal + mockHandler.On("HandleCatchup", mock.Anything, uint64(100)). + Return(datypes.ErrHeightFromFuture).Once() + sub.runCatchup(ctx) + assert.Equal(t, uint64(100), sub.LocalDAHeight()) + + headerCh := make(chan datypes.SubscriptionEvent, 1) + dataCh := make(chan datypes.SubscriptionEvent) + mockClient.EXPECT().Subscribe(mock.Anything, []byte("header"), false). + Return((<-chan datypes.SubscriptionEvent)(headerCh), nil).Once() + mockClient.EXPECT().Subscribe(mock.Anything, []byte("data"), false). + Return((<-chan datypes.SubscriptionEvent)(dataCh), nil).Once() + + ev := datypes.SubscriptionEvent{Height: 100, Blobs: [][]byte{[]byte("header")}} + mockHandler.On("HandleEvent", mock.Anything, ev, false).Return(nil).Once() + headerCh <- ev + close(headerCh) + close(dataCh) + assert.Error(t, sub.runSubscription(ctx)) + + select { + case <-sub.catchupSignal: + default: + t.Fatal("equal-height event did not wake catch-up") + } + mockHandler.On("HandleCatchup", mock.Anything, uint64(100)).Return(nil).Once() + sub.runCatchup(ctx) + mockHandler.AssertExpectations(t) + assert.Equal(t, uint64(101), sub.LocalDAHeight()) +} From 6a1f8f0cbc0897a55a0ffb14a51d1f3c2feafdfd Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 4 Dec 2026 14:19:59 +0100 Subject: [PATCH 4/4] fix(da): wake catch-up while a height is claimed --- block/internal/da/subscriber.go | 4 +- block/internal/da/subscriber_test.go | 58 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/block/internal/da/subscriber.go b/block/internal/da/subscriber.go index ebd133a535..1434317315 100644 --- a/block/internal/da/subscriber.go +++ b/block/internal/da/subscriber.go @@ -342,8 +342,8 @@ func (s *Subscriber) runSubscription(ctx context.Context) error { s.logger.Debug().Err(err).Uint64("da_height", ev.Height). Msg("inline processing skipped/failed, rolling back") } - } else if ev.Height >= local { - // Equal-height events do not wake catch-up through updateHighest. + } else { + // The cursor may include an in-flight catch-up claim that later rolls back. s.signalCatchup() } diff --git a/block/internal/da/subscriber_test.go b/block/internal/da/subscriber_test.go index 68ba3aa0f5..6f6c89f462 100644 --- a/block/internal/da/subscriber_test.go +++ b/block/internal/da/subscriber_test.go @@ -432,3 +432,61 @@ func TestSubscriber_RunSubscription_SplitNamespacesAtStartHeightWakeCatchup(t *t mockHandler.AssertExpectations(t) assert.Equal(t, uint64(101), sub.LocalDAHeight()) } + +func TestSubscriber_RunSubscription_EventDuringCatchupClaimWakesRetry(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + mockHandler := new(MockSubscriberHandler) + mockClient := testmocks.NewMockClient(t) + sub := NewSubscriber(SubscriberConfig{ + Client: mockClient, + Logger: zerolog.Nop(), + Handler: mockHandler, + Namespaces: [][]byte{[]byte("header"), []byte("data")}, + StartHeight: 100, + DABlockTime: time.Hour, + }) + + claimed := make(chan struct{}) + release := make(chan struct{}) + retried := make(chan struct{}) + mockHandler.On("HandleCatchup", mock.Anything, uint64(100)). + Run(func(mock.Arguments) { + close(claimed) + select { + case <-release: + case <-ctx.Done(): + } + }).Return(datypes.ErrHeightFromFuture).Once() + mockHandler.On("HandleCatchup", mock.Anything, uint64(100)). + Run(func(mock.Arguments) { close(retried) }).Return(nil).Once() + + sub.wg.Add(1) + go sub.catchupLoop(ctx) + t.Cleanup(func() { + cancel() + sub.wg.Wait() + }) + waitForLifecycleSignal(t, claimed, "catch-up to claim the starting height") + assert.Equal(t, uint64(101), sub.LocalDAHeight()) + + headerCh := make(chan datypes.SubscriptionEvent, 1) + dataCh := make(chan datypes.SubscriptionEvent) + mockClient.EXPECT().Subscribe(mock.Anything, []byte("header"), false). + Return((<-chan datypes.SubscriptionEvent)(headerCh), nil).Once() + mockClient.EXPECT().Subscribe(mock.Anything, []byte("data"), false). + Return((<-chan datypes.SubscriptionEvent)(dataCh), nil).Once() + ev := datypes.SubscriptionEvent{Height: 100, Blobs: [][]byte{[]byte("header")}} + mockHandler.On("HandleEvent", mock.Anything, ev, false).Return(nil).Once() + headerCh <- ev + close(headerCh) + close(dataCh) + assert.Error(t, sub.runSubscription(ctx)) + + close(release) + waitForLifecycleSignal(t, retried, "catch-up to retry after the in-flight claim rolls back") + cancel() + sub.wg.Wait() + mockHandler.AssertExpectations(t) + assert.Equal(t, uint64(101), sub.LocalDAHeight()) +}