Skip to content

Commit

Permalink
fix: address formating issues
Browse files Browse the repository at this point in the history
  • Loading branch information
enigbe committed Apr 5, 2024
1 parent 05f732b commit 953e40a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn deserialize_f64_greater_than_zero(x: String) -> Result<f64, String> {
/// 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)
Expand All @@ -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)]
Expand Down
18 changes: 12 additions & 6 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WriteResults>,
/// Seed to deterministically run the random activity generator
seed: Option<[u8;32]>
seed: Option<[u8; 32]>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -439,7 +439,7 @@ impl Simulation {
expected_payment_msat: u64,
activity_multiplier: f64,
write_results: Option<WriteResults>,
seed: Option<[u8;32]>
seed: Option<[u8; 32]>,
) -> Self {
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
Self {
Expand All @@ -451,7 +451,7 @@ impl Simulation {
expected_payment_msat,
activity_multiplier,
write_results,
seed
seed,
}
}

Expand Down Expand Up @@ -694,7 +694,10 @@ impl Simulation {
log::debug!("Simulator data collection set up.");
}

async fn activity_executors(&self, seed: Option<[u8;32]>) -> Result<Vec<ExecutorKit>, SimulationError> {
async fn activity_executors(
&self,
seed: Option<[u8; 32]>,
) -> Result<Vec<ExecutorKit>, 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
Expand Down Expand Up @@ -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<Vec<ExecutorKit>, SimulationError> {
async fn random_activity_nodes(
&self,
seed: Option<[u8; 32]>,
) -> Result<Vec<ExecutorKit>, 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();
Expand Down Expand Up @@ -771,7 +777,7 @@ impl Simulation {
*capacity,
self.expected_payment_msat,
self.activity_multiplier,
self.seed
self.seed,
)
.map_err(SimulationError::RandomActivityError)?,
),
Expand Down
3 changes: 1 addition & 2 deletions sim-lib/src/random_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions sim-lib/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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::<u8>();
}

let mut seed = [0u8; 32];
let mut rng = thread_rng();
rng.fill_bytes(&mut seed);
seed
}

0 comments on commit 953e40a

Please sign in to comment.