Skip to content

Commit

Permalink
fix: take care of the case where the requested height is above the he…
Browse files Browse the repository at this point in the history
…ad of latest da block height (#90)

* Return an err `no blob at a given height` in case there is no blob

* Fix lint

* Silly way of checking error for proxy test

* Add logic to check for the requested height

* Fix test

* Fix test
  • Loading branch information
yarikbratashchuk authored Sep 3, 2024
1 parent 67c3cff commit f06b381
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
12 changes: 9 additions & 3 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
// DefaultMaxBlobSize is the default max blob size
const DefaultMaxBlobSize = 64 * 64 * 482

// ErrNoBlobAtHeight is returned when there is no blob at given height.
var ErrNoBlobAtHeight = errors.New("no blob at given height")
// ErrTooHigh is returned when requested height is to high
var ErrTooHigh = errors.New("given height is from the future")

// DummyDA is a simple implementation of in-memory DA. Not production ready! Intended only for testing!
//
Expand Down Expand Up @@ -85,10 +85,16 @@ func (d *DummyDA) Get(ctx context.Context, ids []da.ID, _ da.Namespace) ([]da.Bl
func (d *DummyDA) GetIDs(ctx context.Context, height uint64, _ da.Namespace) ([]da.ID, error) {
d.mu.Lock()
defer d.mu.Unlock()

if height > d.height {
return nil, ErrTooHigh
}

kvps, ok := d.data[height]
if !ok {
return nil, ErrNoBlobAtHeight
return nil, nil
}

ids := make([]da.ID, len(kvps))
for i, kv := range kvps {
ids[i] = kv.key
Expand Down
20 changes: 9 additions & 11 deletions test/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package test
import (
"bytes"
"context"
"strings"
"sync"
"testing"
"time"
Expand All @@ -29,8 +28,8 @@ func RunDATestSuite(t *testing.T, d da.DA) {
t.Run("Concurrent read/write test", func(t *testing.T) {
ConcurrentReadWriteTest(t, d)
})
t.Run("No blobs at a given height", func(t *testing.T) {
NoBlobsAtHeightTest(t, d)
t.Run("Given height is from the future", func(t *testing.T) {
HeightFromFutureTest(t, d)
})
}

Expand Down Expand Up @@ -138,28 +137,27 @@ func ConcurrentReadWriteTest(t *testing.T, d da.DA) {
defer wg.Done()
for i := uint64(1); i <= 100; i++ {
_, err := d.GetIDs(ctx, i, []byte{})
if err != nil && !strings.Contains(err.Error(), ErrNoBlobAtHeight.Error()) {
assert.NoError(t, err)
if err != nil {
assert.Equal(t, err.Error(), ErrTooHigh.Error())
}
}
}()

go func() {
defer wg.Done()
for i := uint64(1); i <= 100; i++ {
_, err := d.Submit(ctx, [][]byte{[]byte("test")}, 0, testNamespace)
_, err := d.Submit(ctx, [][]byte{[]byte("test")}, 0, []byte{})
assert.NoError(t, err)
}
}()

wg.Wait()
}

// NoBlobsAtHeightTest tests the case when there are no blobs at a given height in DA
func NoBlobsAtHeightTest(t *testing.T, d da.DA) {
// HeightFromFutureTest tests the case when the given height is from the future
func HeightFromFutureTest(t *testing.T, d da.DA) {
ctx := context.TODO()
// GetIDs should return ErrNoBlobAtHeight when there are no blobs at a given height
_, err := d.GetIDs(ctx, 999999999, []byte{})
ids, err := d.GetIDs(ctx, 999999999, []byte{})
assert.Error(t, err)
assert.ErrorContains(t, err, ErrNoBlobAtHeight.Error())
assert.Nil(t, ids)
}

0 comments on commit f06b381

Please sign in to comment.