Skip to content

Commit

Permalink
Unexport fields from gossip.BloomFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Dec 23, 2023
1 parent a809807 commit 23b9650
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
34 changes: 21 additions & 13 deletions network/p2p/gossip/bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,43 @@ func NewBloomFilter(

salt, err := randomSalt()
return &BloomFilter{
Bloom: bloom,
Salt: salt,
bloom: bloom,
salt: salt,
}, err
}

type BloomFilter struct {
Bloom *bloomfilter.Filter
// Salt is provided to eventually unblock collisions in Bloom. It's possible
bloom *bloomfilter.Filter
// salt is provided to eventually unblock collisions in Bloom. It's possible
// that conflicting Gossipable items collide in the bloom filter, so a salt
// is generated to eventually resolve collisions.
Salt ids.ID
salt ids.ID
}

func (b *BloomFilter) Add(gossipable Gossipable) {
h := gossipable.GossipID()
salted := &hasher{
hash: h[:],
salt: b.Salt,
salt: b.salt,
}
b.Bloom.Add(salted)
b.bloom.Add(salted)
}

func (b *BloomFilter) Has(gossipable Gossipable) bool {
h := gossipable.GossipID()
salted := &hasher{
hash: h[:],
salt: b.Salt,
salt: b.salt,
}
return b.Bloom.Contains(salted)
return b.bloom.Contains(salted)
}

func (b *BloomFilter) Marshal() ([]byte, []byte, error) {
bloomBytes, err := b.bloom.MarshalBinary()
// salt must be copied here to ensure the bytes aren't overwritten if salt
// is later modified.
salt := b.salt
return bloomBytes, salt[:], err
}

// ResetBloomFilterIfNeeded resets a bloom filter if it breaches a target false
Expand All @@ -69,11 +77,11 @@ func ResetBloomFilterIfNeeded(
bloomFilter *BloomFilter,
falsePositiveProbability float64,
) (bool, error) {
if bloomFilter.Bloom.FalsePosititveProbability() < falsePositiveProbability {
if bloomFilter.bloom.FalsePosititveProbability() < falsePositiveProbability {
return false, nil
}

newBloom, err := bloomfilter.New(bloomFilter.Bloom.M(), bloomFilter.Bloom.K())
newBloom, err := bloomfilter.New(bloomFilter.bloom.M(), bloomFilter.bloom.K())
if err != nil {
return false, err
}
Expand All @@ -82,8 +90,8 @@ func ResetBloomFilterIfNeeded(
return false, err
}

bloomFilter.Bloom = newBloom
bloomFilter.Salt = salt
bloomFilter.bloom = newBloom
bloomFilter.salt = salt
return true, nil
}

Expand Down
15 changes: 13 additions & 2 deletions network/p2p/gossip/bloom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/stretchr/testify/require"

"golang.org/x/exp/slices"

"github.com/ava-labs/avalanchego/ids"
)

Expand Down Expand Up @@ -49,16 +51,25 @@ func TestBloomFilterRefresh(t *testing.T) {
b, err := bloomfilter.New(10, 1)
require.NoError(err)
bloom := BloomFilter{
Bloom: b,
bloom: b,
}

for _, item := range tt.add {
bloomBytes, saltBytes, err := bloom.Marshal()
require.NoError(err)

initialBloomBytes := slices.Clone(bloomBytes)
initialSaltBytes := slices.Clone(saltBytes)

_, err = ResetBloomFilterIfNeeded(&bloom, tt.falsePositiveProbability)
require.NoError(err)
bloom.Add(item)

require.Equal(initialBloomBytes, bloomBytes)
require.Equal(initialSaltBytes, saltBytes)
}

require.Equal(uint64(len(tt.expected)), bloom.Bloom.N())
require.Equal(uint64(len(tt.expected)), bloom.bloom.N())

for _, expected := range tt.expected {
require.True(bloom.Has(expected))
Expand Down
6 changes: 3 additions & 3 deletions network/p2p/gossip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (h Handler[T]) AppRequest(_ context.Context, _ ids.NodeID, _ time.Time, req
}

filter := &BloomFilter{
Bloom: &bloomfilter.Filter{},
Salt: salt,
bloom: &bloomfilter.Filter{},
salt: salt,
}
if err := filter.Bloom.UnmarshalBinary(request.Filter); err != nil {
if err := filter.bloom.UnmarshalBinary(request.Filter); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions network/p2p/gossip/test_gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ func (t *testSet) Iterate(f func(gossipable *testTx) bool) {
}

func (t *testSet) GetFilter() ([]byte, []byte, error) {
bloom, err := t.bloom.Bloom.MarshalBinary()
return bloom, t.bloom.Salt[:], err
return t.bloom.Marshal()
}
3 changes: 1 addition & 2 deletions vms/avm/network/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,5 @@ func (g *gossipMempool) GetFilter() (bloom []byte, salt []byte, err error) {
g.lock.RLock()
defer g.lock.RUnlock()

bloomBytes, err := g.bloom.Bloom.MarshalBinary()
return bloomBytes, g.bloom.Salt[:], err
return g.bloom.Marshal()
}

0 comments on commit 23b9650

Please sign in to comment.