Skip to content

Commit

Permalink
chore!: merge blob pkg into share pkg (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters authored Jun 24, 2024
1 parent 7f4a775 commit 6576955
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 269 deletions.
5 changes: 2 additions & 3 deletions inclusion/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package inclusion
import (
"crypto/sha256"

"github.com/celestiaorg/go-square/blob"
ns "github.com/celestiaorg/go-square/namespace"
sh "github.com/celestiaorg/go-square/shares"
"github.com/celestiaorg/nmt"
Expand All @@ -16,7 +15,7 @@ type MerkleRootFn func([][]byte) []byte
//
// [data square layout rationale]: ../../specs/src/specs/data_square_layout.md
// [blob share commitment rules]: ../../specs/src/specs/data_square_layout.md#blob-share-commitment-rules
func CreateCommitment(blob *blob.Blob, merkleRootFn MerkleRootFn, subtreeRootThreshold int) ([]byte, error) {
func CreateCommitment(blob *sh.Blob, merkleRootFn MerkleRootFn, subtreeRootThreshold int) ([]byte, error) {
shares, err := sh.SplitBlobs(blob)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,7 +69,7 @@ func CreateCommitment(blob *blob.Blob, merkleRootFn MerkleRootFn, subtreeRootThr
return merkleRootFn(subTreeRoots), nil
}

func CreateCommitments(blobs []*blob.Blob, merkleRootFn MerkleRootFn, subtreeRootThreshold int) ([][]byte, error) {
func CreateCommitments(blobs []*sh.Blob, merkleRootFn MerkleRootFn, subtreeRootThreshold int) ([][]byte, error) {
commitments := make([][]byte, len(blobs))
for i, blob := range blobs {
commitment, err := CreateCommitment(blob, merkleRootFn, subtreeRootThreshold)
Expand Down
7 changes: 3 additions & 4 deletions inclusion/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/sha256"
"testing"

"github.com/celestiaorg/go-square/blob"
"github.com/celestiaorg/go-square/inclusion"
"github.com/celestiaorg/go-square/namespace"
"github.com/celestiaorg/go-square/shares"
Expand Down Expand Up @@ -85,10 +84,10 @@ func TestCreateCommitment(t *testing.T) {
{
name: "blob of one share with signer succeeds",
namespace: ns1,
blob: bytes.Repeat([]byte{0xFF}, shares.AvailableBytesFromSparseShares(2)-blob.SignerSize),
blob: bytes.Repeat([]byte{0xFF}, shares.AvailableBytesFromSparseShares(2)-shares.SignerSize),
expected: []byte{0x88, 0x3c, 0x74, 0x6, 0x4e, 0x8e, 0x26, 0x27, 0xad, 0x58, 0x8, 0x38, 0x9f, 0x1f, 0x19, 0x24, 0x19, 0x4c, 0x1a, 0xe2, 0x3c, 0x7d, 0xf9, 0x62, 0xc8, 0xd5, 0x6d, 0xf0, 0x62, 0xa9, 0x2b, 0x2b},
shareVersion: shares.ShareVersionOne,
signer: bytes.Repeat([]byte{1}, blob.SignerSize),
signer: bytes.Repeat([]byte{1}, shares.SignerSize),
},
{
name: "blob with unsupported share version should return error",
Expand All @@ -100,7 +99,7 @@ func TestCreateCommitment(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
blob, err := blob.New(tt.namespace, tt.blob, tt.shareVersion, tt.signer)
blob, err := shares.NewBlob(tt.namespace, tt.blob, tt.shareVersion, tt.signer)
require.NoError(t, err)
res, err := inclusion.CreateCommitment(blob, twoLeafMerkleRoot, defaultSubtreeRootThreshold)
if tt.expectErr {
Expand Down
9 changes: 4 additions & 5 deletions internal/test/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/celestiaorg/go-square/blob"
"github.com/celestiaorg/go-square/namespace"
"github.com/celestiaorg/go-square/shares"
)
Expand Down Expand Up @@ -39,22 +38,22 @@ func RandomBytes(size int) []byte {
}

func GenerateBlobTxWithNamespace(namespaces []namespace.Namespace, blobSizes []int, version uint8) []byte {
blobs := make([]*blob.Blob, len(blobSizes))
blobs := make([]*shares.Blob, len(blobSizes))
if len(namespaces) != len(blobSizes) {
panic("number of namespaces should match number of blob sizes")
}
var err error
var signer []byte
if version == shares.ShareVersionOne {
signer = RandomBytes(blob.SignerSize)
signer = RandomBytes(shares.SignerSize)
}
for i, size := range blobSizes {
blobs[i], err = blob.New(namespaces[i], RandomBytes(size), version, signer)
blobs[i], err = shares.NewBlob(namespaces[i], RandomBytes(size), version, signer)
if err != nil {
panic(err)
}
}
blobTx, err := blob.MarshalBlobTx(MockPFB(toUint32(blobSizes)), blobs...)
blobTx, err := shares.MarshalBlobTx(MockPFB(toUint32(blobSizes)), blobs...)
if err != nil {
panic(err)
}
Expand Down
40 changes: 0 additions & 40 deletions namespace/random_blob.go

This file was deleted.

38 changes: 37 additions & 1 deletion namespace/random_namespace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package namespace

import "crypto/rand"
import (
"crypto/rand"
"slices"
)

func RandomNamespace() Namespace {
for {
Expand All @@ -21,3 +24,36 @@ func RandomVerzionZeroID() []byte {
}
return append(NamespaceVersionZeroPrefix, namespace...)
}

func RandomBlobNamespaceID() []byte {
namespace := make([]byte, NamespaceVersionZeroIDSize)
_, err := rand.Read(namespace)
if err != nil {
panic(err)
}
return namespace
}

func RandomBlobNamespace() Namespace {
for {
id := RandomBlobNamespaceID()
namespace := MustNewV0(id)
if isBlobNamespace(namespace) {
return namespace
}
}
}

// isBlobNamespace returns an true if this namespace is a valid user-specifiable
// blob namespace.
func isBlobNamespace(ns Namespace) bool {
if ns.IsReserved() {
return false
}

if !slices.Contains(SupportedBlobNamespaceVersions, ns.Version()) {
return false
}

return true
}
43 changes: 12 additions & 31 deletions blob/blob.go → shares/blob.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Package blob provides types and functions for working with blobs, blob
// transactions, and index wrapper transactions.
package blob
package shares

import (
"errors"
Expand All @@ -11,23 +9,6 @@ import (
"google.golang.org/protobuf/proto"
)

// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
var SupportedBlobNamespaceVersions = []uint8{ns.NamespaceVersionZero}

// ProtoBlobTxTypeID is included in each encoded BlobTx to help prevent
// decoding binaries that are not actually BlobTxs.
const ProtoBlobTxTypeID = "BLOB"

// ProtoIndexWrapperTypeID is included in each encoded IndexWrapper to help prevent
// decoding binaries that are not actually IndexWrappers.
const ProtoIndexWrapperTypeID = "INDX"

// MaxShareVersion is the maximum value a share version can be. See: [shares.MaxShareVersion].
const MaxShareVersion = 127

// SignerSize is the size of the signer in bytes.
const SignerSize = 20

// Blob (stands for binary large object) is a core type that represents data
// to be submitted to the Celestia network alongside an accompanying namespace
// and optional signer (for proving the author of the blob)
Expand All @@ -40,13 +21,16 @@ type Blob struct {

// New creates a new coretypes.Blob from the provided data after performing
// basic stateless checks over it.
func New(ns ns.Namespace, data []byte, shareVersion uint8, signer []byte) (*Blob, error) {
func NewBlob(ns ns.Namespace, data []byte, shareVersion uint8, signer []byte) (*Blob, error) {
if shareVersion == 0 && signer != nil {
return nil, errors.New("share version 0 does not support signer")
}
if shareVersion == 1 && len(signer) != SignerSize {
return nil, errors.New("share version 1 requires signer of size 20 bytes")
}
if shareVersion > MaxShareVersion {
return nil, errors.New("share version can not be greater than MaxShareVersion")
}
return &Blob{
namespace: ns,
data: data,
Expand All @@ -55,24 +39,21 @@ func New(ns ns.Namespace, data []byte, shareVersion uint8, signer []byte) (*Blob
}, nil
}

func NewV0(ns ns.Namespace, data []byte) *Blob {
blob, err := New(ns, data, 0, nil)
func NewV0Blob(ns ns.Namespace, data []byte) *Blob {
blob, err := NewBlob(ns, data, 0, nil)
if err != nil {
panic(err)
}
return blob
}

func NewV1(ns ns.Namespace, data []byte, signer []byte) (*Blob, error) {
return New(ns, data, 1, signer)
func NewV1Blob(ns ns.Namespace, data []byte, signer []byte) (*Blob, error) {
return NewBlob(ns, data, 1, signer)
}

// NewFromProto creates a Blob from the proto format and performs
// rudimentary validation checks on the structure
func NewFromProto(pb *BlobProto) (*Blob, error) {
if pb.ShareVersion > MaxShareVersion {
return nil, errors.New("share version can not be greater than MaxShareVersion")
}
func NewBlobFromProto(pb *BlobProto) (*Blob, error) {
if pb.NamespaceVersion > ns.NamespaceVersionMax {
return nil, errors.New("namespace version can not be greater than MaxNamespaceVersion")
}
Expand All @@ -83,7 +64,7 @@ func NewFromProto(pb *BlobProto) (*Blob, error) {
if err != nil {
return nil, fmt.Errorf("invalid namespace: %w", err)
}
return New(
return NewBlob(
ns,
pb.Data,
uint8(pb.ShareVersion),
Expand Down Expand Up @@ -174,7 +155,7 @@ func blobsToProto(blobs []*Blob) []*BlobProto {
}

// Sort sorts the blobs by their namespace.
func Sort(blobs []*Blob) {
func SortBlobs(blobs []*Blob) {
sort.SliceStable(blobs, func(i, j int) bool {
return blobs[i].Compare(blobs[j]) < 0
})
Expand Down
Loading

0 comments on commit 6576955

Please sign in to comment.