@@ -57,6 +57,8 @@ func TestGossiperGossip(t *testing.T) {
5757 responder []* testTx // what the peer we're requesting gossip from has
5858 expectedPossibleValues []* testTx // possible values we can have
5959 expectedLen int
60+ expectedObserved bool
61+ expectedObservedVal uint64
6062 }{
6163 {
6264 name : "no gossip - no one knows anything" ,
@@ -75,13 +77,16 @@ func TestGossiperGossip(t *testing.T) {
7577 responder : []* testTx {{id : ids.ID {0 }}},
7678 expectedPossibleValues : []* testTx {{id : ids.ID {0 }}},
7779 expectedLen : 1 ,
80+ expectedObservedVal : 100 ,
81+ expectedObserved : true ,
7882 },
7983 {
8084 name : "gossip - requester knows nothing" ,
8185 targetResponseSize : 1024 ,
8286 responder : []* testTx {{id : ids.ID {0 }}},
8387 expectedPossibleValues : []* testTx {{id : ids.ID {0 }}},
8488 expectedLen : 1 ,
89+ expectedObserved : true ,
8590 },
8691 {
8792 name : "gossip - requester knows less than responder" ,
@@ -90,13 +95,16 @@ func TestGossiperGossip(t *testing.T) {
9095 responder : []* testTx {{id : ids.ID {0 }}, {id : ids.ID {1 }}},
9196 expectedPossibleValues : []* testTx {{id : ids.ID {0 }}, {id : ids.ID {1 }}},
9297 expectedLen : 2 ,
98+ expectedObservedVal : 50 ,
99+ expectedObserved : true ,
93100 },
94101 {
95102 name : "gossip - target response size exceeded" ,
96103 targetResponseSize : 32 ,
97104 responder : []* testTx {{id : ids.ID {0 }}, {id : ids.ID {1 }}, {id : ids.ID {2 }}},
98105 expectedPossibleValues : []* testTx {{id : ids.ID {0 }}, {id : ids.ID {1 }}, {id : ids.ID {2 }}},
99106 expectedLen : 2 ,
107+ expectedObserved : true ,
100108 },
101109 }
102110
@@ -123,6 +131,12 @@ func TestGossiperGossip(t *testing.T) {
123131
124132 metrics , err := NewMetrics (prometheus .NewRegistry (), "" )
125133 require .NoError (err )
134+
135+ testHistogram := & testHistogram {
136+ Histogram : metrics .bloomFilterHitRate ,
137+ }
138+ metrics .bloomFilterHitRate = testHistogram
139+
126140 marshaller := testMarshaller {}
127141 handler := NewHandler [* testTx ](
128142 logging.NoLog {},
@@ -175,6 +189,8 @@ func TestGossiperGossip(t *testing.T) {
175189
176190 require .Len (requestSet .txs , tt .expectedLen )
177191 require .Subset (tt .expectedPossibleValues , maps .Values (requestSet .txs ))
192+ require .Equal (tt .expectedObserved , testHistogram .observed )
193+ require .Equal (tt .expectedObservedVal , testHistogram .observedVal )
178194
179195 // we should not receive anything that we already had before we
180196 // requested the gossip
@@ -611,3 +627,58 @@ type testValidatorSet struct {
611627func (t testValidatorSet ) Has (_ context.Context , nodeID ids.NodeID ) bool {
612628 return t .validators .Contains (nodeID )
613629}
630+
631+ func TestComputeBloomFilterHitPercentage (t * testing.T ) {
632+ tests := []struct {
633+ name string
634+ hits uint64
635+ misses uint64
636+ percentage uint64
637+ ok bool
638+ }{
639+ {
640+ name : "no hits" ,
641+ hits : 0 ,
642+ misses : 10 ,
643+ percentage : 0 ,
644+ ok : true ,
645+ },
646+ {
647+ name : "no misses" ,
648+ hits : 10 ,
649+ misses : 0 ,
650+ percentage : 100 ,
651+ ok : true ,
652+ },
653+ {
654+ name : "some hits" ,
655+ hits : 5 ,
656+ misses : 5 ,
657+ percentage : 50 ,
658+ ok : true ,
659+ },
660+ {
661+ name : "nothing" ,
662+ },
663+ }
664+
665+ for _ , tt := range tests {
666+ t .Run (tt .name , func (t * testing.T ) {
667+ got , ok := computeBloomFilterHitPercentage (tt .hits , tt .misses , logging.NoLog {})
668+ require .Equal (t , tt .ok , ok )
669+ require .Equal (t , tt .percentage , got )
670+ })
671+ }
672+ }
673+
674+ type testHistogram struct {
675+ prometheus.Histogram
676+ observedVal uint64
677+ observed bool
678+ }
679+
680+ func (t * testHistogram ) Observe (value float64 ) {
681+ t .Histogram .Observe (value )
682+ t .observedVal = uint64 (value )
683+ t .observed = true
684+ }
0 commit comments