-
Notifications
You must be signed in to change notification settings - Fork 10
/
storage.go
140 lines (119 loc) · 3.63 KB
/
storage.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
package firecore
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"github.com/spf13/viper"
"github.com/streamingfast/dstore"
"go.uber.org/zap"
)
var commonStoresCreated bool
var indexStoreCreated bool
var tmpDirCreated bool
func GetTmpDir(dataDir string) (tmpDir string, err error) {
if tmpDirCreated {
return
}
tmpDir = MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-tmp-dir"))
err = os.MkdirAll(tmpDir, 0755)
tmpDirCreated = true
return
}
func GetCommonStoresURLs(dataDir string) (mergedBlocksStoreURL, oneBlocksStoreURL, forkedBlocksStoreURL string, err error) {
mergedBlocksStoreURL = MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-merged-blocks-store-url"))
oneBlocksStoreURL = MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-one-block-store-url"))
forkedBlocksStoreURL = MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-forked-blocks-store-url"))
if commonStoresCreated {
return
}
if err = mkdirStorePathIfLocal(forkedBlocksStoreURL); err != nil {
return
}
if err = mkdirStorePathIfLocal(oneBlocksStoreURL); err != nil {
return
}
if err = mkdirStorePathIfLocal(mergedBlocksStoreURL); err != nil {
return
}
commonStoresCreated = true
return
}
func GetIndexStore(dataDir string) (indexStore dstore.Store, possibleIndexSizes []uint64, err error) {
indexStoreURL := MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-index-store-url"))
if indexStoreURL != "" {
s, err := dstore.NewStore(indexStoreURL, "", "", false)
if err != nil {
return nil, nil, fmt.Errorf("couldn't create index store: %w", err)
}
if !indexStoreCreated {
if err = mkdirStorePathIfLocal(indexStoreURL); err != nil {
return nil, nil, err
}
}
indexStoreCreated = true
indexStore = s
}
sizes := viper.GetIntSlice("common-index-block-sizes")
if len(sizes) == 0 {
// viper doesn't parse ints from yaml file correctly
for _, k := range strings.Split(viper.GetString("common-index-block-sizes"), ",") {
if asInt, _err := strconv.ParseInt(k, 10, 64); _err == nil {
sizes = append(sizes, int(asInt))
}
}
}
for _, size := range sizes {
if size < 0 {
return nil, nil, fmt.Errorf("invalid negative size for common-index-block-sizes: %d", size)
}
possibleIndexSizes = append(possibleIndexSizes, uint64(size))
}
return
}
func LastMergedBlockNum(ctx context.Context, startBlockNum uint64, store dstore.Store, logger *zap.Logger) uint64 {
value, err := searchBlockNum(startBlockNum, func(u uint64) (bool, error) {
filepath := fmt.Sprintf("%010d", u)
found, err := store.FileExists(ctx, filepath)
if err != nil {
return false, fmt.Errorf("failed to file exists %s: %w", filepath, err)
}
return found, nil
})
if err != nil {
logger.Warn("failed to resolve block", zap.Error(err))
return startBlockNum
}
return value
}
func searchBlockNum(startBlockNum uint64, f func(uint64) (bool, error)) (uint64, error) {
blockNum, err := blockNumIter(startBlockNum, 10_000_000_000, 1_000_000_000, f)
if err != nil {
return 0, err
}
if blockNum < startBlockNum {
return startBlockNum, nil
}
return blockNum, nil
}
func blockNumIter(startBlockNum, exclusiveEndBlockNum, interval uint64, f func(uint64) (bool, error)) (uint64, error) {
i := exclusiveEndBlockNum
for i >= startBlockNum {
i -= interval
match, err := f(i)
if err != nil {
return 0, fmt.Errorf("failed to match blcok num %d: %w", i, err)
}
if match {
if interval == 100 {
return i, nil
}
return blockNumIter(i, i+interval, interval/10, f)
}
}
return startBlockNum, nil
}
func viperExpandedEnvGetString(key string) string {
return os.ExpandEnv(viper.GetString(key))
}