@@ -45,11 +45,14 @@ type HeaderType uint8
4545const (
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
5558const (
@@ -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 (
7884func (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