-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-x-index-slot2blocktime.go
163 lines (145 loc) · 4.22 KB
/
cmd-x-index-slot2blocktime.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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/ipfs/go-cid"
carv2 "github.com/ipld/go-car/v2"
"github.com/rpcpool/yellowstone-faithful/blocktimeindex"
"github.com/rpcpool/yellowstone-faithful/indexes"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
)
func newCmd_Index_slot2blocktime() *cli.Command {
var epoch uint64
var network indexes.Network
return &cli.Command{
Name: "slot-to-blocktime",
Description: "Given a CAR file containing a Solana epoch, create an index of the file that maps slots to blocktimes.",
ArgsUsage: "<car-path> <index-dir>",
Before: func(c *cli.Context) error {
if network == "" {
network = indexes.NetworkMainnet
}
return nil
},
Flags: []cli.Flag{
&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)
{
startedAt := time.Now()
defer func() {
klog.Infof("Finished in %s", time.Since(startedAt))
}()
klog.Infof("Creating slot-to-blocktime index for %s", carPath)
indexFilepath, err := CreateIndex_slot2blocktime(
context.TODO(),
epoch,
network,
carPath,
indexDir,
)
if err != nil {
panic(err)
}
klog.Info("slot-to-blocktime index created at", indexFilepath)
}
return nil
},
}
}
// CreateIndex_slot2blocktime creates an index file that maps slot numbers to blocktimes.
func CreateIndex_slot2blocktime(
ctx context.Context,
epoch uint64,
network indexes.Network,
carPath string,
indexDir string,
) (string, 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)
}
cr, err := carv2.OpenReader(carPath)
if err != nil {
return "", fmt.Errorf("failed to open CAR file: %w", err)
}
// check it has 1 root
roots, err := cr.Roots()
if err != nil {
return "", fmt.Errorf("failed to get roots: %w", err)
}
// There should be only one root CID in the CAR file.
if len(roots) != 1 {
return "", fmt.Errorf("CAR file has %d roots, expected 1", len(roots))
}
rootCid := roots[0]
slot_to_blocktime := blocktimeindex.NewForEpoch(epoch)
numBlocksIndexed := uint64(0)
klog.Infof("Indexing...")
dr, err := cr.DataReader()
if err != nil {
return "", fmt.Errorf("failed to get data reader: %w", err)
}
// Iterate over all blocks in the CAR file and put them into the index,
// using the slot number as the key and the blocktime as the value.
err = FindBlocks(
ctx,
dr,
func(c cid.Cid, block *ipldbindcode.Block) error {
slotNum := uint64(block.Slot)
err = slot_to_blocktime.Set(slotNum, int64(block.Meta.Blocktime))
if err != nil {
return fmt.Errorf("failed to put cid to offset: %w", err)
}
numBlocksIndexed++
if numBlocksIndexed%1_000 == 0 {
printToStderr(".")
}
return nil
})
if err != nil {
return "", fmt.Errorf("failed to index; error while iterating over blocks: %w", err)
}
// Use the car file name and root CID to name the index file:
klog.Infof("Sealing index...")
indexFilePath := filepath.Join(indexDir, blocktimeindex.FormatFilename(epoch, rootCid, network))
file, err := os.Create(indexFilePath)
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)
}
klog.Infof("Successfully sealed slot_to_blocktime index")
klog.Infof("Index created at %s; %d items indexed", indexFilePath, numBlocksIndexed)
return indexFilePath, nil
}