@@ -176,6 +176,21 @@ impl<
176176 )
177177 }
178178
179+ /// Checks if a peer has already seen a specific block.
180+ fn peer_has_seen_block ( & self , peer_id : & PeerId , block_hash : & B256 ) -> bool {
181+ self . peer_state . get ( peer_id) . is_some_and ( |state| state. contains ( block_hash) )
182+ }
183+
184+ /// Marks a block as seen by a peer, returning `true` if this is a duplicate.
185+ fn mark_block_seen ( & mut self , peer_id : PeerId , block_hash : B256 ) -> bool {
186+ let state = self . peer_state . entry ( peer_id) . or_insert_with ( || LruCache :: new ( LRU_CACHE_SIZE ) ) ;
187+ if state. contains ( & block_hash) {
188+ return true ;
189+ }
190+ state. insert ( block_hash) ;
191+ false
192+ }
193+
179194 /// Main execution loop for the [`ScrollNetworkManager`].
180195 pub async fn run ( mut self ) {
181196 loop {
@@ -249,7 +264,7 @@ impl<
249264
250265 for peer in peers {
251266 // Skip peers that have already seen this block
252- if self . peer_state . get ( & peer. remote_id ) . is_some_and ( |state| state . contains ( & hash) ) {
267+ if self . peer_has_seen_block ( & peer. remote_id , & hash) {
253268 continue ;
254269 }
255270
@@ -313,19 +328,14 @@ impl<
313328
314329 // Check if we have already received this block via scroll-wire from this peer, if
315330 // so penalize it.
316- let state =
317- self . peer_state . entry ( peer_id) . or_insert_with ( || LruCache :: new ( LRU_CACHE_SIZE ) ) ;
318-
319- if state. contains ( & block_hash) {
331+ if self . mark_block_seen ( peer_id, block_hash) {
320332 tracing:: warn!( target: "scroll::network::manager" , peer_id = ?peer_id, block = ?block_hash, "Peer sent duplicate block via scroll-wire, penalizing" ) ;
321333 self . inner_network_handle . reputation_change (
322334 peer_id,
323335 reth_network_api:: ReputationChangeKind :: BadBlock ,
324336 ) ;
325337 return None ;
326338 }
327- // Update the state that the peer has received this block
328- state. insert ( block_hash) ;
329339
330340 if self . blocks_seen . contains ( & ( block_hash, signature) ) {
331341 None
@@ -428,17 +438,12 @@ impl<
428438
429439 // Check if we have already received this block from this peer via eth-wire, if so,
430440 // penalize the peer.
431- let state =
432- self . peer_state . entry ( peer_id) . or_insert_with ( || LruCache :: new ( LRU_CACHE_SIZE ) ) ;
433-
434- if state. contains ( & block_hash) {
441+ if self . mark_block_seen ( peer_id, block_hash) {
435442 tracing:: warn!( target: "scroll::bridge::import" , peer_id = ?peer_id, block = ?block_hash, "Peer sent duplicate block via eth-wire, penalizing" ) ;
436443 self . inner_network_handle
437444 . reputation_change ( peer_id, reth_network_api:: ReputationChangeKind :: BadBlock ) ;
438445 return None ;
439446 }
440- // Update the state that the peer has received this block
441- state. insert ( block_hash) ;
442447
443448 if self . blocks_seen . contains ( & ( block_hash, signature) ) {
444449 None
0 commit comments