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

validator/pow: Use little endian BigUint

x 8 месяцев назад
Родитель
Сommit
1dee0542de
4 измененных файлов с 10 добавлено и 10 удалено
  1. 1 1
      bin/minerd/src/lib.rs
  2. 4 4
      src/validator/pow.rs
  3. 1 1
      src/validator/randomx_factory.rs
  4. 4 4
      src/validator/utils.rs

+ 1 - 1
bin/minerd/src/lib.rs

@@ -193,7 +193,7 @@ fn minerd_programmatic_control() -> Result<()> {
 
     // Generate a dummy mining job
     let target = darkfi::rpc::util::JsonValue::String(
-        num_bigint::BigUint::from_bytes_be(&[0xFF; 32]).to_string(),
+        num_bigint::BigUint::from_bytes_le(&[0xFF; 32]).to_string(),
     );
     let block = darkfi::rpc::util::JsonValue::String(darkfi::util::encoding::base64::encode(
         &darkfi_serial::serialize(&darkfi::blockchain::BlockInfo::default()),

+ 4 - 4
src/validator/pow.rs

@@ -29,7 +29,7 @@ use darkfi_sdk::num_traits::{One, Zero};
 use num_bigint::BigUint;
 use randomx::{RandomXCache, RandomXDataset, RandomXFlags, RandomXVM};
 use smol::channel::Receiver;
-use tracing::debug;
+use tracing::{debug, info};
 
 use crate::{
     blockchain::{
@@ -233,13 +233,13 @@ impl PoWModule {
 
     /// Compute the next mine target.
     pub fn next_mine_target(&self) -> Result<BigUint> {
-        Ok(BigUint::from_bytes_be(&[0xFF; 32]) / &self.next_difficulty()?)
+        Ok(BigUint::from_bytes_le(&[0xFF; 32]) / &self.next_difficulty()?)
     }
 
     /// Compute the next mine target and difficulty.
     pub fn next_mine_target_and_difficulty(&self) -> Result<(BigUint, BigUint)> {
         let difficulty = self.next_difficulty()?;
-        let mine_target = BigUint::from_bytes_be(&[0xFF; 32]) / &difficulty;
+        let mine_target = BigUint::from_bytes_le(&[0xFF; 32]) / &difficulty;
         Ok((mine_target, difficulty))
     }
 
@@ -490,7 +490,7 @@ pub fn mine_block(
                 }
 
                 let out_hash = vm.calculate_hash(header.hash().inner()).unwrap();
-                let out_hash = BigUint::from_bytes_be(&out_hash);
+                let out_hash = BigUint::from_bytes_le(&out_hash);
                 if out_hash <= target {
                     found_header.store(true, Ordering::SeqCst);
                     found_nonce.store(miner_nonce, Ordering::SeqCst);

+ 1 - 1
src/validator/randomx_factory.rs

@@ -24,8 +24,8 @@ use std::{
     time::Instant,
 };
 
-use log::{debug, warn};
 use randomx::{RandomXCache, RandomXDataset, RandomXFlags, RandomXVM};
+use tracing::{debug, warn};
 
 use crate::{
     system::thread_priority::{set_thread_priority, ThreadPriority},

+ 4 - 4
src/validator/utils.rs

@@ -126,7 +126,7 @@ pub fn header_rank(header: &Header, target: &BigUint) -> Result<(BigUint, BigUin
 
     // Compute the output hash
     let out_hash = vm.calculate_hash(header.hash().inner())?;
-    let out_hash = BigUint::from_bytes_be(&out_hash);
+    let out_hash = BigUint::from_bytes_le(&out_hash);
 
     // Verify hash is less than the expected mine target
     if out_hash > *target {
@@ -134,7 +134,7 @@ pub fn header_rank(header: &Header, target: &BigUint) -> Result<(BigUint, BigUin
     }
 
     // Grab the max 32 bytes int
-    let max = BigUint::from_bytes_be(&[0xFF; 32]);
+    let max = BigUint::from_bytes_le(&[0xFF; 32]);
 
     // Compute the squared mining target distance
     let target_distance = &max - target;
@@ -159,7 +159,7 @@ pub fn block_rank(block: &BlockInfo, target: &BigUint) -> Result<(BigUint, BigUi
     }
 
     // Grab the max 32 bytes int
-    let max = BigUint::from_bytes_be(&[0xFF; 32]);
+    let max = BigUint::from_bytes_le(&[0xFF; 32]);
 
     // Compute the squared mining target distance
     let target_distance = &max - target;
@@ -172,7 +172,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_be(&out_hash);
+    let out_hash = BigUint::from_bytes_le(&out_hash);
     let hash_distance = max - out_hash;
     let hash_distance_sq = &hash_distance * &hash_distance;