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

Bump rand to 0.8, rand_chacha to 0.3, getrandom to 0.2 #32871

Merged
merged 35 commits into from
Aug 21, 2023

Conversation

joncinque
Copy link
Contributor

Problem

In order to properly address the cargo audit error in #32836, we'll need to bump ed25519-dalek everywhere, which also entails upgrading the various rand dependencies.

Summary of Changes

Since we expose a dependency to the rand 0.7 types through functions like Keypair::generate, we need to be careful about performing this bump to avoid breaking downstream users. This PR does not break downstream users and avoids touching ed25519-dalek / libsecp256k1, except in tests. That work can be done in other PRs.

Here are the Solana-specific changes in this PR:

  • deprecate hash::new_rand() in favor of hash::new_with_thread_rng(), since just about everywhere that used new_rand was just passing thread_rng, and it's only used in tests
  • keep the dependency to 0.7 in all places that have a strong dependency on ed25519-dalek: solana-zk-token-sdk, solana_sdk::signer::keypair::Keypair, solana_sdk::ed25519_instruction, solana_sdk::secp256k1_instruction
  • inline keypair generation in tests to use the new rand with the old libraries

Here are the rand-required changes in the PR:

  • gen_range now takes a range, so gen_range(1, 2) becomes gen_range(1..2)
  • use map(char::from) when generating a random string
  • use Fill instead of AsByteSliceMut
  • don't depend on ThreadRng implementing Copy

More information on rand at https://rust-random.github.io/book/update-0.8.html

You should be able to follow the commits pretty linearly, except when I wrongly created Keypair::from_secret_key_bytes, only to realize that it was just keypair_from_seed a few commits later. The concept was:

  • bump the crates in be1e6db and 18ebdf3
  • Do git grep -l gen_range | xargs sed -i'' -e 's/gen_range(\(\S*\), /gen_range(\1../' to fix most gen_range instances in 99a0f1e, then clean it up in f78cef6
  • Do git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' to deprecate hash::new_rand everywhere in 5f9675a
  • After that, each commit goes through errors that were triggered in each crate

Please tag anyone else who might be good to review this!

Fixes #

perf/src/test_tx.rs Outdated Show resolved Hide resolved
@@ -25,6 +25,9 @@ use {
pub struct Keypair(ed25519_dalek::Keypair);

impl Keypair {
/// Can be used for generating a Keypair without a dependency on `rand` types
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment might be too specific, but it shows why I needed it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcode 64 and static_assertions::const_assert_eq!(SECRET_KEY_LENGTH, ed25519_dalek::SECRET_KEY_LENGTH) to keep dalek out of the api?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, will do!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in db48acc

Copy link
Contributor

@brooksprumo brooksprumo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me!

For the changes on the actual cryptographic bits, I think another set of eyes with proper experience would be a good idea.

perf/src/test_tx.rs Outdated Show resolved Hide resolved
ledger/src/shred.rs Show resolved Hide resolved
sdk/src/hash.rs Outdated Show resolved Hide resolved
ledger/src/shredder.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@t-nelson t-nelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole change set makes me want to take a hardline that traits have no business being versioned with logic

sdk/src/hash.rs Outdated Show resolved Hide resolved
sdk/src/signer/keypair.rs Outdated Show resolved Hide resolved
sdk/src/signer/keypair.rs Outdated Show resolved Hide resolved
perf/src/test_tx.rs Outdated Show resolved Hide resolved
streamer/src/nonblocking/quic.rs Show resolved Hide resolved
@@ -25,6 +25,7 @@ pub fn get_append_vec_path(path: &str) -> TempFile {
let out_dir = get_append_vec_dir();
let rand_string: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.map(char::from)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole method seems ripe for replacement with tempfile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truth

@@ -25,6 +25,9 @@ use {
pub struct Keypair(ed25519_dalek::Keypair);

impl Keypair {
/// Can be used for generating a Keypair without a dependency on `rand` types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcode 64 and static_assertions::const_assert_eq!(SECRET_KEY_LENGTH, ed25519_dalek::SECRET_KEY_LENGTH) to keep dalek out of the api?

ledger/src/shred.rs Show resolved Hide resolved
@@ -4,7 +4,7 @@ use {
rand::{Rng, SeedableRng},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

til this module exists. think i might need a sick day 🤢

Comment on lines 1179 to 1181
let mut seed = [0u8; Keypair::SECRET_KEY_LENGTH];
rng.fill(&mut seed[..]);
let keypair = keypair_from_seed(&seed).unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one isn't using chacha, so changing to Keypair::new

Comment on lines 1364 to 1366
let mut seed = [0u8; Keypair::SECRET_KEY_LENGTH];
rng.fill(&mut seed[..]);
let keypair = keypair_from_seed(&seed).unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is also using thread_rng, so switching to Keypair::new

@@ -25,6 +25,9 @@ use {
pub struct Keypair(ed25519_dalek::Keypair);

impl Keypair {
/// Can be used for generating a Keypair without a dependency on `rand` types
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in db48acc

@joncinque
Copy link
Contributor Author

Ok, I think all comments have been addressed, thanks for your careful and quick reviews!

brooksprumo
brooksprumo previously approved these changes Aug 18, 2023
Copy link
Contributor

@brooksprumo brooksprumo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm - all my concerns have been resolved. Thanks!

@@ -308,9 +308,16 @@ mod test {
);
}

fn generate_random_hash() -> Hash {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol... Hash::new_unique() is broken. it should be hashing the counter, not using it straight up 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try changing it to that and see if the bloom filter test passes. On the flipside, and I might be understanding this wrong, but it seems like the test is meant for random hashes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah don't mess with it here

@joncinque
Copy link
Contributor Author

Trying to resolve the additional rebuilds of solana-program by unifying features some more. Bear with me!

@joncinque
Copy link
Contributor Author

I couldn't get a combination that linked successfully and also kept the rebuilds at the same number, so I had to increase from 14 to 15 rebuilds, which feels not-too-bad!

@t-nelson
Copy link
Contributor

I couldn't get a combination that linked successfully and also kept the rebuilds at the same number, so I had to increase from 14 to 15 rebuilds, which feels not-too-bad!

presumably we're now linking something against both versions of rand, so expected?

@joncinque
Copy link
Contributor Author

presumably we're now linking something against both versions of rand, so expected?

Yeah probably... all my efforts were useless since they just reintroduced the linking error, which is strange because I couldn't reproduce it on any local machine this time, whereas I could before.

Anyway, had to increase the number of rebuilds to 17 🙃

@codecov
Copy link

codecov bot commented Aug 19, 2023

Codecov Report

Merging #32871 (7054869) into master (a4c8cc3) will increase coverage by 0.0%.
Report is 2 commits behind head on master.
The diff coverage is 96.7%.

@@           Coverage Diff           @@
##           master   #32871   +/-   ##
=======================================
  Coverage    82.0%    82.0%           
=======================================
  Files         785      784    -1     
  Lines      212387   212437   +50     
=======================================
+ Hits       174190   174245   +55     
+ Misses      38197    38192    -5     

@joncinque
Copy link
Contributor Author

It finally passed woo!

@joncinque joncinque merged commit 0fe902c into solana-labs:master Aug 21, 2023
@joncinque joncinque deleted the rand8 branch August 21, 2023 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants