-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathsubmitter.go
More file actions
290 lines (263 loc) · 9.43 KB
/
Copy pathsubmitter.go
File metadata and controls
290 lines (263 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package block
import (
"context"
"fmt"
"time"
coreda "github.com/evstack/ev-node/core/da"
"github.com/evstack/ev-node/types"
"google.golang.org/protobuf/proto"
)
// HeaderSubmissionLoop is responsible for submitting headers to the DA layer.
func (m *Manager) HeaderSubmissionLoop(ctx context.Context) {
timer := time.NewTicker(m.config.DA.BlockTime.Duration)
defer timer.Stop()
for {
select {
case <-ctx.Done():
m.logger.Info("header submission loop stopped")
return
case <-timer.C:
}
if m.pendingHeaders.isEmpty() {
continue
}
headersToSubmit, err := m.pendingHeaders.getPendingHeaders(ctx)
if err != nil {
m.logger.Error("error while fetching headers pending DA", "err", err)
continue
}
if len(headersToSubmit) == 0 {
continue
}
err = m.submitHeadersToDA(ctx, headersToSubmit)
if err != nil {
m.logger.Error("error while submitting header to DA", "error", err)
}
}
}
// DataSubmissionLoop is responsible for submitting data to the DA layer.
func (m *Manager) DataSubmissionLoop(ctx context.Context) {
timer := time.NewTicker(m.config.DA.BlockTime.Duration)
defer timer.Stop()
for {
select {
case <-ctx.Done():
m.logger.Info("data submission loop stopped")
return
case <-timer.C:
}
if m.pendingData.isEmpty() {
continue
}
signedDataToSubmit, err := m.createSignedDataToSubmit(ctx)
if err != nil {
m.logger.Error("failed to create signed data to submit", "error", err)
continue
}
if len(signedDataToSubmit) == 0 {
continue
}
err = m.submitDataToDA(ctx, signedDataToSubmit)
if err != nil {
m.logger.Error("failed to submit data to DA", "error", err)
}
}
}
// submitToDA is a generic helper for submitting items to the DA layer with retry, backoff, and gas price logic.
// marshalFn marshals an item to []byte.
// postSubmit is called after a successful submission to update caches, pending lists, etc.
func submitToDA[T any](
m *Manager,
ctx context.Context,
items []T,
marshalFn func(T) ([]byte, error),
postSubmit func([]T, *coreda.ResultSubmit, float64),
itemType string,
) error {
submittedAll := false
var backoff time.Duration
attempt := 0
initialGasPrice := m.gasPrice
gasPrice := initialGasPrice
remaining := items
numSubmitted := 0
// Marshal all items once before the loop
marshaled := make([][]byte, len(items))
for i, item := range items {
bz, err := marshalFn(item)
if err != nil {
return fmt.Errorf("failed to marshal item: %w", err)
}
marshaled[i] = bz
}
remLen := len(items)
for !submittedAll && attempt < maxSubmitAttempts {
select {
case <-ctx.Done():
m.logger.Info("context done, stopping submission loop")
return nil
case <-time.After(backoff):
}
// Use the current remaining items and marshaled bytes
currMarshaled := marshaled
remLen = len(remaining)
submitctx, submitCtxCancel := context.WithTimeout(ctx, 60*time.Second)
// Record DA submission retry attempt
m.recordDAMetrics("submission", DAModeRetry)
res := types.SubmitWithHelpers(submitctx, m.da, m.logger, currMarshaled, gasPrice, nil)
submitCtxCancel()
switch res.Code {
case coreda.StatusSuccess:
// Record successful DA submission
m.recordDAMetrics("submission", DAModeSuccess)
m.logger.Info(fmt.Sprintf("successfully submitted %s to DA layer with gasPrice %v and count %d", itemType, gasPrice, res.SubmittedCount))
if res.SubmittedCount == uint64(remLen) {
submittedAll = true
}
submitted := remaining[:res.SubmittedCount]
notSubmitted := remaining[res.SubmittedCount:]
notSubmittedMarshaled := currMarshaled[res.SubmittedCount:]
numSubmitted += int(res.SubmittedCount)
postSubmit(submitted, &res, gasPrice)
remaining = notSubmitted
marshaled = notSubmittedMarshaled
backoff = 0
if m.gasMultiplier > 0 && gasPrice != -1 {
gasPrice = gasPrice / m.gasMultiplier
gasPrice = max(gasPrice, initialGasPrice)
}
m.logger.Debug("resetting DA layer submission options", "backoff", backoff, "gasPrice", gasPrice)
case coreda.StatusNotIncludedInBlock, coreda.StatusAlreadyInMempool:
m.logger.Error("DA layer submission failed", "error", res.Message, "attempt", attempt)
// Record failed DA submission (will retry)
m.recordDAMetrics("submission", DAModeFail)
backoff = m.config.DA.BlockTime.Duration * time.Duration(m.config.DA.MempoolTTL)
if m.gasMultiplier > 0 && gasPrice != -1 {
gasPrice = gasPrice * m.gasMultiplier
}
m.logger.Info("retrying DA layer submission with", "backoff", backoff, "gasPrice", gasPrice)
case coreda.StatusContextCanceled:
m.logger.Info("DA layer submission canceled due to context cancellation", "attempt", attempt)
return nil
case coreda.StatusTooBig:
m.logger.Warn("DA layer submission failed due to blob size limit", "error", res.Message, "attempt", attempt, "batchSize", len(remaining))
// Record failed DA submission (will retry)
m.recordDAMetrics("submission", DAModeFail)
// Implement batch splitting when blob is too big
if len(remaining) > 1 {
// Split the batch in half to reduce size
splitPoint := len(remaining) / 2
m.logger.Info("splitting batch due to size limit", "originalSize", len(remaining), "newSize", splitPoint)
// Keep only the first half for this attempt
remaining = remaining[:splitPoint]
marshaled = marshaled[:splitPoint]
remLen = len(remaining)
// Reset backoff since we're trying with a smaller batch
backoff = 0
} else {
// If we have only 1 item and it's still too big, we can't split further
m.logger.Error("single item exceeds DA blob size limit", "itemType", itemType, "attempt", attempt)
backoff = m.exponentialBackoff(backoff)
}
default:
m.logger.Error("DA layer submission failed", "error", res.Message, "attempt", attempt)
// Record failed DA submission (will retry)
m.recordDAMetrics("submission", DAModeFail)
backoff = m.exponentialBackoff(backoff)
}
attempt++
}
if !submittedAll {
// Record final failure after all retries are exhausted
m.recordDAMetrics("submission", DAModeFail)
// If not all items are submitted, the remaining items will be retried in the next submission loop.
return fmt.Errorf("failed to submit all %s(s) to DA layer, submitted %d items (%d left) after %d attempts", itemType, numSubmitted, remLen, attempt)
}
return nil
}
// submitHeadersToDA submits a list of headers to the DA layer using the generic submitToDA helper.
func (m *Manager) submitHeadersToDA(ctx context.Context, headersToSubmit []*types.SignedHeader) error {
return submitToDA(m, ctx, headersToSubmit,
func(header *types.SignedHeader) ([]byte, error) {
headerPb, err := header.ToProto()
if err != nil {
return nil, fmt.Errorf("failed to transform header to proto: %w", err)
}
return proto.Marshal(headerPb)
},
func(submitted []*types.SignedHeader, res *coreda.ResultSubmit, gasPrice float64) {
for _, header := range submitted {
m.headerCache.SetDAIncluded(header.Hash().String(), res.Height)
}
lastSubmittedHeaderHeight := uint64(0)
if l := len(submitted); l > 0 {
lastSubmittedHeaderHeight = submitted[l-1].Height()
}
m.pendingHeaders.setLastSubmittedHeaderHeight(ctx, lastSubmittedHeaderHeight)
// Update sequencer metrics if the sequencer supports it
if seq, ok := m.sequencer.(MetricsRecorder); ok {
seq.RecordMetrics(gasPrice, res.BlobSize, res.Code, m.pendingHeaders.numPendingHeaders(), lastSubmittedHeaderHeight)
}
m.sendNonBlockingSignalToDAIncluderCh()
},
"header",
)
}
// submitDataToDA submits a list of signed data to the DA layer using the generic submitToDA helper.
func (m *Manager) submitDataToDA(ctx context.Context, signedDataToSubmit []*types.SignedData) error {
return submitToDA(m, ctx, signedDataToSubmit,
func(signedData *types.SignedData) ([]byte, error) {
return signedData.MarshalBinary()
},
func(submitted []*types.SignedData, res *coreda.ResultSubmit, gasPrice float64) {
for _, signedData := range submitted {
m.dataCache.SetDAIncluded(signedData.Data.DACommitment().String(), res.Height)
}
lastSubmittedDataHeight := uint64(0)
if l := len(submitted); l > 0 {
lastSubmittedDataHeight = submitted[l-1].Height()
}
m.pendingData.setLastSubmittedDataHeight(ctx, lastSubmittedDataHeight)
// Update sequencer metrics if the sequencer supports it
if seq, ok := m.sequencer.(MetricsRecorder); ok {
seq.RecordMetrics(gasPrice, res.BlobSize, res.Code, m.pendingData.numPendingData(), lastSubmittedDataHeight)
}
m.sendNonBlockingSignalToDAIncluderCh()
},
"data",
)
}
// createSignedDataToSubmit converts the list of pending data to a list of SignedData.
func (m *Manager) createSignedDataToSubmit(ctx context.Context) ([]*types.SignedData, error) {
dataList, err := m.pendingData.getPendingData(ctx)
if err != nil {
return nil, err
}
if m.signer == nil {
return nil, fmt.Errorf("signer is nil; cannot sign data")
}
pubKey, err := m.signer.GetPublic()
if err != nil {
return nil, fmt.Errorf("failed to get public key: %w", err)
}
signer := types.Signer{
PubKey: pubKey,
Address: m.genesis.ProposerAddress,
}
signedDataToSubmit := make([]*types.SignedData, 0, len(dataList))
for _, data := range dataList {
if len(data.Txs) == 0 {
continue
}
signature, err := m.getDataSignature(data)
if err != nil {
return nil, fmt.Errorf("failed to get data signature: %w", err)
}
signedDataToSubmit = append(signedDataToSubmit, &types.SignedData{
Data: *data,
Signature: signature,
Signer: signer,
})
}
return signedDataToSubmit, nil
}