Skip to content

Commit

Permalink
removed checkblock availability method
Browse files Browse the repository at this point in the history
  • Loading branch information
chandiniv1 committed Aug 23, 2023
1 parent 005a235 commit 56a0878
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 68 deletions.
68 changes: 12 additions & 56 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 @@ -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 {

Expand All @@ -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{
Expand Down
21 changes: 10 additions & 11 deletions da/avail/datasubmit/submitdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -85,5 +85,4 @@ func SubmitData(apiURL string, seed string, appID int, data []byte) error {
}

return nil

}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 56a0878

Please sign in to comment.