Skip to content

Commit

Permalink
changed gsrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
chandiniv1 committed Sep 5, 2023
1 parent e840e02 commit b7cd609
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
73 changes: 56 additions & 17 deletions da/avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"net/url"

ds "github.com/ipfs/go-datastore"

Expand All @@ -16,26 +16,34 @@ import (
"github.com/rollkit/rollkit/types"
)

const BLOCK_NOT_FOUND = "\"Not found\""
const PROCESSING_BLOCK = "\"Processing block\""

// Config stores Avail DALC configuration parameters.
type Config struct {
BaseURL string `json:"base_url"`
Seed string `json:"seed"`
ApiURL string `json:"api_url"`
AppDataURL string `json:"app_data_url"`
AppID int `json:"app_id"`
Confidence float64 `json:"confidence"`
}

// DataAvailabilityLayerClient uses Avail-Node configuration parameters
type DataAvailabilityLayerClient struct {
_ types.NamespaceID
config Config
logger log.Logger
}

// Confidence stores block params retireved from Avail Light Node Endpoint
type Confidence struct {
Block uint32 `json:"block"`
Confidence float64 `json:"confidence"`
SerialisedConfidence *string `json:"serialised_confidence,omitempty"`
}

// AppData stores Extrinsics retrieved from Avail Light Node Endpoint
type AppData struct {
Block uint32 `json:"block"`
Extrinsics []string `json:"extrinsics"`
Expand All @@ -58,15 +66,15 @@ func (c *DataAvailabilityLayerClient) Init(_ types.NamespaceID, config []byte, k
// Start prepares DataAvailabilityLayerClient to work.
func (c *DataAvailabilityLayerClient) Start() error {

c.logger.Info("starting avail Data Availability Layer Client", "baseURL", c.config.ApiURL)
c.logger.Info("starting avail data availability layer client", "baseURL", c.config.ApiURL)

return nil
}

// Stop stops DataAvailabilityLayerClient.
func (c *DataAvailabilityLayerClient) Stop() error {

c.logger.Info("stopping Avail Data Availability Layer Client")
c.logger.Info("stopping avail data availability layer client")

return nil
}
Expand All @@ -84,39 +92,39 @@ func (c *DataAvailabilityLayerClient) SubmitBlocks(ctx context.Context, blocks [
},
}
}
err = datasubmit.SubmitData(c.config.ApiURL, c.config.Seed, c.config.AppID, data)

if err != nil {
if err := datasubmit.SubmitData(c.config.ApiURL, c.config.Seed, c.config.AppID, data); err != nil {
return da.ResultSubmitBlocks{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}

}

return da.ResultSubmitBlocks{
BaseResult: da.BaseResult{
Code: da.StatusSuccess,
Message: "data submitted successfully",
Message: "success",
DAHeight: 1,
},
}

}

// RetrieveBlocks gets the block from DA layer.

func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLayerHeight uint64) da.ResultRetrieveBlocks {

blocks := []*types.Block{}

Loop:
blockNumber := dataLayerHeight

appDataURL := fmt.Sprintf(c.config.BaseURL+"/appdata/%d?decode=true", blockNumber)
appDataURL := fmt.Sprintf(c.config.BaseURL+c.config.AppDataURL, blockNumber)

response, err := http.Get(appDataURL)
// Sanitize and validate the URL
parsedURL, err := url.Parse(appDataURL)
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Expand All @@ -125,11 +133,9 @@ func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLa
},
}
}
defer func() {
_ = response.Body.Close()
}()

responseData, err := io.ReadAll(response.Body)
// Create an HTTP request with the sanitized URL
req, err := http.NewRequest("GET", parsedURL.String(), nil)
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Expand All @@ -139,8 +145,22 @@ func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLa
}
}

var appDataObject AppData
err = json.Unmarshal(responseData, &appDataObject)
// Perform the HTTP request
client := http.DefaultClient
response, err := client.Do(req)
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}
defer func() {
_ = response.Body.Close()
}()

responseData, err := io.ReadAll(response.Body)
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Expand All @@ -150,6 +170,26 @@ func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLa
}
}

var appDataObject AppData
if string(responseData) == BLOCK_NOT_FOUND {

appDataObject = AppData{Block: uint32(blockNumber), Extrinsics: []string{}}

} else if string(responseData) == PROCESSING_BLOCK {

goto Loop

} else {
if err = json.Unmarshal(responseData, &appDataObject); err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}
}

txnsByteArray := []byte{}
for _, extrinsic := range appDataObject.Extrinsics {
txnsByteArray = append(txnsByteArray, []byte(extrinsic)...)
Expand All @@ -173,7 +213,6 @@ func (c *DataAvailabilityLayerClient) RetrieveBlocks(ctx context.Context, dataLa
BaseResult: da.BaseResult{
Code: da.StatusSuccess,
DAHeight: uint64(appDataObject.Block),
Message: "block data: " + strings.Join(appDataObject.Extrinsics, " "),
},
Blocks: blocks,
}
Expand Down
5 changes: 2 additions & 3 deletions da/avail/datasubmit/submitdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
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 {
if appID < 1 {
return errors.New("AppID cant be 0")
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func SubmitData(apiURL string, seed string, appID int, data []byte) error {
TransactionVersion: rv.TransactionVersion,
}

// Sign the transaction using Alice's default account
// Sign the transaction
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

}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ require (
)

replace (
//github.com/centrifuge/go-substrate-rpc-client/v4 => /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client
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/centrifuge/go-substrate-rpc-client/v4 => github.com/availproject/go-substrate-rpc-client/v4 v4.0.12-avail-1.6.2-rc1
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

)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
github.com/ashanbrown/forbidigo v1.3.0/go.mod h1:vVW7PEdqEFqapJe95xHkTfB1+XvZXBFg8t0sG2FIxmI=
github.com/ashanbrown/makezero v1.1.1/go.mod h1:i1bJLCRSCHOcOa9Y6MyF2FTfMZMFdHvxKHxgO5Z1axI=
github.com/availproject/go-substrate-rpc-client/v4 v4.0.12-avail-1.6.2-rc1 h1:SpFSpUgNt2/Y3b7boDO5rNQLi5xFnZ9ULPD3Dhrbedw=
github.com/availproject/go-substrate-rpc-client/v4 v4.0.12-avail-1.6.2-rc1/go.mod h1:NF4gqLe3pSyn8QdLF3Mf2AFD8DDzsOG4ws/ZpazxE2M=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.23.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.25.37/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
Expand Down Expand Up @@ -219,8 +221,6 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chandiniv1/go-substrate-rpc-client/v4 v4.0.12-avail-1.5.0-4eb55aaa492.0.20230719142342-dfd1152c8eca h1:LDXxsQcnmEcf5bE+kh/4C3dihhEt8q21f2A3oor3ljA=
github.com/chandiniv1/go-substrate-rpc-client/v4 v4.0.12-avail-1.5.0-4eb55aaa492.0.20230719142342-dfd1152c8eca/go.mod h1:ci/8ueqySAWWtagThy3d8kwHGYNjl0AXw8aasJfnr20=
github.com/charithe/durationcheck v0.0.9/go.mod h1:SSbRIBVfMjCi/kEB6K65XEA83D6prSM8ap1UCpNKtgg=
github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4/go.mod h1:W8EnPSQ8Nv4fUjc/v1/8tHFqhuOJXnRub0dTfuAQktU=
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
Expand Down

0 comments on commit b7cd609

Please sign in to comment.