From 5e6b93458d9a7b9b7c9d57abb2e25ad368a6f70b Mon Sep 17 00:00:00 2001 From: Yarik Bratashchuk Date: Tue, 20 Aug 2024 16:41:09 +0200 Subject: [PATCH] fix: Return an err `no blob at a given height` in case there is no blob (#88) * 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 * test: nit --------- Co-authored-by: Javed Khan --- test/dummy.go | 8 +++++++- test/test_suite.go | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/test/dummy.go b/test/dummy.go index 1cf8548..fcf847c 100644 --- a/test/dummy.go +++ b/test/dummy.go @@ -16,6 +16,9 @@ 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") + // DummyDA is a simple implementation of in-memory DA. Not production ready! Intended only for testing! // // Data is stored in a map, where key is a serialized sequence number. This key is returned as ID. @@ -82,7 +85,10 @@ 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() - kvps := d.data[height] + kvps, ok := d.data[height] + if !ok { + return nil, ErrNoBlobAtHeight + } ids := make([]da.ID, len(kvps)) for i, kv := range kvps { ids[i] = kv.key diff --git a/test/test_suite.go b/test/test_suite.go index 22853ec..117f8b9 100644 --- a/test/test_suite.go +++ b/test/test_suite.go @@ -3,6 +3,7 @@ package test import ( "bytes" "context" + "strings" "sync" "testing" "time" @@ -28,6 +29,9 @@ 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) + }) } // BasicDATest tests round trip of messages to DA and back. @@ -134,7 +138,9 @@ func ConcurrentReadWriteTest(t *testing.T, d da.DA) { defer wg.Done() for i := uint64(1); i <= 100; i++ { _, err := d.GetIDs(ctx, i, []byte{}) - assert.NoError(t, err) + if err != nil && !strings.Contains(err.Error(), ErrNoBlobAtHeight.Error()) { + assert.NoError(t, err) + } } }() @@ -148,3 +154,12 @@ func ConcurrentReadWriteTest(t *testing.T, d da.DA) { 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) { + ctx := context.TODO() + // GetIDs should return ErrNoBlobAtHeight when there are no blobs at a given height + _, err := d.GetIDs(ctx, 999999999, []byte{}) + assert.Error(t, err) + assert.ErrorContains(t, err, ErrNoBlobAtHeight.Error()) +}