Skip to content

Commit

Permalink
feat(blob): extend blob struct with index field
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Feb 5, 2024
1 parent 01585ba commit 43fd867
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
14 changes: 13 additions & 1 deletion blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ type Blob struct {
// the celestia-node's namespace type
// this is to avoid converting to and from app's type
namespace share.Namespace

// index represents index of the first share in the eds.
// before data is being published, the index set by default to -1.
index int
}

// NewBlobV0 constructs a new blob from the provided Namespace and data.
Expand Down Expand Up @@ -127,19 +131,25 @@ func NewBlob(shareVersion uint8, namespace share.Namespace, data []byte) (*Blob,
if err != nil {
return nil, err
}
return &Blob{Blob: blob, Commitment: com, namespace: namespace}, nil
return &Blob{Blob: blob, Commitment: com, namespace: namespace, index: -1}, nil
}

// Namespace returns blob's namespace.
func (b *Blob) Namespace() share.Namespace {
return b.namespace
}

// Index returns the index of the first share in the eds.
func (b *Blob) Index() int {
return b.index
}

type jsonBlob struct {
Namespace share.Namespace `json:"namespace"`
Data []byte `json:"data"`
ShareVersion uint32 `json:"share_version"`
Commitment Commitment `json:"commitment"`
Index int `json:"index"`
}

func (b *Blob) MarshalJSON() ([]byte, error) {
Expand All @@ -148,6 +158,7 @@ func (b *Blob) MarshalJSON() ([]byte, error) {
Data: b.Data,
ShareVersion: b.ShareVersion,
Commitment: b.Commitment,
Index: b.index,
}
return json.Marshal(blob)
}
Expand All @@ -165,6 +176,7 @@ func (b *Blob) UnmarshalJSON(data []byte) error {
b.Blob.ShareVersion = blob.ShareVersion
b.Commitment = blob.Commitment
b.namespace = blob.Namespace
b.index = blob.Index
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions blob/blob_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package blob

import (
"reflect"
"bytes"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -67,7 +67,8 @@ func TestBlob(t *testing.T) {

newBlob := &Blob{}
require.NoError(t, newBlob.UnmarshalJSON(data))
require.True(t, reflect.DeepEqual(blob[0], newBlob))
require.True(t, bytes.Equal(blob[0].Blob.Data, newBlob.Data))
require.True(t, bytes.Equal(blob[0].Commitment, newBlob.Commitment))
},
},
}
Expand Down
22 changes: 19 additions & 3 deletions blob/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ func (s *Service) getByCommitment(

getCtx, getSharesSpan := tracer.Start(ctx, "get-shares-by-namespace")

// ensure that requested namespace can be found inside the DAH before retrieving it.
offset := -1
for i, row := range header.DAH.RowRoots {
if !namespace.IsOutsideRange(row, row) {
offset = i
break
}
}
if offset == -1 {
return nil, nil, ErrBlobNotFound
}

namespacedShares, err := s.shareGetter.GetSharesByNamespace(getCtx, header, namespace)
if err != nil {
if errors.Is(err, share.ErrNotFound) {
Expand Down Expand Up @@ -291,9 +303,10 @@ func (s *Service) getByCommitment(
if err != nil {
return nil, nil, err
}

for _, b := range blobs {
if b.Commitment.Equal(commitment) {
span.AddEvent("blob reconstructed")
b.index = offset*len(header.DAH.RowRoots) + proofs[0].Start()
return b, &proofs, nil
}
// Falling under this flag means that the data from the last row
Expand All @@ -303,15 +316,18 @@ func (s *Service) getByCommitment(
// to the current row and have a single proof.
if spansMultipleRows {
spansMultipleRows = false
// leave proof only for the current row
// move offset to the current row index.
offset += len(proofs) - 1
// leave proof only for the current row.
proofs = proofs[len(proofs)-1:]
}
}

if len(rawShares) > 0 {
spansMultipleRows = true
continue
}
// moving to the next row.
offset++
proofs = nil
}

Expand Down

0 comments on commit 43fd867

Please sign in to comment.