Skip to content

Commit

Permalink
made changes to avail.go
Browse files Browse the repository at this point in the history
  • Loading branch information
chandiniv1 committed Aug 23, 2023
1 parent 869ddff commit e1f03d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 65 deletions.
67 changes: 12 additions & 55 deletions da/avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -105,54 +105,7 @@ func (c *DataAvailabilityLayerClient) SubmitBlocks(ctx context.Context, blocks [

}

// 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 {

Expand All @@ -172,7 +125,11 @@ func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLa
}
}

responseData, err := ioutil.ReadAll(response.Body)
defer func() {
_ = response.Body.Close()
}()

responseData, err := io.ReadAll(response.Body)
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Expand Down
19 changes: 9 additions & 10 deletions da/avail/datasubmit/submitdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ 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
Expand All @@ -20,18 +25,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 cant 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)
Expand All @@ -57,11 +56,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,
Expand All @@ -73,7 +72,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
}
Expand Down

0 comments on commit e1f03d4

Please sign in to comment.