Просмотр исходного кода

validator/consensus: Propagate RandomXFactory to PoWModule

parazyd 1 год назад
Родитель
Сommit
cd46c389a8

+ 2 - 0
bin/darkfid/src/task/unknown_proposal.rs

@@ -333,6 +333,8 @@ async fn handle_reorg(
         validator.consensus.module.read().await.target,
         validator.consensus.module.read().await.fixed_difficulty.clone(),
         Some(last_common_height + 1),
+        validator.consensus.darkfi_rx_factory.clone(),
+        validator.consensus.monero_rx_factory.clone(),
     ) {
         Ok(m) => m,
         Err(e) => {

+ 11 - 2
bin/darkfid/src/tests/forks.rs

@@ -18,7 +18,7 @@
 
 use darkfi::{
     blockchain::{BlockInfo, Blockchain, HeaderHash},
-    validator::{consensus::Fork, pow::PoWModule},
+    validator::{consensus::Fork, pow::PoWModule, RandomXFactory},
     Result,
 };
 use sled_overlay::sled;
@@ -39,7 +39,16 @@ fn forks() -> Result<()> {
         let genesis_block_hash = genesis_block.hash();
 
         // Generate the PoW module
-        let module = PoWModule::new(blockchain.clone(), 90, None, None)?;
+        let darkfi_rx_factory = RandomXFactory::default();
+        let monero_rx_factory = RandomXFactory::default();
+        let module = PoWModule::new(
+            blockchain.clone(),
+            90,
+            None,
+            None,
+            darkfi_rx_factory,
+            monero_rx_factory,
+        )?;
 
         // Create a fork
         let fork = Fork::new(blockchain.clone(), module).await?;

+ 25 - 1
src/validator/consensus.rs

@@ -36,6 +36,7 @@ use crate::{
         pow::PoWModule,
         utils::{best_fork_index, block_rank, find_extended_fork_index},
         verification::{verify_proposal, verify_transaction},
+        RandomXFactory,
     },
     zk::VerifyingKey,
     Error, Result,
@@ -50,6 +51,10 @@ pub struct Consensus {
     pub blockchain: Blockchain,
     /// Fork size(length) after which it can be confirmed
     pub confirmation_threshold: usize,
+    /// RandomXFactory for native PoW
+    pub darkfi_rx_factory: RandomXFactory,
+    /// RandomXFactory for Monero PoW
+    pub monero_rx_factory: RandomXFactory,
     /// Fork chains containing block proposals
     pub forks: RwLock<Vec<Fork>>,
     /// Canonical blockchain PoW module state
@@ -67,14 +72,30 @@ impl Consensus {
         pow_fixed_difficulty: Option<BigUint>,
     ) -> Result<Self> {
         let forks = RwLock::new(vec![]);
+
+        let darkfi_rx_factory = RandomXFactory::default();
+        let monero_rx_factory = RandomXFactory::default();
+
         let module = RwLock::new(PoWModule::new(
             blockchain.clone(),
             pow_target,
             pow_fixed_difficulty,
             None,
+            darkfi_rx_factory.clone(),
+            monero_rx_factory.clone(),
         )?);
+
         let append_lock = RwLock::new(());
-        Ok(Self { blockchain, confirmation_threshold, forks, module, append_lock })
+
+        Ok(Self {
+            blockchain,
+            confirmation_threshold,
+            darkfi_rx_factory,
+            monero_rx_factory,
+            forks,
+            module,
+            append_lock,
+        })
     }
 
     /// Generate a new empty fork.
@@ -624,12 +645,15 @@ impl Consensus {
     /// Auxiliary function to reset PoW module.
     pub async fn reset_pow_module(&self) -> Result<()> {
         debug!(target: "validator::consensus::reset_pow_module", "Resetting PoW module...");
+
         let mut module = self.module.write().await;
         *module = PoWModule::new(
             self.blockchain.clone(),
             module.target,
             module.fixed_difficulty.clone(),
             None,
+            self.darkfi_rx_factory.clone(),
+            self.monero_rx_factory.clone(),
         )?;
         drop(module);
         debug!(target: "validator::consensus::reset_pow_module", "PoW module reset successfully!");

+ 17 - 10
src/validator/mod.rs

@@ -45,7 +45,7 @@ use pow::PoWModule;
 
 /// RandomX infrastructure
 pub mod randomx_factory;
-use randomx_factory::RandomXFactory;
+pub use randomx_factory::RandomXFactory;
 
 /// Monero infrastructure
 pub mod xmr;
@@ -89,10 +89,6 @@ pub struct Validator {
     pub blockchain: Blockchain,
     /// Hot/Live data used by the consensus algorithm
     pub consensus: Consensus,
-    /// RandomXFactory for native PoW
-    pub darkfi_rx_factory: RandomXFactory,
-    /// RandomXFactory for Monero PoW
-    pub monero_rx_factory: RandomXFactory,
     /// Flag signalling node has finished initial sync
     pub synced: RwLock<bool>,
     /// Flag to enable tx fee verification
@@ -133,8 +129,6 @@ impl Validator {
         let state = Arc::new(Self {
             blockchain,
             consensus,
-            darkfi_rx_factory: RandomXFactory::default(),
-            monero_rx_factory: RandomXFactory::default(),
             synced: RwLock::new(false),
             verify_fees: config.verify_fees,
         });
@@ -791,7 +785,14 @@ impl Validator {
         overlay.lock().unwrap().overlay.lock().unwrap().apply()?;
 
         // Create a PoW module to validate each block
-        let mut module = PoWModule::new(blockchain, pow_target, pow_fixed_difficulty, Some(0))?;
+        let mut module = PoWModule::new(
+            blockchain,
+            pow_target,
+            pow_fixed_difficulty,
+            Some(0),
+            self.consensus.darkfi_rx_factory.clone(),
+            self.consensus.monero_rx_factory.clone(),
+        )?;
 
         // Grab current contracts states monotree to validate each block
         let mut state_monotree = overlay.lock().unwrap().get_state_monotree()?;
@@ -894,8 +895,14 @@ impl Validator {
 
         // Create a PoW module and an in memory overlay to compute each
         // block difficulty.
-        let mut module =
-            PoWModule::new(self.blockchain.clone(), pow_target, pow_fixed_difficulty, Some(0))?;
+        let mut module = PoWModule::new(
+            self.blockchain.clone(),
+            pow_target,
+            pow_fixed_difficulty,
+            Some(0),
+            self.consensus.darkfi_rx_factory.clone(),
+            self.consensus.monero_rx_factory.clone(),
+        )?;
 
         // Grab genesis block difficulty to access current ranks
         let genesis_block = self.blockchain.genesis_block()?;

+ 33 - 4
src/validator/pow.rs

@@ -38,7 +38,7 @@ use crate::{
     },
     system::thread_priority::ThreadPriority,
     util::{ringbuffer::RingBuffer, time::Timestamp},
-    validator::{randomx_factory::init_dataset_wrapper, utils::median},
+    validator::{randomx_factory::init_dataset_wrapper, utils::median, RandomXFactory},
     Error, Result,
 };
 
@@ -90,6 +90,10 @@ pub struct PoWModule {
     /// access(optimization), since its always same as
     /// difficulties buffer last.
     pub cumulative_difficulty: BigUint,
+    /// RandomXFactory for native PoW (Arc from parent)
+    pub darkfi_rx_factory: RandomXFactory,
+    /// RandomXFactory for Monero PoW (Arc from parent)
+    pub monero_rx_factory: RandomXFactory,
 }
 
 impl PoWModule {
@@ -100,6 +104,8 @@ impl PoWModule {
         target: u32,
         fixed_difficulty: Option<BigUint>,
         height: Option<u32>,
+        darkfi_rx_factory: RandomXFactory,
+        monero_rx_factory: RandomXFactory,
     ) -> Result<Self> {
         // Retrieve genesis block timestamp
         let genesis = blockchain.genesis_block()?.header.timestamp;
@@ -130,6 +136,8 @@ impl PoWModule {
             timestamps,
             difficulties,
             cumulative_difficulty,
+            darkfi_rx_factory,
+            monero_rx_factory,
         })
     }
 
@@ -461,7 +469,7 @@ mod tests {
         Result,
     };
 
-    use super::PoWModule;
+    use super::{super::RandomXFactory, PoWModule};
 
     const DEFAULT_TEST_THREADS: usize = 2;
     const DEFAULT_TEST_DIFFICULTY_TARGET: u32 = 120;
@@ -472,7 +480,17 @@ mod tests {
         let blockchain = Blockchain::new(&sled_db)?;
         let genesis_block = BlockInfo::default();
         blockchain.add_block(&genesis_block)?;
-        let mut module = PoWModule::new(blockchain, DEFAULT_TEST_DIFFICULTY_TARGET, None, None)?;
+
+        let darkfi_rx_factory = RandomXFactory::default();
+        let monero_rx_factory = RandomXFactory::default();
+        let mut module = PoWModule::new(
+            blockchain,
+            DEFAULT_TEST_DIFFICULTY_TARGET,
+            None,
+            None,
+            darkfi_rx_factory,
+            monero_rx_factory,
+        )?;
 
         let output = Command::new("./script/research/pow/gen_wide_data.py").output().unwrap();
         let reader = Cursor::new(output.stdout);
@@ -508,7 +526,18 @@ mod tests {
         let mut genesis_block = BlockInfo::default();
         genesis_block.header.timestamp = 0.into();
         blockchain.add_block(&genesis_block)?;
-        let module = PoWModule::new(blockchain, DEFAULT_TEST_DIFFICULTY_TARGET, None, None)?;
+
+        let darkfi_rx_factory = RandomXFactory::default();
+        let monero_rx_factory = RandomXFactory::default();
+        let module = PoWModule::new(
+            blockchain,
+            DEFAULT_TEST_DIFFICULTY_TARGET,
+            None,
+            None,
+            darkfi_rx_factory,
+            monero_rx_factory,
+        )?;
+
         let (_, recvr) = smol::channel::bounded(1);
 
         // Mine next block

+ 12 - 1
src/validator/verification.rs

@@ -46,6 +46,7 @@ use crate::{
         consensus::{Consensus, Fork, Proposal, BLOCK_GAS_LIMIT},
         fees::{circuit_gas_use, compute_fee, GasData, PALLAS_SCHNORR_SIGNATURE_FEE},
         pow::PoWModule,
+        RandomXFactory,
     },
     zk::VerifyingKey,
     Error, Result,
@@ -185,7 +186,17 @@ pub fn validate_blockchain(
     pow_fixed_difficulty: Option<BigUint>,
 ) -> Result<()> {
     // Generate a PoW module
-    let mut module = PoWModule::new(blockchain.clone(), pow_target, pow_fixed_difficulty, Some(0))?;
+    let darkfi_rx_factory = RandomXFactory::default();
+    let monero_rx_factory = RandomXFactory::default();
+    let mut module = PoWModule::new(
+        blockchain.clone(),
+        pow_target,
+        pow_fixed_difficulty,
+        Some(0),
+        darkfi_rx_factory,
+        monero_rx_factory,
+    )?;
+
     // We use block order store here so we have all blocks in order
     let blocks = blockchain.blocks.get_all_order()?;
     for (index, block) in blocks[1..].iter().enumerate() {