Skip to content

Commit 71fe78f

Browse files
committed
refactor: switch to custom DA client interface (#11)
* refactor: switch to custom DA client interface - <daName>.go -> client.go for consistency * chore: add docker-compose and smol tweaks * refactor: remove 'da' suffix and rename to `client.go` * forge install: blobstream-contracts v4.1.0 * refactor: DABuilder -> ClientBuilder
1 parent 3343d98 commit 71fe78f

File tree

39 files changed

+392
-194
lines changed

39 files changed

+392
-194
lines changed

.gitmodules

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
[submodule "celestiada/verify/contracts/lib/forge-std"]
2-
path = celestiada/verify/contracts/lib/forge-std
1+
[submodule "celestia/verify/contracts/lib/forge-std"]
2+
path = celestia/verify/contracts/lib/forge-std
33
url = https://github.com/foundry-rs/forge-std
4-
[submodule "celestiada/verify/contracts/lib/blobstream-contracts"]
5-
path = celestiada/verify/contracts/lib/blobstream-contracts
6-
url = https://github.com/celestiaorg/blobstream-contracts
7-
[submodule "availda/verify/contracts/lib/forge-std"]
8-
path = availda/verify/contracts/lib/forge-std
4+
[submodule "avail/verify/contracts/lib/forge-std"]
5+
path = avail/verify/contracts/lib/forge-std
96
url = https://github.com/foundry-rs/forge-std
7+
[submodule "celestia/verify/contracts/lib/blobstream-contracts"]
8+
path = celestia/verify/contracts/lib/blobstream-contracts
9+
url = https://github.com/celestiaorg/blobstream-contracts

availda/availda.go renamed to avail/client.go

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

33
import (
44
"context"
@@ -13,12 +13,10 @@ import (
1313

1414
"log"
1515

16-
"encoding/binary"
17-
1816
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
1917
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
2018
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
21-
"github.com/rollkit/go-da"
19+
"github.com/stackrlabs/go-daash/da"
2220
"go.uber.org/zap"
2321
"golang.org/x/crypto/sha3"
2422
)
@@ -45,7 +43,7 @@ type DataProof struct {
4543
} `json:"roots"`
4644
}
4745

48-
type DAClient struct {
46+
type Client struct {
4947
Config Config
5048
API *gsrpc.SubstrateAPI
5149
Meta *types.Metadata
@@ -58,8 +56,8 @@ type DAClient struct {
5856
}
5957

6058
// Returns a newly initalised Avail DA client
61-
func New(configPath string) (*DAClient, error) {
62-
a := DAClient{}
59+
func NewClient(configPath string) (*Client, error) {
60+
a := Client{}
6361
err := a.Config.GetConfig(configPath)
6462
if err != nil {
6563
return nil, fmt.Errorf("cannot get config", err)
@@ -112,14 +110,14 @@ func New(configPath string) (*DAClient, error) {
112110
}
113111

114112
// MaxBlobSize returns the max blob size
115-
func (c *DAClient) MaxBlobSize(ctx context.Context) (uint64, error) {
113+
func (c *Client) MaxBlobSize(ctx context.Context) (uint64, error) {
116114
var maxBlobSize uint64 = 64 * 64 * 500
117115
return maxBlobSize, nil
118116
}
119117

120118
// Submit a list of blobs to Avail DA
121119
// Currently, we submit to a trusted RPC Avail node. In the future, we will submit via an Avail light client.
122-
func (a *DAClient) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
120+
func (a *Client) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
123121
// TODO: Add support for multiple blobs
124122
daBlob := daBlobs[0]
125123
log.Println("data", zap.Any("data", daBlob))
@@ -243,7 +241,7 @@ out:
243241
}
244242
dataProof := dataProofResp.Result.DataProof
245243
// NOTE: Substrate's BlockNumber type is an alias for u32 type, which is uint32
246-
blobID := MakeID(uint32(block.Block.Header.Number), extIndex)
244+
blobID := ID{Height: uint64(block.Block.Header.Number), ExtIndex: uint32(extIndex)}
247245
blobIDs := make([]da.ID, 1)
248246
blobIDs[0] = blobID
249247

@@ -257,7 +255,7 @@ out:
257255
}
258256

259257
// Get returns Blob for each given ID, or an error.
260-
func (a *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
258+
func (a *Client) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
261259
// TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
262260
ext, err := a.GetExtrinsic(ids[0])
263261
if err != nil {
@@ -269,19 +267,19 @@ func (a *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
269267
}
270268

271269
// GetIDs returns IDs of all Blobs located in DA at given height.
272-
func (a *DAClient) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
270+
func (a *Client) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
273271
// TODO: Need to implement this
274272
return nil, nil
275273
}
276274

277275
// Commit creates a Commitment for each given Blob.
278-
func (a *DAClient) Commit(ctx context.Context, daBlobs []da.Blob) ([]da.Commitment, error) {
276+
func (a *Client) Commit(ctx context.Context, daBlobs []da.Blob) ([]da.Commitment, error) {
279277
// TODO: Need to implement this
280278
return nil, nil
281279
}
282280

283281
// GetProofs returns the proofs for the given IDs
284-
func (a *DAClient) GetProof(ctx context.Context, blockHeight uint32, extIdx int) (DataProofRPCResponse, error) {
282+
func (a *Client) GetProof(ctx context.Context, blockHeight uint32, extIdx int) (DataProofRPCResponse, error) {
285283
var dataProofResp DataProofRPCResponse
286284
blockHash, err := a.API.RPC.Chain.GetBlockHash(uint64(blockHeight))
287285
if err != nil {
@@ -311,7 +309,7 @@ func (a *DAClient) GetProof(ctx context.Context, blockHeight uint32, extIdx int)
311309
}
312310

313311
// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
314-
func (c *DAClient) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof) ([]bool, error) {
312+
func (c *Client) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof) ([]bool, error) {
315313
// TODO: Need to implement this
316314
return nil, nil
317315
}
@@ -327,7 +325,7 @@ func (b BatchDAData) IsEmpty() bool {
327325
return reflect.DeepEqual(b, BatchDAData{})
328326
}
329327

330-
func (a *DAClient) GetAccountNextIndex() (types.UCompact, error) {
328+
func (a *Client) GetAccountNextIndex() (types.UCompact, error) {
331329
// TODO: Add context to the request
332330
resp, err := http.Post(a.Config.HttpApiURL, "application/json", strings.NewReader(fmt.Sprintf("{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"system_accountNextIndex\",\"params\":[\"%v\"]}", a.KeyringPair.Address))) //nolint: noctx
333331
if err != nil {
@@ -348,25 +346,9 @@ func (a *DAClient) GetAccountNextIndex() (types.UCompact, error) {
348346
return types.NewUCompactFromUInt(uint64(accountNextIndex.Result)), nil
349347
}
350348

351-
// makeID creates a unique ID to reference a blob on Avail
352-
func MakeID(blockHeight uint32, extIndex int) da.ID {
353-
// Serialise height and leaf index to binary
354-
heightLen := 4
355-
heightBytes := make([]byte, heightLen)
356-
binary.LittleEndian.PutUint32(heightBytes, blockHeight)
357-
extIndexBytes := make([]byte, 4)
358-
binary.LittleEndian.PutUint32(extIndexBytes, uint32(extIndex))
359-
return da.ID(append(heightBytes, extIndexBytes...))
360-
}
361-
362-
// SplitID returns the block height and leaf index from a unique ID
363-
func SplitID(id da.ID) (uint32, uint32) {
364-
heightLen := 4
365-
heightBytes := id[:heightLen]
366-
extIdxBytes := id[heightLen:]
367-
blockHeight := binary.LittleEndian.Uint32(heightBytes)
368-
extIdx := binary.LittleEndian.Uint32(extIdxBytes)
369-
return blockHeight, extIdx
349+
type ID struct {
350+
Height uint64 `json:"blockHeight"`
351+
ExtIndex uint32 `json:"extIdx"`
370352
}
371353

372354
type Config struct {
@@ -400,15 +382,18 @@ func (c *Config) GetConfig(configFileName string) error {
400382
return nil
401383
}
402384

403-
func (a *DAClient) GetExtrinsic(id da.ID) (types.Extrinsic, error) {
404-
blockHeight, extIdx := SplitID(id)
405-
blockHash, err := a.API.RPC.Chain.GetBlockHash(uint64(blockHeight))
385+
func (a *Client) GetExtrinsic(id da.ID) (types.Extrinsic, error) {
386+
availID, ok := id.(ID)
387+
if !ok {
388+
return types.Extrinsic{}, fmt.Errorf("invalid ID")
389+
}
390+
blockHash, err := a.API.RPC.Chain.GetBlockHash(uint64(availID.Height))
406391
if err != nil {
407392
log.Fatalf("cannot get block hash:%w", err)
408393
}
409394
block, err := a.API.RPC.Chain.GetBlock(blockHash)
410395
if err != nil {
411396
log.Fatalf("cannot get block:%w", err)
412397
}
413-
return block.Block.Extrinsics[extIdx], nil
398+
return block.Block.Extrinsics[availID.ExtIndex], nil
414399
}
File renamed without changes.

availda/verify/verifier.go renamed to avail/verify/verifier.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
"github.com/ethereum/go-ethereum/common"
1212
"github.com/ethereum/go-ethereum/ethclient"
13-
"github.com/rollkit/go-da"
14-
"github.com/stackrlabs/go-daash/availda"
15-
"github.com/stackrlabs/go-daash/availda/verify/bindings/vectorverifier"
13+
"github.com/stackrlabs/go-daash/avail"
14+
"github.com/stackrlabs/go-daash/avail/verify/bindings/vectorverifier"
15+
"github.com/stackrlabs/go-daash/da"
1616
)
1717

1818
const (
@@ -21,7 +21,7 @@ const (
2121

2222
// Verfifier is used to verify availability of Avail blobs on EVM chains
2323
type Verifier struct {
24-
daClient *availda.DAClient
24+
daClient *avail.Client
2525
ethClient *ethclient.Client
2626
vectorXContract common.Address
2727
bridgeContract common.Address
@@ -43,7 +43,7 @@ type SuccinctAPIResponse struct {
4343
} `json:"data"`
4444
}
4545

46-
func NewVerifier(client *availda.DAClient, ethEndpoint string, bridgeContract string, verifierContract string, vectorXContract string, availNetwork string) (*Verifier, error) {
46+
func NewVerifier(client *avail.Client, ethEndpoint string, bridgeContract string, verifierContract string, vectorXContract string, availNetwork string) (*Verifier, error) {
4747
ethClient, err := ethclient.Dial(ethEndpoint)
4848
if err != nil {
4949
return nil, fmt.Errorf("failed to create eth client: %w", err)
@@ -79,19 +79,18 @@ func (d *Verifier) IsDataAvailable(blockHeight uint64, extIndex uint64) (bool, e
7979
}
8080

8181
// IsDataIncluded verifies that the blob data corresponding to the given block height and external index is available on DA
82-
func (d *Verifier) IsDataIncluded(blockHeight uint64, extIndex uint64) (bool, error) {
83-
id := availda.MakeID(uint32(blockHeight), int(extIndex))
82+
func (d *Verifier) IsDataIncluded(id avail.ID) (bool, error) {
8483
blobs, err := d.daClient.Get(context.Background(), []da.ID{id})
8584
if err != nil {
8685
return false, fmt.Errorf("failed to get blob data: %w", err)
8786
}
8887
fmt.Println("size of blob data:", len(blobs[0]))
8988

90-
dataProof, err := d.daClient.GetProof(context.Background(), uint32(blockHeight), int(extIndex))
89+
dataProof, err := d.daClient.GetProof(context.Background(), uint32(id.Height), int(id.ExtIndex))
9190
if err != nil {
9291
return false, fmt.Errorf("failed to get data proof: %w", err)
9392
}
94-
proof, err := d.GetAggregatedProof(dataProof, blockHeight)
93+
proof, err := d.GetAggregatedProof(dataProof, id.Height)
9594
if err != nil {
9695
return false, fmt.Errorf("failed to get aggregated proof: %w", err)
9796
}
@@ -106,7 +105,7 @@ func (d *Verifier) IsDataIncluded(blockHeight uint64, extIndex uint64) (bool, er
106105
return success, nil
107106
}
108107

109-
func (d *Verifier) GetAggregatedProof(dataProof availda.DataProofRPCResponse, blockHeight uint64) (*vectorverifier.IAvailBridgeMerkleProofInput, error) {
108+
func (d *Verifier) GetAggregatedProof(dataProof avail.DataProofRPCResponse, blockHeight uint64) (*vectorverifier.IAvailBridgeMerkleProofInput, error) {
110109
chainID, err := d.ethClient.ChainID(context.Background())
111110
if err != nil {
112111
return nil, fmt.Errorf("cannot get chain id:%w", err)
Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)