Skip to content

Commit

Permalink
adds schnellru 0.2.3 which has iter_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
anujmax committed May 14, 2024
1 parent 129b0f2 commit 857095a
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ async-oneshot = "0.5.9"
futures = "0.3.29"
futures-timer = "3.0.3"
jsonrpsee = { version = "0.22.5", features = ["server", "macros"] }
schnellru = "0.2.1"
parity-scale-codec = "3.6.9"
parking_lot = "0.12.2"
schnellru = "0.2.3"
sc-client-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
sc-consensus-subspace = { version = "0.1.0", path = "../sc-consensus-subspace" }
sc-rpc = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ targets = ["x86_64-unknown-linux-gnu"]
async-trait = "0.1.80"
codec = { package = "parity-scale-codec", version = "3.6.5", features = ["derive"] }
futures = "0.3.29"
schnellru = "0.2.1"
parking_lot = "0.12.2"
rand = "0.8.5"
rand_chacha = "0.3.1"
rayon = "1.10.0"
schnellru = "0.2.3"
schnorrkel = "0.11.4"
sc-client-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
sc-consensus = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-proof-of-time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ atomic = "0.5.3"
core_affinity = "0.8.1"
derive_more = "0.99.17"
futures = "0.3.29"
schnellru = "0.2.1"
parity-scale-codec = { version = "3.6.9", features = ["derive"] }
parking_lot = "0.12.2"
rayon = "1.10.0"
schnellru = "0.2.3"
sc-client-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
sc-consensus-slots = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
sc-network = { git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" }
Expand Down
25 changes: 8 additions & 17 deletions crates/sc-proof-of-time/src/source/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,24 +324,15 @@ where
async fn handle_next_slot_input(&mut self, next_slot_input: PotNextSlotInput) {
let mut old_proofs = HashMap::<GossipProof, Vec<PeerId>>::new();

let changes: HashMap<_, _> = self
.gossip_cache
.iter()
.map(|(sender, proofs)| {
let mut retained_proofs = VecDeque::new();
for proof in proofs {
if proof.slot > next_slot_input.slot {
retained_proofs.push_back(*proof);
} else {
old_proofs.entry(*proof).or_default().push(*sender);
}
for (sender, proofs) in &mut self.gossip_cache.iter_mut() {
proofs.retain(|proof| {
if proof.slot > next_slot_input.slot {
true
} else {
old_proofs.entry(*proof).or_default().push(*sender);
false
}
(*sender, retained_proofs)
})
.collect();

for (sender, retained_proofs) in changes {
self.gossip_cache.insert(sender, retained_proofs);
});
}

let mut potentially_matching_proofs = Vec::new();
Expand Down
34 changes: 11 additions & 23 deletions crates/sc-proof-of-time/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use parking_lot::Mutex;
use schnellru::{ByLength, LruMap};
use sp_consensus_slots::Slot;
use sp_consensus_subspace::{PotNextSlotInput, PotParametersChange};
use std::fmt;
use std::num::NonZeroU32;
use std::sync::Arc;
use subspace_core_primitives::{PotCheckpoints, PotOutput, PotSeed};
Expand All @@ -27,24 +26,14 @@ struct CacheValue {
#[derive(Debug, Clone)]
pub struct PotVerifier {
genesis_seed: PotSeed,
cache: Arc<Mutex<PotVerifierLruMap>>,
}

struct PotVerifierLruMap(LruMap<CacheKey, CacheValue>);

impl fmt::Debug for PotVerifierLruMap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter()).finish()
}
cache: Arc<Mutex<LruMap<CacheKey, CacheValue>>>,
}

impl PotVerifier {
pub fn new(genesis_seed: PotSeed, cache_size: u32) -> Self {
Self {
genesis_seed,
cache: Arc::new(Mutex::new(PotVerifierLruMap(LruMap::new(ByLength::new(
cache_size,
))))),
cache: Arc::new(Mutex::new(LruMap::new(ByLength::new(cache_size)))),
}
}

Expand All @@ -55,7 +44,7 @@ impl PotVerifier {
slot_iterations: NonZeroU32,
checkpoints: PotCheckpoints,
) {
self.cache.lock().0.insert(
self.cache.lock().insert(
CacheKey {
seed,
slot_iterations,
Expand Down Expand Up @@ -94,7 +83,6 @@ impl PotVerifier {

self.cache
.lock()
.0
.get(&cache_key)
.and_then(|value| value.checkpoints.try_lock()?.as_ref().copied())
}
Expand Down Expand Up @@ -182,7 +170,7 @@ impl PotVerifier {

loop {
let mut cache = self.cache.lock();
let maybe_cache_value = cache.0.get(&cache_key).cloned();
let maybe_cache_value = cache.get(&cache_key).cloned();
if let Some(cache_value) = maybe_cache_value {
drop(cache);
let correct_checkpoints = cache_value.checkpoints.lock();
Expand All @@ -208,7 +196,7 @@ impl PotVerifier {
.try_lock()
.expect("No one can access this mutex yet; qed");
// Store pending verification entry in cache
cache.0.insert(cache_key, cache_value);
cache.insert(cache_key, cache_value);
// Cache lock is no longer necessary, other callers should be able to access cache too
drop(cache);

Expand All @@ -219,13 +207,13 @@ impl PotVerifier {
drop(checkpoints);

// Proving failed, remove pending entry from cache such that retries can happen
let maybe_removed_cache_value = self.cache.lock().0.remove(&cache_key);
let maybe_removed_cache_value = self.cache.lock().remove(&cache_key);
if let Some(removed_cache_value) = maybe_removed_cache_value {
// It is possible that we have removed a verified value that we have not
// inserted, check for this and restore if that was the case
let removed_verified_value = removed_cache_value.checkpoints.lock().is_some();
if removed_verified_value {
self.cache.lock().0.insert(cache_key, removed_cache_value);
self.cache.lock().insert(cache_key, removed_cache_value);
}
}
return None;
Expand Down Expand Up @@ -259,7 +247,7 @@ impl PotVerifier {

loop {
let mut cache = self.cache.lock();
if let Some(cache_value) = cache.0.get(&cache_key).cloned() {
if let Some(cache_value) = cache.get(&cache_key).cloned() {
drop(cache);
let correct_checkpoints = cache_value.checkpoints.lock();
if let Some(correct_checkpoints) = correct_checkpoints.as_ref() {
Expand All @@ -279,7 +267,7 @@ impl PotVerifier {
.try_lock()
.expect("No one can access this mutex yet; qed");
// Store pending verification entry in cache
cache.0.insert(cache_key, cache_value);
cache.insert(cache_key, cache_value);
// Cache lock is no longer necessary, other callers should be able to access cache too
drop(cache);

Expand All @@ -292,13 +280,13 @@ impl PotVerifier {
drop(correct_checkpoints);

// Verification failed, remove pending entry from cache such that retries can happen
let maybe_removed_cache_value = self.cache.lock().0.remove(&cache_key);
let maybe_removed_cache_value = self.cache.lock().remove(&cache_key);
if let Some(removed_cache_value) = maybe_removed_cache_value {
// It is possible that we have removed a verified value that we have not
// inserted, check for this and restore if that was the case
let removed_verified_value = removed_cache_value.checkpoints.lock().is_some();
if removed_verified_value {
self.cache.lock().0.insert(cache_key, removed_cache_value);
self.cache.lock().insert(cache_key, removed_cache_value);
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-farmer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ futures = "0.3.29"
hex = { version = "0.4.3", features = ["serde"] }
hwlocality = { version = "1.0.0-alpha.3", features = ["vendored"], optional = true }
jsonrpsee = { version = "0.22.5", features = ["client"] }
schnellru = "0.2.1"
mimalloc = "0.1.41"
num_cpus = "1.16.0"
parity-scale-codec = "3.6.9"
Expand All @@ -41,6 +40,7 @@ pin-project = "1.1.5"
prometheus-client = "0.22.2"
rand = "0.8.5"
rayon = "1.10.0"
schnellru = "0.2.3"
schnorrkel = "0.11.4"
serde = { version = "1.0.199", features = ["derive"] }
serde_json = "1.0.116"
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ fs2 = "0.4.3"
futures = "0.3.29"
futures-timer = "3.0.3"
hex = "0.4.3"
schnellru = "0.2.1"
memmap2 = "0.9.4"
nohash-hasher = "0.2.0"
parity-scale-codec = "3.6.9"
parking_lot = "0.12.2"
pin-project = "1.1.5"
prometheus-client = "0.22.2"
rand = "0.8.5"
schnellru = "0.2.3"
serde = { version = "1.0.199", features = ["derive"] }
serde_json = "1.0.116"
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
Expand Down
19 changes: 4 additions & 15 deletions crates/subspace-networking/src/constructor/temporary_bans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
use libp2p::PeerId;
use schnellru::{ByLength, LruMap};
use std::fmt;
use std::ops::Add;
use std::time::Instant;

Expand Down Expand Up @@ -58,27 +57,19 @@ impl TemporaryBan {
}
}

struct TemporaryBannedPeersLruMap(LruMap<PeerId, TemporaryBan>);

impl fmt::Debug for TemporaryBannedPeersLruMap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter()).finish()
}
}

/// Collection of temporary bans that help to prevent reaching out to the same peer ID over and
/// over again.
#[derive(Debug)]
pub(crate) struct TemporaryBans {
backoff: ExponentialBackoff,
list: TemporaryBannedPeersLruMap,
list: LruMap<PeerId, TemporaryBan>,
}

impl TemporaryBans {
pub(super) fn new(capacity: u32, backoff: ExponentialBackoff) -> Self {
Self {
backoff,
list: TemporaryBannedPeersLruMap(LruMap::new(ByLength::new(capacity))),
list: LruMap::new(ByLength::new(capacity)),
}
}

Expand All @@ -88,19 +79,17 @@ impl TemporaryBans {
/// new connection attempt is allowed to be made.
pub(crate) fn is_banned(&self, peer_id: &PeerId) -> bool {
self.list
.0
.peek(peer_id)
.map(TemporaryBan::is_active)
.unwrap_or_default()
}

/// Create temporary ban for peer or extend existing ban
pub(crate) fn create_or_extend(&mut self, peer_id: &PeerId) {
if let Some(ban) = self.list.0.get(peer_id) {
if let Some(ban) = self.list.get(peer_id) {
ban.try_extend();
} else {
self.list
.0
.insert(*peer_id, TemporaryBan::new(self.backoff.clone()));
}
}
Expand All @@ -109,6 +98,6 @@ impl TemporaryBans {
///
/// Returns `true` if there was an entry for peer during call.
pub(crate) fn remove(&mut self, peer_id: &PeerId) -> bool {
self.list.0.remove(peer_id).is_some()
self.list.remove(peer_id).is_some()
}
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2024-04-22"
components = ["rust-src"]
targets = ["wasm32-unknown-unknown"]
targets = ["x86_64-apple-darwin"]
profile = "default"

0 comments on commit 857095a

Please sign in to comment.