Skip to content

Commit

Permalink
feat(blob): implement length method (celestiaorg#3606)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs authored and sebasti810 committed Aug 14, 2024
1 parent dbaa159 commit 574e149
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
25 changes: 25 additions & 0 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-app/x/blob/types"
"github.com/celestiaorg/nmt"

Expand Down Expand Up @@ -108,6 +109,30 @@ func (b *Blob) Index() int {
return b.index
}

// Length returns the number of shares in the blob.
func (b *Blob) Length() (int, error) {
s, err := BlobsToShares(b)
if err != nil {
return 0, err
}

if len(s) == 0 {
return 0, errors.New("blob with zero shares received")
}

appShare, err := shares.NewShare(s[0])
if err != nil {
return 0, err
}

seqLength, err := appShare.SequenceLen()
if err != nil {
return 0, err
}

return shares.SparseSharesNeeded(seqLength), nil
}

func (b *Blob) compareCommitments(com Commitment) bool {
return bytes.Equal(b.Commitment, com)
}
Expand Down
11 changes: 10 additions & 1 deletion blob/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

func TestBlob(t *testing.T) {
appBlobs, err := blobtest.GenerateV0Blobs([]int{16}, false)
length := 16
appBlobs, err := blobtest.GenerateV0Blobs([]int{length}, false)
require.NoError(t, err)
blob, err := convertBlobs(appBlobs...)
require.NoError(t, err)
Expand Down Expand Up @@ -48,6 +49,14 @@ func TestBlob(t *testing.T) {
require.NoError(t, apptypes.ValidateBlobNamespace(ns))
},
},
{
name: "verify length",
expectedRes: func(t *testing.T) {
blobLength, err := blob[0].Length()
require.NoError(t, err)
assert.Equal(t, length, blobLength)
},
},
{
name: "shares to blobs",
expectedRes: func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions nodebuilder/blobstream/data_root_tuple_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"fmt"
"strconv"

nodeheader "github.com/celestiaorg/celestia-node/header"

"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/libs/bytes"

nodeheader "github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
)

Expand Down

0 comments on commit 574e149

Please sign in to comment.