1- package main
1+ package availda
22
33import (
44 "context"
@@ -23,10 +23,6 @@ import (
2323 "golang.org/x/crypto/sha3"
2424)
2525
26- type DAClient interface {
27- PostData (txData []byte ) (* BatchDAData , error )
28- }
29-
3026type AccountNextIndexRPCResponse struct {
3127 Result uint `json:"result"`
3228}
@@ -55,7 +51,7 @@ type DataProof struct {
5551 Leaf string `json:"leaf"`
5652}
5753
58- type AvailDA struct {
54+ type DAClient struct {
5955 config Config
6056 API * gsrpc.SubstrateAPI
6157 Meta * types.Metadata
@@ -67,9 +63,10 @@ type AvailDA struct {
6763 DestinationDomain types.UCompact
6864}
6965
70- func NewAvailDA () (* AvailDA , error ) {
71- a := AvailDA {}
72- err := a .config .GetConfig ("./avail-config.json" )
66+ // Returns a newly initalised Avail DA client
67+ func New (configPath string ) (* DAClient , error ) {
68+ a := DAClient {}
69+ err := a .config .GetConfig (configPath )
7370 if err != nil {
7471 return nil , fmt .Errorf ("cannot get config" , err )
7572 }
@@ -122,14 +119,14 @@ func NewAvailDA() (*AvailDA, error) {
122119}
123120
124121// MaxBlobSize returns the max blob size
125- func (c * AvailDA ) MaxBlobSize (ctx context.Context ) (uint64 , error ) {
122+ func (c * DAClient ) MaxBlobSize (ctx context.Context ) (uint64 , error ) {
126123 var maxBlobSize uint64 = 64 * 64 * 500
127124 return maxBlobSize , nil
128125}
129126
130127// Submit a list of blobs to Avail DA
131128// Currently, we submit to a trusted RPC Avail node. In the future, we will submit viaΒ an Avail light client.
132- func (a * AvailDA ) Submit (ctx context.Context , daBlobs []da.Blob , gasPrice float64 ) ([]da.ID , []da.Proof , error ) {
129+ func (a * DAClient ) Submit (ctx context.Context , daBlobs []da.Blob , gasPrice float64 ) ([]da.ID , []da.Proof , error ) {
133130 // TODO: Add support for multiple blobs
134131 daBlob := daBlobs [0 ]
135132 log .Println ("data" , zap .Any ("data" , daBlob ))
241238 dataProof := dataProofResp .Result .DataProof
242239
243240 // NOTE: Substrate's BlockNumber type is an alias for u32 type, which is uint32
244- blobID := a . makeID (uint32 (block .Block .Header .Number ), uint32 (dataProof .LeafIndex ))
241+ blobID := makeID (uint32 (block .Block .Header .Number ), uint32 (dataProof .LeafIndex ))
245242 blobIDs := make ([]da.ID , 1 )
246243 blobIDs [0 ] = blobID
247244
255252}
256253
257254// Get returns Blob for each given ID, or an error.
258- func (a * AvailDA ) Get (ctx context.Context , ids []da.ID ) ([]da.Blob , error ) {
255+ func (a * DAClient ) Get (ctx context.Context , ids []da.ID ) ([]da.Blob , error ) {
259256 // TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
260- blockHeight , leafIndex := a . splitID (ids [0 ])
257+ blockHeight , leafIndex := SplitID (ids [0 ])
261258 data , err := a .GetData (uint64 (blockHeight ), uint (leafIndex ))
262259 if err != nil {
263260 return nil , fmt .Errorf ("cannot get data" , err )
@@ -267,19 +264,19 @@ func (a *AvailDA) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
267264}
268265
269266// GetIDs returns IDs of all Blobs located in DA at given height.
270- func (a * AvailDA ) GetIDs (ctx context.Context , height uint64 ) ([]da.ID , error ) {
267+ func (a * DAClient ) GetIDs (ctx context.Context , height uint64 ) ([]da.ID , error ) {
271268 // TODO: Need to implement this
272269 return nil , nil
273270}
274271
275272// Commit creates a Commitment for each given Blob.
276- func (a * AvailDA ) Commit (ctx context.Context , daBlobs []da.Blob ) ([]da.Commitment , error ) {
273+ func (a * DAClient ) Commit (ctx context.Context , daBlobs []da.Blob ) ([]da.Commitment , error ) {
277274 // TODO: Need to implement this
278275 return nil , nil
279276}
280277
281278// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
282- func (c * AvailDA ) Validate (ctx context.Context , ids []da.ID , daProofs []da.Proof ) ([]bool , error ) {
279+ func (c * DAClient ) Validate (ctx context.Context , ids []da.ID , daProofs []da.Proof ) ([]bool , error ) {
283280 // TODO: Need to implement this
284281 return nil , nil
285282}
@@ -295,7 +292,7 @@ func (b BatchDAData) IsEmpty() bool {
295292 return reflect .DeepEqual (b , BatchDAData {})
296293}
297294
298- func (a AvailDA ) GetAccountNextIndex () (types.UCompact , error ) {
295+ func (a * DAClient ) GetAccountNextIndex () (types.UCompact , error ) {
299296 // TODO: Add context to the request
300297 resp , err := http .Post ("https://goldberg.avail.tools/api" , "application/json" , strings .NewReader (fmt .Sprintf ("{\" id\" :1,\" jsonrpc\" :\" 2.0\" ,\" method\" :\" system_accountNextIndex\" ,\" params\" :[\" %v\" ]}" , a .KeyringPair .Address ))) //nolint: noctx
301298 if err != nil {
@@ -318,7 +315,7 @@ func (a AvailDA) GetAccountNextIndex() (types.UCompact, error) {
318315}
319316
320317// makeID creates a unique ID to reference a blob on Avail
321- func ( a * AvailDA ) makeID (blockHeight uint32 , leafIndex uint32 ) da.ID {
318+ func makeID (blockHeight uint32 , leafIndex uint32 ) da.ID {
322319 // Serialise height and leaf index to binary
323320 heightLen := 4
324321 leafIndexLen := 4
@@ -330,8 +327,8 @@ func (a *AvailDA) makeID(blockHeight uint32, leafIndex uint32) da.ID {
330327 return da .ID (idBytes )
331328}
332329
333- // splitID returns the block height and leaf index from a unique ID
334- func ( a * AvailDA ) splitID (id da.ID ) (uint32 , uint32 ) {
330+ // SplitID returns the block height and leaf index from a unique ID
331+ func SplitID (id da.ID ) (uint32 , uint32 ) {
335332 heightLen := 4
336333 leafIndexLen := 4
337334 heightBytes := id [:heightLen ]
@@ -341,7 +338,7 @@ func (a *AvailDA) splitID(id da.ID) (uint32, uint32) {
341338 return blockHeight , leafIndex
342339}
343340
344- func (a AvailDA ) GetData (blockNumber uint64 , index uint ) ([]byte , error ) {
341+ func (a * DAClient ) GetData (blockNumber uint64 , index uint ) ([]byte , error ) {
345342 blockHash , err := a .API .RPC .Chain .GetBlockHash (blockNumber )
346343 if err != nil {
347344 return nil , fmt .Errorf ("cannot get block hash" , err )
0 commit comments