-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd-x-index-all.go
774 lines (700 loc) · 20.7 KB
/
cmd-x-index-all.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"sort"
"time"
"github.com/dustin/go-humanize"
"github.com/ipfs/go-cid"
"github.com/rpcpool/yellowstone-faithful/blocktimeindex"
"github.com/rpcpool/yellowstone-faithful/bucketteer"
"github.com/rpcpool/yellowstone-faithful/carreader"
"github.com/rpcpool/yellowstone-faithful/indexes"
"github.com/rpcpool/yellowstone-faithful/indexmeta"
"github.com/rpcpool/yellowstone-faithful/iplddecoders"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"k8s.io/klog/v2"
)
func newCmd_Index_all() *cli.Command {
var verify bool
var network indexes.Network
return &cli.Command{
Name: "all",
Usage: "Create all the necessary indexes for a Solana epoch.",
Description: "Given a CAR file containing a Solana epoch, create all the necessary indexes and save them in the specified index dir.",
ArgsUsage: "<car-path> <index-dir>",
Before: func(c *cli.Context) error {
if network == "" {
network = indexes.NetworkMainnet
}
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verify",
Usage: "verify the indexes after creating them",
Destination: &verify,
},
&cli.StringFlag{
Name: "tmp-dir",
Usage: "temporary directory to use for storing intermediate files",
Value: os.TempDir(),
},
&cli.StringFlag{
Name: "network",
Usage: "the cluster of the epoch; one of: mainnet, testnet, devnet",
Action: func(c *cli.Context, s string) error {
network = indexes.Network(s)
if !indexes.IsValidNetwork(network) {
return fmt.Errorf("invalid network: %q", network)
}
return nil
},
},
},
Subcommands: []*cli.Command{},
Action: func(c *cli.Context) error {
carPath := c.Args().Get(0)
indexDir := c.Args().Get(1)
tmpDir := c.String("tmp-dir")
if carPath == "" {
return fmt.Errorf("missing car-path argument")
}
if indexDir == "" {
return fmt.Errorf("missing index-dir argument")
}
if ok, err := isDirectory(indexDir); err != nil {
return err
} else if !ok {
return fmt.Errorf("index-dir is not a directory")
}
{
startedAt := time.Now()
defer func() {
klog.Infof("Took %s", time.Since(startedAt))
}()
klog.Infof("Creating all indexes for %s", carPath)
klog.Infof("Indexes will be saved in %s", indexDir)
indexPaths, numTotalItems, err := createAllIndexes(
c.Context,
network,
tmpDir,
carPath,
indexDir,
)
if err != nil {
return err
}
klog.Info("Indexes created:")
fmt.Println(indexPaths.String())
if verify {
return verifyAllIndexes(
context.Background(),
carPath,
indexPaths,
numTotalItems,
)
}
klog.Info("Skipping verification.")
}
return nil
},
}
}
func createAllIndexes(
ctx context.Context,
network indexes.Network,
tmpDir string,
carPath string,
indexDir string,
) (*IndexPaths, uint64, error) {
// Check if the CAR file exists:
exists, err := fileExists(carPath)
if err != nil {
return nil, 0, fmt.Errorf("failed to check if CAR file exists: %w", err)
}
if !exists {
return nil, 0, fmt.Errorf("CAR file %q does not exist", carPath)
}
carFile, err := os.Open(carPath)
if err != nil {
return nil, 0, fmt.Errorf("failed to open car file: %w", err)
}
defer carFile.Close()
rd, err := carreader.New(carFile)
if err != nil {
return nil, 0, fmt.Errorf("failed to create car reader: %w", err)
}
// check it has 1 root
if len(rd.Header.Roots) != 1 {
return nil, 0, fmt.Errorf("car file must have exactly 1 root, but has %d", len(rd.Header.Roots))
}
// print roots:
for _, root := range rd.Header.Roots {
klog.Infof("- Root: %s", root)
}
rootCID := rd.Header.Roots[0]
klog.Infof("Getting car file size")
klog.Infof("Counting items in car file...")
numItems, epochObject, err := carCountItemsByFirstByte(carPath)
if err != nil {
return nil, 0, fmt.Errorf("failed to count items in car file: %w", err)
}
if epochObject == nil {
return nil, 0, fmt.Errorf("failed to find epoch object in the car file")
}
fmt.Println()
klog.Infof("Found items in car file:")
numTotalItems := uint64(0)
var kinds []byte
for kind := range numItems {
kinds = append(kinds, kind)
}
// sort from byte value:
sort.Slice(kinds, func(i, j int) bool {
return kinds[i] < kinds[j]
})
for _, kind := range kinds {
klog.Infof(" %s: %s items", iplddecoders.Kind(kind), humanize.Comma(int64(numItems[kind])))
numTotalItems += numItems[kind]
}
klog.Infof("Total: %s items", humanize.Comma(int64(numTotalItems)))
epoch := uint64(epochObject.Epoch)
klog.Infof("This CAR file is for epoch %d and cluster %s", epoch, network)
cid_to_offset_and_size, err := NewBuilder_CidToOffset(
epoch,
rootCID,
network,
tmpDir,
numTotalItems,
)
if err != nil {
return nil, 0, fmt.Errorf("failed to create cid_to_offset_and_size index: %w", err)
}
defer cid_to_offset_and_size.Close()
slot_to_cid, err := NewBuilder_SlotToCid(
epoch,
rootCID,
network,
tmpDir,
numItems[byte(iplddecoders.KindBlock)],
)
if err != nil {
return nil, 0, fmt.Errorf("failed to create slot_to_cid index: %w", err)
}
defer slot_to_cid.Close()
sig_to_cid, err := NewBuilder_SignatureToCid(
epoch,
rootCID,
network,
tmpDir,
numItems[byte(iplddecoders.KindTransaction)],
)
if err != nil {
return nil, 0, fmt.Errorf("failed to create sig_to_cid index: %w", err)
}
defer sig_to_cid.Close()
sigExistsFilepath := formatSigExistsIndexFilePath(indexDir, epoch, rootCID, network)
sig_exists, err := bucketteer.NewWriter(
sigExistsFilepath,
)
if err != nil {
return nil, 0, fmt.Errorf("failed to create sig_exists index: %w", err)
}
defer sig_exists.Close()
slot_to_blocktime := blocktimeindex.NewForEpoch(epoch)
totalOffset := uint64(0)
{
if size, err := rd.HeaderSize(); err != nil {
return nil, 0, err
} else {
totalOffset += size
}
}
numIndexedOffsets := uint64(0)
numIndexedBlocks := uint64(0)
numIndexedTransactions := uint64(0)
lastCheckpoint := time.Now()
klog.Infof("Indexing...")
var eta time.Duration
startedAt := time.Now()
for {
_cid, sectionLength, block, err := rd.NextNode()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, 0, err
}
// klog.Infof("key: %s, offset: %d", bin.FormatByteSlice(c.Bytes()), totalOffset)
err = cid_to_offset_and_size.Put(_cid, totalOffset, sectionLength)
if err != nil {
return nil, 0, fmt.Errorf("failed to index cid to offset: %w", err)
}
numIndexedOffsets++
kind := iplddecoders.Kind(block.RawData()[1])
switch kind {
case iplddecoders.KindBlock:
{
block, err := iplddecoders.DecodeBlock(block.RawData())
if err != nil {
return nil, 0, fmt.Errorf("failed to decode block: %w", err)
}
err = slot_to_cid.Put(uint64(block.Slot), _cid)
if err != nil {
return nil, 0, fmt.Errorf("failed to index slot to cid: %w", err)
}
err = slot_to_blocktime.Set(uint64(block.Slot), int64(block.Meta.Blocktime))
if err != nil {
return nil, 0, fmt.Errorf("failed to index slot to blocktime: %w", err)
}
numIndexedBlocks++
}
case iplddecoders.KindTransaction:
{
txNode, err := iplddecoders.DecodeTransaction(block.RawData())
if err != nil {
return nil, 0, fmt.Errorf("failed to decode transaction: %w", err)
}
sig, err := readFirstSignature(txNode.Data.Bytes())
if err != nil {
return nil, 0, fmt.Errorf("failed to read signature: %w", err)
}
err = sig_to_cid.Put(sig, _cid)
if err != nil {
return nil, 0, fmt.Errorf("failed to index signature to cid: %w", err)
}
sig_exists.Put(sig)
numIndexedTransactions++
}
}
totalOffset += sectionLength
if numIndexedOffsets%1_000_000 == 0 && numIndexedOffsets > 0 {
timeForChunk := time.Since(lastCheckpoint)
numChunksLeft := ((numTotalItems - numIndexedOffsets) / 1_000_000) + 1
eta = timeForChunk * time.Duration(numChunksLeft)
lastCheckpoint = time.Now()
}
if numIndexedOffsets%100_000 == 0 {
var etaString string
if eta > 0 {
etaString = fmt.Sprintf(" ETA: %s ", eta.Truncate(time.Second).String())
} else {
etaString = ", ETA: --- "
}
printToStderr(
fmt.Sprintf("\rIndexing: %s/%s items [%s%%] %s",
humanize.Comma(int64(numIndexedOffsets)),
humanize.Comma(int64(numTotalItems)),
humanize.CommafWithDigits(float64(numIndexedOffsets)/float64(numTotalItems)*100, 2),
etaString,
),
)
}
}
printToStderr(
fmt.Sprintf("\rIndexed %s items in %s \n",
humanize.Comma(int64(numIndexedOffsets)),
time.Since(startedAt).Truncate(time.Second),
),
)
printToStderr("\n")
klog.Infof(
"Indexed %s offsets, %s blocks, %s transactions",
humanize.Comma(int64(numIndexedOffsets)),
humanize.Comma(int64(numIndexedBlocks)),
humanize.Comma(int64(numIndexedTransactions)),
)
klog.Infof("Preparing to seal indexes (DO NOT EXIT)...")
paths := &IndexPaths{}
paths.SignatureExists = sigExistsFilepath
{
wg := new(errgroup.Group)
// seal the indexes
wg.Go(func() error {
klog.Infof("Sealing cid_to_offset_and_size index...")
err = cid_to_offset_and_size.Seal(ctx, indexDir)
if err != nil {
return fmt.Errorf("failed to seal cid_to_offset_and_size index: %w", err)
}
paths.CidToOffsetAndSize = cid_to_offset_and_size.GetFilepath()
klog.Infof("Successfully sealed cid_to_offset_and_size index: %s", paths.CidToOffsetAndSize)
return nil
})
wg.Go(func() error {
klog.Infof("Sealing slot_to_cid index...")
err = slot_to_cid.Seal(ctx, indexDir)
if err != nil {
return fmt.Errorf("failed to seal slot_to_cid index: %w", err)
}
paths.SlotToCid = slot_to_cid.GetFilepath()
klog.Infof("Successfully sealed slot_to_cid index: %s", paths.SlotToCid)
return nil
})
wg.Go(func() error {
klog.Infof("Sealing sig_to_cid index...")
err = sig_to_cid.Seal(ctx, indexDir)
if err != nil {
return fmt.Errorf("failed to seal sig_to_cid index: %w", err)
}
paths.SignatureToCid = sig_to_cid.GetFilepath()
klog.Infof("Successfully sealed sig_to_cid index: %s", paths.SignatureToCid)
return nil
})
wg.Go(func() error {
klog.Infof("Sealing sig_exists index...")
meta := indexmeta.Meta{}
if err := meta.AddUint64(indexmeta.MetadataKey_Epoch, epoch); err != nil {
return fmt.Errorf("failed to add epoch to sig_exists index metadata: %w", err)
}
if err := meta.AddCid(indexmeta.MetadataKey_RootCid, rootCID); err != nil {
return fmt.Errorf("failed to add root cid to sig_exists index metadata: %w", err)
}
if err := meta.AddString(indexmeta.MetadataKey_Network, string(network)); err != nil {
return fmt.Errorf("failed to add network to sig_exists index metadata: %w", err)
}
if _, err = sig_exists.Seal(meta); err != nil {
return fmt.Errorf("failed to seal sig_exists index: %w", err)
}
klog.Infof("Successfully sealed sig_exists index: %s", paths.SignatureExists)
return nil
})
wg.Go(func() error {
klog.Infof("Sealing slot_to_blocktime index...")
path := filepath.Join(indexDir, blocktimeindex.FormatFilename(epoch, rootCID, network))
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create slot_to_blocktime index file: %w", err)
}
defer file.Close()
if _, err := slot_to_blocktime.WriteTo(file); err != nil {
return fmt.Errorf("failed to write slot_to_blocktime index: %w", err)
}
paths.SlotToBlocktime = path
klog.Infof("Successfully sealed slot_to_blocktime index: %s", paths.SlotToBlocktime)
return nil
})
if err := wg.Wait(); err != nil {
return nil, 0, err
}
}
return paths, numTotalItems, nil
}
func greenBackground(s string) string {
return blackText(fmt.Sprintf("\x1b[42m%s\x1b[0m", s))
}
func blackText(s string) string {
return fmt.Sprintf("\x1b[30m%s\x1b[0m", s)
}
type IndexPaths struct {
CidToOffsetAndSize string
SlotToCid string
SignatureToCid string
SignatureExists string
SlotToBlocktime string
}
// IndexPaths.String
func (p *IndexPaths) String() string {
var builder bytes.Buffer
builder.WriteString(" cid_to_offset_and_size:\n uri: ")
builder.WriteString(quoteSingle(p.CidToOffsetAndSize))
builder.WriteString("\n")
builder.WriteString(" slot_to_cid:\n uri: ")
builder.WriteString(quoteSingle(p.SlotToCid))
builder.WriteString("\n")
builder.WriteString(" sig_to_cid:\n uri: ")
builder.WriteString(quoteSingle(p.SignatureToCid))
builder.WriteString("\n")
builder.WriteString(" sig_exists:\n uri: ")
builder.WriteString(quoteSingle(p.SignatureExists))
builder.WriteString("\n")
builder.WriteString(" slot_to_blocktime:\n uri: ")
builder.WriteString(quoteSingle(p.SlotToBlocktime))
builder.WriteString("\n")
return builder.String()
}
func quoteSingle(s string) string {
return fmt.Sprintf("'%s'", s)
}
func NewBuilder_CidToOffset(
epoch uint64,
rootCid cid.Cid,
network indexes.Network,
tmpDir string,
numItems uint64,
) (*indexes.CidToOffsetAndSize_Writer, error) {
tmpDir = filepath.Join(tmpDir, "index-cid-to-offset-"+time.Now().Format("20060102-150405.000000000")+fmt.Sprintf("-%d", rand.Int63()))
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create cid_to_offset_and_size tmp dir: %w", err)
}
index, err := indexes.NewWriter_CidToOffsetAndSize(
epoch,
rootCid,
network,
tmpDir,
numItems,
)
if err != nil {
return nil, fmt.Errorf("failed to create cid-to-offset-and-size index: %w", err)
}
return index, nil
}
func NewBuilder_SignatureToCid(
epoch uint64,
rootCid cid.Cid,
network indexes.Network,
tmpDir string,
numItems uint64,
) (*indexes.SigToCid_Writer, error) {
tmpDir = filepath.Join(tmpDir, "index-sig-to-cid-"+time.Now().Format("20060102-150405.000000000")+fmt.Sprintf("-%d", rand.Int63()))
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create sig_to_cid tmp dir: %w", err)
}
index, err := indexes.NewWriter_SigToCid(
epoch,
rootCid,
network,
tmpDir,
numItems,
)
if err != nil {
return nil, fmt.Errorf("failed to create sig_to_cid index: %w", err)
}
return index, nil
}
func NewBuilder_SlotToCid(
epoch uint64,
rootCid cid.Cid,
network indexes.Network,
tmpDir string,
numItems uint64,
) (*indexes.SlotToCid_Writer, error) {
tmpDir = filepath.Join(tmpDir, "index-slot-to-cid-"+time.Now().Format("20060102-150405.000000000")+fmt.Sprintf("-%d", rand.Int63()))
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create slot_to_cid tmp dir: %w", err)
}
index, err := indexes.NewWriter_SlotToCid(
epoch,
rootCid,
network,
tmpDir,
numItems,
)
if err != nil {
return nil, fmt.Errorf("failed to create slot_to_cid index: %w", err)
}
return index, nil
}
func verifyAllIndexes(
ctx context.Context,
carPath string,
indexes *IndexPaths,
numTotalItems uint64,
) error {
// Check if the CAR file exists:
exists, err := fileExists(carPath)
if err != nil {
return fmt.Errorf("failed to check if CAR file exists: %w", err)
}
if !exists {
return fmt.Errorf("CAR file %q does not exist", carPath)
}
carFile, err := os.Open(carPath)
if err != nil {
return fmt.Errorf("failed to open car file: %w", err)
}
defer carFile.Close()
rd, err := carreader.New(carFile)
if err != nil {
return fmt.Errorf("failed to create car reader: %w", err)
}
// check it has 1 root
if len(rd.Header.Roots) != 1 {
return fmt.Errorf("car file must have exactly 1 root, but has %d", len(rd.Header.Roots))
}
cid_to_offset_and_size, err := OpenIndex_CidToOffset(
indexes.CidToOffsetAndSize,
)
if err != nil {
return fmt.Errorf("failed to open cid_to_offset_and_size index: %w", err)
}
defer cid_to_offset_and_size.Close()
slot_to_cid, err := OpenIndex_SlotToCid(
indexes.SlotToCid,
)
if err != nil {
return fmt.Errorf("failed to open slot_to_cid index: %w", err)
}
defer slot_to_cid.Close()
sig_to_cid, err := OpenIndex_SigToCid(
indexes.SignatureToCid,
)
if err != nil {
return fmt.Errorf("failed to open sig_to_cid index: %w", err)
}
defer sig_to_cid.Close()
var sig_exists *bucketteer.Reader
if indexes.SignatureExists != "" {
sig_exists, err = bucketteer.Open(
indexes.SignatureExists,
)
if err != nil {
return fmt.Errorf("failed to open sig_exists index: %w", err)
}
defer sig_exists.Close()
}
totalOffset := uint64(0)
{
if size, err := rd.HeaderSize(); err != nil {
return err
} else {
totalOffset += size
}
}
numIndexedOffsets := uint64(0)
numIndexedBlocks := uint64(0)
numIndexedTransactions := uint64(0)
klog.Infof("Verifying indexes...")
lastCheckpoint := time.Now()
var eta time.Duration
startedAt := time.Now()
for {
_cid, sectionLength, block, err := rd.NextNode()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
// klog.Infof("key: %s, offset: %d", bin.FormatByteSlice(c.Bytes()), totalOffset)
offset, err := cid_to_offset_and_size.Get(_cid)
if err != nil {
return fmt.Errorf("failed to lookup offset for %s: %w", _cid, err)
}
if offset.Offset != totalOffset {
return fmt.Errorf("offset mismatch for %s: %d != %d", _cid, offset, totalOffset)
}
if offset.Size != sectionLength {
return fmt.Errorf("length mismatch for %s: %d != %d", _cid, offset, sectionLength)
}
numIndexedOffsets++
kind := iplddecoders.Kind(block.RawData()[1])
switch kind {
case iplddecoders.KindBlock:
{
block, err := iplddecoders.DecodeBlock(block.RawData())
if err != nil {
return fmt.Errorf("failed to decode block: %w", err)
}
got, err := slot_to_cid.Get(uint64(block.Slot))
if err != nil {
return fmt.Errorf("failed to index slot to cid: %w", err)
}
if !got.Equals(_cid) {
return fmt.Errorf("slot to cid mismatch for %d: expected cid %s, got %s", block.Slot, _cid, got)
}
numIndexedBlocks++
}
case iplddecoders.KindTransaction:
{
txNode, err := iplddecoders.DecodeTransaction(block.RawData())
if err != nil {
return fmt.Errorf("failed to decode transaction: %w", err)
}
sig, err := readFirstSignature(txNode.Data.Bytes())
if err != nil {
return fmt.Errorf("failed to read signature: %w", err)
}
got, err := sig_to_cid.Get(sig)
if err != nil {
return fmt.Errorf("failed to index signature to cid: %w", err)
}
if !got.Equals(_cid) {
return fmt.Errorf("sig to cid mismatch for sig %s: expected cid %s, got %s", sig, _cid, got)
}
if sig_exists != nil {
if has, err := sig_exists.Has(sig); err != nil {
return fmt.Errorf("failed to check if sig exists in sig_exists index: %w", err)
} else if !has {
return fmt.Errorf("sig %s does not exist in sig_exists index", sig)
}
}
numIndexedTransactions++
}
}
totalOffset += sectionLength
if numIndexedOffsets%1_000_000 == 0 && numIndexedOffsets > 0 && numTotalItems > 0 {
timeForChunk := time.Since(lastCheckpoint)
numChunksLeft := ((numTotalItems - numIndexedOffsets) / 1_000_000) + 1
eta = timeForChunk * time.Duration(numChunksLeft)
lastCheckpoint = time.Now()
}
if numIndexedOffsets%100_000 == 0 {
if numTotalItems > 0 {
var etaString string
if eta > 0 {
etaString = fmt.Sprintf(", ETA: %s ", eta.Truncate(time.Second).String())
} else {
etaString = ", ETA: --- "
}
printToStderr(
fmt.Sprintf("\rVerifying index: %s/%s items [%s%%] %s",
humanize.Comma(int64(numIndexedOffsets)),
humanize.Comma(int64(numTotalItems)),
humanize.CommafWithDigits(float64(numIndexedOffsets)/float64(numTotalItems)*100, 2),
etaString,
),
)
} else {
printToStderr(
fmt.Sprintf("\rVerifying index: %s items",
humanize.Comma(int64(numIndexedOffsets)),
),
)
}
}
}
printToStderr(
fmt.Sprintf(
"\rVerified %s offsets, %s blocks, %s transactions in %s\n",
humanize.Comma(int64(numIndexedOffsets)),
humanize.Comma(int64(numIndexedBlocks)),
humanize.Comma(int64(numIndexedTransactions)),
time.Since(startedAt).Truncate(time.Second),
))
return nil
}
func OpenIndex_CidToOffset(
indexFilePath string,
) (*indexes.CidToOffsetAndSize_Reader, error) {
index, err := indexes.Open_CidToOffsetAndSize(indexFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open cid_to_offset_and_size index: %w", err)
}
return index, nil
}
func OpenIndex_SlotToCid(
indexFilePath string,
) (*indexes.SlotToCid_Reader, error) {
index, err := indexes.Open_SlotToCid(indexFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open slot_to_cid index: %w", err)
}
return index, nil
}
func OpenIndex_SigToCid(
indexFilePath string,
) (*indexes.SigToCid_Reader, error) {
index, err := indexes.Open_SigToCid(indexFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open sig_to_cid index: %w", err)
}
return index, nil
}