Skip to content

Commit c9f1691

Browse files
authored
Merge pull request #3233 from iosh/rand
chore: upgrade rand version to 0.9
2 parents d3a1140 + 8ec7019 commit c9f1691

File tree

54 files changed

+285
-258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+285
-258
lines changed

Cargo.lock

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,13 @@ clap = "4"
338338
clap-verbosity-flag = "3"
339339

340340
# rand & rng
341-
rand = "0.7"
342-
rand_xorshift = "0.2"
341+
rand = "0.9"
342+
rand_xorshift = "0.4"
343+
rand_chacha = "0.9.0"
344+
345+
# old rand
346+
rand_07 = { package = "rand", version = "0.7" }
343347
rand_08 = { package = "rand", version = "0.8" }
344-
rand_chacha = "0.2.1"
345348

346349
# misc
347350
log = "0.4"

crates/cfx_key/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ parity-secp256k1 = { workspace = true }
1414
lazy_static = { workspace = true }
1515
log = { workspace = true }
1616
parity-wordlist = { workspace = true }
17-
rand = { workspace = true }
17+
rand_07 = { workspace = true }
1818
rustc-hex = { workspace = true }
1919
serde = { workspace = true }
2020
tiny-keccak = { workspace = true }

crates/cfx_key/src/random.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use super::{KeyPair, KeyPairGenerator, SECP256K1};
18-
use rand::rngs::OsRng;
18+
use rand_07::rngs::OsRng;
1919

2020
/// Randomly generates new keypair, instantiating the RNG each time.
2121
pub struct Random;

crates/cfx_store/src/random.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng, RngCore};
17+
use rand::{distr::Alphanumeric, rngs::OsRng, Rng, TryRngCore};
1818

1919
pub trait Random {
2020
fn random() -> Self
@@ -24,20 +24,25 @@ pub trait Random {
2424
impl Random for [u8; 16] {
2525
fn random() -> Self {
2626
let mut result = [0u8; 16];
27-
OsRng.fill_bytes(&mut result);
27+
OsRng.try_fill_bytes(&mut result).unwrap();
2828
result
2929
}
3030
}
3131

3232
impl Random for [u8; 32] {
3333
fn random() -> Self {
3434
let mut result = [0u8; 32];
35-
OsRng.fill_bytes(&mut result);
35+
OsRng.try_fill_bytes(&mut result).unwrap();
3636
result
3737
}
3838
}
3939

4040
/// Generate a random string of given length.
4141
pub fn random_string(length: usize) -> String {
42-
OsRng.sample_iter(&Alphanumeric).take(length).collect()
42+
let mut rng = rand::rng();
43+
(&mut rng)
44+
.sample_iter(Alphanumeric)
45+
.take(length)
46+
.map(char::from)
47+
.collect()
4348
}

crates/cfx_store/tests/util/transient_dir.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ use cfxstore::{
1818
accounts_dir::{KeyDirectory, RootDiskDirectory},
1919
Error, SafeAccount,
2020
};
21-
use rand::{rngs::OsRng, RngCore};
21+
use rand::{rngs::OsRng, TryRngCore};
2222
use std::{env, fs, path::PathBuf};
2323

2424
pub fn random_dir() -> PathBuf {
2525
let mut dir = env::temp_dir();
26-
dir.push(format!("{:x}-{:x}", OsRng.next_u64(), OsRng.next_u64()));
26+
dir.push(format!(
27+
"{:x}-{:x}",
28+
OsRng.try_next_u64().unwrap(),
29+
OsRng.try_next_u64().unwrap()
30+
));
2731
dir
2832
}
2933

crates/cfxcore/core/src/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ mod tests {
256256

257257
// create async sender
258258
let fut3 = async move {
259-
let mut rng = rand::thread_rng();
259+
let mut rng = rand::rng();
260260
let mut sent = vec![];
261-
for t in (0..100).map(|_| rng.gen()) {
261+
for t in (0..100).map(|_| rng.random()) {
262262
chan.send(t);
263263
sent.push(t);
264264
}
@@ -285,9 +285,9 @@ mod tests {
285285

286286
// create async sender
287287
let fut_a = async move {
288-
let mut rng = rand::thread_rng();
288+
let mut rng = rand::rng();
289289

290-
for t in (0..100).map(|_| rng.gen()) {
290+
for t in (0..100).map(|_| rng.random()) {
291291
send_a.send(t);
292292
let t2 = rec_a.recv().await;
293293

crates/cfxcore/core/src/consensus/pivot_hint/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rand::{thread_rng, RngCore};
1+
use rand::{rng, RngCore};
22
use std::{
33
fs::File,
44
io::{Read, Seek, SeekFrom},
@@ -53,7 +53,7 @@ fn test_pivot_hint() {
5353
let pivot_hint = make_test_pivot_hint();
5454
let mut test_file = TestHashFile::new();
5555

56-
let mut rng = thread_rng();
56+
let mut rng = rng();
5757
for _ in 0..100_000 {
5858
let fork_at = rng.next_u64() % 1_500_000;
5959
if fork_at == 0 {

crates/cfxcore/core/src/light_protocol/common/peers.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::message::MsgId;
1515
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
1616
use malloc_size_of_derive::MallocSizeOf as DeriveMallocSizeOf;
1717
use network::{node_table::NodeId, service::ProtocolVersion};
18-
use rand::prelude::SliceRandom;
18+
use rand::prelude::IndexedRandom;
1919
use smart_default::SmartDefault;
2020
use throttling::token_bucket::{ThrottledManager, TokenBucketManager};
2121

@@ -121,9 +121,7 @@ impl FullPeerFilter {
121121
}
122122

123123
pub fn select(self, peers: Arc<Peers<FullPeerState>>) -> Option<NodeId> {
124-
self.select_all(peers)
125-
.choose(&mut rand::thread_rng())
126-
.cloned()
124+
self.select_all(peers).choose(&mut rand::rng()).cloned()
127125
}
128126

129127
pub fn select_all(self, peers: Arc<Peers<FullPeerState>>) -> Vec<NodeId> {

crates/cfxcore/core/src/light_protocol/handler/sync/headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ mod tests {
459459
headers.push(h5.clone());
460460
headers.push(h6.clone());
461461

462-
headers.shuffle(&mut rand::thread_rng());
462+
headers.shuffle(&mut rand::rng());
463463
let mut queue = PriorityQueue::new();
464464
queue.extend(headers);
465465

0 commit comments

Comments
 (0)