Skip to content

Commit 1a81fe5

Browse files
Refactor out submitAllTxs
1 parent 1643246 commit 1a81fe5

2 files changed

Lines changed: 32 additions & 50 deletions

File tree

block/submitter.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (m *Manager) BatchSubmissionLoop(ctx context.Context) {
146146
continue
147147
}
148148

149-
err = m.submitBatchToDA(ctx, signedData)
149+
err = m.submitDataToDA(ctx, signedData)
150150
if err != nil {
151151
m.logger.Error("failed to submit batch to DA", "error", err)
152152
}
@@ -185,7 +185,7 @@ func (m *Manager) createSignedDataFromBatch(batch *coresequencer.Batch) (*types.
185185
}, nil
186186
}
187187

188-
// submitBatchToDA submits a batch of transactions to the Data Availability (DA) layer.
188+
// submitBatchToDA submits signed data to the Data Availability (DA) layer.
189189
// It implements a retry mechanism with exponential backoff and gas price adjustments
190190
// to handle various failure scenarios.
191191
//
@@ -198,28 +198,25 @@ func (m *Manager) createSignedDataFromBatch(batch *coresequencer.Batch) (*types.
198198
// - On other errors: Uses exponential backoff
199199
//
200200
// It returns an error if not all transactions could be submitted after all attempts.
201-
func (m *Manager) submitBatchToDA(ctx context.Context, signedData *types.SignedData) error {
202-
currentSignedData := signedData
203-
submittedAllTxs := false
201+
func (m *Manager) submitDataToDA(ctx context.Context, signedData *types.SignedData) error {
204202
var backoff time.Duration
205-
totalTxCount := len(currentSignedData.Txs)
206-
submittedTxCount := 0
207203
attempt := 0
204+
submitted := false
208205

209206
// Store initial values to be able to reset or compare later
210207
initialGasPrice := m.gasPrice
211208
gasPrice := initialGasPrice
212209

213210
daSubmitRetryLoop:
214-
for !submittedAllTxs && attempt < maxSubmitAttempts {
211+
for !submitted && attempt < maxSubmitAttempts {
215212
// Wait for backoff duration or exit if context is done
216213
select {
217214
case <-ctx.Done():
218215
break daSubmitRetryLoop
219216
case <-time.After(backoff):
220217
}
221218

222-
signedDataBz, err := currentSignedData.MarshalBinary()
219+
signedDataBz, err := signedData.MarshalBinary()
223220
if err != nil {
224221
return fmt.Errorf("failed to marshal signed data: %w", err)
225222
}
@@ -235,22 +232,11 @@ daSubmitRetryLoop:
235232

236233
switch res.Code {
237234
case coreda.StatusSuccess:
238-
submittedTxs := int(res.SubmittedCount)
239-
m.logger.Info("successfully submitted transactions to DA layer",
235+
m.logger.Info("successfully submitted data to DA layer",
240236
"gasPrice", gasPrice,
241-
"height", res.Height,
242-
"submittedTxs", submittedTxs,
243-
"remainingTxs", len(currentSignedData.Txs)-submittedTxs)
244-
245-
submittedTxCount += submittedTxs
246-
247-
// Check if all transactions in the current batch were submitted
248-
if submittedTxs == len(currentSignedData.Txs) {
249-
submittedAllTxs = true
250-
} else {
251-
// Update the current batch to contain only the remaining transactions
252-
currentSignedData.Txs = currentSignedData.Txs[submittedTxs:]
253-
}
237+
"height", res.Height)
238+
239+
submitted = true
254240

255241
// Reset submission parameters after success
256242
backoff = 0
@@ -263,11 +249,10 @@ daSubmitRetryLoop:
263249
}
264250
}
265251
m.logger.Debug("resetting DA layer submission options", "backoff", backoff, "gasPrice", gasPrice)
266-
// Set DA included in manager's dataCache if all txs submitted and manager is set
267-
if submittedAllTxs {
268-
m.DataCache().SetDAIncluded(signedData.DACommitment().String())
269-
m.sendNonBlockingSignalToDAIncluderCh()
270-
}
252+
253+
m.DataCache().SetDAIncluded(signedData.DACommitment().String())
254+
m.sendNonBlockingSignalToDAIncluderCh()
255+
return nil
271256

272257
case coreda.StatusNotIncludedInBlock, coreda.StatusAlreadyInMempool:
273258
m.logger.Error("DA layer submission failed", "error", res.Message, "attempt", attempt)
@@ -291,11 +276,9 @@ daSubmitRetryLoop:
291276
}
292277

293278
// Return error if not all transactions were submitted after all attempts
294-
if !submittedAllTxs {
279+
if !submitted {
295280
return fmt.Errorf(
296-
"failed to submit all transactions to DA layer, submitted %d txs (%d left) after %d attempts",
297-
submittedTxCount,
298-
totalTxCount-submittedTxCount,
281+
"failed to submit data to DA layer after %d attempts",
299282
attempt,
300283
)
301284
}

block/submitter_test.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/libp2p/go-libp2p/core/crypto"
1313
coreda "github.com/rollkit/rollkit/core/da"
14-
coresequencer "github.com/rollkit/rollkit/core/sequencer"
1514
"github.com/rollkit/rollkit/pkg/cache"
1615
"github.com/rollkit/rollkit/pkg/config"
1716
"github.com/rollkit/rollkit/pkg/signer/noop"
@@ -41,13 +40,11 @@ func newTestManagerWithDA(t *testing.T, da *mocks.DA) (m *Manager) {
4140
}
4241
}
4342

44-
// TestSubmitBatchToDA_Success verifies that submitBatchToDA succeeds when the DA layer accepts the batch.
45-
func TestSubmitBatchToDA_Success(t *testing.T) {
43+
// TestSubmitDataToDA_Success verifies that submitDataToDA succeeds when the DA layer accepts the batch.
44+
func TestSubmitDataToDA_Success(t *testing.T) {
4645
da := &mocks.DA{}
4746
m := newTestManagerWithDA(t, da)
4847

49-
batch := coresequencer.Batch{Transactions: [][]byte{[]byte("tx1"), []byte("tx2")}}
50-
5148
// Simulate DA success
5249
da.On("GasMultiplier", mock.Anything).Return(2.0, nil)
5350
da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
@@ -58,36 +55,36 @@ func TestSubmitBatchToDA_Success(t *testing.T) {
5855
addr, err := m.signer.GetAddress()
5956
require.NoError(t, err)
6057

58+
transactions := [][]byte{[]byte("tx1"), []byte("tx2")}
59+
6160
signedData := types.SignedData{
6261
Data: types.Data{
63-
Txs: make(types.Txs, len(batch.Transactions)),
62+
Txs: make(types.Txs, len(transactions)),
6463
},
6564
Signer: types.Signer{
6665
Address: addr,
6766
PubKey: pubKey,
6867
},
6968
}
7069

71-
signedData.Txs = make(types.Txs, len(batch.Transactions))
72-
for i, tx := range batch.Transactions {
70+
signedData.Txs = make(types.Txs, len(transactions))
71+
for i, tx := range transactions {
7372
signedData.Txs[i] = types.Tx(tx)
7473
}
7574

7675
signature, err := m.getDataSignature(&signedData.Data)
7776
require.NoError(t, err)
7877
signedData.Signature = signature
7978

80-
err = m.submitBatchToDA(context.Background(), &signedData)
79+
err = m.submitDataToDA(context.Background(), &signedData)
8180
assert.NoError(t, err)
8281
}
8382

84-
// TestSubmitBatchToDA_Failure verifies that submitBatchToDA returns an error for various DA failures.
85-
func TestSubmitBatchToDA_Failure(t *testing.T) {
83+
// TestSubmitDataToDA_Failure verifies that submitDataToDA returns an error for various DA failures.
84+
func TestSubmitDataToDA_Failure(t *testing.T) {
8685
da := &mocks.DA{}
8786
m := newTestManagerWithDA(t, da)
8887

89-
batch := coresequencer.Batch{Transactions: [][]byte{[]byte("tx1"), []byte("tx2")}}
90-
9188
// Table-driven test for different DA error scenarios
9289
testCases := []struct {
9390
name string
@@ -110,18 +107,20 @@ func TestSubmitBatchToDA_Failure(t *testing.T) {
110107
addr, err := m.signer.GetAddress()
111108
require.NoError(t, err)
112109

110+
transactions := [][]byte{[]byte("tx1"), []byte("tx2")}
111+
113112
signedData := types.SignedData{
114113
Data: types.Data{
115-
Txs: make(types.Txs, len(batch.Transactions)),
114+
Txs: make(types.Txs, len(transactions)),
116115
},
117116
Signer: types.Signer{
118117
Address: addr,
119118
PubKey: pubKey,
120119
},
121120
}
122121

123-
signedData.Txs = make(types.Txs, len(batch.Transactions))
124-
for i, tx := range batch.Transactions {
122+
signedData.Txs = make(types.Txs, len(transactions))
123+
for i, tx := range transactions {
125124
signedData.Txs[i] = types.Tx(tx)
126125
}
127126

@@ -130,7 +129,7 @@ func TestSubmitBatchToDA_Failure(t *testing.T) {
130129
signedData.Signature = signature
131130

132131
// Expect an error from submitBatchToDA
133-
err = m.submitBatchToDA(context.Background(), &signedData)
132+
err = m.submitDataToDA(context.Background(), &signedData)
134133
assert.Error(t, err, "expected error")
135134
})
136135
}

0 commit comments

Comments
 (0)