-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcelestia_app.go
More file actions
232 lines (200 loc) · 6.16 KB
/
celestia_app.go
File metadata and controls
232 lines (200 loc) · 6.16 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package fetch
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
cometpb "github.com/evstack/apex/pkg/api/grpc/gen/cosmos/base/tendermint/v1beta1"
"github.com/evstack/apex/pkg/types"
)
// CelestiaAppFetcher implements DataFetcher using Cosmos SDK gRPC endpoints
// exposed by celestia-app (consensus node). This enables indexing without
// a Celestia DA node.
type CelestiaAppFetcher struct {
conn *grpc.ClientConn
client cometpb.ServiceClient
log zerolog.Logger
mu sync.Mutex
closed bool
cancelSub context.CancelFunc
}
// bearerCreds implements grpc.PerRPCCredentials for bearer token auth.
type bearerCreds struct {
metadata map[string]string // cached; avoids allocation per RPC
}
func newBearerCreds(token string) bearerCreds {
return bearerCreds{metadata: map[string]string{"authorization": "Bearer " + token}}
}
func (b bearerCreds) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error) {
return b.metadata, nil
}
func (b bearerCreds) RequireTransportSecurity() bool { return false }
// NewCelestiaAppFetcher creates a fetcher that reads from celestia-app's
// Cosmos SDK gRPC endpoint. No connection is established at construction time.
func NewCelestiaAppFetcher(grpcAddr, authToken string, log zerolog.Logger) (*CelestiaAppFetcher, error) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
if authToken != "" {
opts = append(opts, grpc.WithPerRPCCredentials(newBearerCreds(authToken)))
}
conn, err := grpc.NewClient(grpcAddr, opts...)
if err != nil {
return nil, fmt.Errorf("create gRPC client: %w", err)
}
return &CelestiaAppFetcher{
conn: conn,
client: cometpb.NewServiceClient(conn),
log: log.With().Str("component", "celestia-app-fetcher").Logger(),
}, nil
}
// newCelestiaAppFetcherFromClient creates a fetcher from an existing gRPC
// client connection. Used in tests with bufconn.
func newCelestiaAppFetcherFromClient(conn *grpc.ClientConn, log zerolog.Logger) *CelestiaAppFetcher {
return &CelestiaAppFetcher{
conn: conn,
client: cometpb.NewServiceClient(conn),
log: log.With().Str("component", "celestia-app-fetcher").Logger(),
}
}
// GetHeader fetches a block at the given height and returns a Header.
func (f *CelestiaAppFetcher) GetHeader(ctx context.Context, height uint64) (*types.Header, error) {
hdr, _, err := f.GetHeightData(ctx, height, nil)
if err != nil {
return nil, err
}
return hdr, nil
}
// GetBlobs fetches a block and extracts blobs matching the given namespaces.
func (f *CelestiaAppFetcher) GetBlobs(ctx context.Context, height uint64, namespaces []types.Namespace) ([]types.Blob, error) {
_, blobs, err := f.GetHeightData(ctx, height, namespaces)
if err != nil {
return nil, err
}
return blobs, nil
}
// GetHeightData fetches header and namespace-filtered blobs for a single height.
func (f *CelestiaAppFetcher) GetHeightData(ctx context.Context, height uint64, namespaces []types.Namespace) (*types.Header, []types.Blob, error) {
resp, err := f.client.GetBlockByHeight(ctx, &cometpb.GetBlockByHeightRequest{
Height: int64(height),
})
if err != nil {
return nil, nil, fmt.Errorf("get block at height %d: %w", height, err)
}
hdr, err := mapBlockResponse(resp.BlockId, resp.Block)
if err != nil {
return nil, nil, err
}
if len(namespaces) == 0 {
return hdr, nil, nil
}
if resp.Block == nil || resp.Block.Data == nil {
return hdr, nil, nil
}
txs := resp.Block.Data.Txs
blobs, err := extractBlobsFromBlock(txs, namespaces, height)
if err != nil {
return nil, nil, fmt.Errorf("extract blobs at height %d: %w", height, err)
}
if len(blobs) == 0 {
return hdr, nil, nil
}
return hdr, blobs, nil
}
// GetNetworkHead returns the header at the latest block height.
func (f *CelestiaAppFetcher) GetNetworkHead(ctx context.Context) (*types.Header, error) {
resp, err := f.client.GetLatestBlock(ctx, &cometpb.GetLatestBlockRequest{})
if err != nil {
return nil, fmt.Errorf("get latest block: %w", err)
}
return mapBlockResponse(resp.BlockId, resp.Block)
}
// SubscribeHeaders polls GetLatestBlock at 1s intervals and emits new headers
// when the height advances. The coordinator already handles gaps, so polling
// is sufficient.
func (f *CelestiaAppFetcher) SubscribeHeaders(ctx context.Context) (<-chan *types.Header, error) {
subCtx, cancel := context.WithCancel(ctx)
f.mu.Lock()
if f.closed {
f.mu.Unlock()
cancel()
return nil, fmt.Errorf("fetcher is closed")
}
if f.cancelSub != nil {
f.cancelSub()
}
f.cancelSub = cancel
f.mu.Unlock()
out := make(chan *types.Header, 64)
go f.pollLoop(subCtx, out)
return out, nil
}
func (f *CelestiaAppFetcher) pollLoop(ctx context.Context, out chan<- *types.Header) {
defer close(out)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
var lastHeight uint64
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
hdr, err := f.GetNetworkHead(ctx)
if err != nil {
if ctx.Err() != nil {
return
}
f.log.Warn().Err(err).Msg("poll latest block failed")
continue
}
if hdr.Height <= lastHeight {
continue
}
lastHeight = hdr.Height
select {
case out <- hdr:
case <-ctx.Done():
return
}
}
}
}
// Close cancels any active subscription and closes the gRPC connection.
func (f *CelestiaAppFetcher) Close() error {
f.mu.Lock()
defer f.mu.Unlock()
if f.closed {
return nil
}
f.closed = true
if f.cancelSub != nil {
f.cancelSub()
}
return f.conn.Close()
}
// mapBlockResponse converts a gRPC block response into a types.Header.
func mapBlockResponse(blockID *cometpb.BlockID, block *cometpb.Block) (*types.Header, error) {
if block == nil || block.Header == nil {
return nil, fmt.Errorf("nil block or header in response")
}
if blockID == nil {
return nil, fmt.Errorf("nil block_id in response")
}
hdr := block.Header
t := hdr.Time.AsTime()
raw, err := json.Marshal(block)
if err != nil {
return nil, fmt.Errorf("marshal raw header: %w", err)
}
return &types.Header{
Height: uint64(hdr.Height),
Hash: blockID.Hash,
DataHash: hdr.DataHash,
Time: t,
RawHeader: raw,
}, nil
}