@@ -16,51 +16,15 @@ use tracing::trace;
1616/// The size of the LRU cache used to track blocks that have been seen by peers.
1717pub const LRU_CACHE_SIZE : u32 = 100 ;
1818
19- /// Tracks block announced and received state for a peer.
20- #[ derive( Debug ) ]
21- pub struct PeerState {
22- /// blocks announced to the peer
23- announced : LruCache < B256 > ,
24- /// blocks received, this is used to penalize peers that send duplicate blocks.
25- received : LruCache < B256 > ,
26- }
27-
28- impl PeerState {
29- /// Creates a new `PeerBlockState` with the specified LRU cache capacity.
30- pub fn new ( capacity : u32 ) -> Self {
31- Self { announced : LruCache :: new ( capacity) , received : LruCache :: new ( capacity) }
32- }
33-
34- /// Check if peer knows about this block (either received or announced).
35- pub fn has_seen ( & self , hash : & B256 ) -> bool {
36- self . announced . contains ( hash) || self . received . contains ( hash)
37- }
38-
39- /// Check if peer has received this block.
40- pub fn has_received ( & self , hash : & B256 ) -> bool {
41- self . received . contains ( hash)
42- }
43-
44- /// Record that this peer has announced this block.
45- pub fn insert_announced ( & mut self , hash : B256 ) {
46- self . announced . insert ( hash) ;
47- }
48-
49- /// Record that this peer has received this block.
50- pub fn insert_received ( & mut self , hash : B256 ) {
51- self . received . insert ( hash) ; // Track for duplicate detection
52- }
53- }
54-
5519/// A manager for the `ScrollWire` protocol.
5620#[ derive( Debug ) ]
5721pub struct ScrollWireManager {
5822 /// A stream of [`ScrollWireEvent`]s produced by the scroll wire protocol.
5923 events : UnboundedReceiverStream < ScrollWireEvent > ,
6024 /// A map of connections to peers.
6125 connections : HashMap < PeerId , UnboundedSender < ScrollMessage > > ,
62- /// Unified state tracking block state and blocks received from each peer via both protocols .
63- peer_state : HashMap < PeerId , PeerState > ,
26+ /// Tracks block hashes received from each peer for duplicate detection .
27+ peer_state : HashMap < PeerId , LruCache < B256 > > ,
6428}
6529
6630impl ScrollWireManager {
@@ -71,7 +35,7 @@ impl ScrollWireManager {
7135 }
7236
7337 /// Announces a new block to the specified peer.
74- pub fn announce_block ( & mut self , peer_id : PeerId , block : & NewBlock , hash : B256 ) {
38+ pub fn announce_block ( & mut self , peer_id : PeerId , block : & NewBlock ) {
7539 if let Entry :: Occupied ( to_connection) = self . connections . entry ( peer_id) {
7640 // We send the block to the peer. If we receive an error we remove the peer from the
7741 // connections map and peer_block_state as the connection is no longer valid.
@@ -81,11 +45,6 @@ impl ScrollWireManager {
8145 to_connection. remove ( ) ;
8246 } else {
8347 trace ! ( target: "scroll::wire::manager" , peer_id = %peer_id, "Announced block to peer" ) ;
84- // Record that we announced this block to the peer
85- self . peer_state
86- . entry ( peer_id)
87- . or_insert_with ( || PeerState :: new ( LRU_CACHE_SIZE ) )
88- . insert_announced ( hash) ;
8948 }
9049 }
9150 }
@@ -96,12 +55,12 @@ impl ScrollWireManager {
9655 }
9756
9857 /// Returns a reference to the peer state map.
99- pub const fn peer_state ( & self ) -> & HashMap < PeerId , PeerState > {
58+ pub const fn peer_state ( & self ) -> & HashMap < PeerId , LruCache < B256 > > {
10059 & self . peer_state
10160 }
10261
10362 /// Returns a mutable reference to the peer state map.
104- pub const fn peer_state_mut ( & mut self ) -> & mut HashMap < PeerId , PeerState > {
63+ pub const fn peer_state_mut ( & mut self ) -> & mut HashMap < PeerId , LruCache < B256 > > {
10564 & mut self . peer_state
10665 }
10766}
0 commit comments