diff --git a/Cargo.lock b/Cargo.lock index e97c90e49236a..4518de0e50201 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,9 +64,9 @@ dependencies = [ [[package]] name = "ahash" -version = "0.7.4" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" dependencies = [ "getrandom 0.2.3", "once_cell", @@ -8333,6 +8333,7 @@ dependencies = [ name = "sc-finality-grandpa" version = "0.10.0-dev" dependencies = [ + "ahash", "assert_matches", "async-trait", "dyn-clone", @@ -8491,6 +8492,7 @@ dependencies = [ name = "sc-network-gossip" version = "0.10.0-dev" dependencies = [ + "ahash", "async-std", "futures 0.3.16", "futures-timer", diff --git a/client/finality-grandpa/Cargo.toml b/client/finality-grandpa/Cargo.toml index 6d4c3b13662dc..6fecebc3fdf60 100644 --- a/client/finality-grandpa/Cargo.toml +++ b/client/finality-grandpa/Cargo.toml @@ -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" } diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index ded918c34aa80..7ac4066780d0f 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -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}, }; @@ -260,7 +260,7 @@ const KEEP_RECENT_ROUNDS: usize = 3; struct KeepTopics { current_set: SetId, rounds: VecDeque<(Round, SetId)>, - reverse_map: HashMap, SetId)>, + reverse_map: AHashMap, SetId)>, } impl KeepTopics { @@ -268,7 +268,7 @@ impl KeepTopics { KeepTopics { current_set: SetId(0), rounds: VecDeque::with_capacity(KEEP_RECENT_ROUNDS + 2), - reverse_map: HashMap::new(), + reverse_map: Default::default(), } } @@ -290,7 +290,7 @@ impl KeepTopics { 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::(self.current_set.0), (None, self.current_set)); for &(round, set) in &self.rounds { @@ -477,10 +477,10 @@ impl PeerInfo { /// The peers we're connected to in gossip. struct Peers { - inner: HashMap>, + inner: AHashMap>, /// The randomly picked set of `LUCKY_PEERS` we'll gossip to in the first stage of round /// gossiping. - first_stage_peers: HashSet, + first_stage_peers: AHashSet, /// 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)`. @@ -492,10 +492,10 @@ struct Peers { impl Default for Peers { 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(), } } } @@ -608,7 +608,7 @@ impl Peers { } }); - 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 diff --git a/client/network-gossip/Cargo.toml b/client/network-gossip/Cargo.toml index 7952d97193ea9..161129c4ce682 100644 --- a/client/network-gossip/Cargo.toml +++ b/client/network-gossip/Cargo.toml @@ -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" } diff --git a/client/network-gossip/src/state_machine.rs b/client/network-gossip/src/state_machine.rs index 7ad9853b3615e..4f06819df64d1 100644 --- a/client/network-gossip/src/state_machine.rs +++ b/client/network-gossip/src/state_machine.rs @@ -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 @@ -56,7 +50,7 @@ mod rep { } struct PeerConsensus { - known_messages: HashSet, + known_messages: AHashSet, } /// Topic stream message with sender. @@ -204,7 +198,8 @@ impl ConsensusGossip { ?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 };