Skip to content

Commit 894d104

Browse files
committed
feat(daash): add human-readable verison og BlobID
1 parent a5f46e7 commit 894d104

File tree

4 files changed

+81
-55
lines changed

4 files changed

+81
-55
lines changed

availda/availda.go

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ out:
254254
// Get returns Blob for each given ID, or an error.
255255
func (a *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
256256
// TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
257-
blockHeight, extHash := SplitID(ids[0])
258-
data, err := a.GetData(uint64(blockHeight), extHash)
257+
ext, err := a.GetExtrinsic(ids[0])
259258
if err != nil {
260-
return nil, fmt.Errorf("cannot get data", err)
259+
return nil, fmt.Errorf("cannot get extrinsic", err)
261260
}
262-
log.Println("📥 received data:%+v", zap.Any("data", data))
263-
return []da.Blob{data}, nil
261+
blobData := ext.Method.Args[2:]
262+
log.Println("📥 received data:%+v", zap.Any("data", blobData))
263+
return []da.Blob{blobData}, nil
264264
}
265265

266266
// GetIDs returns IDs of all Blobs located in DA at given height.
@@ -325,38 +325,13 @@ func makeID(blockHeight uint32, extIndex int) da.ID {
325325
}
326326

327327
// SplitID returns the block height and leaf index from a unique ID
328-
func SplitID(id da.ID) (uint32, string) {
328+
func SplitID(id da.ID) (uint32, uint32) {
329329
heightLen := 4
330330
heightBytes := id[:heightLen]
331-
extHashBytes := id[heightLen:]
331+
extIdxBytes := id[heightLen:]
332332
blockHeight := binary.LittleEndian.Uint32(heightBytes)
333-
return blockHeight, string(extHashBytes)
334-
}
335-
336-
func (a *DAClient) GetData(blockNumber uint64, extHash string) ([]byte, error) {
337-
blockHash, err := a.API.RPC.Chain.GetBlockHash(blockNumber)
338-
if err != nil {
339-
return nil, fmt.Errorf("cannot get block hash", err)
340-
}
341-
342-
block, err := a.API.RPC.Chain.GetBlock(blockHash)
343-
if err != nil {
344-
return nil, fmt.Errorf("cannot get block", err)
345-
}
346-
347-
var data []byte
348-
for _, ext := range block.Block.Extrinsics {
349-
extBytes, err := json.Marshal(ext)
350-
if err != nil {
351-
return nil, fmt.Errorf("cannot marshal extrinsic", err)
352-
}
353-
if string(extBytes) == extHash {
354-
data = ext.Method.Args[2:]
355-
break
356-
}
357-
}
358-
359-
return data, nil
333+
extIdx := binary.LittleEndian.Uint32(extIdxBytes)
334+
return blockHeight, extIdx
360335
}
361336

362337
type Config struct {
@@ -388,3 +363,16 @@ func (c *Config) GetConfig(configFileName string) error {
388363

389364
return nil
390365
}
366+
367+
func (a *DAClient) GetExtrinsic(id da.ID) (types.Extrinsic, error) {
368+
blockHeight, extIdx := SplitID(id)
369+
blockHash, err := a.API.RPC.Chain.GetBlockHash(uint64(blockHeight))
370+
if err != nil {
371+
log.Fatalf("cannot get block hash:%w", err)
372+
}
373+
block, err := a.API.RPC.Chain.GetBlock(blockHash)
374+
if err != nil {
375+
log.Fatalf("cannot get block:%w", err)
376+
}
377+
return block.Block.Extrinsics[extIdx], nil
378+
}

celestiada/celestia.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (c *DAClient) MaxBlobSize(ctx context.Context) (uint64, error) {
6464
func (c *DAClient) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
6565
var blobs []da.Blob
6666
for _, id := range ids {
67-
height, commitment := splitID(id)
67+
height, commitment := SplitID(id)
6868
blob, err := c.client.Blob.Get(ctx, height, c.Namespace, commitment)
6969
if err != nil {
7070
return nil, err
@@ -171,7 +171,7 @@ func (c *DAClient) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proo
171171
proofs = append(proofs, proof)
172172
}
173173
for i, id := range ids {
174-
height, commitment := splitID(id)
174+
height, commitment := SplitID(id)
175175
// TODO(tzdybal): for some reason, if proof doesn't match commitment, API returns (false, "blob: invalid proof")
176176
// but analysis of the code in celestia-node implies this should never happen - maybe it's caused by openrpc?
177177
// there is no way of gently handling errors here, but returned value is fine for us
@@ -193,7 +193,7 @@ func makeID(height uint64, commitment da.Commitment) da.ID {
193193
return id
194194
}
195195

196-
func splitID(id da.ID) (uint64, da.Commitment) {
196+
func SplitID(id da.ID) (uint64, da.Commitment) {
197197
if len(id) <= heightLen {
198198
return 0, nil
199199
}

cmd/blob-server/main.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"github.com/gin-gonic/gin"
1616
"github.com/rollkit/go-da"
1717
"github.com/stackrlabs/go-daash"
18-
"github.com/stackrlabs/go-daash/availda"
19-
"github.com/stackrlabs/go-daash/celestiada"
2018
)
2119

2220
// Constants
@@ -151,21 +149,6 @@ func generateJobID() string {
151149
return randomHexString
152150
}
153151

154-
func getSuccessLink(daClient da.DA, ids []da.ID) string {
155-
switch daClient := daClient.(type) {
156-
case *celestiada.DAClient:
157-
namespace := daClient.Namespace.String()
158-
// remove 2 leading zero of namespace
159-
namespace = namespace[2:]
160-
return fmt.Sprintf("https://mocha-4.celenium.io/namespace/%s", namespace)
161-
case *availda.DAClient:
162-
_, extHash := availda.SplitID(ids[0])
163-
return fmt.Sprintf("https://goldberg.avail.tools/#/extrinsics/decode/%s", extHash)
164-
default:
165-
return ""
166-
}
167-
}
168-
169152
func run(ctx context.Context, b *BlobServer, job Job) {
170153
var jobStatus map[string]any
171154
ids, proofs, err := postToDA(ctx, job.Data, b.Daasher.Clients[job.Layer])
@@ -175,7 +158,10 @@ func run(ctx context.Context, b *BlobServer, job Job) {
175158
"error": err,
176159
}
177160
} else {
178-
successLink := getSuccessLink(b.Daasher.Clients[job.Layer], ids)
161+
successLink, err := daash.GetExplorerLink(b.Daasher.Clients[job.Layer], ids)
162+
if err != nil {
163+
log.Fatalf("cannot get explorer link: %v", err)
164+
}
179165
jobStatus = gin.H{
180166
"status": "Blob daashed and posted to " + string(job.Layer) + " 🏃",
181167
"ids": ids,

daash.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package daash
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"log"
8+
"strings"
79
"time"
810

911
"github.com/cenkalti/backoff"
@@ -96,3 +98,53 @@ func (d *DABuilder) InitClients(ctx context.Context, layers []DALayer, availConf
9698
}
9799
return d, nil
98100
}
101+
102+
func GetHumanReadableID(id da.ID, daLayer DALayer) any {
103+
switch daLayer {
104+
case Avail:
105+
blockHeight, extIdx := availda.SplitID(id)
106+
return struct {
107+
BlockHeight uint32 `json:"blockHeight"`
108+
ExtIdx uint32 `json:"extIdx"`
109+
}{
110+
BlockHeight: blockHeight,
111+
ExtIdx: extIdx,
112+
}
113+
case Celestia:
114+
blockHeight, commitment := celestiada.SplitID(id)
115+
return struct {
116+
BlockHeight uint64 `json:"blockHeight"`
117+
Commitment da.Commitment `json:"commitment"`
118+
}{
119+
BlockHeight: blockHeight,
120+
Commitment: commitment,
121+
}
122+
default:
123+
return ""
124+
}
125+
}
126+
127+
func GetExplorerLink(client da.DA, ids []da.ID) (string, error) {
128+
switch daClient := client.(type) {
129+
case *celestiada.DAClient:
130+
namespace := daClient.Namespace.String()
131+
// remove 2 leading zero of namespace
132+
namespace = namespace[2:]
133+
return fmt.Sprintf("https://mocha-4.celenium.io/namespace/%s", namespace), nil
134+
case *availda.DAClient:
135+
ext, err := daClient.GetExtrinsic(ids[0])
136+
if err != nil {
137+
return "", err
138+
}
139+
extBytes, err := json.Marshal(ext)
140+
if err != nil {
141+
return "", err
142+
}
143+
// Strip string of any leading or following quotes
144+
extString := strings.Trim(string(extBytes), "\"")
145+
fmt.Println(extString)
146+
return fmt.Sprintf("https://goldberg.avail.tools/#/extrinsics/decode/%s", extString), nil
147+
default:
148+
return "", nil
149+
}
150+
}

0 commit comments

Comments
 (0)