-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
54 lines (43 loc) · 1.24 KB
/
client.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
package da
import (
"context"
"fmt"
celestia "github.com/rollkit/celestia-openrpc"
)
type Client interface {
// NOTE: range is end inclusive at the moment
DataCommitments(context.Context, uint64, uint64) ([][]byte, error)
LatestSampledHeight(ctx context.Context) (uint64, error)
}
type celestiaClient struct {
*celestia.Client
}
func NewClient(client *celestia.Client) Client {
return celestiaClient{client}
}
func (c celestiaClient) DataCommitments(
ctx context.Context,
startHeight,
endHeight uint64,
) ([][]byte, error) {
latestHeader, err := c.Client.Header.GetByHeight(ctx, startHeight)
if err != nil {
return nil, fmt.Errorf("getting latest commitment DA header height %d: %w", startHeight, err)
}
hdrs, err := c.Client.Header.GetVerifiedRangeByHeight(ctx, latestHeader, endHeight+1)
if err != nil {
return nil, fmt.Errorf("getting DA header range(%d;%d] for sampling: %w", latestHeader.Height(), endHeight, err)
}
roots := make([][]byte, len(hdrs))
for i, hdr := range hdrs {
roots[i] = hdr.DataHash
}
return roots, nil
}
func (c celestiaClient) LatestSampledHeight(ctx context.Context) (uint64, error) {
stats, err := c.Client.DAS.SamplingStats(ctx)
if err != nil {
return 0, err
}
return stats.SampledChainHead, nil
}