From 56a08780672d755a35a98e2c19ab1658a56179ea Mon Sep 17 00:00:00 2001 From: chandiniv1 Date: Wed, 23 Aug 2023 12:53:50 +0530 Subject: [PATCH] removed checkblock availability method --- da/avail/avail.go | 68 ++++++------------------------- da/avail/datasubmit/submitdata.go | 21 +++++----- go.mod | 1 - 3 files changed, 22 insertions(+), 68 deletions(-) diff --git a/da/avail/avail.go b/da/avail/avail.go index 930cbc524..8ada4d5a1 100644 --- a/da/avail/avail.go +++ b/da/avail/avail.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strings" @@ -20,13 +20,13 @@ type Config struct { Seed string `json:"seed"` ApiURL string `json:"api_url"` AppID int `json:"app_id"` - confidence float64 `json:"confidence"` + Confidence float64 `json:"confidence"` } type DataAvailabilityLayerClient struct { - namespace types.NamespaceID - config Config - logger log.Logger + _ types.NamespaceID + config Config + logger log.Logger } type Confidence struct { @@ -44,7 +44,7 @@ var _ da.DataAvailabilityLayerClient = &DataAvailabilityLayerClient{} var _ da.BlockRetriever = &DataAvailabilityLayerClient{} // Init initializes DataAvailabilityLayerClient instance. -func (c *DataAvailabilityLayerClient) Init(namespaceID types.NamespaceID, config []byte, kvStore ds.Datastore, logger log.Logger) error { +func (c *DataAvailabilityLayerClient) Init(_ types.NamespaceID, config []byte, kvStore ds.Datastore, logger log.Logger) error { c.logger = logger if len(config) > 0 { @@ -98,61 +98,14 @@ func (c *DataAvailabilityLayerClient) SubmitBlocks(ctx context.Context, blocks [ return da.ResultSubmitBlocks{ BaseResult: da.BaseResult{ Code: da.StatusSuccess, - Message: "data submitted succesfully", + Message: "data submitted successfully", DAHeight: 1, }, } } -// CheckBlockAvailability queries DA layer to check data availability of block. -func (c *DataAvailabilityLayerClient) CheckBlockAvailability(ctx context.Context, dataLayerHeight uint64) da.ResultCheckBlock { - - blockNumber := dataLayerHeight - confidenceURL := fmt.Sprintf(c.config.BaseURL+"/confidence/%d", blockNumber) - - response, err := http.Get(confidenceURL) - - if err != nil { - return da.ResultCheckBlock{ - BaseResult: da.BaseResult{ - Code: da.StatusError, - Message: err.Error(), - }, - } - } - - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - return da.ResultCheckBlock{ - BaseResult: da.BaseResult{ - Code: da.StatusError, - Message: err.Error(), - }, - } - } - - var confidenceObject Confidence - err = json.Unmarshal(responseData, &confidenceObject) - if err != nil { - return da.ResultCheckBlock{ - BaseResult: da.BaseResult{ - Code: da.StatusError, - Message: err.Error(), - }, - } - } - - return da.ResultCheckBlock{ - BaseResult: da.BaseResult{ - Code: da.StatusSuccess, - DAHeight: uint64(confidenceObject.Block), - }, - DataAvailable: confidenceObject.Confidence > float64(c.config.confidence), - } -} - -//RetrieveBlocks gets the block from DA layer. +// RetrieveBlocks gets the block from DA layer. func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLayerHeight uint64) da.ResultRetrieveBlocks { @@ -171,8 +124,11 @@ func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLa }, } } + defer func() { + _ = response.Body.Close() + }() - responseData, err := ioutil.ReadAll(response.Body) + responseData, err := io.ReadAll(response.Body) if err != nil { return da.ResultRetrieveBlocks{ BaseResult: da.BaseResult{ diff --git a/da/avail/datasubmit/submitdata.go b/da/avail/datasubmit/submitdata.go index f587c2310..f4d1b7cfd 100644 --- a/da/avail/datasubmit/submitdata.go +++ b/da/avail/datasubmit/submitdata.go @@ -10,6 +10,12 @@ import ( // submit data submits the extrinsic through substrate api func SubmitData(apiURL string, seed string, appID int, data []byte) error { + + // if app id is greater than 0 then it must be created before submitting data + if appID == 0 { + return errors.New("AppID cant be 0") + } + api, err := gsrpc.NewSubstrateAPI(apiURL) if err != nil { return err @@ -20,18 +26,12 @@ func SubmitData(apiURL string, seed string, appID int, data []byte) error { return err } - // if app id is greater than 0 then it must be created before submitting data - err = errors.New("AppID can be 0") - if appID == 0 { - return err - } - c, err := types.NewCall(meta, "DataAvailability.submit_data", data) if err != nil { return err } - //Create the extrinsic + // Create the extrinsic ext := types.NewExtrinsic(c) genesisHash, err := api.RPC.Chain.GetBlockHash(0) @@ -57,11 +57,11 @@ func SubmitData(apiURL string, seed string, appID int, data []byte) error { var accountInfo types.AccountInfo ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil || !ok { - return err + return errors.New("failed to get the latest storage") } nonce := uint32(accountInfo.Nonce) - o := types.SignatureOptions{ + signOptions := types.SignatureOptions{ BlockHash: genesisHash, Era: types.ExtrinsicEra{IsMortalEra: false}, GenesisHash: genesisHash, @@ -73,7 +73,7 @@ func SubmitData(apiURL string, seed string, appID int, data []byte) error { } // Sign the transaction using Alice's default account - err = ext.Sign(keyringPair, o) + err = ext.Sign(keyringPair, signOptions) if err != nil { return err } @@ -85,5 +85,4 @@ func SubmitData(apiURL string, seed string, appID int, data []byte) error { } return nil - } diff --git a/go.mod b/go.mod index e6607d632..8d02af58c 100644 --- a/go.mod +++ b/go.mod @@ -201,7 +201,6 @@ require ( replace ( github.com/centrifuge/go-substrate-rpc-client/v4 => github.com/chandiniv1/go-substrate-rpc-client/v4 v4.0.12-avail-1.5.0-4eb55aaa492.0.20230719142342-dfd1152c8eca - github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4 google.golang.org/grpc => google.golang.org/grpc v1.33.2