Skip to content

Commit

Permalink
Added RngCore to most random functions (#682)
Browse files Browse the repository at this point in the history
* Added RngCore to most random functions

* Fix lint
  • Loading branch information
VictorKoenders committed Nov 3, 2022
1 parent 70877f0 commit 7884fd8
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 57 deletions.
2 changes: 1 addition & 1 deletion examples/dentry-simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async fn main() {
debug!("Running random transactions");
for round in prebaked_count..opt.transactions as u64 {
debug!(?round);
let tx = &state.as_ref().unwrap()[0].create_random_transaction();
let tx = &state.as_ref().unwrap()[0].create_random_transaction(&mut rng);
println!("Round {}:", round);
println!(" - Proposing: {:?}", tx);
debug!("Proposing: {:?}", tx);
Expand Down
3 changes: 2 additions & 1 deletion examples/multi-machine-centralized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ async fn main() {
setup_logging();
setup_backtrace();

let mut rng = rand::thread_rng();
let opts: NodeOpt = NodeOpt::parse();
let addr: SocketAddr = (opts.host, opts.port).into();
error!("Connecting to {addr:?} to retrieve the server config");
Expand Down Expand Up @@ -152,7 +153,7 @@ async fn main() {
let tx_to_gen = transactions_per_round * (cmp::max(rounds / node_count, 1) + 5);
error!("Generated {} transactions", tx_to_gen);
for _ in 0..tx_to_gen {
let mut txn = <DEntryState as TestableState>::create_random_transaction(&state);
let mut txn = <DEntryState as TestableState>::create_random_transaction(&state, &mut rng);
txn.padding = vec![0; adjusted_padding];
txs.push_back(txn);
}
Expand Down
5 changes: 4 additions & 1 deletion examples/multi-machine-libp2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ async fn main() {
setup_logging();
setup_backtrace();

let mut rng = rand::thread_rng();

let args = CliOpt::parse();
let mut server_conn = None;
let config = match args {
Expand Down Expand Up @@ -568,7 +570,8 @@ async fn main() {
let state = hotshot.get_state().await;

for _ in 0..config.num_txn_per_round {
let txn = <DEntryState as TestableState>::create_random_transaction(&state);
let txn =
<DEntryState as TestableState>::create_random_transaction(&state, &mut rng);
info!("Submitting txn on view {}", view);
hotshot.submit_transaction(txn).await.unwrap();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/multi-machine-vrf-centralized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async fn init_state_and_hotshot(
let vrf_key =
VRFPubKey::<BLSSignatureScheme<Param381>>::generated_from_seed_indexed(seed, node_id);
let priv_key = vrf_key.1;
let pub_key = vrf_key.0;
let pub_key = vrf_key.0;

let known_nodes = config.known_nodes.clone();
let mut distribution = Vec::new();
Expand Down Expand Up @@ -125,6 +125,8 @@ async fn main() {
setup_logging();
setup_backtrace();

let mut rng = rand::thread_rng();

let opts: NodeOpt = NodeOpt::parse();
let addr: SocketAddr = (opts.host, opts.port).into();
error!("Connecting to {addr:?} to retrieve the server config");
Expand Down Expand Up @@ -180,7 +182,7 @@ async fn main() {
let tx_to_gen = transactions_per_round * (cmp::max(rounds / node_count, 1) + 5);
error!("Generated {} transactions", tx_to_gen);
for _ in 0..tx_to_gen {
let mut txn = <DEntryState as TestableState>::create_random_transaction(&state);
let mut txn = <DEntryState as TestableState>::create_random_transaction(&state, &mut rng);
txn.padding = vec![0; adjusted_padding];
txs.push_back(txn);
}
Expand Down
4 changes: 3 additions & 1 deletion examples/multi-machine-vrf-libp2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ async fn main() {
setup_logging();
setup_backtrace();

let mut rng = rand::thread_rng();
let args = CliOpt::parse();
let mut server_conn = None;
let config = match args {
Expand Down Expand Up @@ -584,7 +585,8 @@ async fn main() {
let state = hotshot.get_state().await;

for _ in 0..config.num_txn_per_round {
let txn = <DEntryState as TestableState>::create_random_transaction(&state);
let txn =
<DEntryState as TestableState>::create_random_transaction(&state, &mut rng);
info!("Submitting txn on view {}", view);
hotshot.submit_transaction(txn).await.unwrap();
}
Expand Down
9 changes: 6 additions & 3 deletions libp2p-networking/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use libp2p::{
yamux::{WindowUpdateMode, YamuxConfig},
InboundUpgradeExt, Multiaddr, OutboundUpgradeExt, PeerId, Transport,
};
use rand::{seq::IteratorRandom, thread_rng};
use rand::seq::IteratorRandom;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fmt::Debug, io, str::FromStr, sync::Arc, time::Duration};
use tracing::{info, instrument};
Expand Down Expand Up @@ -270,6 +270,9 @@ pub async fn spin_up_swarm<S: std::fmt::Debug + Default>(
/// chooses one
/// # Panics
/// panics if handles is of length 0
pub fn get_random_handle<S>(handles: &[Arc<NetworkNodeHandle<S>>]) -> Arc<NetworkNodeHandle<S>> {
handles.iter().choose(&mut thread_rng()).unwrap().clone()
pub fn get_random_handle<S>(
handles: &[Arc<NetworkNodeHandle<S>>],
rng: &mut dyn rand::RngCore,
) -> Arc<NetworkNodeHandle<S>> {
handles.iter().choose(rng).unwrap().clone()
}
9 changes: 6 additions & 3 deletions libp2p-networking/tests/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ async fn run_gossip_round(
new_state: CounterState,
timeout_duration: Duration,
) -> Result<(), TestError<CounterState>> {
let msg_handle = get_random_handle(handles);
let mut rng = rand::thread_rng();
let msg_handle = get_random_handle(handles, &mut rng);
msg_handle.modify_state(|s| *s = new_state).await;

let mut futs = Vec::new();
Expand Down Expand Up @@ -307,9 +308,10 @@ async fn run_dht_rounds(
starting_val: usize,
num_rounds: usize,
) {
let mut rng = rand::thread_rng();
for i in 0..num_rounds {
error!("round: {:?}", i);
let msg_handle = get_random_handle(handles);
let msg_handle = get_random_handle(handles, &mut rng);
let mut key = vec![0; DHT_KV_PADDING];
key.push((starting_val + i) as u8);
let mut value = vec![0; DHT_KV_PADDING];
Expand Down Expand Up @@ -365,7 +367,8 @@ async fn run_request_response_increment_all(
handles: &[Arc<NetworkNodeHandle<CounterState>>],
timeout: Duration,
) {
let requestee_handle = get_random_handle(handles);
let mut rng = rand::thread_rng();
let requestee_handle = get_random_handle(handles, &mut rng);
requestee_handle.modify_state(|s| *s += 1).await;
info!("RR REQUESTEE IS {:?}", requestee_handle.peer_id());
let mut futs = Vec::new();
Expand Down
30 changes: 18 additions & 12 deletions src/demos/dentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use hotshot_types::{
State,
},
};
use rand::{thread_rng, Rng};
use rand::Rng;
use serde::{Deserialize, Serialize};
use snafu::{ensure, Snafu};
use std::{
Expand Down Expand Up @@ -382,9 +382,11 @@ impl State for DEntryState {
}

impl TestableState for DEntryState {
fn create_random_transaction(&self) -> <Self::BlockType as Block>::Transaction {
fn create_random_transaction(
&self,
rng: &mut dyn rand::RngCore,
) -> <Self::BlockType as Block>::Transaction {
use rand::seq::IteratorRandom;
let mut rng = thread_rng();

let non_zero_balances = self
.balances
Expand All @@ -397,8 +399,8 @@ impl TestableState for DEntryState {
"No nonzero balances were available! Unable to generate transaction."
);

let input_account = non_zero_balances.iter().choose(&mut rng).unwrap().0;
let output_account = self.balances.keys().choose(&mut rng).unwrap();
let input_account = non_zero_balances.iter().choose(rng).unwrap().0;
let output_account = self.balances.keys().choose(rng).unwrap();
let amount = rng.gen_range(0..100);

DEntryTransaction {
Expand Down Expand Up @@ -513,27 +515,31 @@ where
}

/// Provides a random [`QuorumCertificate`]
pub fn random_quorum_certificate<STATE: State>() -> QuorumCertificate<STATE> {
let mut rng = thread_rng();
pub fn random_quorum_certificate<STATE: State>(
rng: &mut dyn rand::RngCore,
) -> QuorumCertificate<STATE> {
QuorumCertificate {
block_commitment: random_commitment(),
leaf_commitment: random_commitment(),
block_commitment: random_commitment(rng),
leaf_commitment: random_commitment(rng),
view_number: ViewNumber::new(rng.gen()),
signatures: BTreeMap::default(),
genesis: rng.gen(),
}
}

/// Provides a random [`Leaf`]
pub fn random_leaf<STATE: State<Time = ViewNumber>>(deltas: STATE::BlockType) -> Leaf<STATE> {
let justify_qc = random_quorum_certificate();
pub fn random_leaf<STATE: State<Time = ViewNumber>>(
deltas: STATE::BlockType,
rng: &mut dyn rand::RngCore,
) -> Leaf<STATE> {
let justify_qc = random_quorum_certificate(rng);
let state = STATE::default()
.append(&deltas, &ViewNumber::new(42))
.unwrap_or_default();
Leaf {
view_number: justify_qc.view_number,
justify_qc,
parent_commitment: random_commitment(),
parent_commitment: random_commitment(rng),
deltas,
state,
rejected: Vec::new(),
Expand Down
14 changes: 9 additions & 5 deletions src/traits/storage/memory_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ mod test {
use std::collections::BTreeMap;
use tracing::instrument;

#[instrument]
fn random_stored_view(number: ViewNumber) -> StoredView<DummyState> {
#[instrument(skip(rng))]
fn random_stored_view(
rng: &mut dyn rand::RngCore,
number: ViewNumber,
) -> StoredView<DummyState> {
// TODO is it okay to be using genesis here?
let dummy_block_commit = fake_commitment::<DummyBlock>();
let dummy_leaf_commit = fake_commitment::<Leaf<DummyState>>();
Expand All @@ -144,8 +147,8 @@ mod test {
signatures: BTreeMap::new(),
view_number: number,
},
DummyBlock::random(),
DummyState::random(),
DummyBlock::random(rng),
DummyState::random(rng),
dummy_leaf_commit,
Vec::new(),
genesis_proposer_id(),
Expand All @@ -159,8 +162,9 @@ mod test {
#[cfg_attr(feature = "async-std-executor", async_std::test)]
#[instrument]
async fn memory_storage() {
let mut rng = rand::thread_rng();
let storage = MemoryStorage::construct_tmp_storage().unwrap();
let genesis = random_stored_view(ViewNumber::genesis());
let genesis = random_stored_view(&mut rng, ViewNumber::genesis());
storage
.append_single_view(genesis.clone())
.await
Expand Down
11 changes: 6 additions & 5 deletions testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,26 +433,26 @@ impl<I: TestableNodeImplementation> TestRunner<I> {
pub async fn add_random_transaction(
&self,
node_id: Option<usize>,
rng: &mut dyn rand::RngCore,
) -> <<I::StateType as State>::BlockType as Block>::Transaction {
if self.nodes.is_empty() {
panic!("Tried to add transaction, but no nodes have been added!");
}

use rand::{seq::IteratorRandom, thread_rng};
let mut rng = thread_rng();
use rand::seq::IteratorRandom;

// we're assuming all nodes have the same state.
// If they don't match, this is probably fine since
// it should be caught by an assertion (and the txn will be rejected anyway)
let state = self.nodes[0].handle.get_state().await;

let txn = <I::StateType as TestableState>::create_random_transaction(&state);
let txn = <I::StateType as TestableState>::create_random_transaction(&state, rng);

let node = if let Some(node_id) = node_id {
self.nodes.get(node_id).unwrap()
} else {
// find a random handle to send this transaction from
self.nodes.iter().choose(&mut rng).unwrap()
self.nodes.iter().choose(rng).unwrap()
};

node.handle
Expand All @@ -467,10 +467,11 @@ impl<I: TestableNodeImplementation> TestRunner<I> {
pub async fn add_random_transactions(
&self,
n: usize,
rng: &mut dyn rand::RngCore,
) -> Option<Vec<<<I::StateType as State>::BlockType as Block>::Transaction>> {
let mut result = Vec::new();
for _ in 0..n {
result.push(self.add_random_transaction(None).await);
result.push(self.add_random_transaction(None, rng).await);
}
Some(result)
}
Expand Down
8 changes: 6 additions & 2 deletions testing/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,15 @@ pub fn default_submitter_id_to_round<
Vec<<<I::StateType as State>::BlockType as Block>::Transaction>,
> {
async move {
let mut rng = rand::thread_rng();
for id in shutdown_ids.clone() {
runner.shutdown(id).await.unwrap();
}
let mut txns = Vec::new();
for id in round_ids.clone() {
let new_txn = runner.add_random_transaction(Some(id as usize)).await;
let new_txn = runner
.add_random_transaction(Some(id as usize), &mut rng)
.await;
txns.push(new_txn);
}
txns
Expand Down Expand Up @@ -381,14 +384,15 @@ pub fn default_randomized_ids_to_round<
Vec<<<I::StateType as State>::BlockType as Block>::Transaction>,
> {
async move {
let mut rng = rand::thread_rng();
if let Some(to_shut_down) = to_kill.clone() {
for idx in to_shut_down {
runner.shutdown(idx).await.unwrap();
}
}

runner
.add_random_transactions(txns_per_round as usize)
.add_random_transactions(txns_per_round as usize, &mut rng)
.await
.unwrap()
}
Expand Down
Loading

0 comments on commit 7884fd8

Please sign in to comment.