Skip to content

Commit 53e55fa

Browse files
committed
Major refactor into library + server (#3)
* refactor: into library pkgs with a command pkg * fix: celestia dependency mismatch * refactor: availda for external use * feat(daash): Add DA Manager to manage/init clients * fix(daash): map allocation * docs(cmd): Add run instructions - go mod tidy
1 parent 410786f commit 53e55fa

File tree

11 files changed

+323
-257
lines changed

11 files changed

+323
-257
lines changed

β€Ž.gitignoreβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.env
1+
/.env
2+
./cmd/blob-server/avail-config.json
3+

β€ŽREADME.mdβ€Ž

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ Each micro-rollup should be easily able to switch
3939

4040
### Steps to run
4141

42-
1. Clone the repository
43-
2.
42+
```bash
43+
git clone https://github.com/stackrlabs/go-daash # clone the repository
44+
cd go-daash/cmd/blob-server
45+
go run . # run the server
46+
curl --location 'localhost:8080/eigen' \
47+
--header 'Content-Type: application/json' \
48+
--data 'gm' # DAash away!
49+
```
4450

4551
### Additional requirements
4652

@@ -53,12 +59,3 @@ You need an auth token to run your Celestia light node. Copy the `.env.example`
5359
cp .env.example .env
5460
```
5561

56-
## Installation/Running
57-
58-
```bash
59-
go build .
60-
go run .
61-
curl --location 'localhost:8080/Celestia' \
62-
--header 'Content-Type: application/json' \
63-
--data 'gm'
64-
```

β€Žavail.goβ€Ž renamed to β€Žavailda/availda.goβ€Ž

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package availda
22

33
import (
44
"context"
@@ -23,10 +23,6 @@ import (
2323
"golang.org/x/crypto/sha3"
2424
)
2525

26-
type DAClient interface {
27-
PostData(txData []byte) (*BatchDAData, error)
28-
}
29-
3026
type AccountNextIndexRPCResponse struct {
3127
Result uint `json:"result"`
3228
}
@@ -55,7 +51,7 @@ type DataProof struct {
5551
Leaf string `json:"leaf"`
5652
}
5753

58-
type AvailDA struct {
54+
type DAClient struct {
5955
config Config
6056
API *gsrpc.SubstrateAPI
6157
Meta *types.Metadata
@@ -67,9 +63,10 @@ type AvailDA struct {
6763
DestinationDomain types.UCompact
6864
}
6965

70-
func NewAvailDA() (*AvailDA, error) {
71-
a := AvailDA{}
72-
err := a.config.GetConfig("./avail-config.json")
66+
// Returns a newly initalised Avail DA client
67+
func New(configPath string) (*DAClient, error) {
68+
a := DAClient{}
69+
err := a.config.GetConfig(configPath)
7370
if err != nil {
7471
return nil, fmt.Errorf("cannot get config", err)
7572
}
@@ -122,14 +119,14 @@ func NewAvailDA() (*AvailDA, error) {
122119
}
123120

124121
// MaxBlobSize returns the max blob size
125-
func (c *AvailDA) MaxBlobSize(ctx context.Context) (uint64, error) {
122+
func (c *DAClient) MaxBlobSize(ctx context.Context) (uint64, error) {
126123
var maxBlobSize uint64 = 64 * 64 * 500
127124
return maxBlobSize, nil
128125
}
129126

130127
// Submit a list of blobs to Avail DA
131128
// Currently, we submit to a trusted RPC Avail node. In the future, we will submit viaΒ an Avail light client.
132-
func (a *AvailDA) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
129+
func (a *DAClient) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
133130
// TODO: Add support for multiple blobs
134131
daBlob := daBlobs[0]
135132
log.Println("data", zap.Any("data", daBlob))
@@ -241,7 +238,7 @@ out:
241238
dataProof := dataProofResp.Result.DataProof
242239

243240
// NOTE: Substrate's BlockNumber type is an alias for u32 type, which is uint32
244-
blobID := a.makeID(uint32(block.Block.Header.Number), uint32(dataProof.LeafIndex))
241+
blobID := makeID(uint32(block.Block.Header.Number), uint32(dataProof.LeafIndex))
245242
blobIDs := make([]da.ID, 1)
246243
blobIDs[0] = blobID
247244

@@ -255,9 +252,9 @@ out:
255252
}
256253

257254
// Get returns Blob for each given ID, or an error.
258-
func (a *AvailDA) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
255+
func (a *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
259256
// TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
260-
blockHeight, leafIndex := a.splitID(ids[0])
257+
blockHeight, leafIndex := SplitID(ids[0])
261258
data, err := a.GetData(uint64(blockHeight), uint(leafIndex))
262259
if err != nil {
263260
return nil, fmt.Errorf("cannot get data", err)
@@ -267,19 +264,19 @@ func (a *AvailDA) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
267264
}
268265

269266
// GetIDs returns IDs of all Blobs located in DA at given height.
270-
func (a *AvailDA) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
267+
func (a *DAClient) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
271268
// TODO: Need to implement this
272269
return nil, nil
273270
}
274271

275272
// Commit creates a Commitment for each given Blob.
276-
func (a *AvailDA) Commit(ctx context.Context, daBlobs []da.Blob) ([]da.Commitment, error) {
273+
func (a *DAClient) Commit(ctx context.Context, daBlobs []da.Blob) ([]da.Commitment, error) {
277274
// TODO: Need to implement this
278275
return nil, nil
279276
}
280277

281278
// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
282-
func (c *AvailDA) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof) ([]bool, error) {
279+
func (c *DAClient) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof) ([]bool, error) {
283280
// TODO: Need to implement this
284281
return nil, nil
285282
}
@@ -295,7 +292,7 @@ func (b BatchDAData) IsEmpty() bool {
295292
return reflect.DeepEqual(b, BatchDAData{})
296293
}
297294

298-
func (a AvailDA) GetAccountNextIndex() (types.UCompact, error) {
295+
func (a *DAClient) GetAccountNextIndex() (types.UCompact, error) {
299296
// TODO: Add context to the request
300297
resp, err := http.Post("https://goldberg.avail.tools/api", "application/json", strings.NewReader(fmt.Sprintf("{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"system_accountNextIndex\",\"params\":[\"%v\"]}", a.KeyringPair.Address))) //nolint: noctx
301298
if err != nil {
@@ -318,7 +315,7 @@ func (a AvailDA) GetAccountNextIndex() (types.UCompact, error) {
318315
}
319316

320317
// makeID creates a unique ID to reference a blob on Avail
321-
func (a *AvailDA) makeID(blockHeight uint32, leafIndex uint32) da.ID {
318+
func makeID(blockHeight uint32, leafIndex uint32) da.ID {
322319
// Serialise height and leaf index to binary
323320
heightLen := 4
324321
leafIndexLen := 4
@@ -330,8 +327,8 @@ func (a *AvailDA) makeID(blockHeight uint32, leafIndex uint32) da.ID {
330327
return da.ID(idBytes)
331328
}
332329

333-
// splitID returns the block height and leaf index from a unique ID
334-
func (a *AvailDA) splitID(id da.ID) (uint32, uint32) {
330+
// SplitID returns the block height and leaf index from a unique ID
331+
func SplitID(id da.ID) (uint32, uint32) {
335332
heightLen := 4
336333
leafIndexLen := 4
337334
heightBytes := id[:heightLen]
@@ -341,7 +338,7 @@ func (a *AvailDA) splitID(id da.ID) (uint32, uint32) {
341338
return blockHeight, leafIndex
342339
}
343340

344-
func (a AvailDA) GetData(blockNumber uint64, index uint) ([]byte, error) {
341+
func (a *DAClient) GetData(blockNumber uint64, index uint) ([]byte, error) {
345342
blockHash, err := a.API.RPC.Chain.GetBlockHash(blockNumber)
346343
if err != nil {
347344
return nil, fmt.Errorf("cannot get block hash", err)

β€Žcelestia.goβ€Ž renamed to β€Žcelestiada/celestia.goβ€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package celestiada
22

33
import (
44
"context"
@@ -28,7 +28,7 @@ type CelestiaDA struct {
2828
}
2929

3030
// NewCelestiaDA returns an instance of CelestiaDA
31-
func NewCelestiaDA(client *rpc.Client, namespace share.Namespace, gasPrice float64, ctx context.Context) *CelestiaDA {
31+
func NewClient(client *rpc.Client, namespace share.Namespace, gasPrice float64, ctx context.Context) *CelestiaDA {
3232
return &CelestiaDA{
3333
client: client,
3434
namespace: namespace,
@@ -181,5 +181,3 @@ func splitID(id da.ID) (uint64, da.Commitment) {
181181
}
182182
return binary.LittleEndian.Uint64(id[:heightLen]), id[heightLen:]
183183
}
184-
185-
var _ da.DA = &CelestiaDA{}

β€Žmain.goβ€Ž renamed to β€Žcmd/blob-server/main.goβ€Ž

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import (
1111
rpc "github.com/celestiaorg/celestia-node/api/rpc/client"
1212
"github.com/celestiaorg/celestia-node/share"
1313
"github.com/joho/godotenv"
14-
"github.com/rollkit/go-da"
1514

1615
"github.com/cenkalti/backoff/v4"
1716
"github.com/gin-gonic/gin"
17+
"github.com/rollkit/go-da"
18+
"github.com/stackrlabs/go-daash/availda"
19+
"github.com/stackrlabs/go-daash/celestiada"
20+
"github.com/stackrlabs/go-daash/eigenda"
1821
)
1922

2023
// Constants
@@ -28,15 +31,15 @@ func main() {
2831
router := gin.Default()
2932
ctx := context.Background()
3033

31-
envFile, err := godotenv.Read(".env")
34+
envFile, err := godotenv.Read("../../.env") // read from root
3235
if err != nil {
3336
fmt.Println("Error reading .env file")
3437

3538
return
3639
}
3740

3841
// Initialise Avail DA client
39-
avail, err := NewAvailDA()
42+
avail, err := availda.New("./avail-config.json")
4043
if err != nil {
4144
fmt.Printf("failed to create avail client: %v", err)
4245
}
@@ -60,10 +63,10 @@ func main() {
6063
log.Fatalln("invalid hex value of a namespace:", err)
6164
}
6265
namespace, err := share.NewBlobNamespaceV0(nsBytes)
63-
celestia := NewCelestiaDA(client, namespace, -1, ctx)
66+
celestia := celestiada.NewClient(client, namespace, -1, ctx)
6467

6568
// Initalise EigenDA client
66-
eigen, err := NewEigendaDAClient(EigenDaRpcUrl, time.Second*90, time.Second*5)
69+
eigen, err := eigenda.New(EigenDaRpcUrl, time.Second*90, time.Second*5)
6770
if err != nil {
6871
fmt.Printf("failed to create eigen client: %v", err)
6972
}
@@ -103,7 +106,7 @@ func main() {
103106
return
104107
}
105108
c.JSON(http.StatusOK, gin.H{
106-
"message": "DAaaaSh",
109+
"message": "Blob daashed and posted to " + daName + " πŸƒ",
107110
"ids": ids,
108111
"proofs": proofs,
109112
})

β€Ždaash.goβ€Ž

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package daash
2+
3+
import (
4+
"time"
5+
6+
"github.com/cenkalti/backoff"
7+
"github.com/rollkit/go-da"
8+
"github.com/stackrlabs/go-daash/availda"
9+
"github.com/stackrlabs/go-daash/eigenda"
10+
)
11+
12+
type DAType string
13+
14+
const (
15+
Avail DAType = "avail"
16+
Eigen DAType = "eigen"
17+
)
18+
19+
type DAManager struct {
20+
Clients map[DAType]da.DA
21+
}
22+
23+
// Initialize all the DA clients
24+
func (d *DAManager) Init(availConfigPath string) error {
25+
d.Clients = make(map[DAType]da.DA)
26+
var err error
27+
// Initialize Avail
28+
var avail da.DA
29+
err = backoff.Retry(func() error {
30+
avail, err = availda.New(availConfigPath)
31+
return err //nolint: wrapcheck
32+
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5))
33+
if err != nil {
34+
return err
35+
}
36+
d.Clients[Avail] = avail
37+
38+
// Initialize Eigen
39+
eigen, err := eigenda.New("disperser-goerli.eigenda.xyz:443", time.Second*90, time.Second*5)
40+
if err != nil {
41+
return err
42+
}
43+
d.Clients[Eigen] = eigen
44+
45+
// TODO: Initialize Celestia
46+
47+
return nil
48+
}

β€Ždisperser.pb.goβ€Ž renamed to β€Žeigenda/disperser.pb.goβ€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ždisperser_grpc.pb.goβ€Ž renamed to β€Žeigenda/disperser_grpc.pb.goβ€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)