1- package availda
1+ package avail
22
33import (
44 "context"
@@ -13,12 +13,10 @@ import (
1313
1414 "log"
1515
16- "encoding/binary"
17-
1816 gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
1917 "github.com/centrifuge/go-substrate-rpc-client/v4/signature"
2018 "github.com/centrifuge/go-substrate-rpc-client/v4/types"
21- "github.com/rollkit /go-da"
19+ "github.com/stackrlabs /go-daash/ da"
2220 "go.uber.org/zap"
2321 "golang.org/x/crypto/sha3"
2422)
@@ -45,7 +43,7 @@ type DataProof struct {
4543 } `json:"roots"`
4644}
4745
48- type DAClient struct {
46+ type Client struct {
4947 Config Config
5048 API * gsrpc.SubstrateAPI
5149 Meta * types.Metadata
@@ -58,8 +56,8 @@ type DAClient struct {
5856}
5957
6058// 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 {}
6361 err := a .Config .GetConfig (configPath )
6462 if err != nil {
6563 return nil , fmt .Errorf ("cannot get config" , err )
@@ -112,14 +110,14 @@ func New(configPath string) (*DAClient, error) {
112110}
113111
114112// 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 ) {
116114 var maxBlobSize uint64 = 64 * 64 * 500
117115 return maxBlobSize , nil
118116}
119117
120118// Submit a list of blobs to Avail DA
121119// 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 ) {
123121 // TODO: Add support for multiple blobs
124122 daBlob := daBlobs [0 ]
125123 log .Println ("data" , zap .Any ("data" , daBlob ))
243241 }
244242 dataProof := dataProofResp .Result .DataProof
245243 // 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 )}
247245 blobIDs := make ([]da.ID , 1 )
248246 blobIDs [0 ] = blobID
249247
257255}
258256
259257// 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 ) {
261259 // TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
262260 ext , err := a .GetExtrinsic (ids [0 ])
263261 if err != nil {
@@ -269,19 +267,19 @@ func (a *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
269267}
270268
271269// 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 ) {
273271 // TODO: Need to implement this
274272 return nil , nil
275273}
276274
277275// 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 ) {
279277 // TODO: Need to implement this
280278 return nil , nil
281279}
282280
283281// 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 ) {
285283 var dataProofResp DataProofRPCResponse
286284 blockHash , err := a .API .RPC .Chain .GetBlockHash (uint64 (blockHeight ))
287285 if err != nil {
@@ -311,7 +309,7 @@ func (a *DAClient) GetProof(ctx context.Context, blockHeight uint32, extIdx int)
311309}
312310
313311// 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 ) {
315313 // TODO: Need to implement this
316314 return nil , nil
317315}
@@ -327,7 +325,7 @@ func (b BatchDAData) IsEmpty() bool {
327325 return reflect .DeepEqual (b , BatchDAData {})
328326}
329327
330- func (a * DAClient ) GetAccountNextIndex () (types.UCompact , error ) {
328+ func (a * Client ) GetAccountNextIndex () (types.UCompact , error ) {
331329 // TODO: Add context to the request
332330 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
333331 if err != nil {
@@ -348,25 +346,9 @@ func (a *DAClient) GetAccountNextIndex() (types.UCompact, error) {
348346 return types .NewUCompactFromUInt (uint64 (accountNextIndex .Result )), nil
349347}
350348
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"`
370352}
371353
372354type Config struct {
@@ -400,15 +382,18 @@ func (c *Config) GetConfig(configFileName string) error {
400382 return nil
401383}
402384
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 ))
406391 if err != nil {
407392 log .Fatalf ("cannot get block hash:%w" , err )
408393 }
409394 block , err := a .API .RPC .Chain .GetBlock (blockHash )
410395 if err != nil {
411396 log .Fatalf ("cannot get block:%w" , err )
412397 }
413- return block .Block .Extrinsics [extIdx ], nil
398+ return block .Block .Extrinsics [availID . ExtIndex ], nil
414399}
0 commit comments