mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-22 11:39:30 +00:00
workspace: add naming convention lints (#261)
* add lint to {Cargo,clippy}.toml * `RandomXVM` -> `RandomXVm` * epee: `TT` -> `T2`
This commit is contained in:
parent
bec8cc0aa4
commit
b837d350a4
10 changed files with 40 additions and 37 deletions
|
@ -262,6 +262,7 @@ empty_structs_with_brackets = "deny"
|
||||||
empty_enum_variants_with_brackets = "deny"
|
empty_enum_variants_with_brackets = "deny"
|
||||||
empty_drop = "deny"
|
empty_drop = "deny"
|
||||||
clone_on_ref_ptr = "deny"
|
clone_on_ref_ptr = "deny"
|
||||||
|
upper_case_acronyms = "deny"
|
||||||
|
|
||||||
# Hot
|
# Hot
|
||||||
# inline_always = "deny"
|
# inline_always = "deny"
|
||||||
|
@ -309,6 +310,7 @@ let_underscore_drop = "deny"
|
||||||
unreachable_pub = "deny"
|
unreachable_pub = "deny"
|
||||||
unused_qualifications = "deny"
|
unused_qualifications = "deny"
|
||||||
variant_size_differences = "deny"
|
variant_size_differences = "deny"
|
||||||
|
non_camel_case_types = "deny"
|
||||||
|
|
||||||
# Hot
|
# Hot
|
||||||
# unused_results = "deny"
|
# unused_results = "deny"
|
||||||
|
|
1
clippy.toml
Normal file
1
clippy.toml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
upper-case-acronyms-aggressive = true
|
|
@ -24,7 +24,7 @@ use crate::{
|
||||||
block::{free::pull_ordered_transactions, PreparedBlock},
|
block::{free::pull_ordered_transactions, PreparedBlock},
|
||||||
context::{
|
context::{
|
||||||
difficulty::DifficultyCache,
|
difficulty::DifficultyCache,
|
||||||
rx_vms::RandomXVM,
|
rx_vms::RandomXVm,
|
||||||
weight::{self, BlockWeightsCache},
|
weight::{self, BlockWeightsCache},
|
||||||
AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
|
AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
|
||||||
},
|
},
|
||||||
|
@ -195,7 +195,7 @@ async fn alt_rx_vm<C>(
|
||||||
parent_chain: Chain,
|
parent_chain: Chain,
|
||||||
alt_chain_context: &mut AltChainContextCache,
|
alt_chain_context: &mut AltChainContextCache,
|
||||||
context_svc: C,
|
context_svc: C,
|
||||||
) -> Result<Option<Arc<RandomXVM>>, ExtendedConsensusError>
|
) -> Result<Option<Arc<RandomXVm>>, ExtendedConsensusError>
|
||||||
where
|
where
|
||||||
C: Service<
|
C: Service<
|
||||||
BlockChainContextRequest,
|
BlockChainContextRequest,
|
||||||
|
|
|
@ -15,7 +15,7 @@ use cuprate_helper::asynch::rayon_spawn_async;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
block::{free::pull_ordered_transactions, PreparedBlock, PreparedBlockExPow},
|
block::{free::pull_ordered_transactions, PreparedBlock, PreparedBlockExPow},
|
||||||
context::rx_vms::RandomXVM,
|
context::rx_vms::RandomXVm,
|
||||||
transactions::new_tx_verification_data,
|
transactions::new_tx_verification_data,
|
||||||
BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError,
|
BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError,
|
||||||
VerifyBlockResponse,
|
VerifyBlockResponse,
|
||||||
|
@ -148,7 +148,7 @@ where
|
||||||
tracing::debug!("New randomX seed in batch, initialising VM");
|
tracing::debug!("New randomX seed in batch, initialising VM");
|
||||||
|
|
||||||
let new_vm = rayon_spawn_async(move || {
|
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;
|
.await;
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ mod tokens;
|
||||||
|
|
||||||
use cuprate_types::Chain;
|
use cuprate_types::Chain;
|
||||||
use difficulty::DifficultyCache;
|
use difficulty::DifficultyCache;
|
||||||
use rx_vms::RandomXVM;
|
use rx_vms::RandomXVm;
|
||||||
use weight::BlockWeightsCache;
|
use weight::BlockWeightsCache;
|
||||||
|
|
||||||
pub(crate) use alt_chains::{sealed::AltChainRequestToken, AltChainContextCache};
|
pub(crate) use alt_chains::{sealed::AltChainRequestToken, AltChainContextCache};
|
||||||
|
@ -236,7 +236,7 @@ pub enum BlockChainContextRequest {
|
||||||
/// seed.
|
/// seed.
|
||||||
///
|
///
|
||||||
/// This should include the seed used to init this VM and the VM.
|
/// This should include the seed used to init this VM and the VM.
|
||||||
NewRXVM(([u8; 32], Arc<RandomXVM>)),
|
NewRXVM(([u8; 32], Arc<RandomXVm>)),
|
||||||
/// A request to add a new block to the cache.
|
/// A request to add a new block to the cache.
|
||||||
Update(NewBlockData),
|
Update(NewBlockData),
|
||||||
/// Pop blocks from the cache to the specified height.
|
/// Pop blocks from the cache to the specified height.
|
||||||
|
@ -313,7 +313,7 @@ pub enum BlockChainContextResponse {
|
||||||
/// Blockchain context response.
|
/// Blockchain context response.
|
||||||
Context(BlockChainContext),
|
Context(BlockChainContext),
|
||||||
/// A map of seed height to RandomX VMs.
|
/// A map of seed height to RandomX VMs.
|
||||||
RxVms(HashMap<usize, Arc<RandomXVM>>),
|
RxVms(HashMap<usize, Arc<RandomXVm>>),
|
||||||
/// A list of difficulties.
|
/// A list of difficulties.
|
||||||
BatchDifficulties(Vec<u128>),
|
BatchDifficulties(Vec<u128>),
|
||||||
/// An alt chain context cache.
|
/// An alt chain context cache.
|
||||||
|
@ -321,7 +321,7 @@ pub enum BlockChainContextResponse {
|
||||||
/// A difficulty cache for an alt chain.
|
/// A difficulty cache for an alt chain.
|
||||||
AltChainDifficultyCache(DifficultyCache),
|
AltChainDifficultyCache(DifficultyCache),
|
||||||
/// A randomX VM for an alt chain.
|
/// A randomX VM for an alt chain.
|
||||||
AltChainRxVM(Arc<RandomXVM>),
|
AltChainRxVM(Arc<RandomXVm>),
|
||||||
/// A weight cache for an alt chain
|
/// A weight cache for an alt chain
|
||||||
AltChainWeightCache(BlockWeightsCache),
|
AltChainWeightCache(BlockWeightsCache),
|
||||||
/// A generic Ok response.
|
/// A generic Ok response.
|
||||||
|
|
|
@ -11,7 +11,7 @@ use cuprate_types::{
|
||||||
use crate::{
|
use crate::{
|
||||||
ExtendedConsensusError,
|
ExtendedConsensusError,
|
||||||
__private::Database,
|
__private::Database,
|
||||||
context::{difficulty::DifficultyCache, rx_vms::RandomXVM, weight::BlockWeightsCache},
|
context::{difficulty::DifficultyCache, rx_vms::RandomXVm, weight::BlockWeightsCache},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) mod sealed {
|
pub(crate) mod sealed {
|
||||||
|
@ -32,7 +32,7 @@ pub struct AltChainContextCache {
|
||||||
pub difficulty_cache: Option<DifficultyCache>,
|
pub difficulty_cache: Option<DifficultyCache>,
|
||||||
|
|
||||||
/// A cached RX VM.
|
/// A cached RX VM.
|
||||||
pub cached_rx_vm: Option<(usize, Arc<RandomXVM>)>,
|
pub cached_rx_vm: Option<(usize, Arc<RandomXVm>)>,
|
||||||
|
|
||||||
/// The chain height of the alt chain.
|
/// The chain height of the alt chain.
|
||||||
pub chain_height: usize,
|
pub chain_height: usize,
|
||||||
|
|
|
@ -9,7 +9,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::{stream::FuturesOrdered, StreamExt};
|
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 rayon::prelude::*;
|
||||||
use thread_local::ThreadLocal;
|
use thread_local::ThreadLocal;
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
@ -33,16 +33,16 @@ const RX_SEEDS_CACHED: usize = 2;
|
||||||
|
|
||||||
/// A multithreaded randomX VM.
|
/// A multithreaded randomX VM.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RandomXVM {
|
pub struct RandomXVm {
|
||||||
/// These RandomX VMs all share the same cache.
|
/// These RandomX VMs all share the same cache.
|
||||||
vms: ThreadLocal<VMInner>,
|
vms: ThreadLocal<VmInner>,
|
||||||
/// The RandomX cache.
|
/// The RandomX cache.
|
||||||
cache: RandomXCache,
|
cache: RandomXCache,
|
||||||
/// The flags used to start the RandomX VMs.
|
/// The flags used to start the RandomX VMs.
|
||||||
flags: RandomXFlag,
|
flags: RandomXFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RandomXVM {
|
impl RandomXVm {
|
||||||
/// Create a new multithreaded randomX VM with the provided seed.
|
/// Create a new multithreaded randomX VM with the provided seed.
|
||||||
pub fn new(seed: &[u8; 32]) -> Result<Self, RandomXError> {
|
pub fn new(seed: &[u8; 32]) -> Result<Self, RandomXError> {
|
||||||
// TODO: allow passing in flags.
|
// TODO: allow passing in flags.
|
||||||
|
@ -50,7 +50,7 @@ impl RandomXVM {
|
||||||
|
|
||||||
let cache = RandomXCache::new(flags, seed.as_slice())?;
|
let cache = RandomXCache::new(flags, seed.as_slice())?;
|
||||||
|
|
||||||
Ok(RandomXVM {
|
Ok(RandomXVm {
|
||||||
vms: ThreadLocal::new(),
|
vms: ThreadLocal::new(),
|
||||||
cache,
|
cache,
|
||||||
flags,
|
flags,
|
||||||
|
@ -58,12 +58,12 @@ impl RandomXVM {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RandomX for RandomXVM {
|
impl RandomX for RandomXVm {
|
||||||
type Error = RandomXError;
|
type Error = RandomXError;
|
||||||
|
|
||||||
fn calculate_hash(&self, buf: &[u8]) -> Result<[u8; 32], Self::Error> {
|
fn calculate_hash(&self, buf: &[u8]) -> Result<[u8; 32], Self::Error> {
|
||||||
self.vms
|
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)
|
.calculate_hash(buf)
|
||||||
.map(|out| out.try_into().unwrap())
|
.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
|
/// 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.
|
/// couple more around this VM.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RandomXVMCache {
|
pub struct RandomXVmCache {
|
||||||
/// The top [`RX_SEEDS_CACHED`] RX seeds.
|
/// The top [`RX_SEEDS_CACHED`] RX seeds.
|
||||||
pub(crate) seeds: VecDeque<(usize, [u8; 32])>,
|
pub(crate) seeds: VecDeque<(usize, [u8; 32])>,
|
||||||
/// The VMs for `seeds` (if after hf 12, otherwise this will be empty).
|
/// The VMs for `seeds` (if after hf 12, otherwise this will be empty).
|
||||||
pub(crate) vms: HashMap<usize, Arc<RandomXVM>>,
|
pub(crate) vms: HashMap<usize, Arc<RandomXVm>>,
|
||||||
|
|
||||||
/// A single cached VM that was given to us from a part of Cuprate.
|
/// A single cached VM that was given to us from a part of Cuprate.
|
||||||
pub(crate) cached_vm: Option<([u8; 32], Arc<RandomXVM>)>,
|
pub(crate) cached_vm: Option<([u8; 32], Arc<RandomXVm>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RandomXVMCache {
|
impl RandomXVmCache {
|
||||||
#[instrument(name = "init_rx_vm_cache", level = "info", skip(database))]
|
#[instrument(name = "init_rx_vm_cache", level = "info", skip(database))]
|
||||||
pub async fn init_from_chain_height<D: Database + Clone>(
|
pub async fn init_from_chain_height<D: Database + Clone>(
|
||||||
chain_height: usize,
|
chain_height: usize,
|
||||||
|
@ -106,7 +106,7 @@ impl RandomXVMCache {
|
||||||
.map(|(height, seed)| {
|
.map(|(height, seed)| {
|
||||||
(
|
(
|
||||||
*height,
|
*height,
|
||||||
Arc::new(RandomXVM::new(seed).expect("Failed to create RandomX VM!")),
|
Arc::new(RandomXVm::new(seed).expect("Failed to create RandomX VM!")),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -117,7 +117,7 @@ impl RandomXVMCache {
|
||||||
HashMap::new()
|
HashMap::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(RandomXVMCache {
|
Ok(RandomXVmCache {
|
||||||
seeds,
|
seeds,
|
||||||
vms,
|
vms,
|
||||||
cached_vm: None,
|
cached_vm: None,
|
||||||
|
@ -125,7 +125,7 @@ impl RandomXVMCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a randomX VM to the cache, with the seed it was created with.
|
/// Add a randomX VM to the cache, with the seed it was created with.
|
||||||
pub fn add_vm(&mut self, vm: ([u8; 32], Arc<RandomXVM>)) {
|
pub fn add_vm(&mut self, vm: ([u8; 32], Arc<RandomXVm>)) {
|
||||||
self.cached_vm.replace(vm);
|
self.cached_vm.replace(vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ impl RandomXVMCache {
|
||||||
height: usize,
|
height: usize,
|
||||||
chain: Chain,
|
chain: Chain,
|
||||||
database: D,
|
database: D,
|
||||||
) -> Result<Arc<RandomXVM>, ExtendedConsensusError> {
|
) -> Result<Arc<RandomXVm>, ExtendedConsensusError> {
|
||||||
let seed_height = randomx_seed_height(height);
|
let seed_height = randomx_seed_height(height);
|
||||||
|
|
||||||
let BlockchainResponse::BlockHash(seed_hash) = database
|
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)
|
Ok(alt_vm)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the main-chain RandomX VMs.
|
/// Get the main-chain RandomX VMs.
|
||||||
pub async fn get_vms(&mut self) -> HashMap<usize, Arc<RandomXVM>> {
|
pub async fn get_vms(&mut self) -> HashMap<usize, Arc<RandomXVm>> {
|
||||||
match self.seeds.len().checked_sub(self.vms.len()) {
|
match self.seeds.len().checked_sub(self.vms.len()) {
|
||||||
// No difference in the amount of seeds to VMs.
|
// No difference in the amount of seeds to VMs.
|
||||||
Some(0) => (),
|
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
|
.await
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -200,7 +200,7 @@ impl RandomXVMCache {
|
||||||
seeds_clone
|
seeds_clone
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|(height, seed)| {
|
.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);
|
let vm = Arc::new(vm);
|
||||||
(*height, vm)
|
(*height, vm)
|
||||||
})
|
})
|
||||||
|
|
|
@ -46,7 +46,7 @@ pub struct ContextTask<D: Database> {
|
||||||
/// The weight cache.
|
/// The weight cache.
|
||||||
weight_cache: weight::BlockWeightsCache,
|
weight_cache: weight::BlockWeightsCache,
|
||||||
/// The RX VM cache.
|
/// The RX VM cache.
|
||||||
rx_vm_cache: rx_vms::RandomXVMCache,
|
rx_vm_cache: rx_vms::RandomXVmCache,
|
||||||
/// The hard-fork state cache.
|
/// The hard-fork state cache.
|
||||||
hardfork_state: hardforks::HardForkState,
|
hardfork_state: hardforks::HardForkState,
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ impl<D: Database + Clone + Send + 'static> ContextTask<D> {
|
||||||
|
|
||||||
let db = database.clone();
|
let db = database.clone();
|
||||||
let rx_seed_handle = tokio::spawn(async move {
|
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 {
|
let context_svc = ContextTask {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use cuprate_consensus_rules::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::rx_vms::{get_last_rx_seed_heights, RandomXVMCache},
|
context::rx_vms::{get_last_rx_seed_heights, RandomXVmCache},
|
||||||
tests::mock_db::*,
|
tests::mock_db::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ fn rx_heights_consistent() {
|
||||||
async fn rx_vm_created_on_hf_12() {
|
async fn rx_vm_created_on_hf_12() {
|
||||||
let db = DummyDatabaseBuilder::default().finish(Some(10));
|
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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ proptest! {
|
||||||
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
|
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
|
||||||
|
|
||||||
rt.block_on(async move {
|
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);
|
assert!(cache.seeds.len() == cache.vms.len() || hf < HardFork::V12);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,12 @@ epee_object!(
|
||||||
a: u8,
|
a: u8,
|
||||||
);
|
);
|
||||||
|
|
||||||
struct TT {
|
struct T2 {
|
||||||
a: u8,
|
a: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
epee_object!(
|
epee_object!(
|
||||||
TT,
|
T2,
|
||||||
a: u8 = 0,
|
a: u8 = 0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -35,5 +35,5 @@ fn duplicate_key_with_default() {
|
||||||
b'a', 0x0B, 0x00,
|
b'a', 0x0B, 0x00,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(from_bytes::<TT, _>(&mut &data[..]).is_err());
|
assert!(from_bytes::<T2, _>(&mut &data[..]).is_err());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue