From 5c3b9a080a5b860bd47b30ab80e1d1c5e0217d02 Mon Sep 17 00:00:00 2001
From: Tomer Levy <tmrlvi@gmail.com>
Date: Fri, 31 Dec 2021 04:04:56 +0200
Subject: [PATCH] Fixed bug in opencl calculation and in devfund address
 checking

---
 Cargo.lock                | 80 ++++++++++++++++++++++++++++++++++++++-
 resources/kaspa-opencl.cl | 32 ++++++++--------
 src/cli.rs                | 10 +++++
 src/miner.rs              |  1 -
 4 files changed, 105 insertions(+), 18 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index a2cdba9..8c12955 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -156,6 +156,25 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
+[[package]]
+name = "cl-sys"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8573fa3ff8acd6c49e8e113296c54277e82376b96c6ca6307848632cce38e44"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "cl3"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c604e1e9889bbe36b5d93787103e8f79a18ba0066a32709955cc121903a7859c"
+dependencies = [
+ "cl-sys",
+ "libc",
+]
+
 [[package]]
 name = "clap"
 version = "2.34.0"
@@ -177,6 +196,38 @@ version = "0.1.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
 
+[[package]]
+name = "cust"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e947a46de036afc50a482cbe88e2b09680b1c8ebea79ce714e4c168371a267c9"
+dependencies = [
+ "bitflags",
+ "cust_derive",
+ "cust_raw",
+ "find_cuda_helper",
+]
+
+[[package]]
+name = "cust_derive"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "57ca82ac24c045b317909d4722bb7e0dad7ef97bdbdba5b68aebd0e8a79904a6"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "cust_raw"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fbf40d6ade12cb9828bbc844b9875c7b93d25e67a3c9bf61c7aa3ae09e402bf8"
+dependencies = [
+ "find_cuda_helper",
+]
+
 [[package]]
 name = "digest"
 version = "0.9.0"
@@ -205,6 +256,15 @@ dependencies = [
  "termcolor",
 ]
 
+[[package]]
+name = "find_cuda_helper"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f9f9e65c593dd01ac77daad909ea4ad17f0d6d1776193fc8ea766356177abdad"
+dependencies = [
+ "glob",
+]
+
 [[package]]
 name = "fixedbitset"
 version = "0.4.0"
@@ -320,6 +380,12 @@ dependencies = [
  "wasi 0.10.2+wasi-snapshot-preview1",
 ]
 
+[[package]]
+name = "glob"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
+
 [[package]]
 name = "h2"
 version = "0.3.9"
@@ -481,16 +547,18 @@ checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
 
 [[package]]
 name = "kaspa-miner"
-version = "0.2.1"
+version = "0.2.1-GPU-0.1"
 dependencies = [
  "blake2b_simd",
  "cc",
+ "cust",
  "env_logger",
  "futures-util",
  "keccak",
  "log",
  "num_cpus",
  "once_cell",
+ "opencl3",
  "parking_lot",
  "prost",
  "rand 0.8.4",
@@ -604,6 +672,16 @@ version = "0.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
 
+[[package]]
+name = "opencl3"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "931cc2ab3068142384dbdaba142681c11b315cf3b96c7a59e8480d062363387f"
+dependencies = [
+ "cl3",
+ "libc",
+]
+
 [[package]]
 name = "parking_lot"
 version = "0.11.2"
diff --git a/resources/kaspa-opencl.cl b/resources/kaspa-opencl.cl
index 8ce6dca..dcdbd1b 100644
--- a/resources/kaspa-opencl.cl
+++ b/resources/kaspa-opencl.cl
@@ -224,22 +224,22 @@ kernel void heavy_hash(
     uchar16 hash_part[4];
     for (int i=0; i<4; i++) {
          hash_part[i] = (uchar16)(
-            (hash_[2*i] & 0xF0) >> 4,
-            (hash_[2*i] & 0x0F),
-            (hash_[2*i+1] & 0xF0) >> 4,
-            (hash_[2*i+1] & 0x0F),
-            (hash_[2*i+2] & 0xF0) >> 4,
-            (hash_[2*i+2] & 0x0F),
-            (hash_[2*i+3] & 0xF0) >> 4,
-            (hash_[2*i+3] & 0x0F),
-            (hash_[2*i+4] & 0xF0) >> 4,
-            (hash_[2*i+4] & 0x0F),
-            (hash_[2*i+5] & 0xF0) >> 4,
-            (hash_[2*i+5] & 0x0F),
-            (hash_[2*i+6] & 0xF0) >> 4,
-            (hash_[2*i+6] & 0x0F),
-            (hash_[2*i+7] & 0xF0) >> 4,
-            (hash_[2*i+7] & 0x0F)
+            (hash_[8*i] & 0xF0) >> 4,
+            (hash_[8*i] & 0x0F),
+            (hash_[8*i+1] & 0xF0) >> 4,
+            (hash_[8*i+1] & 0x0F),
+            (hash_[8*i+2] & 0xF0) >> 4,
+            (hash_[8*i+2] & 0x0F),
+            (hash_[8*i+3] & 0xF0) >> 4,
+            (hash_[8*i+3] & 0x0F),
+            (hash_[8*i+4] & 0xF0) >> 4,
+            (hash_[8*i+4] & 0x0F),
+            (hash_[8*i+5] & 0xF0) >> 4,
+            (hash_[8*i+5] & 0x0F),
+            (hash_[8*i+6] & 0xF0) >> 4,
+            (hash_[8*i+6] & 0x0F),
+            (hash_[8*i+7] & 0xF0) >> 4,
+            (hash_[8*i+7] & 0x0F)
         );
     }
 
diff --git a/src/cli.rs b/src/cli.rs
index cabe27f..168c309 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -107,6 +107,16 @@ impl Opt {
         }
         log::info!("kaspad address: {}", self.kaspad_address);
 
+        let miner_network = self.mining_address.split(":").next();
+        let devfund_network = self.devfund_address.split(":").next();
+        if  miner_network.is_some() && devfund_network.is_some() && miner_network != devfund_network {
+            self.devfund_percent = 0;
+            log::info!(
+                "Mining address ({}) and devfund ({}) are not from the same network. Disabling devfund.",
+                miner_network.unwrap(), devfund_network.unwrap()
+            )
+        }
+
         if self.no_gpu {
             self.cuda_device = None;
             self.opencl_device = None;
diff --git a/src/miner.rs b/src/miner.rs
index 1d2f057..f531373 100644
--- a/src/miner.rs
+++ b/src/miner.rs
@@ -170,7 +170,6 @@ impl MinerManager {
                         } else {
                             let hash = state_ref.calculate_pow(nonces[0]);
                             warn!("Something is wrong in GPU code! Got nonce {}, with hash real {}  (target: {})", nonces[0], hash.0[3], state_ref.target.0[3]);
-                            break;
                         }
                     }