Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

celestia: import celestia-node directly #6

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ package celestia

import (
"context"
"time"
"errors"
"fmt"

openrpc "github.com/rollkit/celestia-openrpc"
"github.com/rollkit/celestia-openrpc/types/blob"
openrpcns "github.com/rollkit/celestia-openrpc/types/namespace"
"github.com/rollkit/celestia-openrpc/types/share"
"github.com/celestiaorg/celestia-app/x/blob/types"
rpc "github.com/celestiaorg/celestia-node/api/rpc/client"
"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/nmt"
"github.com/rollkit/go-da"
"github.com/rollkit/rollkit/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// Config contains the node RPC configuration
type Config struct {
AuthToken string `json:"auth_token"`
BaseURL string `json:"base_url"`
Timeout time.Duration `json:"timeout"`
Fee int64 `json:"fee"`
GasLimit uint64 `json:"gas_limit"`
}

// / CelestiaDA implements the celestia backend for the DA interface
// CelestiaDA implements the celestia backend for the DA interface
type CelestiaDA struct {
rpc *openrpc.Client
client *rpc.Client
height uint64
namespace openrpcns.Namespace
config Config
logger log.Logger
namespace share.Namespace
ctx context.Context
}

// NewCelestiaDA returns an instance of CelestiaDA
func NewCelestiaDA(client *rpc.Client, height uint64, namespace share.Namespace, ctx context.Context) *CelestiaDA {
return &CelestiaDA{
client: client,
height: height,
namespace: namespace,
ctx: ctx,
}
}

func (c *CelestiaDA) Get(ids []da.ID) ([]da.Blob, error) {
var blobs []da.Blob
for _, id := range ids {
// TODO: extract commitment from ID
blob, err := c.rpc.Blob.Get(c.ctx, c.height, share.Namespace(c.namespace.Bytes()), blob.Commitment(id))
blob, err := c.client.Blob.Get(c.ctx, c.height, c.namespace, blob.Commitment(id))
if err != nil {
return nil, err
}
Expand All @@ -46,7 +47,10 @@ func (c *CelestiaDA) Get(ids []da.ID) ([]da.Blob, error) {

func (c *CelestiaDA) GetIDs(height uint64) ([]da.ID, error) {
var ids []da.ID
blobs, err := c.rpc.Blob.GetAll(c.ctx, c.height, []share.Namespace{c.namespace.Bytes()})
blobs, err := c.client.Blob.GetAll(c.ctx, c.height, []share.Namespace{c.namespace})
if errors.Is(err, blob.ErrBlobNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
Expand All @@ -58,15 +62,15 @@ func (c *CelestiaDA) GetIDs(height uint64) ([]da.ID, error) {
}

func (c *CelestiaDA) Commit(daBlobs []da.Blob) ([]da.Commitment, error) {
var blobs []*blob.Blob
var blobs []*tmproto.Blob
for _, daBlob := range daBlobs {
b, err := blob.NewBlobV0(c.namespace.Bytes(), daBlob)
b, err := blob.NewBlobV0(c.namespace, daBlob)
if err != nil {
return nil, err
}
blobs = append(blobs, b)
blobs = append(blobs, &b.Blob)
}
commitments, err := blob.CreateCommitments(blobs)
commitments, err := types.CreateCommitments(blobs)
if err != nil {
return nil, err
}
Expand All @@ -80,29 +84,34 @@ func (c *CelestiaDA) Commit(daBlobs []da.Blob) ([]da.Commitment, error) {
func (c *CelestiaDA) Submit(daBlobs []da.Blob) ([]da.ID, []da.Proof, error) {
var blobs []*blob.Blob
for _, daBlob := range daBlobs {
b, err := blob.NewBlobV0(c.namespace.Bytes(), daBlob)
b, err := blob.NewBlobV0(c.namespace, daBlob)
if err != nil {
return nil, nil, err
}
blobs = append(blobs, b)
}
c.rpc.Blob.Submit(c.ctx, blobs, openrpc.DefaultSubmitOptions())
height, err := c.client.Blob.Submit(c.ctx, blobs, blob.DefaultSubmitOptions())
if err != nil {
return nil, nil, err
}
fmt.Println("succesfully submitted blobs", "height", height)
return nil, nil, nil
}

func (c *CelestiaDA) Validate(ids []da.ID, daProofs []da.Proof) ([]bool, error) {
var included []bool
var proofs []*blob.Proof
for _, daProof := range daProofs {
proof := &blob.Proof{}
if err := proof.UnmarshalJSON(daProof); err != nil {
nmtProof := &nmt.Proof{}
if err := nmtProof.UnmarshalJSON(daProof); err != nil {
return nil, err
}
proof := &blob.Proof{nmtProof}
proofs = append(proofs, proof)
}
for i, id := range ids {
// TODO: extract commitment from ID
isIncluded, err := c.rpc.Blob.Included(c.ctx, c.height, share.Namespace(c.namespace.Bytes()), proofs[i], blob.Commitment(id))
isIncluded, err := c.client.Blob.Included(c.ctx, c.height, share.Namespace(c.namespace), proofs[i], blob.Commitment(id))
if err != nil {
return nil, err
}
Expand Down
Loading