From a6e1c0039cc252c824c5c48d6a7ca49262d47744 Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Fri, 11 Oct 2024 17:44:12 +0200 Subject: [PATCH] clippy: address some of the pedantic lints --- lib/src/clients/blobssss.rs | 3 ++- lib/src/clients/lighthouse.rs | 2 +- lib/src/clients/lighthouse_vc.rs | 2 +- lib/src/clients/mod.rs | 2 +- lib/src/clients/prometheus.rs | 2 +- lib/src/config/ethshadow.rs | 24 ++++++++++---------- lib/src/config/shadow.rs | 2 +- lib/src/error.rs | 4 ++-- lib/src/genesis.rs | 6 ++--- lib/src/gml.rs | 2 +- lib/src/lib.rs | 2 +- lib/src/network_graph.rs | 10 ++++---- lib/src/node.rs | 4 ++-- lib/src/validators.rs | 39 +++++++++++++++----------------- 14 files changed, 51 insertions(+), 53 deletions(-) diff --git a/lib/src/clients/blobssss.rs b/lib/src/clients/blobssss.rs index cd0e2d7..57f4d40 100644 --- a/lib/src/clients/blobssss.rs +++ b/lib/src/clients/blobssss.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use crate::clients::Client; use crate::config::shadow::Process; use crate::node::{NodeInfo, SimulationContext}; @@ -32,7 +33,7 @@ impl Client for Blobssss { self.private_key, ctx.el_http_endpoints().iter().join(","), ), - environment: Default::default(), + environment: HashMap::default(), expected_final_state: "running".into(), start_time: self.start_time.clone().into(), }) diff --git a/lib/src/clients/lighthouse.rs b/lib/src/clients/lighthouse.rs index 86f00ed..40a6c73 100644 --- a/lib/src/clients/lighthouse.rs +++ b/lib/src/clients/lighthouse.rs @@ -22,7 +22,7 @@ impl Default for Lighthouse { fn default() -> Self { Self { executable: "lighthouse".into(), - extra_args: "".into(), + extra_args: String::new(), lower_target_peers: true, } } diff --git a/lib/src/clients/lighthouse_vc.rs b/lib/src/clients/lighthouse_vc.rs index f8654a2..d737e4b 100644 --- a/lib/src/clients/lighthouse_vc.rs +++ b/lib/src/clients/lighthouse_vc.rs @@ -14,7 +14,7 @@ use std::fs::create_dir; #[serde(default)] pub struct LighthouseValidatorClient { pub executable: CowStr, - pub validators: Option, + pub validators: Option, } impl Default for LighthouseValidatorClient { diff --git a/lib/src/clients/mod.rs b/lib/src/clients/mod.rs index 0d62c9e..663f2c7 100644 --- a/lib/src/clients/mod.rs +++ b/lib/src/clients/mod.rs @@ -26,7 +26,7 @@ pub enum ValidatorDemand { Any, /// We want validators, and the slice will have exactly this amount of elements. Generation /// fails if we can not satisfy this. - Count(u64), + Count(usize), } #[typetag::deserialize(tag = "type")] diff --git a/lib/src/clients/prometheus.rs b/lib/src/clients/prometheus.rs index d8804aa..af8b4c7 100644 --- a/lib/src/clients/prometheus.rs +++ b/lib/src/clients/prometheus.rs @@ -78,7 +78,7 @@ impl Client for Prometheus { dir.to_str().ok_or(Error::NonUTF8Path)?, config_file.to_str().ok_or(Error::NonUTF8Path)?, ), - environment: Default::default(), + environment: HashMap::default(), expected_final_state: "running".into(), start_time: "10s".into(), }) diff --git a/lib/src/config/ethshadow.rs b/lib/src/config/ethshadow.rs index 66ac535..2172f4b 100644 --- a/lib/src/config/ethshadow.rs +++ b/lib/src/config/ethshadow.rs @@ -24,7 +24,7 @@ pub struct EthShadowConfig { nodes: Vec, pub locations: HashMap, pub reliabilities: HashMap, - pub validators: Option, + pub validators: Option, pub clients: HashMap>, #[serde(default = "default_clients")] pub default_clients: HashMap, @@ -36,7 +36,7 @@ pub struct EthShadowConfig { #[derive(Deserialize, Clone, Debug)] #[serde(untagged)] enum NodeConfig { - Simple(u64), + Simple(usize), Detailed(Vec), } @@ -83,17 +83,17 @@ struct SugaredNode { } impl SugaredNode { - fn combinations(&self) -> u64 { - self.locations.len() as u64 - * self.reliabilities.len() as u64 + fn combinations(&self) -> usize { + self.locations.len() + * self.reliabilities.len() * self .clients .values() - .map(|clients| clients.len()) - .product::() as u64 + .map(OneOrMany::len) + .product::() } - fn count_per_combination(&self) -> Result { + fn count_per_combination(&self) -> Result { match self.count { NodeCount::CountPerCombination(count) => Ok(count), NodeCount::TotalCount(count) => { @@ -110,9 +110,9 @@ impl SugaredNode { #[derive(Deserialize, Clone, Copy, Debug)] enum NodeCount { #[serde(rename = "per_combination")] - CountPerCombination(u64), + CountPerCombination(usize), #[serde(rename = "total")] - TotalCount(u64), + TotalCount(usize), } impl Default for NodeCount { @@ -400,7 +400,7 @@ impl EthShadowConfig { .map(|client| { self.clients .get(client.as_str()) - .map(|b| b.as_ref()) + .map(AsRef::as_ref) .ok_or_else(|| Error::UnknownClient(client.clone())) }) .try_collect() @@ -445,7 +445,7 @@ pub struct Node<'a> { pub location: &'a str, pub reliability: &'a str, pub clients: Vec<&'a dyn Client>, - pub count: u64, + pub count: usize, pub tag: Option<&'a str>, } diff --git a/lib/src/config/shadow.rs b/lib/src/config/shadow.rs index 0fd04a2..e76bbcf 100644 --- a/lib/src/config/shadow.rs +++ b/lib/src/config/shadow.rs @@ -134,7 +134,7 @@ impl<'a> Iterator for HostsMut<'a> { fn next(&mut self) -> Option { self.hosts .as_mut() - .and_then(|hosts| hosts.next()) + .and_then(Iterator::next) .map(|(_, host)| { host.as_mapping_mut() .ok_or_else(|| Error::ExpectedOtherType("host".to_string())) diff --git a/lib/src/error.rs b/lib/src/error.rs index 7d5d870..1acf1e8 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -31,7 +31,7 @@ pub enum Error { #[error("Unknown client \"{0}\"")] UnknownClient(String), #[error("You have specified {0} total validators, but VCs have requested {1}")] - MoreValidatorsRequested(u64, u64), + MoreValidatorsRequested(usize, usize), #[error( "You have configured all validator clients to take a specific number of validators \ and it disagrees with the configured total number of validators" @@ -41,7 +41,7 @@ pub enum Error { "The specified number of nodes ({0}) must be divisible by the number of possible \ combinations ({1})" )] - InconsistentCount(u64, u64), + InconsistentCount(usize, usize), #[error("Missing env var: {0}")] MissingEnvVar(#[from] VarError), #[error("Output data folder already exists")] diff --git a/lib/src/genesis.rs b/lib/src/genesis.rs index e8a0ad5..b5246a4 100644 --- a/lib/src/genesis.rs +++ b/lib/src/genesis.rs @@ -61,13 +61,13 @@ pub fn write_config( export( file, "ELECTRA_FORK_EPOCH", - genesis.electra_epoch.unwrap_or(9999999), + genesis.electra_epoch.unwrap_or(9_999_999), )?; export(file, "EIP7594_FORK_VERSION", "0x70000000")?; export( file, "EIP7594_FORK_EPOCH", - genesis.eip7594_epoch.unwrap_or(99999999), + genesis.eip7594_epoch.unwrap_or(99_999_999), )?; export(file, "WITHDRAWAL_TYPE", "0x01")?; export( @@ -80,7 +80,7 @@ pub fn write_config( export( file, "GENESIS_GASLIMIT", - genesis.gaslimit.unwrap_or(25000000), + genesis.gaslimit.unwrap_or(25_000_000), )?; export_optional( file, diff --git a/lib/src/gml.rs b/lib/src/gml.rs index 0ec700f..9c8c40b 100644 --- a/lib/src/gml.rs +++ b/lib/src/gml.rs @@ -80,7 +80,7 @@ impl Drop for Gml { } impl NetworkNode { - pub fn id(&self) -> u64 { + pub fn id(self) -> u64 { self.id } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 22f47d6..d12b911 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -111,7 +111,7 @@ pub fn generate>( // postprocessing given shadow config values: overwrite string network ids for host in shadow_config.hosts_mut()? { if let Some(node_id) = host?.network_node_id_mut() { - if let Some((location, reliability)) = node_id.as_str().and_then(|s| s.split_once("-")) + if let Some((location, reliability)) = node_id.as_str().and_then(|s| s.split_once('-')) { let node = network_graph.assign_network_node(location, reliability)?; *node_id = Value::Number(node.id().into()); diff --git a/lib/src/network_graph.rs b/lib/src/network_graph.rs index dbe81d9..08e0aa1 100644 --- a/lib/src/network_graph.rs +++ b/lib/src/network_graph.rs @@ -36,7 +36,7 @@ impl SimpleNetworkGraph<'_> { let mut gml = String::new(); let mut gml_builder = Gml::new(&mut gml, true)?; for location_name in config.locations.keys() { - for (reliability_name, reliability) in config.reliabilities.iter() { + for (reliability_name, reliability) in &config.reliabilities { let node = gml_builder.add_node( &reliability.bandwidth_up, &reliability.bandwidth_down, @@ -49,12 +49,12 @@ impl SimpleNetworkGraph<'_> { .insert(reliability_name, node); } } - for (src_location_name, src_location) in config.locations.iter() { - for (src_reliability_name, src_reliability) in config.reliabilities.iter() { + for (src_location_name, src_location) in &config.locations { + for (src_reliability_name, src_reliability) in &config.reliabilities { let src_node = network_graph.get_network_node(src_location_name, src_reliability_name)?; for dest_location_name in config.locations.keys() { - for (dest_reliability_name, dest_reliability) in config.reliabilities.iter() { + for (dest_reliability_name, dest_reliability) in &config.reliabilities { let dest_node = network_graph .get_network_node(dest_location_name, dest_reliability_name)?; let mut latency = src_location @@ -89,7 +89,7 @@ impl SimpleNetworkGraph<'_> { "{src_location_name}-{src_reliability_name} to \ {dest_location_name}-{dest_reliability_name}" )), - )? + )?; } } } diff --git a/lib/src/node.rs b/lib/src/node.rs index edd9458..d7576eb 100644 --- a/lib/src/node.rs +++ b/lib/src/node.rs @@ -53,8 +53,8 @@ impl<'c, 'n> NodeManager<'c, 'n> { rng, base_dir.join("metadata"), base_dir.join("jwt/jwtsecret"), - num_el_clients as usize, - num_cl_clients as usize, + num_el_clients, + num_cl_clients, ); Self { ctx, diff --git a/lib/src/validators.rs b/lib/src/validators.rs index 9aadda8..fe52f4d 100644 --- a/lib/src/validators.rs +++ b/lib/src/validators.rs @@ -41,25 +41,22 @@ impl ValidatorManager { } } - match config.validators { - Some(validators) => { - let Some(remaining) = validators.checked_sub(requested) else { - return Err(Error::MoreValidatorsRequested(validators, requested)); - }; - validator_count = validators; - if anys != 0 { - val_for_each_any = remaining / anys; - remainder = remaining % anys; - } else if remaining != 0 { - return Err(Error::LeftoverValidators); - } + if let Some(validators) = config.validators { + let Some(remaining) = validators.checked_sub(requested) else { + return Err(Error::MoreValidatorsRequested(validators, requested)); + }; + validator_count = validators; + if anys != 0 { + val_for_each_any = remaining / anys; + remainder = remaining % anys; + } else if remaining != 0 { + return Err(Error::LeftoverValidators); } - None => { - if anys != 0 { - return Err(Error::MissingValidatorCount); - } - validator_count = requested; + } else { + if anys != 0 { + return Err(Error::MissingValidatorCount); } + validator_count = requested; }; info!("Generating {validator_count} validators"); @@ -75,13 +72,13 @@ impl ValidatorManager { .mnemonic .as_deref() .unwrap_or(DEFAULT_MNEMONIC), - validator_count as usize, + validator_count, )?; Ok(ValidatorManager { validators, - val_for_each_any: val_for_each_any as usize, - remainder: remainder as usize, + val_for_each_any, + remainder, already_assigned: 0, }) } @@ -97,7 +94,7 @@ impl ValidatorManager { self.val_for_each_any } } - ValidatorDemand::Count(count) => count as usize, + ValidatorDemand::Count(count) => count, }; let start = self.already_assigned; let end = start + count;