|
1 | 1 | package submitting |
2 | | - |
3 | | -import ( |
4 | | - "context" |
5 | | - "testing" |
6 | | - "time" |
7 | | - |
8 | | - "github.com/rs/zerolog" |
9 | | - "github.com/stretchr/testify/assert" |
10 | | - "github.com/stretchr/testify/mock" |
11 | | - |
12 | | - "github.com/evstack/ev-node/block/internal/common" |
13 | | - "github.com/evstack/ev-node/pkg/config" |
14 | | - datypes "github.com/evstack/ev-node/pkg/da/types" |
15 | | - "github.com/evstack/ev-node/pkg/genesis" |
16 | | - "github.com/evstack/ev-node/test/mocks" |
17 | | -) |
18 | | - |
19 | | -// helper to build a basic submitter with provided DA mock client and config overrides |
20 | | -func newTestSubmitter(t *testing.T, mockClient *mocks.MockClient, override func(*config.Config)) *DASubmitter { |
21 | | - cfg := config.Config{} |
22 | | - // Keep retries small and backoffs minimal |
23 | | - cfg.DA.BlockTime.Duration = 1 * time.Millisecond |
24 | | - cfg.DA.MaxSubmitAttempts = 3 |
25 | | - cfg.DA.SubmitOptions = "opts" |
26 | | - cfg.DA.Namespace = "ns" |
27 | | - cfg.DA.DataNamespace = "ns-data" |
28 | | - if override != nil { |
29 | | - override(&cfg) |
30 | | - } |
31 | | - if mockClient == nil { |
32 | | - mockClient = mocks.NewMockClient(t) |
33 | | - } |
34 | | - mockClient.On("GetHeaderNamespace").Return([]byte(cfg.DA.Namespace)).Maybe() |
35 | | - mockClient.On("GetDataNamespace").Return([]byte(cfg.DA.DataNamespace)).Maybe() |
36 | | - mockClient.On("GetForcedInclusionNamespace").Return([]byte(nil)).Maybe() |
37 | | - mockClient.On("HasForcedInclusionNamespace").Return(false).Maybe() |
38 | | - return NewDASubmitter(mockClient, cfg, genesis.Genesis{} /*options=*/, common.BlockOptions{}, common.NopMetrics(), zerolog.Nop(), nil, nil) |
39 | | -} |
40 | | - |
41 | | -func TestSubmitToDA_MempoolRetry_IncreasesGasAndSucceeds(t *testing.T) { |
42 | | - t.Parallel() |
43 | | - |
44 | | - client := mocks.NewMockClient(t) |
45 | | - |
46 | | - nsBz := datypes.NamespaceFromString("ns").Bytes() |
47 | | - opts := []byte("opts") |
48 | | - var usedGas []float64 |
49 | | - |
50 | | - client.On("Submit", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts). |
51 | | - Run(func(args mock.Arguments) { |
52 | | - usedGas = append(usedGas, args.Get(2).(float64)) |
53 | | - }). |
54 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusNotIncludedInBlock, SubmittedCount: 0}}). |
55 | | - Once() |
56 | | - |
57 | | - ids := [][]byte{[]byte("id1"), []byte("id2"), []byte("id3")} |
58 | | - client.On("Submit", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts). |
59 | | - Run(func(args mock.Arguments) { |
60 | | - usedGas = append(usedGas, args.Get(2).(float64)) |
61 | | - }). |
62 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusSuccess, IDs: ids, SubmittedCount: uint64(len(ids))}}). |
63 | | - Once() |
64 | | - |
65 | | - s := newTestSubmitter(t, client, nil) |
66 | | - |
67 | | - items := []string{"a", "b", "c"} |
68 | | - marshalledItems := make([][]byte, len(items)) |
69 | | - for idx, item := range items { |
70 | | - marshalledItems[idx] = []byte(item) |
71 | | - } |
72 | | - |
73 | | - ctx := context.Background() |
74 | | - err := submitToDA[string]( |
75 | | - s, |
76 | | - ctx, |
77 | | - items, |
78 | | - marshalledItems, |
79 | | - func(_ []string, _ *datypes.ResultSubmit) {}, |
80 | | - "item", |
81 | | - nsBz, |
82 | | - opts, |
83 | | - ) |
84 | | - assert.NoError(t, err) |
85 | | - |
86 | | - // Sentinel value is preserved on retry |
87 | | - assert.Equal(t, []float64{-1, -1}, usedGas) |
88 | | -} |
89 | | - |
90 | | -func TestSubmitToDA_UnknownError_RetriesSameGasThenSucceeds(t *testing.T) { |
91 | | - t.Parallel() |
92 | | - |
93 | | - client := mocks.NewMockClient(t) |
94 | | - |
95 | | - nsBz := datypes.NamespaceFromString("ns").Bytes() |
96 | | - |
97 | | - opts := []byte("opts") |
98 | | - var usedGas []float64 |
99 | | - |
100 | | - // First attempt: unknown failure -> reasonFailure, gas unchanged for next attempt |
101 | | - client.On("Submit", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts). |
102 | | - Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }). |
103 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusError, Message: "boom"}}). |
104 | | - Once() |
105 | | - |
106 | | - // Second attempt: same gas, success |
107 | | - ids := [][]byte{[]byte("id1")} |
108 | | - client.On("Submit", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts). |
109 | | - Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }). |
110 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusSuccess, IDs: ids, SubmittedCount: uint64(len(ids))}}). |
111 | | - Once() |
112 | | - |
113 | | - s := newTestSubmitter(t, client, nil) |
114 | | - |
115 | | - items := []string{"x"} |
116 | | - marshalledItems := make([][]byte, len(items)) |
117 | | - for idx, item := range items { |
118 | | - marshalledItems[idx] = []byte(item) |
119 | | - } |
120 | | - |
121 | | - ctx := context.Background() |
122 | | - err := submitToDA[string]( |
123 | | - s, |
124 | | - ctx, |
125 | | - items, |
126 | | - marshalledItems, |
127 | | - func(_ []string, _ *datypes.ResultSubmit) {}, |
128 | | - "item", |
129 | | - nsBz, |
130 | | - opts, |
131 | | - ) |
132 | | - assert.NoError(t, err) |
133 | | - assert.Equal(t, []float64{-1, -1}, usedGas) |
134 | | -} |
135 | | - |
136 | | -func TestSubmitToDA_TooBig_HalvesBatch(t *testing.T) { |
137 | | - t.Parallel() |
138 | | - |
139 | | - client := mocks.NewMockClient(t) |
140 | | - |
141 | | - nsBz := datypes.NamespaceFromString("ns").Bytes() |
142 | | - |
143 | | - opts := []byte("opts") |
144 | | - var batchSizes []int |
145 | | - |
146 | | - client.On("Submit", mock.Anything, mock.Anything, mock.Anything, nsBz, opts). |
147 | | - Run(func(args mock.Arguments) { |
148 | | - blobs := args.Get(1).([][]byte) |
149 | | - batchSizes = append(batchSizes, len(blobs)) |
150 | | - }). |
151 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusTooBig}}). |
152 | | - Once() |
153 | | - |
154 | | - ids := [][]byte{[]byte("id1"), []byte("id2")} |
155 | | - client.On("Submit", mock.Anything, mock.Anything, mock.Anything, nsBz, opts). |
156 | | - Run(func(args mock.Arguments) { |
157 | | - blobs := args.Get(1).([][]byte) |
158 | | - batchSizes = append(batchSizes, len(blobs)) |
159 | | - }). |
160 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusSuccess, IDs: ids, SubmittedCount: uint64(len(ids))}}). |
161 | | - Once() |
162 | | - |
163 | | - s := newTestSubmitter(t, client, nil) |
164 | | - |
165 | | - items := []string{"a", "b", "c", "d"} |
166 | | - marshalledItems := make([][]byte, len(items)) |
167 | | - for idx, item := range items { |
168 | | - marshalledItems[idx] = []byte(item) |
169 | | - } |
170 | | - |
171 | | - ctx := context.Background() |
172 | | - err := submitToDA[string]( |
173 | | - s, |
174 | | - ctx, |
175 | | - items, |
176 | | - marshalledItems, |
177 | | - func(_ []string, _ *datypes.ResultSubmit) {}, |
178 | | - "item", |
179 | | - nsBz, |
180 | | - opts, |
181 | | - ) |
182 | | - assert.NoError(t, err) |
183 | | - assert.Equal(t, []int{4, 2}, batchSizes) |
184 | | -} |
185 | | - |
186 | | -func TestSubmitToDA_SentinelNoGas_PreservesGasAcrossRetries(t *testing.T) { |
187 | | - t.Parallel() |
188 | | - |
189 | | - client := mocks.NewMockClient(t) |
190 | | - |
191 | | - nsBz := datypes.NamespaceFromString("ns").Bytes() |
192 | | - |
193 | | - opts := []byte("opts") |
194 | | - var usedGas []float64 |
195 | | - |
196 | | - client.On("Submit", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts). |
197 | | - Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }). |
198 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusAlreadyInMempool}}). |
199 | | - Once() |
200 | | - |
201 | | - ids := [][]byte{[]byte("id1")} |
202 | | - client.On("Submit", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts). |
203 | | - Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }). |
204 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusSuccess, IDs: ids, SubmittedCount: uint64(len(ids))}}). |
205 | | - Once() |
206 | | - |
207 | | - s := newTestSubmitter(t, client, nil) |
208 | | - |
209 | | - items := []string{"only"} |
210 | | - marshalledItems := make([][]byte, len(items)) |
211 | | - for idx, item := range items { |
212 | | - marshalledItems[idx] = []byte(item) |
213 | | - } |
214 | | - |
215 | | - ctx := context.Background() |
216 | | - err := submitToDA[string]( |
217 | | - s, |
218 | | - ctx, |
219 | | - items, |
220 | | - marshalledItems, |
221 | | - func(_ []string, _ *datypes.ResultSubmit) {}, |
222 | | - "item", |
223 | | - nsBz, |
224 | | - opts, |
225 | | - ) |
226 | | - assert.NoError(t, err) |
227 | | - assert.Equal(t, []float64{-1, -1}, usedGas) |
228 | | -} |
229 | | - |
230 | | -func TestSubmitToDA_PartialSuccess_AdvancesWindow(t *testing.T) { |
231 | | - t.Parallel() |
232 | | - |
233 | | - client := mocks.NewMockClient(t) |
234 | | - |
235 | | - nsBz := datypes.NamespaceFromString("ns").Bytes() |
236 | | - |
237 | | - opts := []byte("opts") |
238 | | - var totalSubmitted int |
239 | | - |
240 | | - firstIDs := [][]byte{[]byte("id1"), []byte("id2")} |
241 | | - client.On("Submit", mock.Anything, mock.Anything, mock.Anything, nsBz, opts). |
242 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusSuccess, IDs: firstIDs, SubmittedCount: uint64(len(firstIDs))}}). |
243 | | - Once() |
244 | | - |
245 | | - secondIDs := [][]byte{[]byte("id3")} |
246 | | - client.On("Submit", mock.Anything, mock.Anything, mock.Anything, nsBz, opts). |
247 | | - Return(datypes.ResultSubmit{BaseResult: datypes.BaseResult{Code: datypes.StatusSuccess, IDs: secondIDs, SubmittedCount: uint64(len(secondIDs))}}). |
248 | | - Once() |
249 | | - |
250 | | - s := newTestSubmitter(t, client, nil) |
251 | | - |
252 | | - items := []string{"a", "b", "c"} |
253 | | - marshalledItems := make([][]byte, len(items)) |
254 | | - for idx, item := range items { |
255 | | - marshalledItems[idx] = []byte(item) |
256 | | - } |
257 | | - |
258 | | - ctx := context.Background() |
259 | | - err := submitToDA[string]( |
260 | | - s, |
261 | | - ctx, |
262 | | - items, |
263 | | - marshalledItems, |
264 | | - func(submitted []string, _ *datypes.ResultSubmit) { totalSubmitted += len(submitted) }, |
265 | | - "item", |
266 | | - nsBz, |
267 | | - opts, |
268 | | - ) |
269 | | - assert.NoError(t, err) |
270 | | - assert.Equal(t, 3, totalSubmitted) |
271 | | -} |
0 commit comments