|
| 1 | +package syncing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/rs/zerolog" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/evstack/ev-node/block/internal/common" |
| 14 | + datypes "github.com/evstack/ev-node/pkg/da/types" |
| 15 | +) |
| 16 | + |
| 17 | +func TestDAFollower_HandleEvent(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + isInline bool |
| 21 | + blobs [][]byte |
| 22 | + mockEvents []common.DAHeightEvent |
| 23 | + mockPipeErr error |
| 24 | + expectedError string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "ignore_not_inline", |
| 28 | + isInline: false, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "error_no_blobs", |
| 32 | + isInline: true, |
| 33 | + blobs: [][]byte{}, |
| 34 | + expectedError: "skip inline: no blobs", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "error_no_complete_events", |
| 38 | + isInline: true, |
| 39 | + blobs: [][]byte{[]byte("blob")}, |
| 40 | + mockEvents: []common.DAHeightEvent{}, |
| 41 | + expectedError: "skip inline: no complete events", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "error_pipe_fails", |
| 45 | + isInline: true, |
| 46 | + blobs: [][]byte{[]byte("blob")}, |
| 47 | + mockEvents: []common.DAHeightEvent{{DaHeight: 100}}, |
| 48 | + mockPipeErr: errors.New("pipe error"), |
| 49 | + expectedError: "pipe error", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "success", |
| 53 | + isInline: true, |
| 54 | + blobs: [][]byte{[]byte("blob")}, |
| 55 | + mockEvents: []common.DAHeightEvent{{DaHeight: 100}}, |
| 56 | + mockPipeErr: nil, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + daRetriever := NewMockDARetriever(t) |
| 63 | + ctx := t.Context() |
| 64 | + |
| 65 | + var pipedEvents []common.DAHeightEvent |
| 66 | + pipeEvent := func(_ context.Context, ev common.DAHeightEvent) error { |
| 67 | + pipedEvents = append(pipedEvents, ev) |
| 68 | + return tt.mockPipeErr |
| 69 | + } |
| 70 | + |
| 71 | + follower := &daFollower{ |
| 72 | + retriever: daRetriever, |
| 73 | + eventSink: common.EventSinkFunc(pipeEvent), |
| 74 | + logger: zerolog.Nop(), |
| 75 | + } |
| 76 | + |
| 77 | + ev := datypes.SubscriptionEvent{Height: 100, Blobs: tt.blobs} |
| 78 | + |
| 79 | + if tt.isInline && len(tt.blobs) > 0 { |
| 80 | + daRetriever.On("ProcessBlobs", mock.Anything, tt.blobs, uint64(100)).Return(tt.mockEvents) |
| 81 | + } |
| 82 | + |
| 83 | + err := follower.HandleEvent(ctx, ev, tt.isInline) |
| 84 | + |
| 85 | + if tt.expectedError != "" { |
| 86 | + require.Error(t, err) |
| 87 | + assert.Contains(t, err.Error(), tt.expectedError) |
| 88 | + } else { |
| 89 | + require.NoError(t, err) |
| 90 | + if tt.isInline && len(tt.blobs) > 0 { |
| 91 | + assert.Len(t, pipedEvents, len(tt.mockEvents)) |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestDAFollower_HandleCatchup(t *testing.T) { |
| 99 | + t.Run("normal_sequential_fetch_success", func(t *testing.T) { |
| 100 | + daRetriever := NewMockDARetriever(t) |
| 101 | + ctx := t.Context() |
| 102 | + |
| 103 | + var pipedEvents []common.DAHeightEvent |
| 104 | + pipeEvent := func(_ context.Context, ev common.DAHeightEvent) error { |
| 105 | + pipedEvents = append(pipedEvents, ev) |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + follower := &daFollower{ |
| 110 | + retriever: daRetriever, |
| 111 | + eventSink: common.EventSinkFunc(pipeEvent), |
| 112 | + logger: zerolog.Nop(), |
| 113 | + priorityHeights: make([]uint64, 0), |
| 114 | + } |
| 115 | + |
| 116 | + events := []common.DAHeightEvent{{DaHeight: 100}} |
| 117 | + daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).Return(events, nil) |
| 118 | + |
| 119 | + err := follower.HandleCatchup(ctx, 100) |
| 120 | + require.NoError(t, err) |
| 121 | + assert.Len(t, pipedEvents, 1) |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("normal_sequential_fetch_blob_not_found_ignored", func(t *testing.T) { |
| 125 | + daRetriever := NewMockDARetriever(t) |
| 126 | + ctx := t.Context() |
| 127 | + |
| 128 | + follower := &daFollower{ |
| 129 | + retriever: daRetriever, |
| 130 | + eventSink: common.EventSinkFunc(func(_ context.Context, _ common.DAHeightEvent) error { return nil }), |
| 131 | + logger: zerolog.Nop(), |
| 132 | + priorityHeights: make([]uint64, 0), |
| 133 | + } |
| 134 | + |
| 135 | + daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).Return(nil, datypes.ErrBlobNotFound) |
| 136 | + |
| 137 | + err := follower.HandleCatchup(ctx, 100) |
| 138 | + require.NoError(t, err) |
| 139 | + }) |
| 140 | + |
| 141 | + t.Run("normal_sequential_fetch_error_propagated", func(t *testing.T) { |
| 142 | + daRetriever := NewMockDARetriever(t) |
| 143 | + ctx := t.Context() |
| 144 | + |
| 145 | + follower := &daFollower{ |
| 146 | + retriever: daRetriever, |
| 147 | + eventSink: common.EventSinkFunc(func(_ context.Context, _ common.DAHeightEvent) error { return nil }), |
| 148 | + logger: zerolog.Nop(), |
| 149 | + priorityHeights: make([]uint64, 0), |
| 150 | + } |
| 151 | + |
| 152 | + daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).Return(nil, datypes.ErrHeightFromFuture) |
| 153 | + |
| 154 | + err := follower.HandleCatchup(ctx, 100) |
| 155 | + require.ErrorIs(t, err, datypes.ErrHeightFromFuture) |
| 156 | + }) |
| 157 | + |
| 158 | + t.Run("priority_queue_handled_first", func(t *testing.T) { |
| 159 | + daRetriever := NewMockDARetriever(t) |
| 160 | + ctx := t.Context() |
| 161 | + |
| 162 | + var pipedEvents []common.DAHeightEvent |
| 163 | + pipeEvent := func(_ context.Context, ev common.DAHeightEvent) error { |
| 164 | + pipedEvents = append(pipedEvents, ev) |
| 165 | + return nil |
| 166 | + } |
| 167 | + |
| 168 | + follower := &daFollower{ |
| 169 | + retriever: daRetriever, |
| 170 | + eventSink: common.EventSinkFunc(pipeEvent), |
| 171 | + logger: zerolog.Nop(), |
| 172 | + priorityHeights: []uint64{105}, // Priority height to fetch |
| 173 | + } |
| 174 | + |
| 175 | + daRetriever.On("RetrieveFromDA", mock.Anything, uint64(105)).Return([]common.DAHeightEvent{{DaHeight: 105}}, nil).Once() |
| 176 | + daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).Return([]common.DAHeightEvent{{DaHeight: 100}}, nil).Once() |
| 177 | + |
| 178 | + err := follower.HandleCatchup(ctx, 100) |
| 179 | + require.NoError(t, err) |
| 180 | + |
| 181 | + // It should pipe 105 then 100 |
| 182 | + assert.Len(t, pipedEvents, 2) |
| 183 | + assert.Equal(t, uint64(105), pipedEvents[0].DaHeight) |
| 184 | + assert.Equal(t, uint64(100), pipedEvents[1].DaHeight) |
| 185 | + assert.Empty(t, follower.priorityHeights) |
| 186 | + }) |
| 187 | +} |
0 commit comments