Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps): Bump rand to 0.8 and quickcheck to 1 #2857

Merged
merged 37 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e482d91
Upgrade to rand 0.8
kpp Aug 30, 2022
23c65e7
Fix compiler warning
kpp Aug 30, 2022
94634c2
Upgrade tests to quickcheck=1
kpp Aug 31, 2022
7ac75e9
cargo fmt
kpp Aug 31, 2022
f1e4833
Remove rand dep from dev workspace
kpp Aug 31, 2022
a36e551
Remove Rng param from fn random_peers
kpp Aug 31, 2022
cff5192
Fix kad:rand_distance test
kpp Sep 1, 2022
7dd2ab6
Fix kad:bucket_contains_range test
kpp Sep 1, 2022
d805059
Fix kad:closest_and_disjoint_closest_yield_same_result test
kpp Sep 1, 2022
c092d1b
Fix swarm:score_retention test
kpp Sep 2, 2022
ea2e1ab
Merge branch 'master' into rand08
kpp Sep 2, 2022
f6f6899
Apply suggestions from code review for over/under-flow
kpp Sep 2, 2022
55ed655
cargo fmt
kpp Sep 2, 2022
cc64494
Introduce quickcheck-ext crate with Gen::gen_range
kpp Sep 6, 2022
3757134
Use ::gen_range to generate a random number in .. range
kpp Sep 6, 2022
b48c9a1
cargo fmt
kpp Sep 6, 2022
821bb52
Fix multistream:encode_decode_message test
kpp Sep 6, 2022
3e84211
Add a license to quickcheck-ext
kpp Sep 6, 2022
b0a8cda
Merge branch 'master' into rand08
kpp Sep 6, 2022
f695aac
More unsigned types for gen_range
kpp Sep 6, 2022
f79b939
Add T: Unsigned for gen_range
kpp Sep 6, 2022
7cb1c80
More unsigned types for gen_range
kpp Sep 6, 2022
a3fa247
Merge branch 'master' into rand08
thomaseizinger Sep 8, 2022
25f87c5
Fix build error
thomaseizinger Sep 8, 2022
6a34f5c
Merge branch 'master' into rand08
kpp Sep 12, 2022
49d2aa3
Bump versions and add changelog entries
kpp Sep 12, 2022
1b08ba5
Apply suggestions from code review: CHANGELOG [unreleased]
kpp Sep 15, 2022
17182b9
Bump versions and add changelog entries
kpp Sep 15, 2022
8205887
Merge branch 'master' into rand08
kpp Sep 15, 2022
c4d6e6e
Make kad:closest tests deterministic
kpp Sep 15, 2022
e0d783e
Make kad:closest:disjoint:random_peers deterministic
kpp Sep 15, 2022
bc77431
Make kad:closest:disjoint tests deterministic
kpp Sep 15, 2022
aa5427e
::choose_multiple returns an iter instead of vec
kpp Sep 16, 2022
5bd92fc
Make kad:disjoint:PeerVec::arbitrary deterministic
kpp Sep 16, 2022
a14caab
Merge branch 'master' into rand08
kpp Sep 16, 2022
fa17190
Merge branch 'master' into rand08
kpp Sep 21, 2022
44e80e2
Fix kad:rand_distance test
kpp Sep 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ rendezvous = ["dep:libp2p-rendezvous"]
tcp-async-io = ["dep:libp2p-tcp", "libp2p-tcp?/async-io"]
tcp-tokio = ["dep:libp2p-tcp", "libp2p-tcp?/tokio"]
uds = ["dep:libp2p-uds"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "rand/wasm-bindgen"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js"]
wasm-ext = ["dep:libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
websocket = ["dep:libp2p-websocket"]
Expand Down Expand Up @@ -103,7 +103,6 @@ libp2p-yamux = { version = "0.40.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.14.0" }
parking_lot = "0.12.0"
pin-project = "1.0.0"
rand = "0.7.3" # Explicit dependency to be used in `wasm-bindgen` feature
smallvec = "1.6.1"

[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
Expand Down Expand Up @@ -131,6 +130,7 @@ members = [
"misc/rw-stream-sink",
"misc/keygen",
"misc/prost-codec",
"misc/quickcheck-ext",
"muxers/mplex",
"muxers/yamux",
"protocols/dcutr",
Expand Down
3 changes: 1 addition & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ libp2p-mplex = { path = "../muxers/mplex" }
libp2p-noise = { path = "../transports/noise" }
libp2p-tcp = { path = "../transports/tcp" }
multihash = { version = "0.16", default-features = false, features = ["arb"] }
quickcheck = "0.9.0"
rand07 = { package = "rand", version = "0.7" }
quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" }
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
rmp-serde = "1.0"
serde_json = "1.0"

Expand Down
5 changes: 2 additions & 3 deletions core/src/identity/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ impl DerDecodable<'_> for Asn1SubjectPublicKeyInfo {
mod tests {
use super::*;
use quickcheck::*;
use rand07::seq::SliceRandom;
use std::fmt;

const KEY1: &'static [u8] = include_bytes!("test/rsa-2048.pk8");
Expand All @@ -323,8 +322,8 @@ mod tests {
}

impl Arbitrary for SomeKeypair {
fn arbitrary<G: Gen>(g: &mut G) -> SomeKeypair {
let mut key = [KEY1, KEY2, KEY3].choose(g).unwrap().to_vec();
fn arbitrary(g: &mut Gen) -> SomeKeypair {
let mut key = g.choose(&[KEY1, KEY2, KEY3]).unwrap().to_vec();
SomeKeypair(Keypair::from_pkcs8(&mut key).unwrap())
}
}
Expand Down
4 changes: 2 additions & 2 deletions misc/multistream-select/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ libp2p-core = { path = "../../core", default-features = false }
libp2p-swarm = { path = "../../swarm", default-features = false }
libp2p-mplex = { path = "../../muxers/mplex" }
libp2p-plaintext = { path = "../../transports/plaintext" }
quickcheck = "0.9.0"
rand = "0.7.2"
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
rand = "0.8"
rw-stream-sink = { version = "0.3.0", path = "../../misc/rw-stream-sink" }
13 changes: 6 additions & 7 deletions misc/multistream-select/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,24 +455,23 @@ impl fmt::Display for ProtocolError {
mod tests {
use super::*;
use quickcheck::*;
use rand::distributions::Alphanumeric;
use rand::Rng;
use std::iter;

impl Arbitrary for Protocol {
fn arbitrary<G: Gen>(g: &mut G) -> Protocol {
let n = g.gen_range(1, g.size());
fn arbitrary(g: &mut Gen) -> Protocol {
let n = g.gen_range(1..g.size());
let p: String = iter::repeat(())
.map(|()| g.sample(Alphanumeric))
.map(|()| char::arbitrary(g))
.filter(|&c| c.is_ascii_alphanumeric())
.take(n)
.collect();
Protocol(Bytes::from(format!("/{}", p)))
}
}

impl Arbitrary for Message {
fn arbitrary<G: Gen>(g: &mut G) -> Message {
match g.gen_range(0, 5) {
fn arbitrary(g: &mut Gen) -> Message {
match g.gen_range(0..5u8) {
0 => Message::Header(HeaderLine::V1),
1 => Message::NotAvailable,
2 => Message::ListProtocols,
Expand Down
10 changes: 10 additions & 0 deletions misc/quickcheck-ext/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "quickcheck-ext"
version = "0.1.0"
edition = "2021"
publish = false
license = "Unlicense/MIT"

[dependencies]
quickcheck = "1"
num-traits = "0.2"
14 changes: 14 additions & 0 deletions misc/quickcheck-ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub use quickcheck::*;

use core::ops::Range;
use num_traits::sign::Unsigned;

pub trait GenRange {
fn gen_range<T: Unsigned + Arbitrary + Copy>(&mut self, _range: Range<T>) -> T;
}

impl GenRange for Gen {
fn gen_range<T: Unsigned + Arbitrary + Copy>(&mut self, range: Range<T>) -> T {
<T as Arbitrary>::arbitrary(self) % (range.end - range.start) + range.start
}
}
5 changes: 2 additions & 3 deletions muxers/mplex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ libp2p-core = { version = "0.36.0", path = "../../core", default-features = fals
log = "0.4"
nohash-hasher = "0.2"
parking_lot = "0.12"
rand = "0.7"
rand = "0.8"
kpp marked this conversation as resolved.
Show resolved Hide resolved
smallvec = "1.6.1"
unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }

Expand All @@ -29,8 +29,7 @@ env_logger = "0.9"
futures = "0.3"
libp2p-tcp = { path = "../../transports/tcp" }
libp2p-plaintext = { path = "../../transports/plaintext" }
quickcheck = "0.9"
rand = "0.7"
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }

[[bench]]
name = "split_send_size"
Expand Down
14 changes: 6 additions & 8 deletions muxers/mplex/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,27 +1105,25 @@ mod tests {
use asynchronous_codec::{Decoder, Encoder};
use bytes::BytesMut;
use quickcheck::*;
use rand::prelude::*;
use std::collections::HashSet;
use std::num::NonZeroU8;
use std::ops::DerefMut;
use std::pin::Pin;

impl Arbitrary for MaxBufferBehaviour {
fn arbitrary<G: Gen>(g: &mut G) -> MaxBufferBehaviour {
*[MaxBufferBehaviour::Block, MaxBufferBehaviour::ResetStream]
.choose(g)
fn arbitrary(g: &mut Gen) -> MaxBufferBehaviour {
*g.choose(&[MaxBufferBehaviour::Block, MaxBufferBehaviour::ResetStream])
.unwrap()
}
}

impl Arbitrary for MplexConfig {
fn arbitrary<G: Gen>(g: &mut G) -> MplexConfig {
fn arbitrary(g: &mut Gen) -> MplexConfig {
MplexConfig {
max_substreams: g.gen_range(1, 100),
max_buffer_len: g.gen_range(1, 1000),
max_substreams: g.gen_range(1..100),
max_buffer_len: g.gen_range(1..1000),
max_buffer_behaviour: MaxBufferBehaviour::arbitrary(g),
split_send_size: g.gen_range(1, 10000),
split_send_size: g.gen_range(1..10000),
protocol_name: crate::config::DEFAULT_MPLEX_PROTOCOL_NAME,
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/dcutr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ libp2p-identify = { path = "../identify" }
libp2p-plaintext = { path = "../../transports/plaintext" }
libp2p-relay = { path = "../relay" }
libp2p-yamux = { path = "../../muxers/yamux" }
rand = "0.7"
rand = "0.8"
clap = {version = "3.1.6", features = ["derive"]}
2 changes: 1 addition & 1 deletion protocols/floodsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ libp2p-core = { version = "0.36.0", path = "../../core", default-features = fals
libp2p-swarm = { version = "0.39.0", path = "../../swarm" }
log = "0.4"
prost = "0.11"
rand = "0.7"
rand = "0.8"
kpp marked this conversation as resolved.
Show resolved Hide resolved
smallvec = "1.6.1"

[build-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bytes = "1.0"
byteorder = "1.3.4"
fnv = "1.0.7"
futures = "0.3.5"
rand = "0.7.3"
rand = "0.8"
kpp marked this conversation as resolved.
Show resolved Hide resolved
asynchronous-codec = "0.6"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
log = "0.4.11"
Expand All @@ -40,7 +40,7 @@ libp2p-plaintext = { path = "../../transports/plaintext" }
libp2p-yamux = { path = "../../muxers/yamux" }
libp2p-mplex = { path = "../../muxers/mplex" }
libp2p-noise = { path = "../../transports/noise" }
quickcheck = "0.9.2"
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
hex = "0.4.2"
derive_builder = "0.11.1"

Expand Down
11 changes: 5 additions & 6 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3204,7 +3204,7 @@ where
debug!("Peer disconnected: {}", peer_id);
{
let topics = match self.peer_topics.get(peer_id) {
Some(topics) => (topics),
Some(topics) => topics,
None => {
debug_assert!(
self.blacklisted_peers.contains(peer_id),
Expand Down Expand Up @@ -3667,7 +3667,6 @@ mod local_test {
use crate::IdentTopic;
use asynchronous_codec::Encoder;
use quickcheck::*;
use rand::Rng;

fn empty_rpc() -> GossipsubRpc {
GossipsubRpc {
Expand Down Expand Up @@ -3704,16 +3703,16 @@ mod local_test {
}

impl Arbitrary for GossipsubRpc {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
fn arbitrary(g: &mut Gen) -> Self {
let mut rpc = empty_rpc();

for _ in 0..g.gen_range(0, 10) {
for _ in 0..g.gen_range(0..10u8) {
rpc.subscriptions.push(test_subscription());
}
for _ in 0..g.gen_range(0, 10) {
for _ in 0..g.gen_range(0..10u8) {
rpc.messages.push(test_message());
}
for _ in 0..g.gen_range(0, 10) {
for _ in 0..g.gen_range(0..10u8) {
rpc.control_msgs.push(test_control());
}
rpc
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3293,12 +3293,12 @@ mod tests {
*seq += 1;
RawGossipsubMessage {
source: Some(PeerId::random()),
data: (0..rng.gen_range(10, 30))
data: (0..rng.gen_range(10..30))
.into_iter()
.map(|_| rng.gen())
.collect(),
sequence_number: Some(*seq),
topic: topics[rng.gen_range(0, topics.len())].clone(),
topic: topics[rng.gen_range(0..topics.len())].clone(),
signature: None,
key: None,
validated: true,
Expand Down
17 changes: 8 additions & 9 deletions protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,12 @@ mod tests {
use crate::IdentTopic as Topic;
use libp2p_core::identity::Keypair;
use quickcheck::*;
use rand::Rng;

#[derive(Clone, Debug)]
struct Message(RawGossipsubMessage);

impl Arbitrary for Message {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
fn arbitrary(g: &mut Gen) -> Self {
let keypair = TestKeypair::arbitrary(g);

// generate an arbitrary GossipsubMessage using the behaviour signing functionality
Expand All @@ -588,8 +587,8 @@ mod tests {
config,
)
.unwrap();
let data = (0..g.gen_range(10, 10024))
.map(|_| g.gen())
let data = (0..g.gen_range(10..10024u32))
.map(|_| u8::arbitrary(g))
.collect::<Vec<_>>();
let topic_id = TopicId::arbitrary(g).0;
Message(gs.build_raw_message(topic_id, data).unwrap())
Expand All @@ -600,9 +599,9 @@ mod tests {
struct TopicId(TopicHash);

impl Arbitrary for TopicId {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let topic_string: String = (0..g.gen_range(20, 1024))
.map(|_| g.gen::<char>())
fn arbitrary(g: &mut Gen) -> Self {
let topic_string: String = (0..g.gen_range(20..1024u32))
.map(|_| char::arbitrary(g))
.collect::<String>()
.into();
TopicId(Topic::new(topic_string).into())
Expand All @@ -614,8 +613,8 @@ mod tests {

impl Arbitrary for TestKeypair {
#[cfg(feature = "rsa")]
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let keypair = if g.gen() {
fn arbitrary(g: &mut Gen) -> Self {
let keypair = if bool::arbitrary(g) {
// Small enough to be inlined.
Keypair::generate_ed25519()
} else {
Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log = "0.4"
libp2p-core = { version = "0.36.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.39.0", path = "../../swarm" }
prost = "0.11"
rand = "0.7.2"
rand = "0.8"
kpp marked this conversation as resolved.
Show resolved Hide resolved
sha2 = "0.10.0"
smallvec = "1.6.1"
uint = "0.9"
Expand All @@ -37,7 +37,7 @@ env_logger = "0.9.0"
futures-timer = "3.0"
libp2p-noise = { path = "../../transports/noise" }
libp2p-yamux = { path = "../../muxers/yamux" }
quickcheck = "0.9.0"
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }

[build-dependencies]
prost-build = "0.11"
Expand Down
15 changes: 8 additions & 7 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ fn random_multihash() -> Multihash {
struct Seed([u8; 32]);

impl Arbitrary for Seed {
fn arbitrary<G: Gen>(g: &mut G) -> Seed {
Seed(g.gen())
fn arbitrary(g: &mut Gen) -> Seed {
let seed = core::array::from_fn(|_| u8::arbitrary(g));
Seed(seed)
}
}

Expand All @@ -157,14 +158,14 @@ fn bootstrap() {
fn prop(seed: Seed) {
let mut rng = StdRng::from_seed(seed.0);

let num_total = rng.gen_range(2, 20);
let num_total = rng.gen_range(2..20);
// When looking for the closest node to a key, Kademlia considers
// K_VALUE nodes to query at initialization. If `num_group` is larger
// than K_VALUE the remaining locally known nodes will not be
// considered. Given that no other node is aware of them, they would be
// lost entirely. To prevent the above restrict `num_group` to be equal
// or smaller than K_VALUE.
let num_group = rng.gen_range(1, (num_total % K_VALUE.get()) + 2);
let num_group = rng.gen_range(1..(num_total % K_VALUE.get()) + 2);

let mut cfg = KademliaConfig::default();
if rng.gen() {
Expand Down Expand Up @@ -244,7 +245,7 @@ fn query_iter() {
}

fn run(rng: &mut impl Rng) {
let num_total = rng.gen_range(2, 20);
let num_total = rng.gen_range(2..20);
let mut swarms = build_connected_nodes(num_total, 1)
.into_iter()
.map(|(_a, s)| s)
Expand Down Expand Up @@ -492,7 +493,7 @@ fn put_record() {
fn prop(records: Vec<Record>, seed: Seed, filter_records: bool, drop_records: bool) {
let mut rng = StdRng::from_seed(seed.0);
let replication_factor =
NonZeroUsize::new(rng.gen_range(1, (K_VALUE.get() / 2) + 1)).unwrap();
NonZeroUsize::new(rng.gen_range(1..(K_VALUE.get() / 2) + 1)).unwrap();
// At least 4 nodes, 1 under test + 3 bootnodes.
let num_total = usize::max(4, replication_factor.get() * 2);

Expand Down Expand Up @@ -856,7 +857,7 @@ fn add_provider() {
fn prop(keys: Vec<record::Key>, seed: Seed) {
let mut rng = StdRng::from_seed(seed.0);
let replication_factor =
NonZeroUsize::new(rng.gen_range(1, (K_VALUE.get() / 2) + 1)).unwrap();
NonZeroUsize::new(rng.gen_range(1..(K_VALUE.get() / 2) + 1)).unwrap();
// At least 4 nodes, 1 under test + 3 bootnodes.
let num_total = usize::max(4, replication_factor.get() * 2);

Expand Down