Skip to content

Commit

Permalink
chore: rework returned values
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Mar 7, 2024
1 parent db8adb7 commit b88b3d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
51 changes: 26 additions & 25 deletions blob/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"math"
"sync"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -161,52 +160,54 @@ func (s *Service) GetProof(

// GetAll returns all blobs under the given namespaces at the given height.
// GetAll can return blobs and an error in case if some requests failed.
// GetAll can return both empty values in case if blobs were not found for the requested namespaces.
func (s *Service) GetAll(ctx context.Context, height uint64, namespaces []share.Namespace) ([]*Blob, error) {
header, err := s.headerGetter(ctx, height)
if err != nil {
return nil, err
}

var (
resultBlobs = make([][]*Blob, len(namespaces))
resultErr = make([]error, len(namespaces))
)
blobs = make([]*Blob, 0)

for _, namespace := range namespaces {
log.Debugw("performing GetAll request", "namespace", namespace.String(), "height", height)
}
blobsCh = make(chan []*Blob, 1)
errCh = make(chan error, 1)
)

wg := sync.WaitGroup{}
for i, namespace := range namespaces {
wg.Add(1)
go func(i int, namespace share.Namespace) {
defer wg.Done()
log.Debugw("retrieving all blobs from", "namespace", namespace.String(), "height", height)
blobs, err := s.getBlobs(ctx, namespace, header)
if err != nil {
resErr := fmt.Errorf("getting blobs for namespace(%s): %w", namespace.String(), err)
if errors.Is(err, ErrBlobNotFound) {
log.Debug(resErr)
return
}

resultErr[i] = resErr
err = fmt.Errorf("getting blobs for namespace(%s): %w", namespace.String(), err)
errCh <- err
log.Debug(err)
return
}

log.Debugw("receiving blobs", "height", height, "total", len(blobs))
resultBlobs[i] = blobs
blobsCh <- blobs
}(i, namespace)
}
wg.Wait()

blobs := make([]*Blob, 0)
for _, resBlobs := range resultBlobs {
if len(resBlobs) > 0 {
blobs = append(blobs, resBlobs...)
for range namespaces {
select {
case <-ctx.Done():
return blobs, ctx.Err()
case b := <-blobsCh:
blobs = append(blobs, b...)
case resultErr := <-errCh:
if errors.Is(resultErr, ErrBlobNotFound) {
continue

}
err = errors.Join(err, resultErr)
}
}
return blobs, errors.Join(resultErr...)

if len(blobs) == 0 && err == nil {
err = ErrBlobNotFound
}
return blobs, err
}

// Included verifies that the blob was included in a specific height.
Expand Down
6 changes: 1 addition & 5 deletions blob/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ func TestBlobService_Get(t *testing.T) {
blobs, ok := res.([]*Blob)
assert.True(t, ok)
assert.NotEmpty(t, blobs)

assert.Len(t, blobs, 2)
// check the order
require.True(t, bytes.Equal(blobs[0].Namespace(), blobs0[0].Namespace()))
require.True(t, bytes.Equal(blobs[1].Namespace(), blobs0[1].Namespace()))
},
},
{
Expand Down Expand Up @@ -304,7 +300,7 @@ func TestBlobService_Get(t *testing.T) {
blobs, ok := i.([]*Blob)
require.True(t, ok)
assert.Empty(t, blobs)
require.NoError(t, err)
assert.ErrorIs(t, err, ErrBlobNotFound)
},
},
{
Expand Down

0 comments on commit b88b3d3

Please sign in to comment.