From d34143ecf811dcf64c273300a493a004c9e41de5 Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Mon, 9 Oct 2023 16:57:24 +0300 Subject: [PATCH] fix(blob/service): fix getByCommitment --- blob/service.go | 102 ++++++++++++++++++++------------- nodebuilder/tests/blob_test.go | 3 +- 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/blob/service.go b/blob/service.go index 2130904aa7..0257b0e146 100644 --- a/blob/service.go +++ b/blob/service.go @@ -212,16 +212,41 @@ func (s *Service) getByCommitment( return nil, nil, err } for _, row := range namespacedShares { - if len(row.Shares) == 0 { - break - } - rawShares = append(rawShares, row.Shares...) proofs = append(proofs, row.Proof) - // reconstruct the `blobShare` from the first rawShare in range - // in order to get blob's length(first share will contain this info) - if blobShare == nil { + if blobShare != nil { + // move to the next row if the blob is incomplete. + if amount > len(rawShares) { + continue + } + + blob, same, err := constructAndVerifyBlob(rawShares[:amount], commitment) + if err != nil { + return nil, nil, err + } + if same { + return blob, &proofs, nil + } + + // drop info of the checked blob + if amount == len(rawShares) { + rawShares = nil + proofs = nil + } else { + rawShares = rawShares[amount:] + // save proof for the last row in case we have rawShares + proofs = proofs[len(proofs)-1:] + } + blobShare = nil + amount = 0 + } + + // iterate until we will check every blob from the current row(that is not expanded to + // the next row) + for { + // reconstruct the `blobShare` from the first rawShare in range + // in order to get blob's length(first share will contain this info) for i, shr := range rawShares { bShare, err := shares.NewShare(shr) if err != nil { @@ -249,45 +274,40 @@ func (s *Service) getByCommitment( rawShares = rawShares[i:] break } - } - - // move to the next row if the blob is incomplete. - if amount > len(rawShares) { - continue - } - blob, same, err := constructAndVerifyBlob(rawShares[:amount], commitment) - if err != nil { - return nil, nil, err - } - if same { - return blob, &proofs, nil - } + if blobShare == nil { + // clear rawShares in case if we could not find any non-padding share + rawShares = nil + // clear proofs because we are moving to the next row without extra shares + proofs = nil + break + } - // drop info of the checked blob - rawShares = rawShares[amount:] - if len(rawShares) > 0 { - // save proof for the last row in case we have rawShares - proofs = proofs[len(proofs)-1:] - } else { - // otherwise clear proofs - proofs = nil - } - blobShare = nil - } + // move to the next row if the blob is incomplete. + if amount > len(rawShares) { + break + } - if len(rawShares) == 0 { - return nil, nil, ErrBlobNotFound - } + blob, same, err := constructAndVerifyBlob(rawShares[:amount], commitment) + if err != nil { + return nil, nil, err + } + if same { + return blob, &proofs, nil + } - blob, same, err := constructAndVerifyBlob(rawShares, commitment) - if err != nil { - return nil, nil, err - } - if same { - return blob, &proofs, nil + // drop info of the checked blob + if amount == len(rawShares) { + rawShares = nil + proofs = nil + break + } else { + rawShares = rawShares[amount:] + } + blobShare = nil + amount = 0 + } } - return nil, nil, ErrBlobNotFound } diff --git a/nodebuilder/tests/blob_test.go b/nodebuilder/tests/blob_test.go index 2078fbfa74..852ca29c88 100644 --- a/nodebuilder/tests/blob_test.go +++ b/nodebuilder/tests/blob_test.go @@ -3,6 +3,7 @@ package tests import ( "bytes" "context" + "strings" "testing" "time" @@ -120,7 +121,7 @@ func TestBlobModule(t *testing.T) { b, err := fullClient.Blob.Get(ctx, height, newBlob.Namespace(), newBlob.Commitment) assert.Nil(t, b) require.Error(t, err) - require.ErrorIs(t, err, blob.ErrBlobNotFound) + require.True(t, strings.Contains(err.Error(), blob.ErrBlobNotFound.Error())) }, }, }