-
Notifications
You must be signed in to change notification settings - Fork 2
/
celestia_storage.go
68 lines (60 loc) · 1.81 KB
/
celestia_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package celestia
import (
"context"
"encoding/hex"
"fmt"
"time"
client "github.com/celestiaorg/celestia-openrpc"
plasma "github.com/ethereum-optimism/optimism/op-plasma"
"github.com/ethereum/go-ethereum/log"
)
const VersionByte = 0x0c
type CelestiaConfig struct {
URL string
AuthToken string
Namespace []byte
}
// CelestiaStore implements DAStorage with celestia backend
type CelestiaStore struct {
Log log.Logger
GetTimeout time.Duration
Namespace []byte
Client *client.Client
}
// NewCelestiaStore returns a celestia store.
func NewCelestiaStore(cfg CelestiaConfig) *CelestiaStore {
Log := log.New()
ctx := context.Background()
client, err := client.NewClient(ctx, cfg.URL, cfg.AuthToken)
if err != nil {
Log.Crit(err.Error())
}
return &CelestiaStore{
Log: Log,
Client: client,
GetTimeout: time.Minute,
Namespace: cfg.Namespace,
}
}
func (d *CelestiaStore) Get(ctx context.Context, key []byte) ([]byte, error) {
log.Info("celestia: blob request", "id", hex.EncodeToString(key))
ctx, cancel := context.WithTimeout(context.Background(), d.GetTimeout)
blobs, err := d.Client.DA.Get(ctx, [][]byte{key[2:]}, d.Namespace)
cancel()
if err != nil || len(blobs) == 0 {
return nil, fmt.Errorf("celestia: failed to resolve frame: %w", err)
}
if len(blobs) != 1 {
d.Log.Warn("celestia: unexpected length for blobs", "expected", 1, "got", len(blobs))
}
return blobs[0], nil
}
func (d *CelestiaStore) Put(ctx context.Context, data []byte) ([]byte, error) {
ids, err := d.Client.DA.Submit(ctx, [][]byte{data}, -1, d.Namespace)
if err == nil && len(ids) == 1 {
d.Log.Info("celestia: blob successfully submitted", "id", hex.EncodeToString(ids[0]))
commitment := plasma.NewGenericCommitment(append([]byte{VersionByte}, ids[0]...))
return commitment.Encode(), nil
}
return nil, err
}