Skip to content

Commit

Permalink
migrating to go-square v2
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Oct 3, 2024
1 parent 1c62d99 commit 9549956
Show file tree
Hide file tree
Showing 262 changed files with 1,869 additions and 2,112 deletions.
9 changes: 5 additions & 4 deletions api/docgen/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (

"github.com/celestiaorg/go-fraud"
libhead "github.com/celestiaorg/go-header"
"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/share"
"github.com/celestiaorg/celestia-node/share/eds/byzantine"
"github.com/celestiaorg/celestia-node/square"
"github.com/celestiaorg/celestia-node/square/eds/byzantine"
"github.com/celestiaorg/celestia-node/state"
)

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

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

// randomly generated namespace that's used in the blob example above
// (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAMJ/xGlNMdE=)
namespace, err := share.NewBlobNamespaceV0([]byte{0xc2, 0x7f, 0xc4, 0x69, 0x4d, 0x31, 0xd1})
namespace, err := share.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/share"
"github.com/celestiaorg/celestia-node/square"
)

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, share.ErrNotAvailable):
case errors.Is(err, square.ErrNotAvailable):
resp, err := json.Marshal(&AvailabilityResponse{Available: false})
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
Expand Down
23 changes: 11 additions & 12 deletions api/gateway/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (

"github.com/gorilla/mux"

"github.com/celestiaorg/go-square/shares"

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

const (
Expand Down Expand Up @@ -105,11 +103,7 @@ func (h *Handler) getShares(ctx context.Context, height uint64, namespace share.
}

func dataFromShares(input []share.Share) (data [][]byte, err error) {
appShares, err := shares.FromBytes(input)
if err != nil {
return nil, err
}
sequences, err := shares.ParseShares(appShares, false)
sequences, err := share.ParseShares(input, false)
if err != nil {
return nil, err
}
Expand All @@ -129,13 +123,18 @@ func parseGetByNamespaceArgs(r *http.Request) (height uint64, namespace share.Na
if strHeight, ok := vars[heightKey]; ok {
height, err = strconv.ParseUint(strHeight, 10, 64)
if err != nil {
return 0, nil, err
return 0, share.Namespace{}, err
}
}
hexNamespace := vars[namespaceKey]
namespace, err = hex.DecodeString(hexNamespace)
nsString, err := hex.DecodeString(hexNamespace)
if err != nil {
return 0, share.Namespace{}, err
}
ns, err := share.NewNamespaceFromBytes(nsString)
if err != nil {
return 0, nil, err
return 0, share.Namespace{}, err
}
return height, namespace, namespace.ValidateForData()
namespace = ns
return height, namespace, share.ValidateForData(namespace)
}
23 changes: 8 additions & 15 deletions api/gateway/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-app/v2/pkg/appconsts"
"github.com/celestiaorg/go-square/blob"
"github.com/celestiaorg/go-square/shares"

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

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

ns := sharetest.RandV0Namespace()
sss := shares.NewSparseShareSplitter()
ns := share.RandomNamespace()
sss := share.NewSparseShareSplitter()
for _, data := range testData {
b := blob.Blob{
Data: data,
NamespaceId: ns.ID(),
NamespaceVersion: uint32(ns.Version()),
ShareVersion: uint32(appconsts.ShareVersionZero),
}
err := sss.Write(&b)
b, err := share.NewBlob(ns, data, share.ShareVersionZero, nil)
require.NoError(t, err)
require.NoError(t, sss.Write(b))
}

sssShares := sss.Export()
Expand All @@ -41,7 +32,9 @@ func Test_dataFromShares(t *testing.T) {
rawSSSShares[i] = d
}

parsedSSSShares, err := dataFromShares(rawSSSShares)
shrs, err := share.FromBytes(rawSSSShares)
require.NoError(t, err)
parsedSSSShares, err := dataFromShares(shrs)
require.NoError(t, err)

require.Equal(t, testData, parsedSSSShares)
Expand Down
111 changes: 55 additions & 56 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ import (
"errors"
"fmt"

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

"github.com/celestiaorg/celestia-app/v2/pkg/appconsts"
v2 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v2"
"github.com/celestiaorg/go-square/blob"
"github.com/celestiaorg/go-square/inclusion"
"github.com/celestiaorg/go-square/shares"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"

Check failure on line 9 in blob/blob.go

View workflow job for this annotation

GitHub Actions / go-ci / Unit Tests Coverage (ubuntu-latest)

github.com/celestiaorg/celestia-app/v3@v3.0.0: replacement directory ../celestia-app does not exist

Check failure on line 9 in blob/blob.go

View workflow job for this annotation

GitHub Actions / go-ci / Unit Tests Coverage (macos-14)

github.com/celestiaorg/celestia-app/v3@v3.0.0: replacement directory ../celestia-app does not exist
app "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2"

Check failure on line 10 in blob/blob.go

View workflow job for this annotation

GitHub Actions / go-ci / Unit Tests Coverage (ubuntu-latest)

github.com/celestiaorg/celestia-app/v3@v3.0.0: replacement directory ../celestia-app does not exist

Check failure on line 10 in blob/blob.go

View workflow job for this annotation

GitHub Actions / go-ci / Unit Tests Coverage (macos-14)

github.com/celestiaorg/celestia-app/v3@v3.0.0: replacement directory ../celestia-app does not exist
"github.com/celestiaorg/go-square/merkle"
"github.com/celestiaorg/go-square/v2/inclusion"
"github.com/celestiaorg/go-square/v2/share"
"github.com/celestiaorg/nmt"

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

// appVersion is the current application version of celestia-app.
const appVersion = v2.Version
const appVersion = app.Version

var errEmptyShares = errors.New("empty shares")

var subtreeRootThreshold = appconsts.SubtreeRootThreshold(appVersion)

// The Proof is a set of nmt proofs that can be verified only through
// the included method (due to limitation of the nmt https://github.com/celestiaorg/nmt/issues/218).
// Proof proves the WHOLE namespaced data to the row roots.
Expand Down Expand Up @@ -61,14 +59,10 @@ func (p Proof) equal(input Proof) error {

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

Commitment Commitment `json:"commitment"`

// the celestia-node's namespace type
// this is to avoid converting to and from app's type
namespace share.Namespace

// index represents the index of the blob's first share in the EDS.
// Only retrieved, on-chain blobs will have the index set. Default is -1.
index int
Expand All @@ -77,35 +71,43 @@ 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(appconsts.ShareVersionZero, namespace, data)
return NewBlob(share.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)
}

// NewBlob constructs a new blob from the provided Namespace, data and share version.
func NewBlob(shareVersion uint8, namespace share.Namespace, data []byte) (*Blob, error) {
func NewBlob(shareVersion uint8, namespace share.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 := namespace.ValidateForBlob(); err != nil {
return nil, err

if err := share.ValidateUserNamespace(namespace.Version(), namespace.ID()); err != nil {
return nil, fmt.Errorf("invalid user namespace: %w", err)
}
if !namespace.IsUsableNamespace() {
return nil, fmt.Errorf("namespace %s is not usable", namespace.ID()) // rename
}

blob := blob.Blob{
NamespaceId: namespace.ID(),
Data: data,
ShareVersion: uint32(shareVersion),
NamespaceVersion: uint32(namespace.Version()),
squareBlob, err := share.NewBlob(namespace, data, shareVersion, signer)
if err != nil {
return nil, err
}

com, err := inclusion.CreateCommitment(&blob, merkle.HashFromByteSlices, appconsts.SubtreeRootThreshold(appVersion))
com, err := inclusion.CreateCommitment(squareBlob, merkle.HashFromByteSlices, subtreeRootThreshold)
if err != nil {
return nil, err
}
return &Blob{Blob: &blob, Commitment: com, namespace: namespace, index: -1}, nil
return &Blob{Blob: squareBlob, Commitment: com, index: -1}, nil
}

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

// Index returns the blob's first share index in the EDS.
Expand All @@ -124,38 +126,34 @@ 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
}

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
// Signer returns blob's author.
func (b *Blob) Signer() []byte {
return b.Blob.Signer()
}

func (b *Blob) compareCommitments(com Commitment) bool {
return bytes.Equal(b.Commitment, com)
}

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"`
Namespace []byte `json:"namespace"`
Data []byte `json:"data"`
ShareVersion uint32 `json:"share_version"`
Commitment Commitment `json:"commitment"`
Signer []byte `json:"signer,omitempty"`
Index int `json:"index"`
}

func (b *Blob) MarshalJSON() ([]byte, error) {
blob := &jsonBlob{
Namespace: b.Namespace(),
Data: b.Data,
ShareVersion: b.ShareVersion,
Namespace: b.Namespace().Bytes(),
Data: b.Data(),

Check failure on line 153 in blob/blob.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

b.Data undefined (type *Blob has no field or method Data) (typecheck)
ShareVersion: uint32(b.ShareVersion()),

Check failure on line 154 in blob/blob.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

b.ShareVersion undefined (type *Blob has no field or method ShareVersion) (typecheck)
Commitment: b.Commitment,
Signer: b.Signer(),
Index: b.index,
}
return json.Marshal(blob)
Expand All @@ -168,17 +166,18 @@ func (b *Blob) UnmarshalJSON(data []byte) error {
return err
}

if len(jsonBlob.Namespace) == 0 {
return errors.New("expected a non-empty namespace")
ns, err := share.NewNamespaceFromBytes(jsonBlob.Namespace)
if err != nil {
return err
}

blob, err := NewBlob(uint8(jsonBlob.ShareVersion), ns, jsonBlob.Data, jsonBlob.Signer)
if err != nil {
return err
}

b.Blob = &blob.Blob{}
b.Blob.NamespaceVersion = uint32(jsonBlob.Namespace.Version())
b.Blob.NamespaceId = jsonBlob.Namespace.ID()
b.Blob.Data = jsonBlob.Data
b.Blob.ShareVersion = jsonBlob.ShareVersion
b.Commitment = jsonBlob.Commitment
b.namespace = jsonBlob.Namespace
b.index = jsonBlob.Index
blob.Commitment = jsonBlob.Commitment
blob.index = jsonBlob.Index
*b = *blob
return nil
}
Loading

0 comments on commit 9549956

Please sign in to comment.