Skip to content

Commit

Permalink
return share package back
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Oct 7, 2024
1 parent b440d3f commit 3b1ddaa
Show file tree
Hide file tree
Showing 239 changed files with 1,387 additions and 1,618 deletions.
10 changes: 5 additions & 5 deletions api/docgen/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (

"github.com/celestiaorg/go-fraud"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/das"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/square"
"github.com/celestiaorg/celestia-node/square/eds/byzantine"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/eds/byzantine"
"github.com/celestiaorg/celestia-node/state"
)

Expand Down Expand Up @@ -80,7 +80,7 @@ var ExampleValues = map[reflect.Type]interface{}{
}

func init() {
addToExampleValues(square.EmptyEDS())
addToExampleValues(share.EmptyEDS())
addr, err := sdk.AccAddressFromBech32("celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h")
if err != nil {
panic(err)
Expand Down Expand Up @@ -166,7 +166,7 @@ func init() {

// randomly generated namespace that's used in the blob example above
// (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAMJ/xGlNMdE=)
namespace, err := share.NewV0Namespace([]byte{0xc2, 0x7f, 0xc4, 0x69, 0x4d, 0x31, 0xd1})
namespace, err := gosquare.NewV0Namespace([]byte{0xc2, 0x7f, 0xc4, 0x69, 0x4d, 0x31, 0xd1})
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions api/gateway/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/gorilla/mux"

"github.com/celestiaorg/celestia-node/square"
"github.com/celestiaorg/celestia-node/share"
)

const heightAvailabilityEndpoint = "/data_available"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (h *Handler) handleHeightAvailabilityRequest(w http.ResponseWriter, r *http
if werr != nil {
log.Errorw("serving request", "endpoint", heightAvailabilityEndpoint, "err", err)
}
case errors.Is(err, square.ErrNotAvailable):
case errors.Is(err, share.ErrNotAvailable):
resp, err := json.Marshal(&AvailabilityResponse{Available: false})
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
Expand Down
24 changes: 12 additions & 12 deletions api/gateway/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/gorilla/mux"

"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
)

const (
Expand All @@ -22,8 +22,8 @@ var namespaceKey = "nid"
// NamespacedSharesResponse represents the response to a
// SharesByNamespace request.
type NamespacedSharesResponse struct {
Shares []share.Share `json:"shares"`
Height uint64 `json:"height"`
Shares []gosquare.Share `json:"shares"`
Height uint64 `json:"height"`
}

// NamespacedDataResponse represents the response to a
Expand Down Expand Up @@ -88,7 +88,7 @@ func (h *Handler) handleDataByNamespaceRequest(w http.ResponseWriter, r *http.Re
}
}

func (h *Handler) getShares(ctx context.Context, height uint64, namespace share.Namespace) ([]share.Share, error) {
func (h *Handler) getShares(ctx context.Context, height uint64, namespace gosquare.Namespace) ([]gosquare.Share, error) {
header, err := h.header.GetByHeight(ctx, height)
if err != nil {
return nil, err
Expand All @@ -102,8 +102,8 @@ func (h *Handler) getShares(ctx context.Context, height uint64, namespace share.
return shares.Flatten(), nil
}

func dataFromShares(input []share.Share) (data [][]byte, err error) {
sequences, err := share.ParseShares(input, false)
func dataFromShares(input []gosquare.Share) (data [][]byte, err error) {
sequences, err := gosquare.ParseShares(input, false)
if err != nil {
return nil, err
}
Expand All @@ -117,24 +117,24 @@ func dataFromShares(input []share.Share) (data [][]byte, err error) {
return data, nil
}

func parseGetByNamespaceArgs(r *http.Request) (height uint64, namespace share.Namespace, err error) {
func parseGetByNamespaceArgs(r *http.Request) (height uint64, namespace gosquare.Namespace, err error) {
vars := mux.Vars(r)
// if a height was given, parse it, otherwise get namespaced shares/data from the latest header
if strHeight, ok := vars[heightKey]; ok {
height, err = strconv.ParseUint(strHeight, 10, 64)
if err != nil {
return 0, share.Namespace{}, err
return 0, gosquare.Namespace{}, err
}
}
hexNamespace := vars[namespaceKey]
nsString, err := hex.DecodeString(hexNamespace)
if err != nil {
return 0, share.Namespace{}, err
return 0, gosquare.Namespace{}, err
}
ns, err := share.NewNamespaceFromBytes(nsString)
ns, err := gosquare.NewNamespaceFromBytes(nsString)
if err != nil {
return 0, share.Namespace{}, err
return 0, gosquare.Namespace{}, err
}
namespace = ns
return height, namespace, share.ValidateForData(namespace)
return height, namespace, gosquare.ValidateForData(namespace)
}
10 changes: 5 additions & 5 deletions api/gateway/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
)

func Test_dataFromShares(t *testing.T) {
Expand All @@ -16,10 +16,10 @@ func Test_dataFromShares(t *testing.T) {
[]byte("BEEEEAHP"),
}

ns := share.RandomNamespace()
sss := share.NewSparseShareSplitter()
ns := gosquare.RandomNamespace()
sss := gosquare.NewSparseShareSplitter()
for _, data := range testData {
b, err := share.NewBlob(ns, data, share.ShareVersionZero, nil)
b, err := gosquare.NewBlob(ns, data, gosquare.ShareVersionZero, nil)
require.NoError(t, err)
require.NoError(t, sss.Write(b))
}
Expand All @@ -32,7 +32,7 @@ func Test_dataFromShares(t *testing.T) {
rawSSSShares[i] = d
}

shrs, err := share.FromBytes(rawSSSShares)
shrs, err := gosquare.FromBytes(rawSSSShares)
require.NoError(t, err)
parsedSSSShares, err := dataFromShares(shrs)
require.NoError(t, err)
Expand Down
32 changes: 15 additions & 17 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
app "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2"
"github.com/celestiaorg/go-square/merkle"
"github.com/celestiaorg/go-square/v2/inclusion"
"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
"github.com/celestiaorg/nmt"
)

Expand Down Expand Up @@ -59,7 +59,7 @@ func (p Proof) equal(input Proof) error {

// Blob represents any application-specific binary data that anyone can submit to Celestia.
type Blob struct {
*share.Blob `json:"blob"`
*gosquare.Blob `json:"blob"`

Commitment Commitment `json:"commitment"`

Expand All @@ -70,33 +70,31 @@ type Blob struct {

// NewBlobV0 constructs a new blob from the provided Namespace and data.
// The blob will be formatted as v0 shares.
func NewBlobV0(namespace share.Namespace, data []byte) (*Blob, error) {
return NewBlob(share.ShareVersionZero, namespace, data, nil)
func NewBlobV0(namespace gosquare.Namespace, data []byte) (*Blob, error) {
return NewBlob(gosquare.ShareVersionZero, namespace, data, nil)
}

// NewBlobV1 constructs a new blob from the provided Namespace and data.
// The blob will be formatted as v0 shares.
func NewBlobV1(namespace share.Namespace, data, signer []byte) (*Blob, error) {
return NewBlob(share.ShareVersionOne, namespace, data, signer)
func NewBlobV1(namespace gosquare.Namespace, data, signer []byte) (*Blob, error) {
return NewBlob(gosquare.ShareVersionOne, namespace, data, signer)
}

// NewBlob constructs a new blob from the provided Namespace, data and share version.
func NewBlob(shareVersion uint8, namespace share.Namespace, data, signer []byte) (*Blob, error) {
func NewBlob(shareVersion uint8, namespace gosquare.Namespace, data, signer []byte) (*Blob, error) {
if len(data) == 0 || len(data) > appconsts.DefaultMaxBytes {
return nil, fmt.Errorf("blob data must be > 0 && <= %d, but it was %d bytes", appconsts.DefaultMaxBytes, len(data))
}

if err := share.ValidateForData(namespace); err != nil {
if err := gosquare.ValidateForData(namespace); err != nil {
return nil, fmt.Errorf("invalid user namespace: %w", err)
}
if !namespace.IsUsableNamespace() {
return nil, fmt.Errorf("parity of tail padding namespaces %s are not allowed", namespace.ID())
}
if namespace.IsReserved() {
return nil, fmt.Errorf("reserved namespace %s is not allowed", namespace.ID())

if !gosquare.IsBlobNamespace(namespace) {
return nil, fmt.Errorf("namespace %s is not a blob namespace", namespace)
}

squareBlob, err := share.NewBlob(namespace, data, shareVersion, signer)
squareBlob, err := gosquare.NewBlob(namespace, data, shareVersion, signer)
if err != nil {
return nil, err
}
Expand All @@ -109,7 +107,7 @@ func NewBlob(shareVersion uint8, namespace share.Namespace, data, signer []byte)
}

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

Expand All @@ -129,7 +127,7 @@ func (b *Blob) Length() (int, error) {
if len(s) == 0 {
return 0, errors.New("blob with zero shares received")
}
return share.SparseSharesNeeded(s[0].SequenceLen()), nil
return gosquare.SparseSharesNeeded(s[0].SequenceLen()), nil
}

// Signer returns blob's author.
Expand Down Expand Up @@ -169,7 +167,7 @@ func (b *Blob) UnmarshalJSON(data []byte) error {
return err
}

ns, err := share.NewNamespaceFromBytes(jsonBlob.Namespace)
ns, err := gosquare.NewNamespaceFromBytes(jsonBlob.Namespace)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions blob/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/celestiaorg/go-square/merkle"
"github.com/celestiaorg/go-square/v2/inclusion"
"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"

"github.com/celestiaorg/celestia-node/blob/blobtest"
)
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestBlob(t *testing.T) {
name: "verify namespace",
expectedRes: func(t *testing.T) {
ns := blob[0].Namespace()
require.NoError(t, share.ValidateUserNamespace(ns.Version(), ns.ID()))
require.NoError(t, gosquare.ValidateUserNamespace(ns.Version(), ns.ID()))
},
},
{
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestBlob(t *testing.T) {
}
}

func convertBlobs(appBlobs ...*share.Blob) ([]*Blob, error) {
func convertBlobs(appBlobs ...*gosquare.Blob) ([]*Blob, error) {
blobs := make([]*Blob, 0, len(appBlobs))
for _, appBlob := range appBlobs {
blob, err := NewBlob(appBlob.ShareVersion(), appBlob.Namespace(), appBlob.Data(), appBlob.Signer())
Expand Down
22 changes: 14 additions & 8 deletions blob/blobtest/testing.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package blobtest

import (
"encoding/binary"
"fmt"

tmrand "github.com/tendermint/tendermint/libs/rand"

"github.com/celestiaorg/celestia-app/v3/test/util/testfactory"
"github.com/celestiaorg/go-square/shares"
"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
)

// GenerateV0Blobs is a test utility producing v0 share formatted blobs with the
// requested size and random namespaces.
func GenerateV0Blobs(sizes []int, sameNamespace bool) ([]*share.Blob, error) {
blobs := make([]*share.Blob, 0, len(sizes))
func GenerateV0Blobs(sizes []int, sameNamespace bool) ([]*gosquare.Blob, error) {
blobs := make([]*gosquare.Blob, 0, len(sizes))
for _, size := range sizes {
size := RawBlobSize(share.FirstSparseShareContentSize * size)
size := RawBlobSize(gosquare.FirstSparseShareContentSize * size)
appBlob := testfactory.GenerateRandomBlob(size)
if !sameNamespace {
namespace, err := share.NewV0Namespace(tmrand.Bytes(7))
namespace, err := gosquare.NewV0Namespace(tmrand.Bytes(7))
if err != nil {
return nil, err
}
if namespace.IsReserved() {
return nil, fmt.Errorf("reserved namespace")
}
appBlob, err = share.NewV0Blob(namespace, appBlob.Data())
appBlob, err = gosquare.NewV0Blob(namespace, appBlob.Data())
if err != nil {
return nil, err
}
Expand All @@ -37,5 +37,11 @@ func GenerateV0Blobs(sizes []int, sameNamespace bool) ([]*share.Blob, error) {
}

func RawBlobSize(totalSize int) int {
return totalSize - shares.DelimLen(uint64(totalSize))
return totalSize - delimLen(uint64(totalSize))
}

// delimLen calculates the length of the delimiter for a given unit size
func delimLen(size uint64) int {
lenBuf := make([]byte, binary.MaxVarintLen64)
return binary.PutUvarint(lenBuf, size)
}
6 changes: 3 additions & 3 deletions blob/commitment_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v3/pkg/proof"
"github.com/celestiaorg/go-square/v2/inclusion"
"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"
)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (commitmentProof *CommitmentProof) Validate() error {
}

// Verify verifies that a commitment proof is valid, i.e., the subtree roots commit
// to some data that was posted to a square.
// to some data that was posted to a share.
// Expects the commitment proof to be properly formulated and validated
// using the Validate() function.
func (commitmentProof *CommitmentProof) Verify(root []byte, subtreeRootThreshold int) (bool, error) {
Expand All @@ -94,7 +94,7 @@ func (commitmentProof *CommitmentProof) Verify(root []byte, subtreeRootThreshold
return false, err
}

nmtHasher := nmt.NewNmtHasher(appconsts.NewBaseHashFunc(), share.NamespaceSize, true)
nmtHasher := nmt.NewNmtHasher(appconsts.NewBaseHashFunc(), gosquare.NamespaceSize, true)

// computes the total number of shares proven.
numberOfShares := 0
Expand Down
10 changes: 5 additions & 5 deletions blob/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"fmt"
"sort"

"github.com/celestiaorg/go-square/v2/share"
gosquare "github.com/celestiaorg/go-square/v2/share"
)

// BlobsToShares accepts blobs and convert them to the Shares.
func BlobsToShares(nodeBlobs ...*Blob) ([]share.Share, error) {
func BlobsToShares(nodeBlobs ...*Blob) ([]gosquare.Share, error) {
sort.Slice(nodeBlobs, func(i, j int) bool {
return nodeBlobs[i].Blob.Namespace().Compare(nodeBlobs[j].Blob.Namespace()) < 0
})

splitter := share.NewSparseShareSplitter()
splitter := gosquare.NewSparseShareSplitter()
for i, nodeBlob := range nodeBlobs {
err := splitter.Write(nodeBlob.Blob)
if err != nil {
Expand All @@ -25,8 +25,8 @@ func BlobsToShares(nodeBlobs ...*Blob) ([]share.Share, error) {
}

// ToAppBlobs converts node's blob type to the blob type from go-square.
func ToAppBlobs(blobs ...*Blob) []*share.Blob {
appBlobs := make([]*share.Blob, len(blobs))
func ToAppBlobs(blobs ...*Blob) []*gosquare.Blob {
appBlobs := make([]*gosquare.Blob, len(blobs))
for i := range blobs {
appBlobs[i] = blobs[i].Blob
}
Expand Down
Loading

0 comments on commit 3b1ddaa

Please sign in to comment.