From caca4120315434ead45f49c122a61ec8b2438adf Mon Sep 17 00:00:00 2001 From: skoupidi Date: Wed, 11 Feb 2026 15:20:38 +0200 Subject: [PATCH] validator: use a single MAX_32_BYTES constant --- src/validator/pow.rs | 10 +++++----- src/validator/utils.rs | 9 +++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/validator/pow.rs b/src/validator/pow.rs index 7951df790..fb4610248 100644 --- a/src/validator/pow.rs +++ b/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 = LazyLock::new(|| BigUint::from_bytes_le(&[0xFF; 32])); - /// This struct represents the information required by the PoW algorithm #[derive(Clone)] pub struct PoWModule { diff --git a/src/validator/utils.rs b/src/validator/utils.rs index 597ffbe5a..83b8b3c0e 100644 --- a/src/validator/utils.rs +++ b/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 = LazyLock::new(|| BigUint::from_bytes_le(&[0xFF; 32])); +pub static MAX_32_BYTES: LazyLock = 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))