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

Commit

Permalink
Reduce CPU overhead of gossip (#10859)
Browse files Browse the repository at this point in the history
  • Loading branch information
koute authored Feb 16, 2022
1 parent b9927cd commit ba88ea2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
6 changes: 4 additions & 2 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 client/finality-grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ futures-timer = "3.0.1"
log = "0.4.8"
parking_lot = "0.11.2"
rand = "0.8.4"
ahash = "0.7.6"
parity-scale-codec = { version = "2.3.1", features = ["derive"] }
sp-application-crypto = { version = "5.0.0", path = "../../primitives/application-crypto" }
sp-arithmetic = { version = "4.0.0", path = "../../primitives/arithmetic" }
Expand Down
34 changes: 17 additions & 17 deletions client/finality-grandpa/src/communication/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@
//!
//! We only send polite messages to peers,

use parity_scale_codec::{Decode, Encode};
use sc_network::{ObservedRole, PeerId, ReputationChange};
use sc_network_gossip::{MessageIntent, ValidatorContext};
use sp_finality_grandpa::AuthorityId;
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero};

use ahash::{AHashMap, AHashSet};
use log::{debug, trace};
use parity_scale_codec::{Decode, Encode};
use prometheus_endpoint::{register, CounterVec, Opts, PrometheusError, Registry, U64};
use rand::seq::SliceRandom;
use sc_network::{ObservedRole, PeerId, ReputationChange};
use sc_network_gossip::{MessageIntent, ValidatorContext};
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use sp_finality_grandpa::AuthorityId;
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero};

use super::{benefit, cost, Round, SetId};
use crate::{environment, CatchUp, CompactCommit, SignedMessage};

use std::{
collections::{HashMap, HashSet, VecDeque},
collections::{HashSet, VecDeque},
time::{Duration, Instant},
};

Expand Down Expand Up @@ -260,15 +260,15 @@ const KEEP_RECENT_ROUNDS: usize = 3;
struct KeepTopics<B: BlockT> {
current_set: SetId,
rounds: VecDeque<(Round, SetId)>,
reverse_map: HashMap<B::Hash, (Option<Round>, SetId)>,
reverse_map: AHashMap<B::Hash, (Option<Round>, SetId)>,
}

impl<B: BlockT> KeepTopics<B> {
fn new() -> Self {
KeepTopics {
current_set: SetId(0),
rounds: VecDeque::with_capacity(KEEP_RECENT_ROUNDS + 2),
reverse_map: HashMap::new(),
reverse_map: Default::default(),
}
}

Expand All @@ -290,7 +290,7 @@ impl<B: BlockT> KeepTopics<B> {
let _ = self.rounds.pop_front();
}

let mut map = HashMap::with_capacity(KEEP_RECENT_ROUNDS + 3);
let mut map = AHashMap::with_capacity(KEEP_RECENT_ROUNDS + 3);
map.insert(super::global_topic::<B>(self.current_set.0), (None, self.current_set));

for &(round, set) in &self.rounds {
Expand Down Expand Up @@ -477,10 +477,10 @@ impl<N> PeerInfo<N> {

/// The peers we're connected to in gossip.
struct Peers<N> {
inner: HashMap<PeerId, PeerInfo<N>>,
inner: AHashMap<PeerId, PeerInfo<N>>,
/// The randomly picked set of `LUCKY_PEERS` we'll gossip to in the first stage of round
/// gossiping.
first_stage_peers: HashSet<PeerId>,
first_stage_peers: AHashSet<PeerId>,
/// The randomly picked set of peers we'll gossip to in the second stage of gossiping if the
/// first stage didn't allow us to spread the voting data enough to conclude the round. This
/// set should have size `sqrt(connected_peers)`.
Expand All @@ -492,10 +492,10 @@ struct Peers<N> {
impl<N> Default for Peers<N> {
fn default() -> Self {
Peers {
inner: HashMap::new(),
first_stage_peers: HashSet::new(),
second_stage_peers: HashSet::new(),
lucky_light_peers: HashSet::new(),
inner: Default::default(),
first_stage_peers: Default::default(),
second_stage_peers: Default::default(),
lucky_light_peers: Default::default(),
}
}
}
Expand Down Expand Up @@ -608,7 +608,7 @@ impl<N: Ord> Peers<N> {
}
});

let mut first_stage_peers = HashSet::new();
let mut first_stage_peers = AHashSet::new();
let mut second_stage_peers = HashSet::new();

// we start by allocating authorities to the first stage set and when the minimum of
Expand Down
1 change: 1 addition & 0 deletions client/network-gossip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ futures-timer = "3.0.1"
libp2p = { version = "0.40.0", default-features = false }
log = "0.4.8"
lru = "0.7.0"
ahash = "0.7.6"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../utils/prometheus" }
sc-network = { version = "0.10.0-dev", path = "../network" }
sp-runtime = { version = "5.0.0", path = "../../primitives/runtime" }
Expand Down
15 changes: 5 additions & 10 deletions client/network-gossip/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@

use crate::{MessageIntent, Network, ValidationResult, Validator, ValidatorContext};

use ahash::AHashSet;
use libp2p::PeerId;
use lru::LruCache;
use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
use sc_network::ObservedRole;
use sp_runtime::traits::{Block as BlockT, Hash, HashFor};
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
iter,
sync::Arc,
time,
time::Instant,
};
use std::{borrow::Cow, collections::HashMap, iter, sync::Arc, time, time::Instant};

// FIXME: Add additional spam/DoS attack protection: https://github.com/paritytech/substrate/issues/1115
// NOTE: The current value is adjusted based on largest production network deployment (Kusama) and
Expand All @@ -56,7 +50,7 @@ mod rep {
}

struct PeerConsensus<H> {
known_messages: HashSet<H>,
known_messages: AHashSet<H>,
}

/// Topic stream message with sender.
Expand Down Expand Up @@ -204,7 +198,8 @@ impl<B: BlockT> ConsensusGossip<B> {
?role,
"Registering peer",
);
self.peers.insert(who.clone(), PeerConsensus { known_messages: HashSet::new() });
self.peers
.insert(who.clone(), PeerConsensus { known_messages: Default::default() });

let validator = self.validator.clone();
let mut context = NetworkContext { gossip: self, network };
Expand Down

0 comments on commit ba88ea2

Please sign in to comment.