-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-rpc-server-car.go
503 lines (464 loc) · 14.4 KB
/
cmd-rpc-server-car.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package main
import (
"bufio"
"context"
"fmt"
"io"
"time"
"github.com/gagliardetto/solana-go"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/util"
carv2 "github.com/ipld/go-car/v2"
"github.com/patrickmn/go-cache"
"github.com/rpcpool/yellowstone-faithful/compactindex"
"github.com/rpcpool/yellowstone-faithful/compactindex36"
"github.com/rpcpool/yellowstone-faithful/gsfa"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
"github.com/rpcpool/yellowstone-faithful/iplddecoders"
"github.com/sourcegraph/jsonrpc2"
"github.com/urfave/cli/v2"
"github.com/valyala/fasthttp"
"k8s.io/klog/v2"
)
func newCmd_rpcServerCar() *cli.Command {
var listenOn string
var gsfaOnlySignatures bool
return &cli.Command{
Name: "rpc-server-car",
Description: "Start a Solana JSON RPC that exposes getTransaction and getBlock",
ArgsUsage: "<car-filepath-or-url> <cid-to-offset-index-filepath-or-url> <slot-to-cid-index-filepath-or-url> <sig-to-cid-index-filepath-or-url> <gsfa-index-dir>",
Before: func(c *cli.Context) error {
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Usage: "Listen address",
Value: ":8899",
Destination: &listenOn,
},
&cli.BoolFlag{
Name: "gsfa-only-signatures",
Usage: "gSFA: only return signatures",
Value: false,
Destination: &gsfaOnlySignatures,
},
},
Action: func(c *cli.Context) error {
carFilepath := c.Args().Get(0)
if carFilepath == "" {
return cli.Exit("Must provide a CAR filepath", 1)
}
cidToOffsetIndexFilepath := c.Args().Get(1)
if cidToOffsetIndexFilepath == "" {
return cli.Exit("Must provide a CID-to-offset index filepath/url", 1)
}
slotToCidIndexFilepath := c.Args().Get(2)
if slotToCidIndexFilepath == "" {
return cli.Exit("Must provide a slot-to-CID index filepath/url", 1)
}
sigToCidIndexFilepath := c.Args().Get(3)
if sigToCidIndexFilepath == "" {
return cli.Exit("Must provide a signature-to-CID index filepath/url", 1)
}
cidToOffsetIndexFile, err := openIndexStorage(
c.Context,
cidToOffsetIndexFilepath,
DebugMode,
)
if err != nil {
return fmt.Errorf("failed to open index file: %w", err)
}
defer cidToOffsetIndexFile.Close()
cidToOffsetIndex, err := compactindex.Open(cidToOffsetIndexFile)
if err != nil {
return fmt.Errorf("failed to open index: %w", err)
}
slotToCidIndexFile, err := openIndexStorage(
c.Context,
slotToCidIndexFilepath,
DebugMode,
)
if err != nil {
return fmt.Errorf("failed to open index file: %w", err)
}
defer slotToCidIndexFile.Close()
slotToCidIndex, err := compactindex36.Open(slotToCidIndexFile)
if err != nil {
return fmt.Errorf("failed to open index: %w", err)
}
sigToCidIndexFile, err := openIndexStorage(
c.Context,
sigToCidIndexFilepath,
DebugMode,
)
if err != nil {
return fmt.Errorf("failed to open index file: %w", err)
}
defer sigToCidIndexFile.Close()
sigToCidIndex, err := compactindex36.Open(sigToCidIndexFile)
if err != nil {
return fmt.Errorf("failed to open index: %w", err)
}
localCarReader, remoteCarReader, err := openCarStorage(c.Context, carFilepath)
if err != nil {
return fmt.Errorf("failed to open CAR file: %w", err)
}
var gsfaIndex *gsfa.GsfaReader
gsfaIndexDir := c.Args().Get(4)
if gsfaIndexDir != "" {
gsfaIndex, err = gsfa.NewGsfaReader(gsfaIndexDir)
if err != nil {
return fmt.Errorf("failed to open gsfa index: %w", err)
}
defer gsfaIndex.Close()
}
options := &RpcServerOptions{
ListenOn: listenOn,
GsfaOnlySignatures: gsfaOnlySignatures,
}
return createAndStartRPCServer_withCar(
c.Context,
options,
localCarReader,
remoteCarReader,
cidToOffsetIndex,
slotToCidIndex,
sigToCidIndex,
gsfaIndex,
)
},
}
}
// createAndStartRPCServer_withCar creates and starts a JSON RPC server.
// Data:
// - Nodes: the node data is read from a CAR file (which can be a local file or a remote URL).
// - Indexes: the indexes are read from files (which can be a local file or a remote URL).
//
// The server is backed by a CAR file (meaning that it can only serve the content of the CAR file).
// It blocks until the server is stopped.
// It returns an error if the server fails to start or stops unexpectedly.
// It returns nil if the server is stopped gracefully.
func createAndStartRPCServer_withCar(
ctx context.Context,
options *RpcServerOptions,
carReader *carv2.Reader,
remoteCarReader ReaderAtCloser,
cidToOffsetIndex *compactindex.DB,
slotToCidIndex *compactindex36.DB,
sigToCidIndex *compactindex36.DB,
gsfaReader *gsfa.GsfaReader,
) error {
if options == nil {
panic("options cannot be nil")
}
listenOn := options.ListenOn
ca := cache.New(30*time.Second, 1*time.Minute)
handler := &deprecatedRPCServer{
localCarReader: carReader,
remoteCarReader: remoteCarReader,
cidToOffsetIndex: cidToOffsetIndex,
slotToCidIndex: slotToCidIndex,
sigToCidIndex: sigToCidIndex,
gsfaReader: gsfaReader,
cidToBlockCache: ca,
options: options,
}
h := newRPCHandler_fast(handler)
h = fasthttp.CompressHandler(h)
klog.Infof("RPC server listening on %s", listenOn)
return fasthttp.ListenAndServe(listenOn, h)
}
func createAndStartRPCServer_lassie(
ctx context.Context,
options *RpcServerOptions,
lassieWr *lassieWrapper,
slotToCidIndex *compactindex36.DB,
sigToCidIndex *compactindex36.DB,
gsfaReader *gsfa.GsfaReader,
) error {
if options == nil {
panic("options cannot be nil")
}
listenOn := options.ListenOn
ca := cache.New(30*time.Second, 1*time.Minute)
handler := &deprecatedRPCServer{
lassieFetcher: lassieWr,
slotToCidIndex: slotToCidIndex,
sigToCidIndex: sigToCidIndex,
gsfaReader: gsfaReader,
cidToBlockCache: ca,
options: options,
}
h := newRPCHandler_fast(handler)
h = fasthttp.CompressHandler(h)
klog.Infof("RPC server listening on %s", listenOn)
return fasthttp.ListenAndServe(listenOn, h)
}
type RpcServerOptions struct {
ListenOn string
GsfaOnlySignatures bool
}
type deprecatedRPCServer struct {
lassieFetcher *lassieWrapper
localCarReader *carv2.Reader
remoteCarReader ReaderAtCloser
cidToOffsetIndex *compactindex.DB
slotToCidIndex *compactindex36.DB
sigToCidIndex *compactindex36.DB
gsfaReader *gsfa.GsfaReader
cidToBlockCache *cache.Cache // TODO: prevent OOM
options *RpcServerOptions
}
func getCidCacheKey(off int64, p []byte) string {
return fmt.Sprintf("%d-%d", off, len(p))
}
func (r *deprecatedRPCServer) getNodeFromCache(c cid.Cid) (v []byte, err error, has bool) {
if v, ok := r.cidToBlockCache.Get(c.String()); ok {
return v.([]byte), nil, true
}
return nil, nil, false
}
func (r *deprecatedRPCServer) putNodeInCache(c cid.Cid, data []byte) {
r.cidToBlockCache.Set(c.String(), data, cache.DefaultExpiration)
}
func (s *deprecatedRPCServer) prefetchSubgraph(ctx context.Context, wantedCid cid.Cid) error {
if s.lassieFetcher != nil {
// Fetch the subgraph from lassie
sub, err := s.lassieFetcher.GetSubgraph(ctx, wantedCid)
if err == nil {
// put in cache
return sub.Each(ctx, func(c cid.Cid, data []byte) error {
s.putNodeInCache(c, data)
return nil
})
}
klog.Errorf("failed to get subgraph from lassie: %v", err)
return err
}
return nil
}
func (s *deprecatedRPCServer) GetNodeByCid(ctx context.Context, wantedCid cid.Cid) ([]byte, error) {
{
// try from cache
data, err, has := s.getNodeFromCache(wantedCid)
if err != nil {
return nil, err
}
if has {
return data, nil
}
}
if s.lassieFetcher != nil {
// Fetch the node from lassie.
data, err := s.lassieFetcher.GetNodeByCid(ctx, wantedCid)
if err == nil {
// put in cache
s.putNodeInCache(wantedCid, data)
return data, nil
}
klog.Errorf("failed to get node from lassie: %v", err)
return nil, err
}
// Find CAR file offset for CID in index.
offset, err := s.FindOffsetFromCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to find offset for CID %s: %v", wantedCid, err)
// not found or error
return nil, err
}
return s.GetNodeByOffset(ctx, wantedCid, offset)
}
func (s *deprecatedRPCServer) ReadAtFromCar(ctx context.Context, offset uint64, length uint64) ([]byte, error) {
if s.localCarReader == nil {
// try remote reader
if s.remoteCarReader == nil {
return nil, fmt.Errorf("no CAR reader available")
}
return readSectionFromReaderAt(s.remoteCarReader, offset, length)
}
// Get reader and seek to offset, then read node.
dr, err := s.localCarReader.DataReader()
if err != nil {
klog.Errorf("failed to get data reader: %v", err)
return nil, err
}
dr.Seek(int64(offset), io.SeekStart)
data := make([]byte, length)
_, err = io.ReadFull(dr, data)
if err != nil {
klog.Errorf("failed to read node: %v", err)
return nil, err
}
return data, nil
}
func (s *deprecatedRPCServer) GetNodeByOffset(ctx context.Context, wantedCid cid.Cid, offset uint64) ([]byte, error) {
if s.localCarReader == nil {
// try remote reader
if s.remoteCarReader == nil {
return nil, fmt.Errorf("no CAR reader available")
}
return readNodeFromReaderAt(s.remoteCarReader, wantedCid, offset)
}
// Get reader and seek to offset, then read node.
dr, err := s.localCarReader.DataReader()
if err != nil {
klog.Errorf("failed to get data reader: %v", err)
return nil, err
}
dr.Seek(int64(offset), io.SeekStart)
br := bufio.NewReader(dr)
gotCid, data, err := util.ReadNode(br)
if err != nil {
klog.Errorf("failed to read node: %v", err)
return nil, err
}
// verify that the CID we read matches the one we expected.
if !gotCid.Equals(wantedCid) {
klog.Errorf("CID mismatch: expected %s, got %s", wantedCid, gotCid)
return nil, fmt.Errorf("CID mismatch: expected %s, got %s", wantedCid, gotCid)
}
return data, nil
}
func (ser *deprecatedRPCServer) FindCidFromSlot(ctx context.Context, slot uint64) (cid.Cid, error) {
return findCidFromSlot(ser.slotToCidIndex, slot)
}
func (ser *deprecatedRPCServer) FindCidFromSignature(ctx context.Context, sig solana.Signature) (cid.Cid, error) {
return findCidFromSignature(ser.sigToCidIndex, sig)
}
func (ser *deprecatedRPCServer) FindOffsetFromCid(ctx context.Context, cid cid.Cid) (uint64, error) {
return findOffsetFromCid(ser.cidToOffsetIndex, cid)
}
func (ser *deprecatedRPCServer) GetBlock(ctx context.Context, slot uint64) (*ipldbindcode.Block, error) {
// get the slot by slot number
wantedCid, err := ser.FindCidFromSlot(ctx, slot)
if err != nil {
klog.Errorf("failed to find CID for slot %d: %v", slot, err)
return nil, err
}
klog.Infof("found CID for slot %d: %s", slot, wantedCid)
{
doPrefetch := getValueFromContext(ctx, "prefetch")
if doPrefetch != nil && doPrefetch.(bool) {
// prefetch the block
ser.prefetchSubgraph(ctx, wantedCid)
}
}
// get the block by CID
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to find node by cid: %v", err)
return nil, err
}
// try parsing the data as a Block node.
decoded, err := iplddecoders.DecodeBlock(data)
if err != nil {
klog.Errorf("failed to decode block: %v", err)
return nil, err
}
return decoded, nil
}
func (ser *deprecatedRPCServer) GetEntryByCid(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.Entry, error) {
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to find node by cid: %v", err)
return nil, err
}
// try parsing the data as an Entry node.
decoded, err := iplddecoders.DecodeEntry(data)
if err != nil {
klog.Errorf("failed to decode entry: %v", err)
return nil, err
}
return decoded, nil
}
func (ser *deprecatedRPCServer) GetTransactionByCid(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.Transaction, error) {
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to find node by cid: %v", err)
return nil, err
}
// try parsing the data as a Transaction node.
decoded, err := iplddecoders.DecodeTransaction(data)
if err != nil {
klog.Errorf("failed to decode transaction: %v", err)
return nil, err
}
return decoded, nil
}
func (ser *deprecatedRPCServer) GetDataFrameByCid(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.DataFrame, error) {
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to find node by cid: %v", err)
return nil, err
}
// try parsing the data as a DataFrame node.
decoded, err := iplddecoders.DecodeDataFrame(data)
if err != nil {
klog.Errorf("failed to decode data frame: %v", err)
return nil, err
}
return decoded, nil
}
func (ser *deprecatedRPCServer) GetRewardsByCid(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.Rewards, error) {
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to find node by cid: %v", err)
return nil, err
}
// try parsing the data as a Rewards node.
decoded, err := iplddecoders.DecodeRewards(data)
if err != nil {
klog.Errorf("failed to decode rewards: %v", err)
return nil, err
}
return decoded, nil
}
func (ser *deprecatedRPCServer) GetTransaction(ctx context.Context, sig solana.Signature) (*ipldbindcode.Transaction, error) {
// get the CID by signature
wantedCid, err := ser.FindCidFromSignature(ctx, sig)
if err != nil {
klog.Errorf("failed to find CID for signature %s: %v", sig, err)
return nil, err
}
klog.Infof("found CID for signature %s: %s", sig, wantedCid)
{
doPrefetch := getValueFromContext(ctx, "prefetch")
if doPrefetch != nil && doPrefetch.(bool) {
// prefetch the block
ser.prefetchSubgraph(ctx, wantedCid)
}
}
// get the transaction by CID
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
klog.Errorf("failed to get node by cid: %v", err)
return nil, err
}
// try parsing the data as a Transaction node.
decoded, err := iplddecoders.DecodeTransaction(data)
if err != nil {
klog.Errorf("failed to decode transaction: %v", err)
return nil, err
}
return decoded, nil
}
// jsonrpc2.RequestHandler interface
func (ser *deprecatedRPCServer) Handle(ctx context.Context, conn *requestContext, req *jsonrpc2.Request) {
switch req.Method {
case "getBlock":
ser.handleGetBlock(ctx, conn, req)
case "getTransaction":
ser.handleGetTransaction(ctx, conn, req)
case "getSignaturesForAddress":
ser.handleGetSignaturesForAddress(ctx, conn, req)
default:
conn.ReplyWithError(
ctx,
req.ID,
&jsonrpc2.Error{
Code: jsonrpc2.CodeMethodNotFound,
Message: "Method not found",
})
}
}