forked from rollkit/celestia-da
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celestia.go
217 lines (196 loc) · 6.58 KB
/
celestia.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package celestia
import (
"context"
"encoding/binary"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"log"
"strings"
"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/rollkit/go-da"
)
const (
// DefaultMaxBytes is the maximum blob size accepted by celestia core
// ADR-13 claims worst case padding approaches 2 rows for a full data square:
// see: https://github.com/celestiaorg/celestia-app/blob/main/docs/architecture/adr-013-non-interactive-default-rules-for-zero-padding.md
// square size (64) * two rows = 128 shares
// 128 shares * 512 bytes per share = 65,536 bytes to account for padding
// 1,973,786 - 65,536 = 1,908,250 bytes
DefaultMaxBytes = 1908250
)
// CelestiaDA implements the celestia backend for the DA interface
type CelestiaDA struct {
client *rpc.Client
namespace share.Namespace
gasPrice float64
ctx context.Context
}
// NewCelestiaDA returns an instance of CelestiaDA
func NewCelestiaDA(client *rpc.Client, namespace share.Namespace, gasPrice float64, ctx context.Context) *CelestiaDA {
return &CelestiaDA{
client: client,
namespace: namespace,
gasPrice: gasPrice,
ctx: ctx,
}
}
func (c *CelestiaDA) defaultNamespace(ns da.Namespace) da.Namespace {
if ns == nil {
return c.namespace
}
return ns
}
// MaxBlobSize returns the max blob size
func (c *CelestiaDA) MaxBlobSize(ctx context.Context) (uint64, error) {
// TODO: pass-through query to node, app
return DefaultMaxBytes, nil
}
// Get returns Blob for each given ID, or an error.
func (c *CelestiaDA) Get(ctx context.Context, ids []da.ID, ns da.Namespace) ([]da.Blob, error) {
c.namespace = c.defaultNamespace(ns)
var blobs []da.Blob
for _, id := range ids {
height, commitment := splitID(id)
blob, err := c.client.Blob.Get(ctx, height, c.namespace, commitment)
if err != nil {
return nil, err
}
blobs = append(blobs, blob.Data)
}
return blobs, nil
}
// getPeerInfo return the peer info, i.e : node id
func (c *CelestiaDA) GetPeerInfo(ctx context.Context) (string, error) {
peerInfo, err := c.client.P2P.Info(ctx)
if err != nil {
return "", err
}
return peerInfo.ID.String(), nil
}
// GetIDs returns IDs of all Blobs located in DA at given height.
func (c *CelestiaDA) GetIDs(ctx context.Context, height uint64, ns da.Namespace) ([]da.ID, error) {
c.namespace = c.defaultNamespace(ns)
var ids []da.ID
blobs, err := c.client.Blob.GetAll(ctx, height, []share.Namespace{c.namespace})
if err != nil {
if strings.Contains(err.Error(), blob.ErrBlobNotFound.Error()) {
return nil, nil
}
return nil, err
}
for _, b := range blobs {
ids = append(ids, makeID(height, b.Commitment))
}
return ids, nil
}
// Commit creates a Commitment for each given Blob.
func (c *CelestiaDA) Commit(ctx context.Context, daBlobs []da.Blob, ns da.Namespace) ([]da.Commitment, error) {
c.namespace = c.defaultNamespace(ns)
_, commitments, err := c.blobsAndCommitments(daBlobs, c.namespace)
return commitments, err
}
// Submit submits the Blobs to Data Availability layer.
func (c *CelestiaDA) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64, ns da.Namespace) ([]da.ID, error) {
c.namespace = c.defaultNamespace(ns)
blobs, _, err := c.blobsAndCommitments(daBlobs, c.namespace)
if err != nil {
return nil, err
}
height, err := c.client.Blob.Submit(ctx, blobs, blob.GasPrice(gasPrice))
if err != nil {
return nil, err
}
log.Println("successfully submitted blobs", "height", height, "gasPrice", gasPrice)
ids := make([]da.ID, len(blobs))
for i, blob := range blobs {
ids[i] = makeID(height, blob.Commitment)
}
return ids, nil
}
// GetProofs returns the inclusion proofs for the given IDs.
func (c *CelestiaDA) GetProofs(ctx context.Context, daIDs []da.ID, ns da.Namespace) ([]da.Proof, error) {
c.namespace = c.defaultNamespace(ns)
proofs := make([]da.Proof, len(daIDs))
for i, id := range daIDs {
height, commitment := splitID(id)
proof, err := c.client.Blob.GetProof(ctx, height, c.namespace, commitment)
if err != nil {
return nil, err
}
proofs[i], err = proof.MarshalJSON()
if err != nil {
return nil, err
}
}
return proofs, nil
}
// blobsAndCommitments converts []da.Blob to []*blob.Blob and generates corresponding []da.Commitment
func (c *CelestiaDA) blobsAndCommitments(daBlobs []da.Blob, ns da.Namespace) ([]*blob.Blob, []da.Commitment, error) {
var blobs []*blob.Blob
var commitments []da.Commitment
for _, daBlob := range daBlobs {
b, err := blob.NewBlobV0(ns, daBlob)
if err != nil {
return nil, nil, err
}
blobs = append(blobs, b)
commitment, err := types.CreateCommitment(&b.Blob)
if err != nil {
return nil, nil, err
}
commitments = append(commitments, commitment)
}
return blobs, commitments, nil
}
// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
func (c *CelestiaDA) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof, ns da.Namespace) ([]bool, error) {
c.namespace = c.defaultNamespace(ns)
var included []bool
var proofs []*blob.Proof
for _, daProof := range daProofs {
var blobProof *blob.Proof
err := blobProof.UnmarshalJSON(daProof)
if err != nil {
fmt.Println(string(daProof))
return nil, err
}
for _, v := range *blobProof {
bz, _ := v.MarshalJSON()
fmt.Println(string(bz))
}
if err != nil {
return nil, err
}
proofs = append(proofs, blobProof)
}
for i, id := range ids {
height, commitment := splitID(id)
fmt.Println("height", height, "commit", hexutil.Encode(commitment))
// TODO(tzdybal): for some reason, if proof doesn't match commitment, API returns (false, "blob: invalid proof")
// but analysis of the code in celestia-node implies this should never happen - maybe it's caused by openrpc?
// there is no way of gently handling errors here, but returned value is fine for us
isIncluded, _ := c.client.Blob.Included(ctx, height, c.namespace, proofs[i], commitment)
included = append(included, isIncluded)
}
return included, nil
}
// heightLen is a length (in bytes) of serialized height.
//
// This is 8 as uint64 consist of 8 bytes.
const heightLen = 8
func makeID(height uint64, commitment da.Commitment) da.ID {
id := make([]byte, heightLen+len(commitment))
binary.LittleEndian.PutUint64(id, height)
copy(id[heightLen:], commitment)
return id
}
func splitID(id da.ID) (uint64, da.Commitment) {
if len(id) <= heightLen {
return 0, nil
}
return binary.LittleEndian.Uint64(id[:heightLen]), id[heightLen:]
}
var _ da.DA = &CelestiaDA{}