diff --git a/blob/blob.go b/blob/blob.go index 610b0cba96..7fca83859d 100644 --- a/blob/blob.go +++ b/blob/blob.go @@ -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" @@ -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) } diff --git a/blob/blob_test.go b/blob/blob_test.go index 665abb7d89..3756e2fffc 100644 --- a/blob/blob_test.go +++ b/blob/blob_test.go @@ -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) @@ -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) { diff --git a/nodebuilder/blobstream/data_root_tuple_root.go b/nodebuilder/blobstream/data_root_tuple_root.go index de2d60c4fa..5596ee2723 100644 --- a/nodebuilder/blobstream/data_root_tuple_root.go +++ b/nodebuilder/blobstream/data_root_tuple_root.go @@ -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" )