validator: Add wrapper for initializing RandomX datasets

This commit is contained in:
parazyd
2025-05-21 19:56:30 +02:00
committed by skoupidi
parent 8e18778e46
commit 3a42ba5fda
4 changed files with 33 additions and 8 deletions

View File

@@ -175,6 +175,7 @@ validator = [
"randomx", "randomx",
"smol", "smol",
"system",
"wasm-runtime", "wasm-runtime",
] ]
@@ -257,6 +258,7 @@ rpc = [
] ]
system = [ system = [
"futures",
"pin-project-lite", "pin-project-lite",
"rand", "rand",
"smol", "smol",

View File

@@ -43,6 +43,9 @@ use consensus::{Consensus, Fork, Proposal};
pub mod pow; pub mod pow;
use pow::PoWModule; use pow::PoWModule;
/// RandomX infrastructure
pub mod randomx_factory;
/// Monero infrastructure /// Monero infrastructure
pub mod xmr; pub mod xmr;

View File

@@ -37,7 +37,7 @@ use crate::{
Blockchain, BlockchainOverlayPtr, Blockchain, BlockchainOverlayPtr,
}, },
util::{ringbuffer::RingBuffer, time::Timestamp}, util::{ringbuffer::RingBuffer, time::Timestamp},
validator::utils::median, validator::{randomx_factory::init_dataset_wrapper, utils::median},
Error, Result, Error, Result,
}; };
@@ -380,9 +380,9 @@ pub fn mine_block(
let dataset = if threads > 1 { let dataset = if threads > 1 {
let a = (dataset_item_count * (t as u32)) / (threads as u32); let a = (dataset_item_count * (t as u32)) / (threads as u32);
let b = (dataset_item_count * (t as u32 + 1)) / (threads as u32); let b = (dataset_item_count * (t as u32 + 1)) / (threads as u32);
RandomXDataset::new(flags, cache.clone(), a, b - a)? init_dataset_wrapper(flags, cache.clone(), a, b - a)?
} else { } else {
RandomXDataset::new(flags, cache.clone(), 0, dataset_item_count)? init_dataset_wrapper(flags, cache.clone(), 0, dataset_item_count)?
}; };
let stop_signal = stop_signal.clone(); let stop_signal = stop_signal.clone();

View File

@@ -29,6 +29,26 @@ use randomx::{RandomXCache, RandomXDataset, RandomXFlags, RandomXVM};
use crate::Result; use crate::Result;
/// Wrapper for creating a [`RandomXDataset`]
pub fn init_dataset_wrapper(
flags: RandomXFlags,
cache: RandomXCache,
start_item: u32,
item_count: u32,
/* priority: i32, */
) -> Result<RandomXDataset> {
/* set_thread_priority(priority); */
/*
if (is_x86_feature_detected!("avx2") && item_count % 5) {
let dataset = RandomXDataset::new(flags, cache, start_item, item_count - (item_count % 5))?;
let dataset = RandomXDataset::new(flags, cache, start_item + item_count - 5, 5)?;
}
*/
Ok(RandomXDataset::new(flags, cache, start_item, item_count)?)
}
/// The RandomX virtual machine instance used to verify mining. /// The RandomX virtual machine instance used to verify mining.
#[derive(Clone)] #[derive(Clone)]
pub struct RandomXVMInstance { pub struct RandomXVMInstance {
@@ -56,7 +76,7 @@ impl RandomXVMInstance {
Err(err) => { Err(err) => {
warn!( warn!(
target: "validator::randomx", target: "validator::randomx",
"[VALIDATOR] Error initializing RandomX cache with flags {:?}: {}" "[VALIDATOR] Error initializing RandomX cache with flags {:?}: {}",
flags, err, flags, err,
); );
warn!( warn!(
@@ -77,7 +97,7 @@ impl RandomXVMInstance {
/// Calculate the RandomX mining hash /// Calculate the RandomX mining hash
pub fn calculate_hash(&self, input: &[u8]) -> Result<Vec<u8>> { pub fn calculate_hash(&self, input: &[u8]) -> Result<Vec<u8>> {
let lock = self.instance.write()?; let lock = self.instance.write().unwrap();
Ok(lock.calculate_hash(input)?) Ok(lock.calculate_hash(input)?)
} }
} }
@@ -119,7 +139,7 @@ impl RandomXFactory {
) -> Result<RandomXVMInstance> { ) -> Result<RandomXVMInstance> {
let res; let res;
{ {
let mut inner = self.inner.write()?; let mut inner = self.inner.write().unwrap();
res = inner.create(key, cache, dataset)?; res = inner.create(key, cache, dataset)?;
} }
Ok(res) Ok(res)
@@ -127,13 +147,13 @@ impl RandomXFactory {
/// Get the number of VMs currently allocated /// Get the number of VMs currently allocated
pub fn get_count(&self) -> Result<usize> { pub fn get_count(&self) -> Result<usize> {
let inner = self.inner.read()?; let inner = self.inner.read().unwrap();
Ok(inner.get_count()) Ok(inner.get_count())
} }
/// Get the flags used to create the VMs /// Get the flags used to create the VMs
pub fn get_flags(&self) -> Result<RandomXFlags> { pub fn get_flags(&self) -> Result<RandomXFlags> {
let inner = self.inner.read()?; let inner = self.inner.read().unwrap();
Ok(inner.get_flags()) Ok(inner.get_flags())
} }
} }