-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcelestia.go
201 lines (184 loc) Β· 6.13 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
package celestiada
import (
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"log"
"math"
"strings"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"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"
sdktypes "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/rollkit/go-da"
)
// CelestiaDA implements the celestia backend for the DA interface
type DAClient struct {
client *rpc.Client
Namespace share.Namespace
gasPrice float64
ctx context.Context
}
// Returns an intialised Celestia DA client
func New(ctx context.Context, lightCLientRPCUrl string, authToken string, hexNamespace string, gasPrice float64) (*DAClient, error) {
nsBytes := make([]byte, 10)
_, err := hex.Decode(nsBytes, []byte(hexNamespace))
if err != nil {
log.Fatalln("invalid hex value of a namespace:", err)
return nil, err
}
namespace, err := share.NewBlobNamespaceV0(nsBytes)
if err != nil {
return nil, err
}
client, err := rpc.NewClient(ctx, lightCLientRPCUrl, authToken)
if err != nil {
fmt.Printf("failed to create rpc client: %v", err)
return nil, err
}
return &DAClient{
client: client,
Namespace: namespace,
gasPrice: gasPrice,
ctx: ctx,
}, nil
}
// MaxBlobSize returns the max blob size
func (c *DAClient) MaxBlobSize(ctx context.Context) (uint64, error) {
// TODO: pass-through query to node, app
return appconsts.DefaultMaxBytes, nil
}
// Get returns Blob for each given ID, or an error.
func (c *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
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
}
// GetIDs returns IDs of all Blobs located in DA at given height.
func (c *DAClient) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
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 *DAClient) Commit(ctx context.Context, daBlobs []da.Blob) ([]da.Commitment, error) {
_, commitments, err := c.blobsAndCommitments(daBlobs)
return commitments, err
}
// Submit submits the Blobs to Data Availability layer.
func (c *DAClient) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
blobs, commitments, err := c.blobsAndCommitments(daBlobs)
if err != nil {
return nil, nil, err
}
options := blob.DefaultSubmitOptions()
// if gas price was configured globally use that as the default
if c.gasPrice >= 0 && gasPrice < 0 {
gasPrice = c.gasPrice
}
if gasPrice >= 0 {
blobSizes := make([]uint32, len(blobs))
for i, blob := range blobs {
blobSizes[i] = uint32(len(blob.Data))
}
options.GasLimit = types.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
options.Fee = sdktypes.NewInt(int64(math.Ceil(gasPrice * float64(options.GasLimit)))).Int64()
}
height, err := c.client.Blob.Submit(ctx, blobs, options)
if err != nil {
return nil, nil, err
}
log.Println("successfully submitted blobs", "height", height, "gas", options.GasLimit, "fee", options.Fee)
ids := make([]da.ID, len(daBlobs))
proofs := make([]da.Proof, len(daBlobs))
for i, commitment := range commitments {
ids[i] = makeID(height, commitment)
proof, err := c.client.Blob.GetProof(ctx, height, c.Namespace, commitment)
if err != nil {
return nil, nil, err
}
// TODO(tzdybal): does always len(*proof) == 1?
proofs[i], err = (*proof)[0].MarshalJSON()
if err != nil {
return nil, nil, err
}
}
return ids, proofs, nil
}
// blobsAndCommitments converts []da.Blob to []*blob.Blob and generates corresponding []da.Commitment
func (c *DAClient) blobsAndCommitments(daBlobs []da.Blob) ([]*blob.Blob, []da.Commitment, error) {
var blobs []*blob.Blob
var commitments []da.Commitment
for _, daBlob := range daBlobs {
b, err := blob.NewBlobV0(c.Namespace, 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 *DAClient) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof) ([]bool, error) {
var included []bool
var proofs []*blob.Proof
for _, daProof := range daProofs {
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 {
height, commitment := SplitID(id)
// 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:]
}