Ver código fonte

validator/pow: use HeaderHash for randomx_key references

skoupidi 8 meses atrás
pai
commit
245d142f16
4 arquivos alterados com 31 adições e 29 exclusões
  1. 6 2
      bin/darkfid/src/task/miner.rs
  2. 3 4
      bin/minerd/src/rpc.rs
  3. 9 11
      src/blockchain/mod.rs
  4. 13 12
      src/validator/pow.rs

+ 6 - 2
bin/darkfid/src/task/miner.rs

@@ -280,9 +280,13 @@ async fn mine_next_block(
     let randomx_key = if next_block.header.height > RANDOMX_KEY_CHANGING_HEIGHT &&
         next_block.header.height % RANDOMX_KEY_CHANGING_HEIGHT == RANDOMX_KEY_CHANGE_DELAY
     {
-        JsonValue::String(base64::encode(&extended_fork.module.darkfi_rx_keys.1))
+        JsonValue::String(base64::encode(
+            &serialize_async(&extended_fork.module.darkfi_rx_keys.1).await,
+        ))
     } else {
-        JsonValue::String(base64::encode(&extended_fork.module.darkfi_rx_keys.0))
+        JsonValue::String(base64::encode(
+            &serialize_async(&extended_fork.module.darkfi_rx_keys.0).await,
+        ))
     };
     let header = JsonValue::String(base64::encode(&serialize_async(&next_block.header).await));
     let response = node

+ 3 - 4
bin/minerd/src/rpc.rs

@@ -103,7 +103,7 @@ impl MinerNode {
             error!(target: "minerd::rpc", "Failed to parse RandomX key bytes");
             return server_error(RpcError::BlockParseError, id, None)
         };
-        let Ok(randomx_key) = deserialize_async::<[u8; 32]>(&randomx_key_bytes).await else {
+        let Ok(randomx_key) = deserialize_async::<HeaderHash>(&randomx_key_bytes).await else {
             error!(target: "minerd::rpc", "Failed to parse RandomX key");
             return server_error(RpcError::BlockParseError, id, None)
         };
@@ -116,8 +116,7 @@ impl MinerNode {
             return server_error(RpcError::BlockParseError, id, None)
         };
         let header_hash = header.hash();
-        let randomx_key_hash = HeaderHash::new(randomx_key);
-        info!(target: "minerd::rpc", "Received request to mine block header {header_hash} with key {randomx_key_hash} for target: {target}");
+        info!(target: "minerd::rpc", "Received request to mine block header {header_hash} with key {randomx_key} for target: {target}");
 
         // If we have a requested mining height, we'll keep dropping here.
         if self.stop_at_height > 0 && header.height >= self.stop_at_height {
@@ -131,7 +130,7 @@ impl MinerNode {
         };
 
         // Mine provided block header
-        info!(target: "minerd::rpc", "Mining block header {header_hash} with key {randomx_key_hash} for target: {target}");
+        info!(target: "minerd::rpc", "Mining block header {header_hash} with key {randomx_key} for target: {target}");
         if let Err(e) =
             mine_block(&target, &randomx_key, &mut header, self.threads, &self.stop_signal.clone())
         {

+ 9 - 11
src/blockchain/mod.rs

@@ -443,7 +443,7 @@ impl Blockchain {
         key_change_height: &u32,
         key_change_delay: &u32,
         height: Option<u32>,
-    ) -> Result<([u8; 32], [u8; 32])> {
+    ) -> Result<(HeaderHash, HeaderHash)> {
         // Grab last known block header
         let last = match height {
             Some(h) => &self.get_headers_by_heights(&[if h != 0 { h - 1 } else { 0 }])?[0],
@@ -453,11 +453,10 @@ impl Blockchain {
         // Check if we passed the first key change height
         if &last.height <= key_change_height {
             // Genesis is our current
-            let current = *self.genesis()?.1.inner();
+            let current = self.genesis()?.1;
 
             // Check if last known block header is the next key
-            let next =
-                if &last.height == key_change_height { *last.hash().inner() } else { current };
+            let next = if &last.height == key_change_height { last.hash() } else { current };
 
             return Ok((current, next))
         }
@@ -471,8 +470,8 @@ impl Blockchain {
         // last known block header is the next key.
         if distance == 0 {
             return Ok((
-                *self.get_headers_by_heights(&[last.height - key_change_height])?[0].hash().inner(),
-                *last.hash().inner(),
+                self.get_headers_by_heights(&[last.height - key_change_height])?[0].hash(),
+                last.hash(),
             ))
         }
 
@@ -482,17 +481,16 @@ impl Blockchain {
         // height is the next key.
         if &distance < key_change_delay {
             return Ok((
-                *self.get_headers_by_heights(&[last.height - (distance + key_change_height)])?[0]
-                    .hash()
-                    .inner(),
-                *self.get_headers_by_heights(&[last.height - distance])?[0].hash().inner(),
+                self.get_headers_by_heights(&[last.height - (distance + key_change_height)])?[0]
+                    .hash(),
+                self.get_headers_by_heights(&[last.height - distance])?[0].hash(),
             ))
         }
 
         // When distance is greater or equal to key change delay,
         // current key is the block header located at last_height - distance
         // height and we don't know the next key.
-        let current = *self.get_headers_by_heights(&[last.height - distance])?[0].hash().inner();
+        let current = self.get_headers_by_heights(&[last.height - distance])?[0].hash();
         Ok((current, current))
     }
 }

+ 13 - 12
src/validator/pow.rs

@@ -35,7 +35,7 @@ use crate::{
     blockchain::{
         block_store::BlockDifficulty,
         header_store::{
-            Header,
+            Header, HeaderHash,
             PowData::{DarkFi, Monero},
         },
         Blockchain, BlockchainOverlayPtr,
@@ -99,7 +99,7 @@ pub struct PoWModule {
     /// difficulties buffer last.
     pub cumulative_difficulty: BigUint,
     /// Native PoW RandomX VMs current and next keys pair
-    pub darkfi_rx_keys: ([u8; 32], [u8; 32]),
+    pub darkfi_rx_keys: (HeaderHash, HeaderHash),
     /// RandomXFactory for native PoW (Arc from parent)
     pub darkfi_rx_factory: RandomXFactory,
     /// RandomXFactory for Monero PoW (Arc from parent)
@@ -311,17 +311,18 @@ impl PoWModule {
                 let randomx_key = if header.height > RANDOMX_KEY_CHANGING_HEIGHT &&
                     header.height % RANDOMX_KEY_CHANGING_HEIGHT == RANDOMX_KEY_CHANGE_DELAY
                 {
-                    &self.darkfi_rx_keys.1[..]
+                    &self.darkfi_rx_keys.1
                 } else {
-                    &self.darkfi_rx_keys.0[..]
+                    &self.darkfi_rx_keys.0
                 };
 
                 debug!(
                     target: "validator::pow::verify_block",
                     "[VERIFIER] Creating DarkFi PoW RandomXCache",
                 );
-                let cache = RandomXCache::new(flags, randomx_key)?;
-                let vm = self.darkfi_rx_factory.create(randomx_key, Some(cache), None)?;
+                let cache = RandomXCache::new(flags, &randomx_key.inner()[..])?;
+                let vm =
+                    self.darkfi_rx_factory.create(&randomx_key.inner()[..], Some(cache), None)?;
 
                 debug!(
                     target: "validator::pow::verify_block",
@@ -377,10 +378,10 @@ impl PoWModule {
 
         // Check if need to set the new key
         if header.height % RANDOMX_KEY_CHANGING_HEIGHT == 0 {
-            let next_key = *header.hash().inner();
+            let next_key = header.hash();
             let flags = RandomXFlags::get_recommended_flags();
-            let cache = RandomXCache::new(flags, &next_key[..])?;
-            let _ = self.darkfi_rx_factory.create(&next_key[..], Some(cache), None)?;
+            let cache = RandomXCache::new(flags, &next_key.inner()[..])?;
+            let _ = self.darkfi_rx_factory.create(&next_key.inner()[..], Some(cache), None)?;
             self.darkfi_rx_keys.1 = next_key;
             return Ok(())
         }
@@ -443,7 +444,7 @@ impl std::fmt::Display for PoWModule {
 /// Mine provided block, based on provided PoW module next mine target.
 pub fn mine_block(
     target: &BigUint,
-    input: &[u8; 32],
+    input: &HeaderHash,
     miner_header: &mut Header,
     threads: usize,
     stop_signal: &Receiver<()>,
@@ -451,7 +452,7 @@ pub fn mine_block(
     let miner_setup = Instant::now();
 
     debug!(target: "validator::pow::mine_block", "[MINER] Mine target: 0x{target:064x}");
-    debug!(target: "validator::pow::mine_block", "[MINER] PoW input: {}", blake3::hash(input));
+    debug!(target: "validator::pow::mine_block", "[MINER] PoW input: {input}");
 
     let mut flags = RandomXFlags::get_recommended_flags() | RandomXFlags::FULLMEM;
     #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
@@ -462,7 +463,7 @@ pub fn mine_block(
     }
 
     debug!(target: "validator::pow::mine_block", "[MINER] Initializing RandomX cache...");
-    let cache = RandomXCache::new(flags, &input[..])?;
+    let cache = RandomXCache::new(flags, &input.inner()[..])?;
 
     debug!(target: "validator::pow::mine_block", "[MINER] Setup time: {:?}", miner_setup.elapsed());