-
Notifications
You must be signed in to change notification settings - Fork 16
/
cmd-x-index-cid2offset.go
104 lines (99 loc) · 2.48 KB
/
cmd-x-index-cid2offset.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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/rpcpool/yellowstone-faithful/indexes"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
)
func newCmd_Index_cid2offset() *cli.Command {
var verify bool
var epoch uint64
var network indexes.Network
return &cli.Command{
Name: "cid-to-offset",
Description: "Given a CAR file containing a Solana epoch, create an index of the file that maps CIDs to offsets in the CAR file.",
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 index after creating it",
Destination: &verify,
},
&cli.StringFlag{
Name: "tmp-dir",
Usage: "temporary directory to use for storing intermediate files",
Value: os.TempDir(),
},
&cli.Uint64Flag{
Name: "epoch",
Usage: "the epoch of the CAR file",
Destination: &epoch,
Required: true,
},
&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 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("Finished in %s", time.Since(startedAt))
}()
klog.Infof("Creating CID-to-offset index for %s", carPath)
indexFilepath, err := CreateIndex_cid2offset(
context.TODO(),
epoch,
network,
tmpDir,
carPath,
indexDir,
)
if err != nil {
panic(err)
}
klog.Info("Index created")
if verify {
klog.Infof("Verifying index for %s located at %s", carPath, indexFilepath)
startedAt := time.Now()
defer func() {
klog.Infof("Finished in %s", time.Since(startedAt))
}()
err := VerifyIndex_cid2offset(context.TODO(), carPath, indexFilepath)
if err != nil {
return cli.Exit(err, 1)
}
klog.Info("Index verified")
return nil
}
}
return nil
},
}
}