-
Notifications
You must be signed in to change notification settings - Fork 19
/
index-slot-to-cid.go
190 lines (164 loc) · 4.85 KB
/
index-slot-to-cid.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
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/indexes"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
"github.com/rpcpool/yellowstone-faithful/slottools"
"k8s.io/klog/v2"
)
// CreateIndex_slot2cid creates an index file that maps slot numbers to CIDs.
func CreateIndex_slot2cid(
ctx context.Context,
epoch uint64,
network indexes.Network,
tmpDir string,
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]
tmpDir = filepath.Join(tmpDir, "index-slot-to-cid-"+time.Now().Format("20060102-150405.000000000"))
if err = os.MkdirAll(tmpDir, 0o755); err != nil {
return "", fmt.Errorf("failed to create tmp dir: %w", err)
}
numItems := uint64(slottools.EpochLen)
klog.Infof("Creating builder with %d items", numItems)
sl2c, err := indexes.NewWriter_SlotToCid(
epoch,
rootCid,
network,
tmpDir,
numItems,
)
if err != nil {
return "", fmt.Errorf("failed to open index store: %w", err)
}
defer sl2c.Close()
numItemsIndexed := 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 CID as the value.
err = FindBlocks(
ctx,
dr,
func(c cid.Cid, block *ipldbindcode.Block) error {
slotNum := block.Slot
err = sl2c.Put(uint64(slotNum), c)
if err != nil {
return fmt.Errorf("failed to put cid to offset: %w", err)
}
numItemsIndexed++
if numItemsIndexed%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...")
if err = sl2c.Seal(ctx, indexDir); err != nil {
return "", fmt.Errorf("failed to seal index: %w", err)
}
indexFilePath := sl2c.GetFilepath()
klog.Infof("Index created at %s; %d items indexed", indexFilePath, numItemsIndexed)
return indexFilePath, nil
}
// VerifyIndex_slot2cid verifies that the index file is correct for the given car file.
// It does this by reading the car file and comparing the offsets in the index
// file to the offsets in the car file.
func VerifyIndex_slot2cid(ctx context.Context, carPath string, indexFilePath 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 %s does not exist", carPath)
}
// Check if the index file exists:
exists, err = fileExists(indexFilePath)
if err != nil {
return fmt.Errorf("failed to check if index file exists: %w", err)
}
if !exists {
return fmt.Errorf("index file %s does not exist", indexFilePath)
}
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))
}
c2o, err := indexes.Open_SlotToCid(indexFilePath)
if err != nil {
return fmt.Errorf("failed to open index: %w", err)
}
dr, err := cr.DataReader()
if err != nil {
return fmt.Errorf("failed to get data reader: %w", err)
}
numItems := uint64(0)
// Iterate over all blocks in the CAR file and put them into the index,
// using the slot number as the key and the CID as the value.
err = FindBlocks(
ctx,
dr,
func(c cid.Cid, block *ipldbindcode.Block) error {
slotNum := uint64(block.Slot)
got, err := c2o.Get(slotNum)
if err != nil {
return fmt.Errorf("failed to put cid to offset: %w", err)
}
if !got.Equals(c) {
return fmt.Errorf("slot %d: expected cid %s, got %s", slotNum, c, got)
}
numItems++
if numItems%1_000 == 0 {
printToStderr(".")
}
return nil
})
if err != nil {
return fmt.Errorf("failed to verify index; error while iterating over blocks: %w", err)
}
return nil
}