Răsfoiți Sursa

validator: Add wrapper for initializing RandomX datasets

parazyd 1 an în urmă
părinte
comite
3a42ba5fda
4 a modificat fișierele cu 33 adăugiri și 8 ștergeri
  1. 2 0
      Cargo.toml
  2. 3 0
      src/validator/mod.rs
  3. 3 3
      src/validator/pow.rs
  4. 25 5
      src/validator/randomx_factory.rs

+ 2 - 0
Cargo.toml

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

+ 3 - 0
src/validator/mod.rs

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

+ 3 - 3
src/validator/pow.rs

@@ -37,7 +37,7 @@ use crate::{
         Blockchain, BlockchainOverlayPtr,
     },
     util::{ringbuffer::RingBuffer, time::Timestamp},
-    validator::utils::median,
+    validator::{randomx_factory::init_dataset_wrapper, utils::median},
     Error, Result,
 };
 
@@ -380,9 +380,9 @@ pub fn mine_block(
         let dataset = if threads > 1 {
             let a = (dataset_item_count * (t as u32)) / (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 {
-            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();

+ 25 - 5
src/validator/randomx.rs → src/validator/randomx_factory.rs

@@ -29,6 +29,26 @@ use randomx::{RandomXCache, RandomXDataset, RandomXFlags, RandomXVM};
 
 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.
 #[derive(Clone)]
 pub struct RandomXVMInstance {
@@ -56,7 +76,7 @@ impl RandomXVMInstance {
                 Err(err) => {
                     warn!(
                         target: "validator::randomx",
-                        "[VALIDATOR] Error initializing RandomX cache with flags {:?}: {}"
+                        "[VALIDATOR] Error initializing RandomX cache with flags {:?}: {}",
                         flags, err,
                     );
                     warn!(
@@ -77,7 +97,7 @@ impl RandomXVMInstance {
 
     /// Calculate the RandomX mining hash
     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)?)
     }
 }
@@ -119,7 +139,7 @@ impl RandomXFactory {
     ) -> Result<RandomXVMInstance> {
         let res;
         {
-            let mut inner = self.inner.write()?;
+            let mut inner = self.inner.write().unwrap();
             res = inner.create(key, cache, dataset)?;
         }
         Ok(res)
@@ -127,13 +147,13 @@ impl RandomXFactory {
 
     /// Get the number of VMs currently allocated
     pub fn get_count(&self) -> Result<usize> {
-        let inner = self.inner.read()?;
+        let inner = self.inner.read().unwrap();
         Ok(inner.get_count())
     }
 
     /// Get the flags used to create the VMs
     pub fn get_flags(&self) -> Result<RandomXFlags> {
-        let inner = self.inner.read()?;
+        let inner = self.inner.read().unwrap();
         Ok(inner.get_flags())
     }
 }