From b837d350a448ba400fdd0296464a58672db2cb7b Mon Sep 17 00:00:00 2001 From: hinto-janai Date: Mon, 2 Sep 2024 13:10:45 -0400 Subject: [PATCH] workspace: add naming convention lints (#261) * add lint to {Cargo,clippy}.toml * `RandomXVM` -> `RandomXVm` * epee: `TT` -> `T2` --- Cargo.toml | 2 ++ clippy.toml | 1 + consensus/src/block/alt_block.rs | 4 +-- consensus/src/block/batch_prepare.rs | 4 +-- consensus/src/context.rs | 8 ++--- consensus/src/context/alt_chains.rs | 4 +-- consensus/src/context/rx_vms.rs | 38 ++++++++++++------------ consensus/src/context/task.rs | 4 +-- consensus/src/tests/context/rx_vms.rs | 6 ++-- net/epee-encoding/tests/duplicate_key.rs | 6 ++-- 10 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 clippy.toml diff --git a/Cargo.toml b/Cargo.toml index 06b49a0..1a0c667 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -262,6 +262,7 @@ empty_structs_with_brackets = "deny" empty_enum_variants_with_brackets = "deny" empty_drop = "deny" clone_on_ref_ptr = "deny" +upper_case_acronyms = "deny" # Hot # inline_always = "deny" @@ -309,6 +310,7 @@ let_underscore_drop = "deny" unreachable_pub = "deny" unused_qualifications = "deny" variant_size_differences = "deny" +non_camel_case_types = "deny" # Hot # unused_results = "deny" diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..cc94ec5 --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +upper-case-acronyms-aggressive = true diff --git a/consensus/src/block/alt_block.rs b/consensus/src/block/alt_block.rs index 9b94a27..b20b4f2 100644 --- a/consensus/src/block/alt_block.rs +++ b/consensus/src/block/alt_block.rs @@ -24,7 +24,7 @@ use crate::{ block::{free::pull_ordered_transactions, PreparedBlock}, context::{ difficulty::DifficultyCache, - rx_vms::RandomXVM, + rx_vms::RandomXVm, weight::{self, BlockWeightsCache}, AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW, }, @@ -195,7 +195,7 @@ async fn alt_rx_vm( parent_chain: Chain, alt_chain_context: &mut AltChainContextCache, context_svc: C, -) -> Result>, ExtendedConsensusError> +) -> Result>, ExtendedConsensusError> where C: Service< BlockChainContextRequest, diff --git a/consensus/src/block/batch_prepare.rs b/consensus/src/block/batch_prepare.rs index 9974d6d..d32cd76 100644 --- a/consensus/src/block/batch_prepare.rs +++ b/consensus/src/block/batch_prepare.rs @@ -15,7 +15,7 @@ use cuprate_helper::asynch::rayon_spawn_async; use crate::{ block::{free::pull_ordered_transactions, PreparedBlock, PreparedBlockExPow}, - context::rx_vms::RandomXVM, + context::rx_vms::RandomXVm, transactions::new_tx_verification_data, BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError, VerifyBlockResponse, @@ -148,7 +148,7 @@ where tracing::debug!("New randomX seed in batch, initialising VM"); let new_vm = rayon_spawn_async(move || { - Arc::new(RandomXVM::new(&new_vm_seed).expect("RandomX VM gave an error on set up!")) + Arc::new(RandomXVm::new(&new_vm_seed).expect("RandomX VM gave an error on set up!")) }) .await; diff --git a/consensus/src/context.rs b/consensus/src/context.rs index 26be75c..9e71304 100644 --- a/consensus/src/context.rs +++ b/consensus/src/context.rs @@ -33,7 +33,7 @@ mod tokens; use cuprate_types::Chain; use difficulty::DifficultyCache; -use rx_vms::RandomXVM; +use rx_vms::RandomXVm; use weight::BlockWeightsCache; pub(crate) use alt_chains::{sealed::AltChainRequestToken, AltChainContextCache}; @@ -236,7 +236,7 @@ pub enum BlockChainContextRequest { /// seed. /// /// This should include the seed used to init this VM and the VM. - NewRXVM(([u8; 32], Arc)), + NewRXVM(([u8; 32], Arc)), /// A request to add a new block to the cache. Update(NewBlockData), /// Pop blocks from the cache to the specified height. @@ -313,7 +313,7 @@ pub enum BlockChainContextResponse { /// Blockchain context response. Context(BlockChainContext), /// A map of seed height to RandomX VMs. - RxVms(HashMap>), + RxVms(HashMap>), /// A list of difficulties. BatchDifficulties(Vec), /// An alt chain context cache. @@ -321,7 +321,7 @@ pub enum BlockChainContextResponse { /// A difficulty cache for an alt chain. AltChainDifficultyCache(DifficultyCache), /// A randomX VM for an alt chain. - AltChainRxVM(Arc), + AltChainRxVM(Arc), /// A weight cache for an alt chain AltChainWeightCache(BlockWeightsCache), /// A generic Ok response. diff --git a/consensus/src/context/alt_chains.rs b/consensus/src/context/alt_chains.rs index 5586226..937e847 100644 --- a/consensus/src/context/alt_chains.rs +++ b/consensus/src/context/alt_chains.rs @@ -11,7 +11,7 @@ use cuprate_types::{ use crate::{ ExtendedConsensusError, __private::Database, - context::{difficulty::DifficultyCache, rx_vms::RandomXVM, weight::BlockWeightsCache}, + context::{difficulty::DifficultyCache, rx_vms::RandomXVm, weight::BlockWeightsCache}, }; pub(crate) mod sealed { @@ -32,7 +32,7 @@ pub struct AltChainContextCache { pub difficulty_cache: Option, /// A cached RX VM. - pub cached_rx_vm: Option<(usize, Arc)>, + pub cached_rx_vm: Option<(usize, Arc)>, /// The chain height of the alt chain. pub chain_height: usize, diff --git a/consensus/src/context/rx_vms.rs b/consensus/src/context/rx_vms.rs index 01aa973..b1ab102 100644 --- a/consensus/src/context/rx_vms.rs +++ b/consensus/src/context/rx_vms.rs @@ -9,7 +9,7 @@ use std::{ }; use futures::{stream::FuturesOrdered, StreamExt}; -use randomx_rs::{RandomXCache, RandomXError, RandomXFlag, RandomXVM as VMInner}; +use randomx_rs::{RandomXCache, RandomXError, RandomXFlag, RandomXVM as VmInner}; use rayon::prelude::*; use thread_local::ThreadLocal; use tower::ServiceExt; @@ -33,16 +33,16 @@ const RX_SEEDS_CACHED: usize = 2; /// A multithreaded randomX VM. #[derive(Debug)] -pub struct RandomXVM { +pub struct RandomXVm { /// These RandomX VMs all share the same cache. - vms: ThreadLocal, + vms: ThreadLocal, /// The RandomX cache. cache: RandomXCache, /// The flags used to start the RandomX VMs. flags: RandomXFlag, } -impl RandomXVM { +impl RandomXVm { /// Create a new multithreaded randomX VM with the provided seed. pub fn new(seed: &[u8; 32]) -> Result { // TODO: allow passing in flags. @@ -50,7 +50,7 @@ impl RandomXVM { let cache = RandomXCache::new(flags, seed.as_slice())?; - Ok(RandomXVM { + Ok(RandomXVm { vms: ThreadLocal::new(), cache, flags, @@ -58,12 +58,12 @@ impl RandomXVM { } } -impl RandomX for RandomXVM { +impl RandomX for RandomXVm { type Error = RandomXError; fn calculate_hash(&self, buf: &[u8]) -> Result<[u8; 32], Self::Error> { self.vms - .get_or_try(|| VMInner::new(self.flags, Some(self.cache.clone()), None))? + .get_or_try(|| VmInner::new(self.flags, Some(self.cache.clone()), None))? .calculate_hash(buf) .map(|out| out.try_into().unwrap()) } @@ -72,17 +72,17 @@ impl RandomX for RandomXVM { /// The randomX VMs cache, keeps the VM needed to calculate the current block's PoW hash (if a VM is needed) and a /// couple more around this VM. #[derive(Clone, Debug)] -pub struct RandomXVMCache { +pub struct RandomXVmCache { /// The top [`RX_SEEDS_CACHED`] RX seeds. pub(crate) seeds: VecDeque<(usize, [u8; 32])>, /// The VMs for `seeds` (if after hf 12, otherwise this will be empty). - pub(crate) vms: HashMap>, + pub(crate) vms: HashMap>, /// A single cached VM that was given to us from a part of Cuprate. - pub(crate) cached_vm: Option<([u8; 32], Arc)>, + pub(crate) cached_vm: Option<([u8; 32], Arc)>, } -impl RandomXVMCache { +impl RandomXVmCache { #[instrument(name = "init_rx_vm_cache", level = "info", skip(database))] pub async fn init_from_chain_height( chain_height: usize, @@ -106,7 +106,7 @@ impl RandomXVMCache { .map(|(height, seed)| { ( *height, - Arc::new(RandomXVM::new(seed).expect("Failed to create RandomX VM!")), + Arc::new(RandomXVm::new(seed).expect("Failed to create RandomX VM!")), ) }) .collect() @@ -117,7 +117,7 @@ impl RandomXVMCache { HashMap::new() }; - Ok(RandomXVMCache { + Ok(RandomXVmCache { seeds, vms, cached_vm: None, @@ -125,7 +125,7 @@ impl RandomXVMCache { } /// Add a randomX VM to the cache, with the seed it was created with. - pub fn add_vm(&mut self, vm: ([u8; 32], Arc)) { + pub fn add_vm(&mut self, vm: ([u8; 32], Arc)) { self.cached_vm.replace(vm); } @@ -136,7 +136,7 @@ impl RandomXVMCache { height: usize, chain: Chain, database: D, - ) -> Result, ExtendedConsensusError> { + ) -> Result, ExtendedConsensusError> { let seed_height = randomx_seed_height(height); let BlockchainResponse::BlockHash(seed_hash) = database @@ -156,13 +156,13 @@ impl RandomXVMCache { } } - let alt_vm = rayon_spawn_async(move || Arc::new(RandomXVM::new(&seed_hash).unwrap())).await; + let alt_vm = rayon_spawn_async(move || Arc::new(RandomXVm::new(&seed_hash).unwrap())).await; Ok(alt_vm) } /// Get the main-chain RandomX VMs. - pub async fn get_vms(&mut self) -> HashMap> { + pub async fn get_vms(&mut self) -> HashMap> { match self.seeds.len().checked_sub(self.vms.len()) { // No difference in the amount of seeds to VMs. Some(0) => (), @@ -184,7 +184,7 @@ impl RandomXVMCache { } }; - rayon_spawn_async(move || Arc::new(RandomXVM::new(&next_seed_hash).unwrap())) + rayon_spawn_async(move || Arc::new(RandomXVm::new(&next_seed_hash).unwrap())) .await }; @@ -200,7 +200,7 @@ impl RandomXVMCache { seeds_clone .par_iter() .map(|(height, seed)| { - let vm = RandomXVM::new(seed).expect("Failed to create RandomX VM!"); + let vm = RandomXVm::new(seed).expect("Failed to create RandomX VM!"); let vm = Arc::new(vm); (*height, vm) }) diff --git a/consensus/src/context/task.rs b/consensus/src/context/task.rs index 2376c35..bc54285 100644 --- a/consensus/src/context/task.rs +++ b/consensus/src/context/task.rs @@ -46,7 +46,7 @@ pub struct ContextTask { /// The weight cache. weight_cache: weight::BlockWeightsCache, /// The RX VM cache. - rx_vm_cache: rx_vms::RandomXVMCache, + rx_vm_cache: rx_vms::RandomXVmCache, /// The hard-fork state cache. hardfork_state: hardforks::HardForkState, @@ -128,7 +128,7 @@ impl ContextTask { let db = database.clone(); let rx_seed_handle = tokio::spawn(async move { - rx_vms::RandomXVMCache::init_from_chain_height(chain_height, ¤t_hf, db).await + rx_vms::RandomXVmCache::init_from_chain_height(chain_height, ¤t_hf, db).await }); let context_svc = ContextTask { diff --git a/consensus/src/tests/context/rx_vms.rs b/consensus/src/tests/context/rx_vms.rs index f18a9b5..5c198cf 100644 --- a/consensus/src/tests/context/rx_vms.rs +++ b/consensus/src/tests/context/rx_vms.rs @@ -9,7 +9,7 @@ use cuprate_consensus_rules::{ }; use crate::{ - context::rx_vms::{get_last_rx_seed_heights, RandomXVMCache}, + context::rx_vms::{get_last_rx_seed_heights, RandomXVmCache}, tests::mock_db::*, }; @@ -42,7 +42,7 @@ fn rx_heights_consistent() { async fn rx_vm_created_on_hf_12() { let db = DummyDatabaseBuilder::default().finish(Some(10)); - let mut cache = RandomXVMCache::init_from_chain_height(10, &HardFork::V11, db) + let mut cache = RandomXVmCache::init_from_chain_height(10, &HardFork::V11, db) .await .unwrap(); @@ -67,7 +67,7 @@ proptest! { let rt = Builder::new_multi_thread().enable_all().build().unwrap(); rt.block_on(async move { - let cache = RandomXVMCache::init_from_chain_height(10, &hf, db).await.unwrap(); + let cache = RandomXVmCache::init_from_chain_height(10, &hf, db).await.unwrap(); assert!(cache.seeds.len() == cache.vms.len() || hf < HardFork::V12); }); } diff --git a/net/epee-encoding/tests/duplicate_key.rs b/net/epee-encoding/tests/duplicate_key.rs index c1b3148..0ed87af 100644 --- a/net/epee-encoding/tests/duplicate_key.rs +++ b/net/epee-encoding/tests/duplicate_key.rs @@ -9,12 +9,12 @@ epee_object!( a: u8, ); -struct TT { +struct T2 { a: u8, } epee_object!( - TT, + T2, a: u8 = 0, ); @@ -35,5 +35,5 @@ fn duplicate_key_with_default() { b'a', 0x0B, 0x00, ]; - assert!(from_bytes::(&mut &data[..]).is_err()); + assert!(from_bytes::(&mut &data[..]).is_err()); }