Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Avail DA to Rollkit #2

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b134701
added da for avail
chandiniv1 Jul 17, 2023
64a7b9b
fix: messages and appID + dependencies
prabal-banerjee Jul 19, 2023
b0382f7
replaced substrate rpc
chandiniv1 Jul 24, 2023
d7d815f
made changes in retrieve blocks
chandiniv1 Jul 26, 2023
5cbc78e
changed naming conventions
chandiniv1 Aug 3, 2023
619de31
taken dalayerheight as blocknumber
chandiniv1 Aug 3, 2023
4ebf625
renamed mock to datasubmit
chandiniv1 Aug 3, 2023
964575d
renamed mock to datasubmit and added desired confidence to config
chandiniv1 Aug 3, 2023
ae3fc03
added baseurl and confidence to config
chandiniv1 Aug 7, 2023
eb7269b
removed size from config and handled errors
chandiniv1 Aug 7, 2023
9ea8b1c
removed rpc in dataavailabilitylayerclient
chandiniv1 Aug 7, 2023
303d5ff
return error when appID is 0
chandiniv1 Aug 9, 2023
7add807
changed imports in go mod
chandiniv1 Aug 9, 2023
8cb14ba
Merge branch 'vitwit/avail-da' into chandini/avail-da
murthyvitwit Aug 9, 2023
8f17072
added block extrincs to blocks
chandiniv1 Aug 10, 2023
d92ead6
Merge branch 'chandini/avail-da' of https://github.com/chandiniv1/rol…
chandiniv1 Aug 10, 2023
773fb54
modified submit block to submit blocks
chandiniv1 Aug 10, 2023
f0ab456
Merge branch 'main' of https://github.com/rollkit/rollkit into chandi…
chandiniv1 Aug 23, 2023
0712600
updated avail.go
chandiniv1 Aug 23, 2023
6243a29
closed the response body
chandiniv1 Aug 23, 2023
86aa124
removed empty fields
chandiniv1 Aug 29, 2023
e840e02
fixed linting
chandiniv1 Sep 4, 2023
b7cd609
changed gsrpc
chandiniv1 Sep 5, 2023
b79c701
Merge branch 'main' of https://github.com/rollkit/rollkit into chandi…
chandiniv1 Sep 5, 2023
e71fcb6
changed go mod
chandiniv1 Sep 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions da/avail/avail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package avail

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

ds "github.com/ipfs/go-datastore"
openrpc "github.com/rollkit/celestia-openrpc"
openrpcns "github.com/rollkit/celestia-openrpc/types/namespace"
"github.com/rollkit/rollkit/da"
"github.com/rollkit/rollkit/da/avail/datasubmit"
"github.com/rollkit/rollkit/log"
"github.com/rollkit/rollkit/types"
)

type Config struct {
BaseURL string `json:"base_url"`
Seed string `json:"seed"`
ApiURL string `json:"api_url"`
Size int `json:"size"`
AppID int `json:"app_id"`
confidence float64 `json:"confidence"`
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved
}

type DataAvailabilityLayerClient struct {
rpc *openrpc.Client
namespace openrpcns.Namespace
config Config
logger log.Logger
}

type Confidence struct {
Block uint32 `json:"block"`
Confidence float64 `json:"confidence"`
SerialisedConfidence *string `json:"serialised_confidence,omitempty"`
}

type AppData struct {
Block uint32 `json:"block"`
Extrinsics string `json:"extrinsics"`
}

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 {
c.logger = logger

if len(config) > 0 {
return json.Unmarshal(config, &c.config)
}

return nil
}
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved

// Start prepares DataAvailabilityLayerClient to work.
func (c *DataAvailabilityLayerClient) Start() error {

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")

return nil
}

// SubmitBlock submits a block to DA layer.
func (c *DataAvailabilityLayerClient) SubmitBlock(ctx context.Context, block *types.Block) da.ResultSubmitBlock {

data, err := block.MarshalBinary()
if err != nil {
return da.ResultSubmitBlock{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}

txHash, err := datasubmit.SubmitData(c.config.Size, c.config.ApiURL, c.config.Seed, c.config.AppID, data)

if err != nil {
return da.ResultSubmitBlock{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}

return da.ResultSubmitBlock{
BaseResult: da.BaseResult{
Code: da.StatusSuccess,
Message: "tx hash: " + hex.EncodeToString(txHash[:]),
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why new variable?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akhilkumarpilli ,datalayerheight and blocknumber are two different fields but currently we are considering datalayerheight as blocknumber that's why I have taken a new variable.If there is no necessity of new variable,I'll remove it. Please suggest

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, you can remove it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay akhil

confidenceURL := fmt.Sprintf(c.config.BaseURL+"/confidence/%d", blockNumber)

response, err := http.Get(confidenceURL)
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved

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
json.Unmarshal(responseData, &confidenceObject)
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved

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.
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved

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

blocks := make([]*types.Block, 1)
blocks[0] = new(types.Block)

blockNumber := dataLayerHeight
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

appDataURL := fmt.Sprintf(c.config.BaseURL+"/appdata/%d?decode=true", blockNumber)
response, err := http.Get(appDataURL)
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Code: da.StatusError,
Message: err.Error(),
},
}
}
var appDataObject AppData
json.Unmarshal(responseData, &appDataObject)

return da.ResultRetrieveBlocks{
BaseResult: da.BaseResult{
Code: da.StatusSuccess,
DAHeight: uint64(appDataObject.Block),
Message: "block data: " + appDataObject.Extrinsics,
},
Blocks: blocks,
aterentic-ethernal marked this conversation as resolved.
Show resolved Hide resolved
}
}
88 changes: 88 additions & 0 deletions da/avail/datasubmit/submitdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package datasubmit

import (
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"

Check failure on line 4 in da/avail/datasubmit/submitdata.go

View workflow job for this annotation

GitHub Actions / test / Run Unit Tests with Race Detector

github.com/centrifuge/go-substrate-rpc-client/v4@v4.0.12: replacement directory /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client does not exist

Check failure on line 4 in da/avail/datasubmit/submitdata.go

View workflow job for this annotation

GitHub Actions / test / Run Unit Tests

github.com/centrifuge/go-substrate-rpc-client/v4@v4.0.12: replacement directory /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client does not exist
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"

Check failure on line 5 in da/avail/datasubmit/submitdata.go

View workflow job for this annotation

GitHub Actions / test / Run Unit Tests with Race Detector

github.com/centrifuge/go-substrate-rpc-client/v4@v4.0.12: replacement directory /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client does not exist

Check failure on line 5 in da/avail/datasubmit/submitdata.go

View workflow job for this annotation

GitHub Actions / test / Run Unit Tests

github.com/centrifuge/go-substrate-rpc-client/v4@v4.0.12: replacement directory /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client does not exist
"github.com/centrifuge/go-substrate-rpc-client/v4/types"

Check failure on line 6 in da/avail/datasubmit/submitdata.go

View workflow job for this annotation

GitHub Actions / test / Run Unit Tests with Race Detector

github.com/centrifuge/go-substrate-rpc-client/v4@v4.0.12: replacement directory /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client does not exist

Check failure on line 6 in da/avail/datasubmit/submitdata.go

View workflow job for this annotation

GitHub Actions / test / Run Unit Tests

github.com/centrifuge/go-substrate-rpc-client/v4@v4.0.12: replacement directory /home/chandini/go/src/github.com/chandiniv1/go-substrate-rpc-client does not exist
)

// submit data submits the extrinsic through substrate api
func SubmitData(size int, apiURL string, seed string, AppID int, data []byte) (types.Hash, error) {
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved
api, err := gsrpc.NewSubstrateAPI(apiURL)
if err != nil {
return types.Hash{}, err
}

meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return types.Hash{}, err
}

var appID int

// if app id is greater than 0 then it must be created before submitting data
if AppID != 0 {
appID = AppID
}
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved

c, err := types.NewCall(meta, "DataAvailability.submit_data", data)
if err != nil {
return types.Hash{}, err
}

//Create the extrinsic
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved
ext := types.NewExtrinsic(c)

genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
return types.Hash{}, err
}

rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
return types.Hash{}, err
}

keyringPair, err := signature.KeyringPairFromSecret(seed, 42)
if err != nil {
return types.Hash{}, err
}

key, err := types.CreateStorageKey(meta, "System", "Account", keyringPair.PublicKey)
if err != nil {
return types.Hash{}, err
}

var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
return types.Hash{}, err
}

nonce := uint32(accountInfo.Nonce)
o := types.SignatureOptions{
chandiniv1 marked this conversation as resolved.
Show resolved Hide resolved
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
AppID: types.NewUCompactFromUInt(uint64(appID)),
TransactionVersion: rv.TransactionVersion,
}

// Sign the transaction using Alice's default account
err = ext.Sign(keyringPair, o)
if err != nil {
return types.Hash{}, err
}

// Send the extrinsic
hash, err := api.RPC.Author.SubmitExtrinsic(ext)
if err != nil {
return types.Hash{}, err
}

return hash, nil

}
3 changes: 3 additions & 0 deletions da/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"

"github.com/rollkit/rollkit/da"
"github.com/rollkit/rollkit/da/avail"
"github.com/rollkit/rollkit/da/celestia"

"github.com/rollkit/rollkit/da/grpc"
"github.com/rollkit/rollkit/da/mock"
)
Expand All @@ -23,6 +25,7 @@ var clients = map[string]func() da.DataAvailabilityLayerClient{
"mock": func() da.DataAvailabilityLayerClient { return &mock.DataAvailabilityLayerClient{} },
"grpc": func() da.DataAvailabilityLayerClient { return &grpc.DataAvailabilityLayerClient{} },
"celestia": func() da.DataAvailabilityLayerClient { return &celestia.DataAvailabilityLayerClient{} },
"avail": func() da.DataAvailabilityLayerClient { return &avail.DataAvailabilityLayerClient{} },
}

// GetClient returns client identified by name.
Expand Down
Loading
Loading