-
Notifications
You must be signed in to change notification settings - Fork 4
/
filter_repository.go
58 lines (47 loc) · 1.3 KB
/
filter_repository.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
package repository
import (
"context"
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/gcs"
"github.com/btcsuite/btcd/btcutil/gcs/builder"
)
const (
// RegularFilter is only filter type supported for now
RegularFilter FilterType = iota
)
var ErrFilterNotFound = errors.New("filter not found")
type FilterRepository interface {
PutFilter(context.Context, *FilterEntry) error
GetFilter(context.Context, FilterKey) (*FilterEntry, error)
}
// FilterEntry is the base filter structure using to store filter data.
type FilterEntry struct {
Key FilterKey
NBytes []byte
}
func NewFilterEntry(key FilterKey, filter *gcs.Filter) (*FilterEntry, error) {
nBytes, err := filter.NBytes()
if err != nil {
return nil, err
}
return &FilterEntry{
Key: key,
NBytes: nBytes,
}, nil
}
func (f *FilterEntry) GcsFilter() (*gcs.Filter, error) {
return gcs.FromNBytes(builder.DefaultP, builder.DefaultM, f.NBytes)
}
type FilterType byte
// FilterKey is the unique key for a filter.
// for each possible key, the repository should store 1 unique filter
type FilterKey struct {
BlockHash []byte
FilterType FilterType
}
func (k FilterKey) String() string {
hashedKey := btcutil.Hash160(append(k.BlockHash, byte(k.FilterType)))
return hex.EncodeToString(hashedKey[:6])
}