From 953e40a66fe81fb8ef9ca5fb307b0a93f9fc7da7 Mon Sep 17 00:00:00 2001 From: Enigbe Ochekliye Date: Fri, 5 Apr 2024 20:08:37 +0100 Subject: [PATCH] fix: address formating issues --- sim-cli/src/main.rs | 4 ++-- sim-lib/src/lib.rs | 18 ++++++++++++------ sim-lib/src/random_activity.rs | 3 +-- sim-lib/src/test_utils.rs | 12 +++++------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/sim-cli/src/main.rs b/sim-cli/src/main.rs index 8e662221..a2cf9906 100644 --- a/sim-cli/src/main.rs +++ b/sim-cli/src/main.rs @@ -48,7 +48,7 @@ fn deserialize_f64_greater_than_zero(x: String) -> Result { /// Deserializes a hexadecimal seed string into a byte array fn deserialize_fix_seed(hex: String) -> Result<[u8; 32], String> { if hex.len() != 64 { - return Err(format!("Seed hex must be 64-characters long.")); + return Err("Seed hex must be 64-characters long.".to_string()); } hex::decode(hex) @@ -58,7 +58,7 @@ fn deserialize_fix_seed(hex: String) -> Result<[u8; 32], String> { seed.clone_from_slice(seed_from_hex); seed }) - .map_err(|e| format!("{}", e.to_string())) + .map_err(|e| e.to_string()) } #[derive(Parser)] diff --git a/sim-lib/src/lib.rs b/sim-lib/src/lib.rs index 1c5a2c19..76c0ba27 100644 --- a/sim-lib/src/lib.rs +++ b/sim-lib/src/lib.rs @@ -410,7 +410,7 @@ pub struct Simulation { /// Configurations for printing results to CSV. Results are not written if this option is None. write_results: Option, /// Seed to deterministically run the random activity generator - seed: Option<[u8;32]> + seed: Option<[u8; 32]>, } #[derive(Clone)] @@ -439,7 +439,7 @@ impl Simulation { expected_payment_msat: u64, activity_multiplier: f64, write_results: Option, - seed: Option<[u8;32]> + seed: Option<[u8; 32]>, ) -> Self { let (shutdown_trigger, shutdown_listener) = triggered::trigger(); Self { @@ -451,7 +451,7 @@ impl Simulation { expected_payment_msat, activity_multiplier, write_results, - seed + seed, } } @@ -694,7 +694,10 @@ impl Simulation { log::debug!("Simulator data collection set up."); } - async fn activity_executors(&self, seed: Option<[u8;32]>) -> Result, SimulationError> { + async fn activity_executors( + &self, + seed: Option<[u8; 32]>, + ) -> Result, SimulationError> { let mut generators = Vec::new(); // Note: when we allow configuring both defined and random activity, this will no longer be an if/else, we'll @@ -726,7 +729,10 @@ impl Simulation { /// Returns the list of nodes that are eligible for generating random activity on. This is the subset of nodes /// that have sufficient capacity to generate payments of our expected payment amount. - async fn random_activity_nodes(&self, seed: Option<[u8;32]>) -> Result, SimulationError> { + async fn random_activity_nodes( + &self, + seed: Option<[u8; 32]>, + ) -> Result, SimulationError> { // Collect capacity of each node from its view of its own channels. Total capacity is divided by two to // avoid double counting capacity (as each node has a counterparty in the channel). let mut generators = Vec::new(); @@ -771,7 +777,7 @@ impl Simulation { *capacity, self.expected_payment_msat, self.activity_multiplier, - self.seed + self.seed, ) .map_err(SimulationError::RandomActivityError)?, ), diff --git a/sim-lib/src/random_activity.rs b/sim-lib/src/random_activity.rs index f2e12e4e..8860805b 100644 --- a/sim-lib/src/random_activity.rs +++ b/sim-lib/src/random_activity.rs @@ -226,8 +226,7 @@ impl PaymentGenerator for RandomPaymentActivity { /// Returns the amount of time until the next payment should be scheduled for the node. fn next_payment_wait(&self) -> Duration { let mut rng = sim_rng(self.seed); - let duration = Duration::from_secs(self.event_dist.sample(&mut rng) as u64); - duration + Duration::from_secs(self.event_dist.sample(&mut rng) as u64) } /// Returns the payment amount for a payment to a node with the destination capacity provided. The expected value diff --git a/sim-lib/src/test_utils.rs b/sim-lib/src/test_utils.rs index a6b3bd2d..3bd665a9 100644 --- a/sim-lib/src/test_utils.rs +++ b/sim-lib/src/test_utils.rs @@ -1,6 +1,6 @@ use lightning::ln::features::Features; -use rand::distributions::Uniform; -use rand::Rng; +use rand::{distributions::Uniform, thread_rng}; +use rand::{Rng, RngCore}; use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; @@ -53,10 +53,8 @@ pub fn seed_hex(seed: [u8; 32]) -> String { /// Generates a random seed pub fn generate_random_seed() -> [u8; 32] { - let mut seed = [0_u8; 32]; - for index in 0..32 { - seed[index] = rand::random::(); - } - + let mut seed = [0u8; 32]; + let mut rng = thread_rng(); + rng.fill_bytes(&mut seed); seed }