Skip to content
Open
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.

17 changes: 14 additions & 3 deletions p2p/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ pub fn peer_db(c: &mut Criterion) {
let db_store = peerdb_inmemory_store();
let chain_config = create_unit_test_config();
let p2p_config = Arc::new(test_p2p_config());
let mut peerdb =
PeerDb::<_>::new(&chain_config, p2p_config, Default::default(), db_store).unwrap();
let mut peerdb = PeerDb::<_>::new(
&chain_config,
p2p_config,
Default::default(),
db_store,
&mut rng,
)
.unwrap();

for _ in 0..100000 {
peerdb.peer_discovered(TestAddressMaker::new_random_address(&mut rng).into());
Expand All @@ -54,7 +60,12 @@ pub fn peer_db(c: &mut Criterion) {

c.bench_function("PeerDb", |b| {
b.iter(|| {
peerdb.select_non_reserved_outbound_addresses(&outbound_addr_groups, &|_| true, 11)
peerdb.select_non_reserved_outbound_addresses(
&outbound_addr_groups,
&|_| true,
11,
&mut rng,
)
})
});
}
Expand Down
3 changes: 3 additions & 0 deletions p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use networking::transport::{
TcpTransportSocket,
};
use peer_manager::peerdb::storage::PeerDbStorage;
use randomness::make_true_rng;
use types::socket_address::SocketAddress;

use crate::{
Expand Down Expand Up @@ -155,6 +156,8 @@ where
peer_mgr_event_receiver,
time_getter.clone(),
peerdb_storage,
// Note: a "true" rng is not really needed here, but `make_pseudo_rng`'s result is not `Send`.
make_true_rng(),
)?;
let shutdown_ = Arc::clone(&shutdown);
let peer_manager_task = tokio_spawn_in_current_tracing_span(
Expand Down
18 changes: 12 additions & 6 deletions p2p/src/peer_manager/addr_list_response_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
};

use common::primitives::time::Time;
use randomness::{make_pseudo_rng, Rng};
use randomness::Rng;

use super::{peer_context::PeerContext, peerdb::salt::Salt};

Expand Down Expand Up @@ -50,7 +50,13 @@ impl AddrListResponseCache {
}
}

pub fn get_or_create<F>(&mut self, peer_ctx: &PeerContext, now: Time, create: F) -> &Addresses
pub fn get_or_create<F>(
&mut self,
peer_ctx: &PeerContext,
now: Time,
create: F,
rng: &mut impl Rng,
) -> &Addresses
where
F: FnOnce() -> Addresses,
{
Expand All @@ -61,13 +67,13 @@ impl AddrListResponseCache {
let cache_entry = match self.cache.entry(id) {
Entry::Vacant(entry) => entry.insert(CacheEntry {
addresses: create(),
expiration_time: Self::new_expiration_time_from_now(now),
expiration_time: Self::new_expiration_time_from_now(now, rng),
}),
Entry::Occupied(mut entry) => {
if entry.get().expiration_time <= now {
*entry.get_mut() = CacheEntry {
addresses: create(),
expiration_time: Self::new_expiration_time_from_now(now),
expiration_time: Self::new_expiration_time_from_now(now, rng),
};
}
entry.into_mut()
Expand Down Expand Up @@ -101,10 +107,10 @@ impl AddrListResponseCache {
hasher.finish()
}

fn new_expiration_time_from_now(now: Time) -> Time {
fn new_expiration_time_from_now(now: Time, rng: &mut impl Rng) -> Time {
let min_secs = EXPIRATION_INTERVAL_MIN.as_secs();
let max_secs = EXPIRATION_INTERVAL_MAX.as_secs();
let secs = make_pseudo_rng().gen_range(min_secs..=max_secs);
let secs = rng.gen_range(min_secs..=max_secs);
(now + Duration::from_secs(secs)).expect("Unexpected time overflow")
}
}
Expand Down
9 changes: 5 additions & 4 deletions p2p/src/peer_manager/dns_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
use std::sync::Arc;

use async_trait::async_trait;

use common::chain::ChainConfig;
use logging::log;
use p2p_types::{peer_address::PeerAddress, socket_address::SocketAddress};
use randomness::{make_pseudo_rng, seq::IteratorRandom};
use randomness::{seq::IteratorRandom, RngCore};

use crate::config::P2pConfig;

#[async_trait]
pub trait DnsSeed: Send + Sync {
async fn obtain_addresses(&self) -> Vec<SocketAddress>;
async fn obtain_addresses(&self, rng: &mut (dyn RngCore + Send)) -> Vec<SocketAddress>;
}

pub struct DefaultDnsSeed {
Expand All @@ -47,7 +48,7 @@ const MAX_DNS_RECORDS: usize = 10;

#[async_trait]
impl DnsSeed for DefaultDnsSeed {
async fn obtain_addresses(&self) -> Vec<SocketAddress> {
async fn obtain_addresses(&self, rng: &mut (dyn RngCore + Send)) -> Vec<SocketAddress> {
let dns_seeds = self.chain_config.dns_seeds();

if dns_seeds.is_empty() {
Expand All @@ -73,7 +74,7 @@ impl DnsSeed for DefaultDnsSeed {
)
})
// Randomize selection because records can be sorted by type (A and AAAA)
.choose_multiple(&mut make_pseudo_rng(), MAX_DNS_RECORDS)
.choose_multiple(rng, MAX_DNS_RECORDS)
.into_iter()
.for_each(|addr| {
addresses.push(addr);
Expand Down
Loading
Loading