Skip to content
Draft
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
318 changes: 280 additions & 38 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ multiaddr = "0.18.1"
multihash = "0.19.1"
multistream-select = { version = "0.14.0", path = "misc/multistream-select" }
prometheus-client = "0.24"
rand = "0.10"
quick-protobuf-codec = { version = "0.4.0", path = "misc/quick-protobuf-codec" }
quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" }
rcgen = "0.13"
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ multistream-select = { workspace = true }
parking_lot = "0.12.3"
pin-project = "1.1.11"
quick-protobuf = "0.8"
rand = "0.8"
rand = { workspace = true }
rw-stream-sink = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/autonatv2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ clap = { version = "4.4.18", features = ["derive"] }
tokio = { version = "1.50.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.44"
tracing-subscriber = { workspace = true, features = ["env-filter"] }
rand = "0.8.5"
rand = { workspace = true }
opentelemetry = { version = "0.27.0", optional = true }
opentelemetry_sdk = { version = "0.27.0", optional = true, features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.27.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions examples/autonatv2/src/bin/autonatv2_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use libp2p::{
swarm::{NetworkBehaviour, SwarmEvent, dial_opts::DialOpts},
tcp, yamux,
};
use rand::rngs::OsRng;
use rand::{SeedableRng, rngs::StdRng};
use tracing_subscriber::EnvFilter;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Behaviour {
pub fn new(key: identity::PublicKey, probe_interval: u64) -> Self {
Self {
autonat: autonat::v2::client::Behaviour::new(
OsRng,
StdRng::from_rng(&mut rand::rng()),
autonat::v2::client::Config::default()
.with_probe_interval(Duration::from_secs(probe_interval)),
),
Expand Down
4 changes: 2 additions & 2 deletions examples/autonatv2/src/bin/autonatv2_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use libp2p::{
swarm::{NetworkBehaviour, SwarmEvent},
tcp, yamux,
};
use rand::rngs::OsRng;
use rand::{SeedableRng, rngs::StdRng};

#[derive(Debug, Parser)]
#[command(name = "libp2p autonatv2 server")]
Expand Down Expand Up @@ -91,7 +91,7 @@ pub struct Behaviour {
impl Behaviour {
pub fn new(key: identity::PublicKey) -> Self {
Self {
autonat: autonat::v2::server::Behaviour::new(OsRng),
autonat: autonat::v2::server::Behaviour::new(StdRng::from_rng(&mut rand::rng())),
identify: identify::Behaviour::new(identify::Config::new("/ipfs/0.1.0".into(), key)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-webrtc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crate-type = ["cdylib"]
[dependencies]
anyhow = "1.0.102"
futures = { workspace = true }
rand = "0.8"
rand = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

Expand Down
3 changes: 1 addition & 2 deletions examples/browser-webrtc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use libp2p::{
swarm::SwarmEvent,
};
use libp2p_webrtc as webrtc;
use rand::thread_rng;
use tokio::net::TcpListener;
use tower_http::cors::{Any, CorsLayer};

Expand All @@ -33,7 +32,7 @@ async fn main() -> anyhow::Result<()> {
.with_other_transport(|id_keys| {
Ok(webrtc::tokio::Transport::new(
id_keys.clone(),
webrtc::tokio::Certificate::generate(&mut thread_rng())?,
webrtc::tokio::Certificate::generate(&mut rand::rng())?,
)
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn))))
})?
Expand Down
2 changes: 1 addition & 1 deletion examples/stream/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ anyhow = "1"
futures = { workspace = true }
libp2p = { path = "../../libp2p", features = [ "tokio", "quic"] }
libp2p-stream = { path = "../../protocols/stream", version = "0.5.0-alpha" }
rand = "0.8"
rand = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Expand Down
6 changes: 3 additions & 3 deletions examples/stream/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use futures::{AsyncReadExt, AsyncWriteExt, StreamExt};
use libp2p::{Multiaddr, PeerId, Stream, StreamProtocol, multiaddr::Protocol};
use libp2p_stream as stream;
use rand::RngCore;
use rand::RngExt;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -137,10 +137,10 @@ async fn echo(mut stream: Stream) -> io::Result<usize> {
}

async fn send(mut stream: Stream) -> io::Result<()> {
let num_bytes = rand::random::<usize>() % 1000;
let num_bytes = rand::rng().random_range(0..1000);

let mut bytes = vec![0; num_bytes];
rand::thread_rng().fill_bytes(&mut bytes);
rand::fill(&mut bytes);

stream.write_all(&bytes).await?;

Expand Down
2 changes: 1 addition & 1 deletion identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tracing = { workspace = true }
multihash = { version = "0.19.1", optional = true }
p256 = { version = "0.13", default-features = false, features = ["ecdsa", "std", "pem"], optional = true }
quick-protobuf = { version = "0.8.1", optional = true }
rand = { version = "0.8", optional = true }
rand = { workspace = true, optional = true }
sec1 = { version = "0.7", default-features = false, optional = true }
serde = { version = "1", optional = true, features = ["derive"] }
sha2 = { version = "0.10.8", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion identity/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use p256::{
signature::{Signer, Verifier},
Signature, SigningKey, VerifyingKey,
},
elliptic_curve::rand_core::OsRng,
EncodedPoint,
};
use sec1::{DecodeEcPrivateKey, EncodeEcPrivateKey};
Expand Down Expand Up @@ -96,7 +97,7 @@ impl SecretKey {
/// Generate a new random ECDSA secret key.
#[cfg(feature = "rand")]
pub fn generate() -> SecretKey {
SecretKey(SigningKey::random(&mut rand::thread_rng()))
SecretKey(SigningKey::random(&mut OsRng))
}

/// Sign a message with this secret key, producing a DER-encoded ECDSA signature.
Expand Down
6 changes: 1 addition & 5 deletions identity/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ impl SecretKey {
/// Generate a new Ed25519 secret key.
#[cfg(feature = "rand")]
pub fn generate() -> SecretKey {
use rand::RngCore as _;

let mut secret = ed25519::SecretKey::default();
rand::rngs::OsRng.fill_bytes(&mut secret);
SecretKey(secret)
SecretKey(rand::random())
}

/// Try to parse an Ed25519 secret key from a byte slice
Expand Down
4 changes: 1 addition & 3 deletions identity/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

use std::{fmt, str::FromStr};

#[cfg(feature = "rand")]
use rand::Rng;
use sha2::Digest as _;
use thiserror::Error;

Expand Down Expand Up @@ -106,7 +104,7 @@ impl PeerId {
/// This is useful for randomly walking on a DHT, or for testing purposes.
#[cfg(feature = "rand")]
pub fn random() -> PeerId {
let peer_id = rand::thread_rng().gen::<[u8; 32]>();
let peer_id: [u8; 32] = rand::random();
PeerId {
multihash: Multihash::wrap(0x0, &peer_id).expect("The digest size is never too large"),
}
Expand Down
3 changes: 2 additions & 1 deletion identity/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use core::{cmp, fmt, hash};
use asn1_der::typed::{DerDecodable, Sequence};
use k256::{
ecdsa::Signature,
elliptic_curve::rand_core::OsRng,
sha2::{Digest as ShaDigestTrait, Sha256},
ProjectivePoint,
};
Expand Down Expand Up @@ -94,7 +95,7 @@ impl SecretKey {
/// Generate a new random Secp256k1 secret key.
#[cfg(feature = "rand")]
pub fn generate() -> SecretKey {
SecretKey(k256::ecdsa::SigningKey::random(&mut rand::thread_rng()))
SecretKey(k256::ecdsa::SigningKey::random(&mut OsRng))
}

/// Create a secret key from a byte slice, zeroing the slice on success.
Expand Down
2 changes: 1 addition & 1 deletion interop-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
anyhow = "1"
futures = { workspace = true }
rand = "0.8.5"
rand = { workspace = true }
serde = { version = "1", features = ["derive"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Expand Down
2 changes: 1 addition & 1 deletion interop-tests/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub(crate) mod native {
.with_other_transport(|key| {
Ok(webrtc::tokio::Transport::new(
key.clone(),
webrtc::tokio::Certificate::generate(&mut rand::thread_rng())?,
webrtc::tokio::Certificate::generate(&mut rand::rng())?,
))
})?
.with_behaviour(behaviour_constructor)?
Expand Down
3 changes: 1 addition & 2 deletions misc/connection-limits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ libp2p-ping = { workspace = true }
libp2p-swarm-derive = { path = "../../swarm-derive" }
libp2p-swarm-test = { path = "../../swarm-test" }
quickcheck = { workspace = true }
rand = "0.8.5"

rand = { workspace = true }
[lints]
workspace = true
4 changes: 2 additions & 2 deletions misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ mod tests {
use super::*;

fn fill_outgoing() -> (Swarm<Behaviour>, Multiaddr, u32) {
use rand::Rng;
use rand::RngExt;

let outgoing_limit = rand::thread_rng().gen_range(1..10);
let outgoing_limit = rand::rng().random_range(1..10);

let mut network = Swarm::new_ephemeral_tokio(|_| {
Behaviour::new(
Expand Down
2 changes: 1 addition & 1 deletion misc/webrtc-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ libp2p-identity = { workspace = true }
libp2p-noise = { workspace = true }
quick-protobuf = "0.8"
quick-protobuf-codec = { workspace = true }
rand = "0.8"
rand = { workspace = true }
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.10.8"
tinytemplate = "1.2"
Expand Down
4 changes: 2 additions & 2 deletions misc/webrtc-utils/src/sdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// DEALINGS IN THE SOFTWARE.
use std::net::{IpAddr, SocketAddr};

use rand::{Rng, distributions::Alphanumeric, thread_rng};
use rand::{RngExt, distr::Alphanumeric};
use serde::Serialize;
use tinytemplate::TinyTemplate;

Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn render_description(
pub fn random_ufrag() -> String {
format!(
"libp2p+webrtc+v1/{}",
thread_rng()
rand::rng()
.sample_iter(&Alphanumeric)
.take(64)
.map(char::from)
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ libp2p-core = { workspace = true }
libp2p-identity = { workspace = true }
nohash-hasher = "0.2"
parking_lot = "0.12"
rand = "0.8"
rand = { workspace = true }
smallvec = "1.15.1"
tracing = { workspace = true }
unsigned-varint = { workspace = true, features = ["asynchronous_codec"] }
Expand Down
5 changes: 2 additions & 3 deletions protocols/autonat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ libp2p-swarm = { workspace = true }
quick-protobuf = "0.8"
tracing = { workspace = true }
quick-protobuf-codec = { workspace = true }
rand = "0.8"
rand_core = { version = "0.6", optional = true }
rand = { workspace = true }
thiserror = { workspace = true, optional = true }

[dev-dependencies]
Expand All @@ -43,7 +42,7 @@ libp2p-swarm = { workspace = true, features = ["macros"] }
[features]
default = ["v1", "v2"]
v1 = ["dep:libp2p-request-response", "dep:web-time"]
v2 = ["dep:either", "dep:futures-bounded", "dep:thiserror", "dep:rand_core"]
v2 = ["dep:either", "dep:futures-bounded", "dep:thiserror"]

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
4 changes: 2 additions & 2 deletions protocols/autonat/src/v1/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use libp2p_core::Multiaddr;
use libp2p_identity::PeerId;
use libp2p_request_response::{self as request_response, OutboundFailure, OutboundRequestId};
use libp2p_swarm::{ConnectionId, ListenAddresses, ToSwarm};
use rand::{seq::SliceRandom, thread_rng};
use rand::prelude::IndexedRandom;
use web_time::Instant;

use super::{
Expand Down Expand Up @@ -265,7 +265,7 @@ impl AsClient<'_> {

servers.retain(|s| !self.throttled_servers.iter().any(|(id, _)| s == &id));

servers.choose(&mut thread_rng()).map(|&&p| p)
servers.choose(&mut rand::rng()).map(|&&p| p)
}

// Send a dial-request to a randomly selected server.
Expand Down
17 changes: 8 additions & 9 deletions protocols/autonat/src/v2/client/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use libp2p_swarm::{
NetworkBehaviour, NewExternalAddrCandidate, NotifyHandler, ToSwarm,
behaviour::ConnectionEstablished,
};
use rand::prelude::*;
use rand_core::OsRng;
use rand::{Rng, RngExt, SeedableRng, prelude::*, rngs::StdRng};

use super::handler::{
dial_back::{self, IncomingNonce},
Expand Down Expand Up @@ -58,9 +57,9 @@ impl Default for Config {
}
}

pub struct Behaviour<R = OsRng>
pub struct Behaviour<R = StdRng>
where
R: RngCore + 'static,
R: Rng + Send + 'static,
{
rng: R,
config: Config,
Expand All @@ -77,7 +76,7 @@ where

impl<R> NetworkBehaviour for Behaviour<R>
where
R: RngCore + 'static,
R: Rng + Send + 'static,
{
type ConnectionHandler = Either<dial_request::Handler, dial_back::Handler>;

Expand Down Expand Up @@ -268,7 +267,7 @@ where

impl<R> Behaviour<R>
where
R: RngCore + 'static,
R: Rng + Send + 'static,
{
pub fn new(rng: R, config: Config) -> Self {
Self {
Expand All @@ -294,7 +293,7 @@ where
return;
};

let nonce = self.rng.r#gen();
let nonce = self.rng.random();
self.address_candidates
.get_mut(&addr)
.expect("only emit candidates")
Expand Down Expand Up @@ -369,9 +368,9 @@ where
}
}

impl Default for Behaviour<OsRng> {
impl Default for Behaviour<StdRng> {
fn default() -> Self {
Self::new(OsRng, Config::default())
Self::new(StdRng::from_rng(&mut rand::rng()), Config::default())
}
}

Expand Down
6 changes: 3 additions & 3 deletions protocols/autonat/src/v2/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use asynchronous_codec::{Framed, FramedRead, FramedWrite};
use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt};
use libp2p_core::Multiaddr;
use quick_protobuf_codec::Codec;
use rand::Rng;
use rand::RngExt;

use crate::v2::{Nonce, generated::structs as proto};

Expand Down Expand Up @@ -237,8 +237,8 @@ impl From<Response> for proto::Message {
}

impl DialDataRequest {
pub(crate) fn from_rng<R: rand_core::RngCore>(addr_idx: usize, mut rng: R) -> Self {
let num_bytes = rng.gen_range(DATA_LEN_LOWER_BOUND..=DATA_LEN_UPPER_BOUND);
pub(crate) fn from_rng<R: rand::Rng>(addr_idx: usize, mut rng: R) -> Self {
let num_bytes = rng.random_range(DATA_LEN_LOWER_BOUND..=DATA_LEN_UPPER_BOUND);
Self {
addr_idx,
num_bytes,
Expand Down
Loading
Loading