From 9361ef376dd36fea6233d2b3272f358751d7891d Mon Sep 17 00:00:00 2001 From: asynchronous rob Date: Fri, 18 Mar 2022 11:12:26 -0500 Subject: [PATCH] have some lenience on outdated messages in statement distribution (#5150) * have some lenience on outdated messages in statement distribution * fmt --- .../network/statement-distribution/src/lib.rs | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/node/network/statement-distribution/src/lib.rs b/node/network/statement-distribution/src/lib.rs index e4f77f406de0..1931c545c0d5 100644 --- a/node/network/statement-distribution/src/lib.rs +++ b/node/network/statement-distribution/src/lib.rs @@ -58,7 +58,7 @@ use indexmap::{map::Entry as IEntry, IndexMap}; use sp_keystore::SyncCryptoStorePtr; use util::runtime::RuntimeInfo; -use std::collections::{hash_map::Entry, HashMap, HashSet}; +use std::collections::{hash_map::Entry, HashMap, HashSet, VecDeque}; use fatality::Nested; @@ -153,6 +153,27 @@ impl StatementDistributionSubsystem { } } +#[derive(Default)] +struct RecentOutdatedHeads { + buf: VecDeque, +} + +impl RecentOutdatedHeads { + fn note_outdated(&mut self, hash: Hash) { + const MAX_BUF_LEN: usize = 10; + + self.buf.push_back(hash); + + while self.buf.len() > MAX_BUF_LEN { + let _ = self.buf.pop_front(); + } + } + + fn is_recent_outdated(&self, hash: &Hash) -> bool { + self.buf.contains(hash) + } +} + /// Tracks our impression of a single peer's view of the candidates a validator has seconded /// for a given relay-parent. /// @@ -1287,6 +1308,7 @@ async fn handle_incoming_message_and_circulate<'a>( gossip_peers: &HashSet, peers: &mut HashMap, active_heads: &'a mut HashMap, + recent_outdated_heads: &RecentOutdatedHeads, ctx: &mut (impl SubsystemContext + overseer::SubsystemContext), message: protocol_v1::StatementDistributionMessage, req_sender: &mpsc::Sender, @@ -1294,8 +1316,17 @@ async fn handle_incoming_message_and_circulate<'a>( ) { let handled_incoming = match peers.get_mut(&peer) { Some(data) => - handle_incoming_message(peer, data, active_heads, ctx, message, req_sender, metrics) - .await, + handle_incoming_message( + peer, + data, + active_heads, + recent_outdated_heads, + ctx, + message, + req_sender, + metrics, + ) + .await, None => None, }; @@ -1331,6 +1362,7 @@ async fn handle_incoming_message<'a>( peer: PeerId, peer_data: &mut PeerData, active_heads: &'a mut HashMap, + recent_outdated_heads: &RecentOutdatedHeads, ctx: &mut (impl SubsystemContext + overseer::SubsystemContext), message: protocol_v1::StatementDistributionMessage, req_sender: &mpsc::Sender, @@ -1347,7 +1379,11 @@ async fn handle_incoming_message<'a>( %relay_parent, "our view out-of-sync with active heads; head not found", ); - report_peer(ctx, peer, COST_UNEXPECTED_STATEMENT).await; + + if !recent_outdated_heads.is_recent_outdated(&relay_parent) { + report_peer(ctx, peer, COST_UNEXPECTED_STATEMENT).await; + } + return None }, }; @@ -1556,6 +1592,7 @@ async fn handle_network_update( gossip_peers: &mut HashSet, authorities: &mut HashMap, active_heads: &mut HashMap, + recent_outdated_heads: &RecentOutdatedHeads, ctx: &mut (impl SubsystemContext + overseer::SubsystemContext), req_sender: &mpsc::Sender, update: NetworkBridgeEvent, @@ -1612,6 +1649,7 @@ async fn handle_network_update( gossip_peers, peers, active_heads, + &*recent_outdated_heads, ctx, message, req_sender, @@ -1653,6 +1691,7 @@ impl StatementDistributionSubsystem { let mut gossip_peers: HashSet = HashSet::new(); let mut authorities: HashMap = HashMap::new(); let mut active_heads: HashMap = HashMap::new(); + let mut recent_outdated_heads = RecentOutdatedHeads::default(); let mut runtime = RuntimeInfo::new(Some(self.keystore.clone())); @@ -1684,6 +1723,7 @@ impl StatementDistributionSubsystem { &mut gossip_peers, &mut authorities, &mut active_heads, + &mut recent_outdated_heads, &req_sender, result?, ) @@ -1701,6 +1741,7 @@ impl StatementDistributionSubsystem { &gossip_peers, &mut peers, &mut active_heads, + &recent_outdated_heads, &req_sender, result.ok_or(FatalError::RequesterReceiverFinished)?, ) @@ -1767,6 +1808,7 @@ impl StatementDistributionSubsystem { gossip_peers: &HashSet, peers: &mut HashMap, active_heads: &mut HashMap, + recent_outdated_heads: &RecentOutdatedHeads, req_sender: &mpsc::Sender, message: RequesterMessage, ) -> JfyiErrorResult<()> { @@ -1814,6 +1856,7 @@ impl StatementDistributionSubsystem { gossip_peers, peers, active_heads, + recent_outdated_heads, ctx, message, req_sender, @@ -1874,6 +1917,7 @@ impl StatementDistributionSubsystem { gossip_peers: &mut HashSet, authorities: &mut HashMap, active_heads: &mut HashMap, + recent_outdated_heads: &mut RecentOutdatedHeads, req_sender: &mpsc::Sender, message: FromOverseer, ) -> Result { @@ -1893,6 +1937,8 @@ impl StatementDistributionSubsystem { hash = ?deactivated, "Deactivating leaf", ); + + recent_outdated_heads.note_outdated(deactivated); } } @@ -1985,6 +2031,7 @@ impl StatementDistributionSubsystem { gossip_peers, authorities, active_heads, + &*recent_outdated_heads, ctx, req_sender, event,