-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcfilter-files.go
225 lines (178 loc) · 4.79 KB
/
cfilter-files.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
const (
HeadersPerFile = 100_000
FiltersPerFile = 2_000
HeaderFileDir = "headers"
FilterFileDir = "filters"
HeaderFileNamePattern = "%s/block-%07d-%07d.header"
FilterFileSuffix = ".cfilter"
FilterFileNamePattern = "%s/block-%07d-%07d.cfilter"
FilterFileNameExtractPattern = "block-[0-9]{7}-([0-9]{7})\\.cfilter"
FilterHeaderFileNamePattern = "%s/block-%07d-%07d.cfheader"
DirectoryMode = 0755
)
var (
filterBasic = btcjson.FilterTypeBasic
filterFileNameExtractRegex = regexp.MustCompile(
FilterFileNameExtractPattern,
)
)
func (s *server) writeHeaders(fileName string, startIndex,
endIndex int32) error {
s.cacheLock.RLock()
defer s.cacheLock.RUnlock()
log.Debugf("Writing header file %s", fileName)
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("error creating file %s: %w", fileName, err)
}
err = s.serializeHeaders(file, startIndex, endIndex)
if err != nil {
return fmt.Errorf("error writing headers to file %s: %w",
fileName, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("error closing file %s: %w", fileName, err)
}
return nil
}
func (s *server) serializeHeaders(w io.Writer, startIndex,
endIndex int32) error {
for j := startIndex; j <= endIndex; j++ {
hash, ok := s.heightToHash[j]
if !ok {
return fmt.Errorf("invalid height %d", j)
}
header, ok := s.headers[hash]
if !ok {
return fmt.Errorf("missing header for hash %s (height "+
"%d)", hash.String(), j)
}
err := header.Serialize(w)
if err != nil {
return fmt.Errorf("error writing headers: %w", err)
}
}
return nil
}
func (s *server) writeFilterHeaders(fileName string, startIndex,
endIndex int32) error {
s.cacheLock.RLock()
defer s.cacheLock.RUnlock()
log.Debugf("Writing filter header file %s", fileName)
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("error creating file %s: %w", fileName, err)
}
err = s.serializeFilterHeaders(file, startIndex, endIndex)
if err != nil {
return fmt.Errorf("error writing filter headers to file %s: %w",
fileName, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("error closing file %s: %w", fileName, err)
}
return nil
}
func (s *server) serializeFilterHeaders(w io.Writer, startIndex,
endIndex int32) error {
for j := startIndex; j <= endIndex; j++ {
hash, ok := s.heightToHash[j]
if !ok {
return fmt.Errorf("invalid height %d", j)
}
filterHeader, ok := s.filterHeaders[hash]
if !ok {
return fmt.Errorf("missing filter header for hash %s "+
"(height %d)", hash.String(), j)
}
num, err := w.Write(filterHeader[:])
if err != nil {
return fmt.Errorf("error writing filter header: %w",
err)
}
if num != chainhash.HashSize {
return fmt.Errorf("short write when writing filter " +
"headers")
}
}
return nil
}
func (s *server) writeFilters(fileName string, startIndex,
endIndex int32) error {
s.cacheLock.RLock()
defer s.cacheLock.RUnlock()
log.Debugf("Writing filter file %s", fileName)
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("error creating file %s: %w", fileName, err)
}
err = s.serializeFilters(file, startIndex, endIndex)
if err != nil {
return fmt.Errorf("error writing filters to file %s: %w",
fileName, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("error closing file %s: %w", fileName, err)
}
return nil
}
func (s *server) serializeFilters(w io.Writer, startIndex,
endIndex int32) error {
for j := startIndex; j <= endIndex; j++ {
hash, ok := s.heightToHash[j]
if !ok {
return fmt.Errorf("invalid height %d", j)
}
filter, ok := s.filters[hash]
if !ok {
return fmt.Errorf("missing filter for hash %s (height "+
"%d)", hash.String(), j)
}
err := wire.WriteVarBytes(w, 0, filter)
if err != nil {
return fmt.Errorf("error writing filters: %w", err)
}
}
return nil
}
func lastFile(fileDir, searchPattern string,
extractPattern *regexp.Regexp) (int32, error) {
globPattern := fmt.Sprintf("%s/*%s", fileDir, searchPattern)
files, err := filepath.Glob(globPattern)
if err != nil {
return 0, fmt.Errorf("error listing files '%s' in %s: %w",
globPattern, fileDir, err)
}
if len(files) == 0 {
return 0, nil
}
sort.Strings(files)
last := files[len(files)-1]
matches := extractPattern.FindStringSubmatch(last)
if len(matches) != 2 || matches[1] == "" {
return 0, fmt.Errorf("error extracting number from file %s",
last)
}
numUint, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
return 0, fmt.Errorf("error parsing number '%s': %w",
matches[1], err)
}
return int32(numUint), nil
}