Skip to content

Commit

Permalink
Unexport fields from gossip.BloomFilter (#2547)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Dec 23, 2023
1 parent a809807 commit 653c2f1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/DataDog/zstd v1.5.2
github.com/Microsoft/go-winio v0.5.2
github.com/NYTimes/gziphandler v1.1.1
github.com/ava-labs/coreth v0.12.9-rc.9.0.20231222191417-2e3f762373e9
github.com/ava-labs/coreth v0.12.10-rc.2
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/coreth v0.12.9-rc.9.0.20231222191417-2e3f762373e9 h1:DiJBkm2IJ/My4u5DP4gq2wIbdflFRuZJbDm8DbgNDdg=
github.com/ava-labs/coreth v0.12.9-rc.9.0.20231222191417-2e3f762373e9/go.mod h1:Xftjgk8T46k5/pWSQWcmdPanNl68kTcufd9S4kB58bM=
github.com/ava-labs/coreth v0.12.10-rc.2 h1:+2YK7PzStcLCHtsBb1VQjw6DyMl1sMZatReZAyIy7w8=
github.com/ava-labs/coreth v0.12.10-rc.2/go.mod h1:RIbv14KMyWSj4hB1danS+vEPCUsgArZUEo99SaP5nW8=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34 h1:mg9Uw6oZFJKytJxgxnl3uxZOs/SB8CVHg6Io4Tf99Zc=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
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 653c2f1

Please sign in to comment.