Skip to content

Commit

Permalink
implemented da interface
Browse files Browse the repository at this point in the history
  • Loading branch information
chandiniv1 committed Nov 29, 2023
1 parent f59576d commit f24c5be
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 1 deletion.
157 changes: 157 additions & 0 deletions avail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package avail

import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"

"github.com/rollkit/go-da"
)

type SubmitRequest struct {
Data string `json:"data"`
}

type SubmitResponse struct {
BlockNumber uint32 `json:"block_number"`
BlockHash string `json:"block_hash"`
TransactionHash string `json:"hash"`
TransactionIndex uint32 `json:"index"`
}

type BlocksResponse struct {
BlockNumber uint32 `json:"block_number"`
DataTransactions []DataTransactions `json:"data_transactions"`
}

type DataTransactions struct {
Data string `json:"data"`
Extrinsic string `json:"extrinsic"`
}

type Config struct {
AppID uint32 `json:"app_ID"`
LcURL string `json:"lc_url"`
}

const BlockURL = "/blocks/%d/data?fields=data,extrinsic"

// AvailDA implements the avail backend for the DA interface
type AvailDA struct {
config Config
ctx context.Context
}

// NewAvailDA returns an instance of AvailDA
func NewAvailDA(config Config, ctx context.Context) *AvailDA {
return &AvailDA{
ctx: ctx,
config: Config{LcURL: config.LcURL, AppID: config.AppID},
}
}

var _ da.DA = &AvailDA{}

// submits each blob to avail data availability layer
func (c *AvailDA) Submit(daBlobs []da.Blob) ([]da.ID, []da.Proof, error) {
ids := make([]da.ID, len(daBlobs))
for index, blob := range daBlobs {
encodedBlob := base64.StdEncoding.EncodeToString(blob)
requestData := SubmitRequest{
Data: encodedBlob,
}
requestBody, err := json.Marshal(requestData)
if err != nil {
return nil, nil, err
}
// Make a POST request to the /v2/submit endpoint.
response, err := http.Post(c.config.LcURL+"/submit", "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return nil, nil, err
}
defer response.Body.Close()

responseData, err := io.ReadAll(response.Body)
if err != nil {
return nil, nil, err
}

var submitResponse SubmitResponse
err = json.Unmarshal(responseData, &submitResponse)
if err != nil {
return nil, nil, err
}
ids[index] = makeID(submitResponse.BlockNumber)
}
fmt.Println("succesfully submitted blobs to avail")
return ids, nil, nil
}

// Get returns Blob for each given ID, or an error
func (c *AvailDA) Get(ids []da.ID) ([]da.Blob, error) {
var blobs [][]byte
var blockNumber uint32
for _, id := range ids {
blockNumber = binary.BigEndian.Uint32(id)
blocksURL := fmt.Sprintf(c.config.LcURL+BlockURL, blockNumber)
parsedURL, err := url.Parse(blocksURL)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", parsedURL.String(), nil)
if err != nil {
return nil, err
}
client := http.DefaultClient
response, err := client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = response.Body.Close()
}()
responseData, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var blocksObject BlocksResponse
err = json.Unmarshal(responseData, &blocksObject)
if err != nil {
return nil, err
}
for _, dataTransaction := range blocksObject.DataTransactions {
blobs = append(blobs, []byte(dataTransaction.Data))
}
}
return blobs, nil
}

func (c *AvailDA) GetIDs(height uint64) ([]da.ID, error) {
//todo:currently returning height as ID, need to extend avail-light api
ids := make([]byte, 8)
binary.BigEndian.PutUint64(ids, height)
return [][]byte{ids}, nil
}

// Commit creates a Commitment for each given Blob.
func (c *AvailDA) Commit(daBlobs []da.Blob) ([]da.Commitment, error) {
return nil, nil
}

// Validate validates Commitments against the corresponding Proofs
func (c *AvailDA) Validate(ids []da.ID, daProofs []da.Proof) ([]bool, error) {
return nil, nil
}

func makeID(blockNumber uint32) da.ID {
// IDs are not unique in rollkit context and that this has to be improved
id := make([]byte, 8)
binary.BigEndian.PutUint32(id, blockNumber)
return id
}
10 changes: 10 additions & 0 deletions avail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package avail_test

import (
"testing"
)

func TestAvailDA(t *testing.T) {
t.Skip()
//todo
}
41 changes: 41 additions & 0 deletions cmd/avail-da/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"encoding/json"
"errors"
"log"
"net"
"os"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/rollkit/avail-da"
"github.com/rollkit/go-da/proxy"
)

func main() {
data, err := os.ReadFile("config.json")
if err != nil {
log.Fatalln("Error reading config file:", err)
}
// Parse the configuration data into a Config struct
var config avail.Config
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatalln("Error parsing config file:", err)
}
ctx := context.Background()
da := avail.NewAvailDA(config, ctx)
srv := proxy.NewServer(da, grpc.Creds(insecure.NewCredentials()))
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatalln("failed to create network listener:", err)
}
log.Println("serving avail-da over gRPC on:", lis.Addr())
err = srv.Serve(lis)
if !errors.Is(err, grpc.ErrServerStopped) {
log.Fatalln("gRPC server stopped with error:", err)
}
}
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lc_url" : "http://localhost:8000/v2",
"app_ID":1
}
17 changes: 16 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
module github.com/rollkit/avail-da

go 1.21.0
go 1.20

require (
github.com/rollkit/go-da v0.0.0-20231024133951-57bc36006772
google.golang.org/grpc v1.59.0
)

require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
56 changes: 56 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rollkit/go-da v0.0.0-20231024133951-57bc36006772 h1:0qbVvvxy++RIjwoI2GmqgZDNP5yShBMA+swWjKt7mQE=
github.com/rollkit/go-da v0.0.0-20231024133951-57bc36006772/go.mod h1:cy1LA9kCyCJHgszKkTh9hJn816l5Oa87GMA2c1imfqA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 comments on commit f24c5be

Please sign in to comment.