Skip to content

Commit

Permalink
Refactor: removed EDS and ODS usage, finding blob share indexes strai…
Browse files Browse the repository at this point in the history
…ghtly from data square
  • Loading branch information
k-karuna committed Dec 4, 2024
1 parent c2c1f58 commit aea26bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 102 deletions.
33 changes: 3 additions & 30 deletions cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import (
"encoding/base64"
"encoding/hex"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v3/pkg/da"
"github.com/celestiaorg/go-square/shares"
"net/http"
"time"

"github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/celestiaorg/go-square/v2"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
testsuite "github.com/celenium-io/celestia-indexer/internal/test_suite"
"github.com/celenium-io/celestia-indexer/pkg/node"
"github.com/celestiaorg/go-square/square"
"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -774,37 +772,12 @@ func (handler *NamespaceHandler) BlobProofs(c echo.Context) error {
return internalServerError(c, err)
}

eds, err := da.ExtendShares(shares.ToBytes(dataSquare))
startBlobIndex, endBlobIndex, err := responses.GetBlobShareIndexes(dataSquare, req.Hash, req.Commitment)
if err != nil {
return internalServerError(c, err)
}

ods, err := responses.NewODS(eds)
if err != nil {
return internalServerError(c, err)
}

namespaceOds, err := ods.FindODSByNamespace(req.Hash)
if err != nil {
return internalServerError(c, err)
}

namespaceShares, err := responses.GetNamespaceShares(eds, namespaceOds.From, namespaceOds.To)
if err != nil {
return internalServerError(c, err)
}

startBlobIdx, endBlobIdx, err := responses.GetBlobShareIdxs(
namespaceShares,
namespaceOds.From,
eds.Width()/2,
req.Commitment,
)
if err != nil {
return internalServerError(c, err)
}

proofs, err := handler.node.BlobProofs(c.Request().Context(), req.Height, startBlobIdx, endBlobIdx)
proofs, err := handler.node.BlobProofs(c.Request().Context(), req.Height, startBlobIndex, endBlobIndex)
if err != nil {
return handleError(c, err, handler.namespace)
}
Expand Down
107 changes: 35 additions & 72 deletions cmd/api/handler/responses/ods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package responses

import (
"bytes"
"encoding/base64"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
"github.com/celestiaorg/go-square/namespace"
Expand All @@ -30,25 +31,6 @@ type ODSItem struct {
Type NamespaceKind `json:"type"`
}

type sequence struct {
ns share.Namespace
shareVersion uint8
startShareIdx int
endShareIdx int
data []byte
sequenceLen uint32
signer []byte
}

func (ods *ODS) FindODSByNamespace(namespace string) (*ODSItem, error) {
for _, item := range ods.Items {
if item.Namespace == namespace {
return &item, nil
}
}
return nil, errors.New("item with specified namespace not found")
}

func NewODS(eds *rsmt2d.ExtendedDataSquare) (ODS, error) {
ods := ODS{
Width: eds.Width() / 2,
Expand Down Expand Up @@ -86,40 +68,6 @@ func NewODS(eds *rsmt2d.ExtendedDataSquare) (ODS, error) {
return ods, nil
}

func GetNamespaceShares(eds *rsmt2d.ExtendedDataSquare, from, to []uint) ([]share.Share, error) {
if len(from) != len(to) {
return nil, errors.New("length of 'from' and 'to' must match")
}

var resultShares []share.Share
startRow, startCol := from[0], from[1]
endRow, endCol := to[0], to[1]

if startRow > endRow || (startRow == endRow && startCol > endCol) {
return nil, errors.New("invalid from and to params")
}
currentRow, currentCol := startRow, startCol

for {
cell := eds.GetCell(currentRow, currentCol)
cellShare, err := share.NewShare(cell)
if err != nil {
return nil, err
}
resultShares = append(resultShares, *cellShare)
if currentRow == endRow && currentCol == endCol {
break
}
currentCol++
if currentCol == eds.Width()/2 {
currentCol = 0
currentRow++
}
}

return resultShares, nil
}

type NamespaceKind string

const (
Expand Down Expand Up @@ -148,21 +96,32 @@ func getNamespaceType(ns namespace.Namespace) NamespaceKind {
}
}

func GetBlobShareIdxs(
type sequence struct {
ns share.Namespace
shareVersion uint8
startShareIdx int
endShareIdx int
data []byte
sequenceLen uint32
signer []byte
}

func GetBlobShareIndexes(
shares []share.Share,
nsStartFromIdx []uint,
edsWidth uint,
b64commitment string,
) (blobStartIdx, blobEndIdx int, err error) {
base64namespace string,
base64commitment string,
) (blobStartIndex, blobEndIndex int, err error) {
if len(shares) == 0 {
return 0, 0, errors.New("invalid shares length")

}
sequences := make([]sequence, 0)
startRow, startCol := nsStartFromIdx[0], nsStartFromIdx[1]
nsStartIdx := int(startRow*edsWidth + startCol)
namespaceBytes, err := base64.StdEncoding.DecodeString(base64namespace)
if err != nil {
return 0, 0, errors.Wrap(err, "decoding base64 namespace")
}

for shareIdx, s := range shares {
for shareIndex, s := range shares {
if !(s.Version() <= 1) {
return 0, 0, errors.New("unsupported share version")
}
Expand All @@ -171,37 +130,41 @@ func GetBlobShareIdxs(
continue
}

if !bytes.Equal(s.Namespace().Bytes(), namespaceBytes) {
continue
}

if s.IsSequenceStart() {
sequences = append(sequences, sequence{
ns: s.Namespace(),
shareVersion: s.Version(),
startShareIdx: shareIdx,
endShareIdx: shareIdx,
startShareIdx: shareIndex,
endShareIdx: shareIndex,
data: s.RawData(),
sequenceLen: s.SequenceLen(),
signer: share.GetSigner(s),
})
} else {
if len(sequences) == 0 {
return 0, 0, errors.New("continuation share without a sequence start share")
return 0, 0, errors.New("continuation share without a s start share")
}
prev := &sequences[len(sequences)-1]
prev.data = append(prev.data, s.RawData()...)
prev.endShareIdx = shareIdx
prev.endShareIdx = shareIndex
}
}
for _, seq := range sequences {
seq.data = seq.data[:seq.sequenceLen]
blob, err := share.NewBlob(seq.ns, seq.data, seq.shareVersion, seq.signer)
for _, s := range sequences {
s.data = s.data[:s.sequenceLen]
blob, err := share.NewBlob(s.ns, s.data, s.shareVersion, s.signer)
if err != nil {
return 0, 0, err
return 0, 0, errors.Wrap(err, "creating blob")
}
commitment, err := incl.CreateCommitment(blob, merkle.HashFromByteSlices, appconsts.SubtreeRootThreshold(0))
if err != nil {
return 0, 0, err
return 0, 0, errors.Wrap(err, "creating commitment")
}
if base64.StdEncoding.EncodeToString(commitment) == b64commitment {
return nsStartIdx + seq.startShareIdx, nsStartIdx + seq.endShareIdx + 1, err
if base64.StdEncoding.EncodeToString(commitment) == base64commitment {
return s.startShareIdx, s.endShareIdx + 1, err
}
}
return 0, 0, err
Expand Down

0 comments on commit aea26bb

Please sign in to comment.