Skip to content

Commit 565c80b

Browse files
committed
feat: enhance single sequencer with context management and testing improvements
This commit introduces context management in the sequencer's initialization and submission processes, ensuring better control over timeouts. Additionally, it adds new tests to validate the behavior of the sequencer when handling rollup transactions before data availability (DA) submission, improving overall test coverage and reliability.
1 parent f30d6d6 commit 565c80b

4 files changed

Lines changed: 78 additions & 6 deletions

File tree

rollups/testapp/cmd/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"os"
56
"path/filepath"
67

@@ -74,7 +75,10 @@ var RunCmd = &cobra.Command{
7475
return err
7576
}
7677

78+
ctx, cancel := context.WithCancel(context.Background())
79+
defer cancel()
7780
sequencer, err := single.NewSequencer(
81+
ctx,
7882
logger,
7983
datastore,
8084
daJrpc,

sequencers/single/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/rollkit/rollkit v0.0.0-00010101000000-000000000000
1717
github.com/rollkit/rollkit/core v0.0.0-00010101000000-000000000000
1818
github.com/rollkit/rollkit/da v0.0.0-00010101000000-000000000000
19+
github.com/stretchr/testify v1.10.0
1920
google.golang.org/protobuf v1.36.5
2021
)
2122

@@ -25,18 +26,22 @@ require (
2526
github.com/bytedance/sonic/loader v0.2.4 // indirect
2627
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2728
github.com/cloudwego/base64x v0.1.5 // indirect
29+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2830
github.com/google/uuid v1.6.0 // indirect
2931
github.com/jbenet/goprocess v0.1.4 // indirect
3032
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
3133
github.com/mattn/go-colorable v0.1.13 // indirect
3234
github.com/mattn/go-isatty v0.0.20 // indirect
3335
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
3436
github.com/pkg/errors v0.9.1 // indirect
37+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
3538
github.com/prometheus/client_model v0.6.1 // indirect
3639
github.com/prometheus/common v0.62.0 // indirect
3740
github.com/prometheus/procfs v0.15.1 // indirect
3841
github.com/rs/zerolog v1.33.0 // indirect
42+
github.com/stretchr/objx v0.5.2 // indirect
3943
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
4044
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
4145
golang.org/x/sys v0.31.0 // indirect
46+
gopkg.in/yaml.v3 v3.0.1 // indirect
4247
)

sequencers/single/sequencer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ func NewSequencer(
5656
metrics *Metrics,
5757
proposer bool,
5858
) (*Sequencer, error) {
59-
loadCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
60-
defer cancel()
6159

6260
dalc := dac.NewDAClient(da, -1, -1, daNamespace, nil, logger)
6361

@@ -73,8 +71,9 @@ func NewSequencer(
7371
proposer: proposer,
7472
}
7573

76-
err := s.queue.Load(loadCtx)
77-
if err != nil {
74+
loadCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
75+
defer cancel()
76+
if err := s.queue.Load(loadCtx); err != nil {
7877
return nil, fmt.Errorf("failed to load batch queue from DB: %w", err)
7978
}
8079

@@ -112,6 +111,7 @@ func (c *Sequencer) daSubmissionLoop(ctx context.Context) {
112111
for {
113112
select {
114113
case <-ctx.Done():
114+
c.logger.Info("DA submission loop stopped")
115115
return
116116
case batch := <-c.daSubmissionChan:
117117
err := c.submitBatchToDA(ctx, batch)

sequencers/single/sequencer_test.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package single
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
@@ -12,14 +13,18 @@ import (
1213

1314
coreda "github.com/rollkit/rollkit/core/da"
1415
coresequencer "github.com/rollkit/rollkit/core/sequencer"
16+
damocks "github.com/rollkit/rollkit/da/mocks"
17+
"github.com/stretchr/testify/mock"
1518
)
1619

1720
func TestNewSequencer(t *testing.T) {
1821
// Create a new sequencer with mock DA client
1922
dummyDA := coreda.NewDummyDA(100_000_000, 0, 0)
2023
metrics, _ := NopMetrics()
2124
db := ds.NewMapDatastore()
22-
seq, err := NewSequencer(log.NewNopLogger(), db, dummyDA, []byte("namespace"), []byte("rollup1"), 10*time.Second, metrics, false)
25+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
26+
defer cancel()
27+
seq, err := NewSequencer(ctx, log.NewNopLogger(), db, dummyDA, []byte("namespace"), []byte("rollup1"), 10*time.Second, metrics, false)
2328
if err != nil {
2429
t.Fatalf("Failed to create sequencer: %v", err)
2530
}
@@ -48,7 +53,9 @@ func TestSequencer_SubmitRollupBatchTxs(t *testing.T) {
4853
metrics, _ := NopMetrics()
4954
dummyDA := coreda.NewDummyDA(100_000_000, 0, 0)
5055
db := ds.NewMapDatastore()
51-
seq, err := NewSequencer(log.NewNopLogger(), db, dummyDA, []byte("namespace"), []byte("rollup1"), 10*time.Second, metrics, false)
56+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
57+
defer cancel()
58+
seq, err := NewSequencer(ctx, log.NewNopLogger(), db, dummyDA, []byte("namespace"), []byte("rollup1"), 10*time.Second, metrics, false)
5259
if err != nil {
5360
t.Fatalf("Failed to create sequencer: %v", err)
5461
}
@@ -216,3 +223,59 @@ func TestSequencer_VerifyBatch(t *testing.T) {
216223
t.Fatal("Expected status to be false for invalid batch hash")
217224
}
218225
}
226+
227+
func TestSequencer_GetNextBatch_BeforeDASubmission(t *testing.T) {
228+
// Initialize a new sequencer with mock DA
229+
metrics, _ := NopMetrics()
230+
mockDA := &damocks.DA{}
231+
db := ds.NewMapDatastore()
232+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
233+
defer cancel()
234+
seq, err := NewSequencer(ctx, log.NewNopLogger(), db, mockDA, []byte("namespace"), []byte("rollup1"), 1*time.Second, metrics, false)
235+
if err != nil {
236+
t.Fatalf("Failed to create sequencer: %v", err)
237+
}
238+
defer func() {
239+
err := db.Close()
240+
if err != nil {
241+
t.Fatalf("Failed to close sequencer: %v", err)
242+
}
243+
}()
244+
245+
// Set up mock expectations
246+
mockDA.On("MaxBlobSize", mock.Anything).Return(uint64(100_000_000), nil)
247+
mockDA.On("GasPrice", mock.Anything).Return(float64(0), nil)
248+
mockDA.On("GasMultiplier", mock.Anything).Return(float64(0), nil)
249+
mockDA.On("Submit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
250+
Return(nil, errors.New("mock DA always rejects submissions"))
251+
252+
// Submit a batch
253+
rollupId := []byte("rollup1")
254+
tx := []byte("transaction1")
255+
res, err := seq.SubmitRollupBatchTxs(context.Background(), coresequencer.SubmitRollupBatchTxsRequest{
256+
RollupId: rollupId,
257+
Batch: &coresequencer.Batch{Transactions: [][]byte{tx}},
258+
})
259+
if err != nil {
260+
t.Fatalf("Failed to submit rollup transaction: %v", err)
261+
}
262+
if res == nil {
263+
t.Fatal("Expected response to not be nil")
264+
}
265+
time.Sleep(100 * time.Millisecond)
266+
267+
// Try to get the batch before DA submission
268+
nextBatchResp, err := seq.GetNextBatch(context.Background(), coresequencer.GetNextBatchRequest{RollupId: rollupId})
269+
if err != nil {
270+
t.Fatalf("Failed to get next batch: %v", err)
271+
}
272+
if len(nextBatchResp.Batch.Transactions) != 1 {
273+
t.Fatalf("Expected 1 transaction, got %d", len(nextBatchResp.Batch.Transactions))
274+
}
275+
if !bytes.Equal(nextBatchResp.Batch.Transactions[0], tx) {
276+
t.Fatal("Expected transaction to match submitted transaction")
277+
}
278+
279+
// Verify all mock expectations were met
280+
mockDA.AssertExpectations(t)
281+
}

0 commit comments

Comments
 (0)