Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

gadget: add global timeout for rebroadcasting messages #243

Merged
merged 3 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions beefy-gadget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hex = "0.4"
log = "0.4"
parking_lot = "0.11"
thiserror = "1.0"
wasm-timer = "0.2.5"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::time::Instant does not work on wasm target, we need this for the browser node.


codec = { version = "2.0.0", package = "parity-scale-codec", features = ["derive"] }
prometheus = { package = "substrate-prometheus-endpoint", git = "https://github.com/paritytech/substrate", branch = "master"}
Expand Down
29 changes: 25 additions & 4 deletions beefy-gadget/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::collections::BTreeMap;
use std::{collections::BTreeMap, time::Duration};

use sc_network::PeerId;
use sc_network_gossip::{MessageIntent, ValidationResult, Validator, ValidatorContext};
Expand All @@ -23,7 +23,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},
Expand All @@ -35,6 +36,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: Block>() -> B::Hash
where
Expand Down Expand Up @@ -62,6 +66,7 @@ where
{
topic: B::Hash,
known_votes: RwLock<KnownVotes<B>>,
next_rebroadcast: Mutex<Instant>,
}

impl<B> GossipValidator<B>
Expand All @@ -72,6 +77,7 @@ where
GossipValidator {
topic: topic::<B>(),
known_votes: RwLock::new(BTreeMap::new()),
next_rebroadcast: Mutex::new(Instant::now() + REBROADCAST_AFTER),
}
}

Expand Down Expand Up @@ -188,8 +194,23 @@ where

#[allow(clippy::type_complexity)]
fn message_allowed<'a>(&'a self) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> 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::<MmrRootHash, NumberFor<B>, Public, Signature>::decode(&mut data) {
Ok(vote) => vote,
Err(_) => return true,
Expand All @@ -198,7 +219,7 @@ where
let round = msg.commitment.block_number;
let allowed = GossipValidator::<B>::is_live(&known_votes, &round);

trace!(target: "beefy", "🥩 Message for round #{} allowed: {}", round, allowed);
debug!(target: "beefy", "🥩 Message for round #{} allowed: {}", round, allowed);

allowed
})
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
edition = "2018"
hard_tabs = true
max_width = 120
imports_granularity = "Crate"
#width_heuristics = "Off"