@@ -20,12 +20,13 @@ use crate::{MessageIntent, Network, ValidationResult, Validator, ValidatorContex
2020
2121use ahash:: AHashSet ;
2222use libp2p:: PeerId ;
23- use lru:: LruCache ;
23+ use schnellru:: { ByLength , LruMap } ;
24+
2425use prometheus_endpoint:: { register, Counter , PrometheusError , Registry , U64 } ;
2526use sc_network:: types:: ProtocolName ;
2627use sc_network_common:: role:: ObservedRole ;
2728use sp_runtime:: traits:: { Block as BlockT , Hash , HashFor } ;
28- use std:: { collections:: HashMap , iter, num :: NonZeroUsize , sync:: Arc , time, time:: Instant } ;
29+ use std:: { collections:: HashMap , iter, sync:: Arc , time, time:: Instant } ;
2930
3031// FIXME: Add additional spam/DoS attack protection: https://github.com/paritytech/substrate/issues/1115
3132// NOTE: The current value is adjusted based on largest production network deployment (Kusama) and
@@ -36,7 +37,7 @@ use std::{collections::HashMap, iter, num::NonZeroUsize, sync::Arc, time, time::
3637//
3738// Assuming that each known message is tracked with a 32 byte hash (common for `Block::Hash`), then
3839// this cache should take about 256 KB of memory.
39- const KNOWN_MESSAGES_CACHE_SIZE : usize = 8192 ;
40+ const KNOWN_MESSAGES_CACHE_SIZE : u32 = 8192 ;
4041
4142const REBROADCAST_INTERVAL : time:: Duration = time:: Duration :: from_millis ( 750 ) ;
4243
@@ -155,7 +156,7 @@ where
155156pub struct ConsensusGossip < B : BlockT > {
156157 peers : HashMap < PeerId , PeerConsensus < B :: Hash > > ,
157158 messages : Vec < MessageEntry < B > > ,
158- known_messages : LruCache < B :: Hash , ( ) > ,
159+ known_messages : LruMap < B :: Hash , ( ) > ,
159160 protocol : ProtocolName ,
160161 validator : Arc < dyn Validator < B > > ,
161162 next_broadcast : Instant ,
@@ -181,11 +182,7 @@ impl<B: BlockT> ConsensusGossip<B> {
181182 ConsensusGossip {
182183 peers : HashMap :: new ( ) ,
183184 messages : Default :: default ( ) ,
184- known_messages : {
185- let cap = NonZeroUsize :: new ( KNOWN_MESSAGES_CACHE_SIZE )
186- . expect ( "cache capacity is not zero" ) ;
187- LruCache :: new ( cap)
188- } ,
185+ known_messages : { LruMap :: new ( ByLength :: new ( KNOWN_MESSAGES_CACHE_SIZE ) ) } ,
189186 protocol,
190187 validator,
191188 next_broadcast : Instant :: now ( ) + REBROADCAST_INTERVAL ,
@@ -216,7 +213,7 @@ impl<B: BlockT> ConsensusGossip<B> {
216213 message : Vec < u8 > ,
217214 sender : Option < PeerId > ,
218215 ) {
219- if self . known_messages . put ( message_hash, ( ) ) . is_none ( ) {
216+ if self . known_messages . insert ( message_hash, ( ) ) {
220217 self . messages . push ( MessageEntry { message_hash, topic, message, sender } ) ;
221218
222219 if let Some ( ref metrics) = self . metrics {
@@ -313,7 +310,7 @@ impl<B: BlockT> ConsensusGossip<B> {
313310 ) ;
314311
315312 for ( _, ref mut peer) in self . peers . iter_mut ( ) {
316- peer. known_messages . retain ( |h| known_messages. contains ( h ) ) ;
313+ peer. known_messages . retain ( |h| known_messages. get ( h ) . is_some ( ) ) ;
317314 }
318315 }
319316
@@ -348,7 +345,7 @@ impl<B: BlockT> ConsensusGossip<B> {
348345 for message in messages {
349346 let message_hash = HashFor :: < B > :: hash ( & message[ ..] ) ;
350347
351- if self . known_messages . contains ( & message_hash) {
348+ if self . known_messages . get ( & message_hash) . is_some ( ) {
352349 tracing:: trace!(
353350 target: "gossip" ,
354351 %who,
@@ -545,7 +542,7 @@ mod tests {
545542
546543 macro_rules! push_msg {
547544 ( $consensus: expr, $topic: expr, $hash: expr, $m: expr) => {
548- if $consensus. known_messages. put ( $hash, ( ) ) . is_none ( ) {
545+ if $consensus. known_messages. insert ( $hash, ( ) ) {
549546 $consensus. messages. push( MessageEntry {
550547 message_hash: $hash,
551548 topic: $topic,
@@ -720,8 +717,8 @@ mod tests {
720717
721718 push_msg ! ( consensus, prev_hash, m1_hash, m1) ;
722719 push_msg ! ( consensus, best_hash, m2_hash, m2) ;
723- consensus. known_messages . put ( m1_hash, ( ) ) ;
724- consensus. known_messages . put ( m2_hash, ( ) ) ;
720+ consensus. known_messages . insert ( m1_hash, ( ) ) ;
721+ consensus. known_messages . insert ( m2_hash, ( ) ) ;
725722
726723 consensus. collect_garbage ( ) ;
727724 assert_eq ! ( consensus. messages. len( ) , 2 ) ;
@@ -734,7 +731,7 @@ mod tests {
734731 assert_eq ! ( consensus. messages. len( ) , 1 ) ;
735732 // known messages are only pruned based on size.
736733 assert_eq ! ( consensus. known_messages. len( ) , 2 ) ;
737- assert ! ( consensus. known_messages. contains ( & m2_hash) ) ;
734+ assert ! ( consensus. known_messages. get ( & m2_hash) . is_some ( ) ) ;
738735 }
739736
740737 #[ test]
0 commit comments