Эх сурвалжийг харах

validator: use a single MAX_32_BYTES constant

skoupidi 5 сар өмнө
parent
commit
caca412031

+ 5 - 5
src/validator/pow.rs

@@ -19,7 +19,7 @@
 use std::{
     sync::{
         atomic::{AtomicBool, AtomicU32, Ordering},
-        Arc, LazyLock,
+        Arc,
     },
     thread,
     time::Instant,
@@ -41,7 +41,10 @@ use crate::{
         Blockchain, BlockchainOverlayPtr,
     },
     util::{ringbuffer::RingBuffer, time::Timestamp},
-    validator::{utils::median, RandomXFactory},
+    validator::{
+        utils::{median, MAX_32_BYTES},
+        RandomXFactory,
+    },
     Error, Result,
 };
 
@@ -79,9 +82,6 @@ pub const RANDOMX_KEY_CHANGING_HEIGHT: u32 = 2048;
 /// RandomX VM key change delay
 pub const RANDOMX_KEY_CHANGE_DELAY: u32 = 64;
 
-/// Max 32 bytes integer, cached to avoid repeated allocation
-static MAX_32_BYTES: LazyLock<BigUint> = LazyLock::new(|| BigUint::from_bytes_le(&[0xFF; 32]));
-
 /// This struct represents the information required by the PoW algorithm
 #[derive(Clone)]
 pub struct PoWModule {

+ 3 - 6
src/validator/utils.rs

@@ -38,7 +38,7 @@ use crate::{
 
 /// Max 32 bytes integer, used in rank calculations.
 /// Cached to avoid repeated allocation.
-static MAX_32_BYTES: LazyLock<BigUint> = LazyLock::new(|| BigUint::from_bytes_le(&[0xFF; 32]));
+pub static MAX_32_BYTES: LazyLock<BigUint> = LazyLock::new(|| BigUint::from_bytes_le(&[0xFF; 32]));
 
 /// Deploy DarkFi native wasm contracts to provided blockchain overlay.
 ///
@@ -159,11 +159,8 @@ pub fn block_rank(block: &BlockInfo, target: &BigUint) -> Result<(BigUint, BigUi
         return Ok((0u64.into(), 0u64.into()))
     }
 
-    // Grab the max 32 bytes int
-    let max = &*MAX_32_BYTES;
-
     // Compute the squared mining target distance
-    let target_distance = max - target;
+    let target_distance = &*MAX_32_BYTES - target;
     let target_distance_sq = &target_distance * &target_distance;
 
     // Setup RandomX verifier
@@ -174,7 +171,7 @@ pub fn block_rank(block: &BlockInfo, target: &BigUint) -> Result<(BigUint, BigUi
     // Compute the output hash distance
     let out_hash = vm.calculate_hash(block.hash().inner())?;
     let out_hash = BigUint::from_bytes_le(&out_hash);
-    let hash_distance = max - out_hash;
+    let hash_distance = &*MAX_32_BYTES - out_hash;
     let hash_distance_sq = &hash_distance * &hash_distance;
 
     Ok((target_distance_sq, hash_distance_sq))