Skip to content

Commit

Permalink
Merge pull request #3254 from niklaslong/fix/cache
Browse files Browse the repository at this point in the history
[Fix] Clear cached validator requests on disconnect
  • Loading branch information
howardwu authored May 23, 2024
2 parents 9cc67c9 + e00e52d commit 7ff06a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions node/bft/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,11 @@ impl<N: Network> Disconnect for Gateway<N> {
async fn handle_disconnect(&self, peer_addr: SocketAddr) {
if let Some(peer_ip) = self.resolver.get_listener(peer_addr) {
self.remove_connected_peer(peer_ip);

// We don't clear this map based on time but only on peer disconnect.
// This is sufficient to avoid infinite growth as the committee has a fixed number
// of members.
self.cache.clear_outbound_validators_requests(peer_ip);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions node/bft/src/helpers/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ impl<N: Network> Cache<N> {
pub fn decrement_outbound_validators_requests(&self, peer_ip: SocketAddr) -> u32 {
Self::decrement_counter(&self.seen_outbound_validators_requests, peer_ip)
}

/// Clears the the IP's number of validator requests.
pub fn clear_outbound_validators_requests(&self, peer_ip: SocketAddr) {
self.seen_outbound_validators_requests.write().remove(&peer_ip);
}
}

impl<N: Network> Cache<N> {
Expand Down Expand Up @@ -293,4 +298,27 @@ mod tests {
outbound_certificate,
outbound_transmission
}

#[test]
fn test_seen_outbound_validators_requests() {
let cache = Cache::<CurrentNetwork>::default();
let input = Input::input();

// Check the map is empty.
assert!(!cache.contains_outbound_validators_request(input));

// Insert some requests.
for _ in 0..3 {
cache.increment_outbound_validators_requests(input);
assert!(cache.contains_outbound_validators_request(input));
}

// Remove a request.
cache.decrement_outbound_validators_requests(input);
assert!(cache.contains_outbound_validators_request(input));

// Clear all requests.
cache.clear_outbound_validators_requests(input);
assert!(!cache.contains_outbound_validators_request(input));
}
}

0 comments on commit 7ff06a8

Please sign in to comment.