From fec42eb8cd25e471f1e8086544548a0527165924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Mon, 19 Jul 2021 16:52:42 +0100 Subject: [PATCH 1/3] gadget: add global timeout for rebroadcasting messages --- Cargo.lock | 1 + beefy-gadget/Cargo.toml | 1 + beefy-gadget/src/gossip.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22aa6f43..5ec419eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -469,6 +469,7 @@ dependencies = [ "sp-utils", "substrate-prometheus-endpoint", "thiserror", + "wasm-timer", ] [[package]] diff --git a/beefy-gadget/Cargo.toml b/beefy-gadget/Cargo.toml index ac6c6836..e4b83eaf 100644 --- a/beefy-gadget/Cargo.toml +++ b/beefy-gadget/Cargo.toml @@ -12,6 +12,7 @@ hex = "0.4" log = "0.4" parking_lot = "0.11" thiserror = "1.0" +wasm-timer = "0.2.5" codec = { version = "2.0.0", package = "parity-scale-codec", features = ["derive"] } prometheus = { package = "substrate-prometheus-endpoint", git = "https://github.com/paritytech/substrate", branch = "master"} diff --git a/beefy-gadget/src/gossip.rs b/beefy-gadget/src/gossip.rs index 7aa473dc..eccbb6ec 100644 --- a/beefy-gadget/src/gossip.rs +++ b/beefy-gadget/src/gossip.rs @@ -15,6 +15,7 @@ // along with this program. If not, see . use std::collections::BTreeMap; +use std::time::Duration; use sc_network::PeerId; use sc_network_gossip::{MessageIntent, ValidationResult, Validator, ValidatorContext}; @@ -23,7 +24,8 @@ use sp_runtime::traits::{Block, Hash, Header, NumberFor}; use codec::{Decode, Encode}; use log::{debug, trace}; -use parking_lot::RwLock; +use parking_lot::{Mutex, RwLock}; +use wasm_timer::Instant; use beefy_primitives::{ crypto::{Public, Signature}, @@ -35,6 +37,9 @@ use crate::keystore::BeefyKeystore; // Limit BEEFY gossip by keeping only a bound number of voting rounds alive. const MAX_LIVE_GOSSIP_ROUNDS: usize = 3; +// Timeout for rebroadcasting messages. +const REBROADCAST_AFTER: Duration = Duration::from_secs(60 * 5); + /// Gossip engine messages topic pub(crate) fn topic() -> B::Hash where @@ -62,6 +67,7 @@ where { topic: B::Hash, known_votes: RwLock>, + next_rebroadcast: Mutex, } impl GossipValidator @@ -72,6 +78,7 @@ where GossipValidator { topic: topic::(), known_votes: RwLock::new(BTreeMap::new()), + next_rebroadcast: Mutex::new(Instant::now() + REBROADCAST_AFTER), } } @@ -188,8 +195,23 @@ where #[allow(clippy::type_complexity)] fn message_allowed<'a>(&'a self) -> Box bool + 'a> { + let do_rebroadcast = { + let now = Instant::now(); + let mut next_rebroadcast = self.next_rebroadcast.lock(); + if now >= *next_rebroadcast { + *next_rebroadcast = now + REBROADCAST_AFTER; + true + } else { + false + } + }; + let known_votes = self.known_votes.read(); - Box::new(move |_who, _intent, _topic, mut data| { + Box::new(move |_who, intent, _topic, mut data| { + if let MessageIntent::PeriodicRebroadcast = intent { + return do_rebroadcast; + } + let msg = match VoteMessage::, Public, Signature>::decode(&mut data) { Ok(vote) => vote, Err(_) => return true, From f0d026fc00bf1a3ea2c84b22a19b60aa7d12588c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Mon, 19 Jul 2021 16:59:18 +0100 Subject: [PATCH 2/3] update rustfmt.toml --- beefy-gadget/src/gossip.rs | 3 +-- rustfmt.toml | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/beefy-gadget/src/gossip.rs b/beefy-gadget/src/gossip.rs index eccbb6ec..730eae1f 100644 --- a/beefy-gadget/src/gossip.rs +++ b/beefy-gadget/src/gossip.rs @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::collections::BTreeMap; -use std::time::Duration; +use std::{collections::BTreeMap, time::Duration}; use sc_network::PeerId; use sc_network_gossip::{MessageIntent, ValidationResult, Validator, ValidatorContext}; diff --git a/rustfmt.toml b/rustfmt.toml index c6aca3b5..0609742c 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,5 @@ edition = "2018" hard_tabs = true max_width = 120 +imports_granularity = "Crate" #width_heuristics = "Off" From aafc761e1b2956de9058e95f1bc4306e588cc617 Mon Sep 17 00:00:00 2001 From: adoerr <0xad@gmx.net> Date: Mon, 19 Jul 2021 19:06:14 +0200 Subject: [PATCH 3/3] make message_allowed() a debug trace --- beefy-gadget/src/gossip.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beefy-gadget/src/gossip.rs b/beefy-gadget/src/gossip.rs index 730eae1f..8244a089 100644 --- a/beefy-gadget/src/gossip.rs +++ b/beefy-gadget/src/gossip.rs @@ -219,7 +219,7 @@ where let round = msg.commitment.block_number; let allowed = GossipValidator::::is_live(&known_votes, &round); - trace!(target: "beefy", "🥩 Message for round #{} allowed: {}", round, allowed); + debug!(target: "beefy", "🥩 Message for round #{} allowed: {}", round, allowed); allowed })