Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Clear cached validator requests on disconnect #3254

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node/bft/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ 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);
self.cache.clear_outbound_validators_requests(peer_ip);
raychu86 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions node/bft/src/helpers/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct Cache<N: Network> {
/// The ordered timestamp map of peer IPs and their cache hits on transmission requests.
seen_outbound_transmissions: RwLock<BTreeMap<i64, HashMap<SocketAddr, u32>>>,
/// The map of IPs to the number of validators requests.
///
raychu86 marked this conversation as resolved.
Show resolved Hide resolved
/// Note: 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.
niklaslong marked this conversation as resolved.
Show resolved Hide resolved
seen_outbound_validators_requests: RwLock<HashMap<SocketAddr, u32>>,
}

Expand Down Expand Up @@ -119,6 +123,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 +302,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));
}
}