Skip to content

Commit a98a59d

Browse files
headerfs: extract Size and TipKey methods
1 parent be48a19 commit a98a59d

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

headerfs/file.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,11 @@ func (h *headerStore) appendRaw(header []byte) error {
5050
// amount of bytes read past the seek distance is determined by the specified
5151
// header type.
5252
func (h *headerStore) readRaw(seekDist uint64) ([]byte, error) {
53-
var headerSize uint32
54-
55-
// Based on the defined header type, we'll determine the number of
56-
// bytes that we need to read past the sync point.
57-
switch h.indexType {
58-
case Block:
59-
headerSize = 80
60-
61-
case RegularFilter:
62-
headerSize = 32
63-
64-
default:
65-
return nil, fmt.Errorf("unknown index type: %v", h.indexType)
53+
// Based on the defined header type, we'll determine the number of bytes
54+
// that we need to read past the sync point.
55+
headerSize, err := h.indexType.Size()
56+
if err != nil {
57+
return nil, err
6658
}
6759

6860
// TODO(roasbeef): add buffer pool

headerfs/index.go

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ type HeaderType uint8
4545
const (
4646
// Block is the header type that represents regular Bitcoin block
4747
// headers.
48-
Block HeaderType = iota
48+
Block HeaderType = 0
4949

5050
// RegularFilter is a header type that represents the basic filter
5151
// header type for the filter header chain.
52-
RegularFilter
52+
RegularFilter HeaderType = 1
53+
54+
// UnknownHeader represents an unrecognized header type.
55+
UnknownHeader HeaderType = 255
5356
)
5457

5558
const (
@@ -60,6 +63,9 @@ const (
6063
// header type.
6164
RegularFilterHeaderSize = 32
6265

66+
// UnknownHeaderSize is the size for the unknown header type.
67+
UnknownHeaderSize = 0
68+
6369
// numSubBucketBytes is the number of bytes of a hash that's used as a
6470
// sub bucket to store the index keys (hash->height) in. Storing a large
6571
// number of keys in the same bucket has a large impact on memory usage
@@ -78,14 +84,40 @@ const (
7884
func (h HeaderType) String() string {
7985
switch h {
8086
case Block:
81-
return "Block"
87+
return "BlockHeader"
8288
case RegularFilter:
83-
return "RegularFilter"
89+
return "RegularFilterHeader"
8490
default:
8591
return fmt.Sprintf("UnknownHeaderType(%d)", h)
8692
}
8793
}
8894

95+
// Size returns the size in bytes for a given header type.
96+
func (h HeaderType) Size() (int, error) {
97+
switch h {
98+
case Block:
99+
return BlockHeaderSize, nil
100+
case RegularFilter:
101+
return RegularFilterHeaderSize, nil
102+
default:
103+
return UnknownHeaderSize, fmt.Errorf("unknown header type: "+
104+
"%d", h)
105+
}
106+
}
107+
108+
// TipKey returns the current tip key for the given header type.
109+
// Returns an error if the header type is unknown.
110+
func (h HeaderType) TipKey() ([]byte, error) {
111+
switch h {
112+
case Block:
113+
return bitcoinTip, nil
114+
case RegularFilter:
115+
return regFilterTip, nil
116+
default:
117+
return nil, fmt.Errorf("unknown header type: %d", h)
118+
}
119+
}
120+
89121
// headerIndex is an index stored within the database that allows for random
90122
// access into the on-disk header file. This, in conjunction with a flat file
91123
// of headers consists of header database. The keys have been specifically
@@ -174,13 +206,9 @@ func (h *headerIndex) addHeaders(batch headerBatch) error {
174206
// so we can update the index once all the header entries have
175207
// been updated.
176208
// TODO(roasbeef): only need block tip?
177-
switch h.indexType {
178-
case Block:
179-
tipKey = bitcoinTip
180-
case RegularFilter:
181-
tipKey = regFilterTip
182-
default:
183-
return fmt.Errorf("unknown index type: %v", h.indexType)
209+
tipKey, err := h.indexType.TipKey()
210+
if err != nil {
211+
return err
184212
}
185213

186214
var (
@@ -260,18 +288,11 @@ func (h *headerIndex) chainTipWithTx(tx walletdb.ReadTx) (*chainhash.Hash,
260288

261289
rootBucket := tx.ReadBucket(indexBucket)
262290

263-
var tipKey []byte
264-
265291
// Based on the specified index type of this instance of the index,
266292
// we'll grab the particular key that tracks the chain tip.
267-
switch h.indexType {
268-
case Block:
269-
tipKey = bitcoinTip
270-
case RegularFilter:
271-
tipKey = regFilterTip
272-
default:
273-
return nil, 0, fmt.Errorf("unknown chain tip index type: %v",
274-
h.indexType)
293+
tipKey, err := h.indexType.TipKey()
294+
if err != nil {
295+
return nil, 0, err
275296
}
276297

277298
// Now that we have the particular tip key for this header type, we'll
@@ -312,13 +333,9 @@ func (h *headerIndex) truncateIndex(newTip *chainhash.Hash, remove bool) error {
312333
// Based on the specified index type of this instance of the
313334
// index, we'll grab the key that tracks the tip of the chain
314335
// we need to update.
315-
switch h.indexType {
316-
case Block:
317-
tipKey = bitcoinTip
318-
case RegularFilter:
319-
tipKey = regFilterTip
320-
default:
321-
return fmt.Errorf("unknown index type: %v", h.indexType)
336+
tipKey, err := h.indexType.TipKey()
337+
if err != nil {
338+
return err
322339
}
323340

324341
// If the remove flag is set, then we'll also delete this entry

0 commit comments

Comments
 (0)