1
- package availda
1
+ package avail
2
2
3
3
import (
4
4
"context"
@@ -13,12 +13,10 @@ import (
13
13
14
14
"log"
15
15
16
- "encoding/binary"
17
-
18
16
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
19
17
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
20
18
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
21
- "github.com/rollkit /go-da"
19
+ "github.com/stackrlabs /go-daash/ da"
22
20
"go.uber.org/zap"
23
21
"golang.org/x/crypto/sha3"
24
22
)
@@ -45,7 +43,7 @@ type DataProof struct {
45
43
} `json:"roots"`
46
44
}
47
45
48
- type DAClient struct {
46
+ type Client struct {
49
47
Config Config
50
48
API * gsrpc.SubstrateAPI
51
49
Meta * types.Metadata
@@ -58,8 +56,8 @@ type DAClient struct {
58
56
}
59
57
60
58
// Returns a newly initalised Avail DA client
61
- func New (configPath string ) (* DAClient , error ) {
62
- a := DAClient {}
59
+ func NewClient (configPath string ) (* Client , error ) {
60
+ a := Client {}
63
61
err := a .Config .GetConfig (configPath )
64
62
if err != nil {
65
63
return nil , fmt .Errorf ("cannot get config" , err )
@@ -112,14 +110,14 @@ func New(configPath string) (*DAClient, error) {
112
110
}
113
111
114
112
// MaxBlobSize returns the max blob size
115
- func (c * DAClient ) MaxBlobSize (ctx context.Context ) (uint64 , error ) {
113
+ func (c * Client ) MaxBlobSize (ctx context.Context ) (uint64 , error ) {
116
114
var maxBlobSize uint64 = 64 * 64 * 500
117
115
return maxBlobSize , nil
118
116
}
119
117
120
118
// Submit a list of blobs to Avail DA
121
119
// Currently, we submit to a trusted RPC Avail node. In the future, we will submit via an Avail light client.
122
- func (a * DAClient ) Submit (ctx context.Context , daBlobs []da.Blob , gasPrice float64 ) ([]da.ID , []da.Proof , error ) {
120
+ func (a * Client ) Submit (ctx context.Context , daBlobs []da.Blob , gasPrice float64 ) ([]da.ID , []da.Proof , error ) {
123
121
// TODO: Add support for multiple blobs
124
122
daBlob := daBlobs [0 ]
125
123
log .Println ("data" , zap .Any ("data" , daBlob ))
243
241
}
244
242
dataProof := dataProofResp .Result .DataProof
245
243
// NOTE: Substrate's BlockNumber type is an alias for u32 type, which is uint32
246
- blobID := MakeID ( uint32 ( block .Block .Header .Number ), extIndex )
244
+ blobID := ID { Height : uint64 ( block .Block .Header .Number ), ExtIndex : uint32 ( extIndex )}
247
245
blobIDs := make ([]da.ID , 1 )
248
246
blobIDs [0 ] = blobID
249
247
257
255
}
258
256
259
257
// Get returns Blob for each given ID, or an error.
260
- func (a * DAClient ) Get (ctx context.Context , ids []da.ID ) ([]da.Blob , error ) {
258
+ func (a * Client ) Get (ctx context.Context , ids []da.ID ) ([]da.Blob , error ) {
261
259
// TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
262
260
ext , err := a .GetExtrinsic (ids [0 ])
263
261
if err != nil {
@@ -269,19 +267,19 @@ func (a *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
269
267
}
270
268
271
269
// GetIDs returns IDs of all Blobs located in DA at given height.
272
- func (a * DAClient ) GetIDs (ctx context.Context , height uint64 ) ([]da.ID , error ) {
270
+ func (a * Client ) GetIDs (ctx context.Context , height uint64 ) ([]da.ID , error ) {
273
271
// TODO: Need to implement this
274
272
return nil , nil
275
273
}
276
274
277
275
// Commit creates a Commitment for each given Blob.
278
- func (a * DAClient ) Commit (ctx context.Context , daBlobs []da.Blob ) ([]da.Commitment , error ) {
276
+ func (a * Client ) Commit (ctx context.Context , daBlobs []da.Blob ) ([]da.Commitment , error ) {
279
277
// TODO: Need to implement this
280
278
return nil , nil
281
279
}
282
280
283
281
// GetProofs returns the proofs for the given IDs
284
- func (a * DAClient ) GetProof (ctx context.Context , blockHeight uint32 , extIdx int ) (DataProofRPCResponse , error ) {
282
+ func (a * Client ) GetProof (ctx context.Context , blockHeight uint32 , extIdx int ) (DataProofRPCResponse , error ) {
285
283
var dataProofResp DataProofRPCResponse
286
284
blockHash , err := a .API .RPC .Chain .GetBlockHash (uint64 (blockHeight ))
287
285
if err != nil {
@@ -311,7 +309,7 @@ func (a *DAClient) GetProof(ctx context.Context, blockHeight uint32, extIdx int)
311
309
}
312
310
313
311
// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
314
- func (c * DAClient ) Validate (ctx context.Context , ids []da.ID , daProofs []da.Proof ) ([]bool , error ) {
312
+ func (c * Client ) Validate (ctx context.Context , ids []da.ID , daProofs []da.Proof ) ([]bool , error ) {
315
313
// TODO: Need to implement this
316
314
return nil , nil
317
315
}
@@ -327,7 +325,7 @@ func (b BatchDAData) IsEmpty() bool {
327
325
return reflect .DeepEqual (b , BatchDAData {})
328
326
}
329
327
330
- func (a * DAClient ) GetAccountNextIndex () (types.UCompact , error ) {
328
+ func (a * Client ) GetAccountNextIndex () (types.UCompact , error ) {
331
329
// TODO: Add context to the request
332
330
resp , err := http .Post (a .Config .HttpApiURL , "application/json" , strings .NewReader (fmt .Sprintf ("{\" id\" :1,\" jsonrpc\" :\" 2.0\" ,\" method\" :\" system_accountNextIndex\" ,\" params\" :[\" %v\" ]}" , a .KeyringPair .Address ))) //nolint: noctx
333
331
if err != nil {
@@ -348,25 +346,9 @@ func (a *DAClient) GetAccountNextIndex() (types.UCompact, error) {
348
346
return types .NewUCompactFromUInt (uint64 (accountNextIndex .Result )), nil
349
347
}
350
348
351
- // makeID creates a unique ID to reference a blob on Avail
352
- func MakeID (blockHeight uint32 , extIndex int ) da.ID {
353
- // Serialise height and leaf index to binary
354
- heightLen := 4
355
- heightBytes := make ([]byte , heightLen )
356
- binary .LittleEndian .PutUint32 (heightBytes , blockHeight )
357
- extIndexBytes := make ([]byte , 4 )
358
- binary .LittleEndian .PutUint32 (extIndexBytes , uint32 (extIndex ))
359
- return da .ID (append (heightBytes , extIndexBytes ... ))
360
- }
361
-
362
- // SplitID returns the block height and leaf index from a unique ID
363
- func SplitID (id da.ID ) (uint32 , uint32 ) {
364
- heightLen := 4
365
- heightBytes := id [:heightLen ]
366
- extIdxBytes := id [heightLen :]
367
- blockHeight := binary .LittleEndian .Uint32 (heightBytes )
368
- extIdx := binary .LittleEndian .Uint32 (extIdxBytes )
369
- return blockHeight , extIdx
349
+ type ID struct {
350
+ Height uint64 `json:"blockHeight"`
351
+ ExtIndex uint32 `json:"extIdx"`
370
352
}
371
353
372
354
type Config struct {
@@ -400,15 +382,18 @@ func (c *Config) GetConfig(configFileName string) error {
400
382
return nil
401
383
}
402
384
403
- func (a * DAClient ) GetExtrinsic (id da.ID ) (types.Extrinsic , error ) {
404
- blockHeight , extIdx := SplitID (id )
405
- blockHash , err := a .API .RPC .Chain .GetBlockHash (uint64 (blockHeight ))
385
+ func (a * Client ) GetExtrinsic (id da.ID ) (types.Extrinsic , error ) {
386
+ availID , ok := id .(ID )
387
+ if ! ok {
388
+ return types.Extrinsic {}, fmt .Errorf ("invalid ID" )
389
+ }
390
+ blockHash , err := a .API .RPC .Chain .GetBlockHash (uint64 (availID .Height ))
406
391
if err != nil {
407
392
log .Fatalf ("cannot get block hash:%w" , err )
408
393
}
409
394
block , err := a .API .RPC .Chain .GetBlock (blockHash )
410
395
if err != nil {
411
396
log .Fatalf ("cannot get block:%w" , err )
412
397
}
413
- return block .Block .Extrinsics [extIdx ], nil
398
+ return block .Block .Extrinsics [availID . ExtIndex ], nil
414
399
}
0 commit comments