Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions da/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"time"

"github.com/gogo/protobuf/proto"

Expand All @@ -14,6 +15,14 @@ import (
pb "github.com/rollkit/rollkit/types/pb/rollkit"
)

var (
// submitTimeout is the timeout for block submission
submitTimeout = 60 * time.Second
Comment thread
tuxcanfly marked this conversation as resolved.
Resolved

// retrieveTimeout is the timeout for block retrieval
retrieveTimeout = 60 * time.Second
)

var (
// ErrBlobNotFound is used to indicate that the blob was not found.
ErrBlobNotFound = errors.New("blob: not found")
Expand Down Expand Up @@ -111,6 +120,8 @@ func (dac *DAClient) SubmitBlocks(ctx context.Context, blocks []*types.Block) Re
},
}
}
ctx, cancel := context.WithTimeout(ctx, submitTimeout)
defer cancel()
ids, _, err := dac.DA.Submit(ctx, blobs, dac.GasPrice)
if err != nil {
return ResultSubmitBlocks{
Expand Down Expand Up @@ -161,6 +172,8 @@ func (dac *DAClient) RetrieveBlocks(ctx context.Context, dataLayerHeight uint64)
}
}

ctx, cancel := context.WithTimeout(ctx, retrieveTimeout)
defer cancel()
blobs, err := dac.DA.Get(ctx, ids)
if err != nil {
return ResultRetrieveBlocks{
Expand Down
46 changes: 40 additions & 6 deletions da/da_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package da

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -67,20 +68,43 @@ func (m *MockDA) Commit(ctx context.Context, blobs []da.Blob) ([]da.Commitment,

func (m *MockDA) Submit(ctx context.Context, blobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
args := m.Called(blobs, gasPrice)
return args.Get(0).([]da.ID), args.Get(1).([]da.Proof), args.Error(2)
select {
case <-ctx.Done():
return nil, nil, ctx.Err()
default:
return args.Get(0).([]da.ID), args.Get(1).([]da.Proof), args.Error(2)
}
}

func (m *MockDA) Validate(ctx context.Context, ids []da.ID, proofs []da.Proof) ([]bool, error) {
args := m.Called(ids, proofs)
return args.Get(0).([]bool), args.Error(1)
}

func TestMockDA(t *testing.T) {
mockDA := &MockDA{}
// Set up the mock to return an error for MaxBlobSize
mockDA.On("MaxBlobSize").Return(uint64(0), errors.New("mock error"))
dalc := &DAClient{DA: mockDA, GasPrice: -1, Logger: log.TestingLogger()}
func TestMockDAErrors(t *testing.T) {
t.Run("submit_timeout", func(t *testing.T) {
mockDA := &MockDA{}
dalc := &DAClient{DA: mockDA, GasPrice: -1, Logger: log.TestingLogger()}
blocks := []*types.Block{types.GetRandomBlock(1, 0)}
var blobs []da.Blob
for _, block := range blocks {
blockBytes, err := block.MarshalBinary()
require.NoError(t, err)
blobs = append(blobs, blockBytes)
}
// Set up the mock to throw context deadline exceeded
mockDA.On("MaxBlobSize").Return(uint64(1234), nil)
mockDA.
On("Submit", blobs, float64(-1)).
After(100*time.Millisecond).
Return([]da.ID{bytes.Repeat([]byte{0x00}, 8)}, []da.Proof{[]byte("proof")}, nil)
doTestSubmitTimeout(t, dalc, blocks)
})
t.Run("max_blob_size_error", func(t *testing.T) {
mockDA := &MockDA{}
dalc := &DAClient{DA: mockDA, GasPrice: -1, Logger: log.TestingLogger()}
// Set up the mock to return an error for MaxBlobSize
mockDA.On("MaxBlobSize").Return(uint64(0), errors.New("mock error"))
doTestMaxBlockSizeError(t, dalc)
})
}
Expand Down Expand Up @@ -134,6 +158,16 @@ func startMockGRPCClient() (*DAClient, error) {
return &DAClient{DA: client, GasPrice: -1, Logger: log.TestingLogger()}, nil
}

func doTestSubmitTimeout(t *testing.T, dalc *DAClient, blocks []*types.Block) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

assert := assert.New(t)
submitTimeout = 50 * time.Millisecond
resp := dalc.SubmitBlocks(ctx, blocks)
assert.Contains(resp.Message, "context deadline exceeded", "should return context timeout error")
}

func doTestMaxBlockSizeError(t *testing.T, dalc *DAClient) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down