From 15ba1ecac7346c4898788f2394f2366649fc90b6 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 8 Jul 2020 12:25:18 +0000 Subject: [PATCH 1/2] use different flood settings --- client/network/src/protocol.rs | 57 +++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index ff3748bd55cf2..db243e04956b5 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -199,6 +199,17 @@ impl Metrics { } } +/// Flood settings. +/// +/// Rules how many peers are supposed to recieve the message. +#[derive(Clone, Copy)] +pub enum FloodSettings { + /// Send message to everyone. + Everyone, + /// Send message to sqrt(current peer count), but no less than `minimum`. + Sqrt { minimum: usize } +} + struct PendingTransaction { validation: TransactionImportFuture, peer_id: PeerId, @@ -1192,6 +1203,8 @@ impl Protocol { } /// Propagate one transaction. + /// + /// Use this when new valid transsaction with `hash` becomes known. pub fn propagate_transaction( &mut self, hash: &H, @@ -1202,7 +1215,10 @@ impl Protocol { return; } if let Some(transaction) = self.transaction_pool.transaction(hash) { - let propagated_to = self.do_propagate_transactions(&[(hash.clone(), transaction)]); + let propagated_to = self.do_propagate_transactions( + &[(hash.clone(), transaction)], + FloodSettings::Sqrt { minimum: 5 }, + ); self.transaction_pool.on_broadcasted(propagated_to); } } @@ -1210,13 +1226,34 @@ impl Protocol { fn do_propagate_transactions( &mut self, transactions: &[(H, B::Extrinsic)], + flood_settings: FloodSettings, ) -> HashMap> { + use rand::seq::IteratorRandom; + let mut propagated_to = HashMap::new(); - for (who, peer) in self.context_data.peers.iter_mut() { - // never send transactions to the light node - if !peer.info.roles.is_full() { - continue; - } + + let (selected_peers, limit) = match flood_settings { + FloodSettings::Everyone => ( + self.context_data.peers + .iter() + .filter(|(_, peer)| peer.info.roles.is_full()) + .map(|(id, _)| id.clone()) + .collect::>(), + None, + ), + FloodSettings::Sqrt { minimum } => ( + self.context_data.peers + .iter() + .filter(|(_, peer)| peer.info.roles.is_full()) + .map(|(id, _)| id.clone()) + .choose_multiple(&mut rand::thread_rng(), self.context_data.peers.len()), + Some(std::cmp::max(minimum, (self.context_data.peers.len() as f64).sqrt().ceil() as u64 as _)) + ), + }; + + for who in selected_peers.into_iter() { + let peer = self.context_data.peers.get_mut(&who) + .expect("selected_peers has been chosen from existing entries only; qed"); let (hashes, to_send): (Vec<_>, Vec<_>) = transactions .iter() @@ -1239,7 +1276,11 @@ impl Protocol { &who, Some((self.transactions_protocol.clone(), encoded)), GenericMessage::Transactions(to_send) - ) + ); + + if limit.map_or(false, |v| v >= propagated_to.len()) { + break; + } } } @@ -1260,7 +1301,7 @@ impl Protocol { return; } let transactions = self.transaction_pool.transactions(); - let propagated_to = self.do_propagate_transactions(&transactions); + let propagated_to = self.do_propagate_transactions(&transactions, FloodSettings::Everyone); self.transaction_pool.on_broadcasted(propagated_to); } From 1611f43b298e5046a304e72eb71d7fbc13aa1d94 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 8 Jul 2020 17:34:25 +0300 Subject: [PATCH 2/2] Update client/network/src/protocol.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomasz Drwięga --- client/network/src/protocol.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index db243e04956b5..b10a86f1f3739 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -1204,7 +1204,7 @@ impl Protocol { /// Propagate one transaction. /// - /// Use this when new valid transsaction with `hash` becomes known. + /// Use this when new valid transaction with `hash` becomes known. pub fn propagate_transaction( &mut self, hash: &H,