From b88b3d326407c43b8ac1b9f4da20b6d6a8ff0a65 Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Thu, 7 Mar 2024 14:46:41 +0200 Subject: [PATCH] chore: rework returned values --- blob/service.go | 51 ++++++++++++++++++++++---------------------- blob/service_test.go | 6 +----- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/blob/service.go b/blob/service.go index cd40ca3c4f..138f856670 100644 --- a/blob/service.go +++ b/blob/service.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "math" - "sync" sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/types" @@ -161,7 +160,6 @@ 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 { @@ -169,44 +167,47 @@ func (s *Service) GetAll(ctx context.Context, height uint64, namespaces []share. } 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. diff --git a/blob/service_test.go b/blob/service_test.go index 5b60c2900a..7844c023f3 100644 --- a/blob/service_test.go +++ b/blob/service_test.go @@ -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())) }, }, { @@ -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) }, }, {