Skip to content

Commit

Permalink
Use the underlying reproducible random impls instead of StdRng. (#7717)
Browse files Browse the repository at this point in the history
* Use the underlying reproducible random impls.

* Improve testing

* fix deny

* fmt

Co-authored-by: Michal Nazarewicz <mina86@mina86.com>
  • Loading branch information
robin-near and mina86 authored Sep 28, 2022
1 parent c72bc7e commit a10b5d7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 136 deletions.
130 changes: 20 additions & 110 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ protobuf = "3.0.1"
protobuf-codegen = "3.0.1"
quote = "1.0"
rand = "0.8.5"
rand_chacha = "0.3.1"
rand_hc = "0.3.1"
rand_xorshift = "0.3"
rayon = "1.5"
redis = "0.21.5"
Expand Down
1 change: 1 addition & 0 deletions chain/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lru.workspace = true
num-rational.workspace = true
once_cell.workspace = true
rand.workspace = true
rand_chacha.workspace = true
rayon.workspace = true
strum.workspace = true
thiserror.workspace = true
Expand Down
33 changes: 28 additions & 5 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use itertools::Itertools;
use near_o11y::log_assert;
use near_primitives::sandbox::state_patch::SandboxStatePatch;
use near_primitives::time::Clock;
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use tracing::{debug, error, info, warn, Span};

use near_chain_primitives::error::{BlockKnownError, Error, LogTransientStorageError};
Expand Down Expand Up @@ -1536,15 +1536,22 @@ impl Chain {
}
// sort the receipts deterministically so the order that they will be processed is deterministic
for (_, receipt_proofs) in receipt_proofs_by_shard_id.iter_mut() {
let mut slice = [0u8; 32];
slice.copy_from_slice(block.hash().as_ref());
let mut rng: StdRng = SeedableRng::from_seed(slice);
receipt_proofs.shuffle(&mut rng);
Self::shuffle_receipt_proofs(receipt_proofs, block.hash());
}

Ok(receipt_proofs_by_shard_id)
}

fn shuffle_receipt_proofs<ReceiptProofType>(
receipt_proofs: &mut Vec<ReceiptProofType>,
block_hash: &CryptoHash,
) {
let mut slice = [0u8; 32];
slice.copy_from_slice(block_hash.as_ref());
let mut rng: ChaCha20Rng = SeedableRng::from_seed(slice);
receipt_proofs.shuffle(&mut rng);
}

#[cfg(test)]
pub(crate) fn mark_block_as_challenged(
&mut self,
Expand Down Expand Up @@ -5358,3 +5365,19 @@ impl Chain {
.collect()
}
}

#[cfg(test)]
mod tests {
use near_primitives::hash::CryptoHash;

#[test]
pub fn receipt_randomness_reproducibility() {
// Sanity check that the receipt shuffling implementation does not change.
let mut receipt_proofs = vec![0, 1, 2, 3, 4, 5, 6];
crate::Chain::shuffle_receipt_proofs(
&mut receipt_proofs,
&CryptoHash::hash_bytes(&[1, 2, 3, 4, 5]),
);
assert_eq!(receipt_proofs, vec![2, 3, 1, 4, 0, 5, 6],);
}
}
4 changes: 1 addition & 3 deletions chain/epoch-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ edition.workspace = true


[dependencies]
# Changing this version will lead to change to the protocol, as will change how validators get shuffled.
protocol_defining_rand = { package = "rand", version = "0.6.5", default-features = false }

borsh.workspace = true
chrono = { workspace = true, optional = true }
num-rational.workspace = true
primitive-types.workspace = true
rand.workspace = true
rand_hc.workspace = true
serde_json.workspace = true
smart-default.workspace = true
tracing.workspace = true
Expand Down
Loading

0 comments on commit a10b5d7

Please sign in to comment.