Allow construction of empty filters.

This commit is contained in:
Jim Posen 2018-01-17 14:56:28 -08:00 committed by Olaoluwa Osuntokun
parent 884680ddbd
commit cbc2d0fee6
2 changed files with 7 additions and 13 deletions

View file

@ -372,14 +372,9 @@ func BuildExtFilter(block *wire.MsgBlock) (*gcs.Filter, error) {
// GetFilterHash returns the double-SHA256 of the filter. // GetFilterHash returns the double-SHA256 of the filter.
func GetFilterHash(filter *gcs.Filter) (chainhash.Hash, error) { func GetFilterHash(filter *gcs.Filter) (chainhash.Hash, error) {
var zero chainhash.Hash
if filter == nil {
return zero, nil
}
filterData, err := filter.NBytes() filterData, err := filter.NBytes()
if err != nil { if err != nil {
return zero, err return chainhash.Hash{}, err
} }
return chainhash.DoubleHashH(filterData), nil return chainhash.DoubleHashH(filterData), nil

View file

@ -25,9 +25,6 @@ var (
// ErrPTooBig signifies that the filter can't handle `1/2**P` // ErrPTooBig signifies that the filter can't handle `1/2**P`
// collision probability. // collision probability.
ErrPTooBig = fmt.Errorf("P is too big to fit in uint32") ErrPTooBig = fmt.Errorf("P is too big to fit in uint32")
// ErrNoData signifies that an empty slice was passed.
ErrNoData = fmt.Errorf("No data provided")
) )
const ( const (
@ -98,9 +95,6 @@ func BuildGCSFilter(P uint8, key [KeySize]byte, data [][]byte) (*Filter, error)
// Some initial parameter checks: make sure we have data from which to // Some initial parameter checks: make sure we have data from which to
// build the filter, and make sure our parameters will fit the hash // build the filter, and make sure our parameters will fit the hash
// function we're using. // function we're using.
if len(data) == 0 {
return nil, ErrNoData
}
if uint64(len(data)) >= (1 << 32) { if uint64(len(data)) >= (1 << 32) {
return nil, ErrNTooBig return nil, ErrNTooBig
} }
@ -115,6 +109,11 @@ func BuildGCSFilter(P uint8, key [KeySize]byte, data [][]byte) (*Filter, error)
} }
f.modulusNP = uint64(f.n) << P f.modulusNP = uint64(f.n) << P
// Shortcut if the filter is empty.
if f.n == 0 {
return &f, nil
}
// Build the filter. // Build the filter.
values := make(uint64Slice, 0, len(data)) values := make(uint64Slice, 0, len(data))
b := bstream.NewBStreamWriter(0) b := bstream.NewBStreamWriter(0)
@ -358,7 +357,7 @@ func (f *Filter) MatchAny(key [KeySize]byte, data [][]byte) (bool, error) {
// Basic sanity check. // Basic sanity check.
if len(data) == 0 { if len(data) == 0 {
return false, ErrNoData return false, nil
} }
// Create a filter bitstream. // Create a filter bitstream.