From faa77bfd39091df8315982350d1453b6a1fed308 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Mon, 24 Jul 2023 21:33:15 +0200 Subject: [PATCH 1/4] Add best diff to miner screen --- src/mining.cpp | 12 +++++++++--- src/monitor.cpp | 11 +++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/mining.cpp b/src/mining.cpp index dc4e5db..d96709a 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -18,10 +18,12 @@ unsigned long Mhashes = 0; unsigned long totalKHashes = 0; unsigned long elapsedKHs = 0; -unsigned long halfshares; // increase if blockhash has 16 bits of zeroes unsigned int shares; // increase if blockhash has 32 bits of zeroes unsigned int valids; // increased if blockhash <= target +// Track best diff +double best_diff = 0.0; + // Variables to hold data from custom textboxes extern char poolString[80]; extern int portNumber; @@ -301,14 +303,18 @@ void runMiner(void * task_id) { nonce += 2; continue; } - halfshares++; - + //Check target to submit //Difficulty of 1 > 0x00000000FFFF0000000000000000000000000000000000000000000000000000 //NM2 pool diff 1e-9 > Target = diff_1 / diff_pool > 0x00003B9ACA00....00 //Swapping diff bytes little endian >>>>>>>>>>>>>>>> 0x0000DC59D300....00 //if((hash[29] <= 0xDC) && (hash[28] <= 0x59)) //0x00003B9ACA00 > diff value for 1e-9 double diff_hash = diff_from_target(hash); + + // update best diff + if (diff_hash > best_diff) + best_diff = diff_hash; + if(diff_hash > mMiner.poolDifficulty)//(hash[29] <= 0x3B)//(diff_hash > 1e-9) { tx_mining_submit(client, mWorker, mJob, nonce); diff --git a/src/monitor.cpp b/src/monitor.cpp index cdc1da6..6c16b04 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -19,9 +19,10 @@ extern unsigned long Mhashes; extern unsigned long totalKHashes; extern unsigned long elapsedKHs; -extern unsigned long halfshares; // increase if blockhash has 16 bits of zeroes extern unsigned int shares; // increase if blockhash has 32 bits of zeroes -extern unsigned int valids; // increased if blockhash <= targethalfshares +extern unsigned int valids; // increased if blockhash <= target + +extern double best_diff; // track best diff extern OpenFontRender render; extern TFT_eSprite background; @@ -243,9 +244,11 @@ void show_MinerScreen(unsigned long mElapsed){ //Block templates render.setFontSize(36); render.drawString(String(templates).c_str(), 186, 20, 0xDEDB); - //16Bit shares + //Best diff + char best_diff_string[8] = {0}; + sprintf(best_diff_string, "%.4f", best_diff); render.setFontSize(36); - render.drawString(String(halfshares).c_str(), 186, 48, 0xDEDB); + render.drawString(String(best_diff_string).c_str(), 186, 48, 0xDEDB); //32Bit shares render.setFontSize(36); render.drawString(String(shares).c_str(), 186, 76, 0xDEDB); From 2231ae949fc930e88022bc183d4502b2d5d1328d Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Thu, 27 Jul 2023 16:30:57 +0200 Subject: [PATCH 2/4] Log successful submit as such --- src/mining.cpp | 1 + src/stratum.cpp | 9 +++++++-- src/stratum.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/mining.cpp b/src/mining.cpp index d96709a..3aabdb8 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -199,6 +199,7 @@ void runStratumWorker(void *name) { case MINING_SET_DIFFICULTY: parse_mining_set_difficulty(line, currentPoolDifficulty); mMiner.poolDifficulty = currentPoolDifficulty; break; + case STRATUM_SUCCESS: Serial.println(" Parsed JSON: Success"); break; default: Serial.println(" Parsed JSON: unknown"); break; } diff --git a/src/stratum.cpp b/src/stratum.cpp index 4970656..00d72a4 100644 --- a/src/stratum.cpp +++ b/src/stratum.cpp @@ -142,8 +142,13 @@ stratum_method parse_mining_method(String line) if (error || checkError(doc)) return STRATUM_PARSE_ERROR; - if (!doc.containsKey("method")) return STRATUM_UNKNOWN; - + if (!doc.containsKey("method")) { + // "error":null means success + if (doc["error"].isNull()) + return STRATUM_SUCCESS; + else + return STRATUM_UNKNOWN; + } stratum_method result = STRATUM_UNKNOWN; if (strcmp("mining.notify", (const char*) doc["method"]) == 0) { diff --git a/src/stratum.h b/src/stratum.h index 44f65de..d8110e7 100644 --- a/src/stratum.h +++ b/src/stratum.h @@ -38,6 +38,7 @@ typedef struct { } mining_job; typedef enum { + STRATUM_SUCCESS, STRATUM_UNKNOWN, STRATUM_PARSE_ERROR, MINING_NOTIFY, From 5d22a15ac2d10c4b0e5a8f2052cd75f82e558ea8 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Thu, 27 Jul 2023 23:24:33 +0200 Subject: [PATCH 3/4] Stop miners instantly when losing pool connection --- src/mining.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mining.cpp b/src/mining.cpp index 3aabdb8..3efd499 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -143,6 +143,8 @@ void runStratumWorker(void *name) { if(!isMinerSuscribed){ + //Stop miner current jobs + mMiner.inRun = false; mWorker = init_mining_subscribe(); // STEP 1: Pool server connection (SUBSCRIBE) From 0a142ab52aa931f1da4e2f38728a7a56be9859c4 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Tue, 1 Aug 2023 22:39:37 +0200 Subject: [PATCH 4/4] Fix checkValid function Diff target needs to be converted to little endian for comparison. --- src/mining.cpp | 8 ++------ src/utils.cpp | 11 +++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/mining.cpp b/src/mining.cpp index 3efd499..3126293 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -346,16 +346,12 @@ void runMiner(void * task_id) { } shares++; - // check if valid header + // check if valid header if(checkValid(hash, mMiner.bytearray_target)){ Serial.printf("[WORKER] %d CONGRATULATIONS! Valid block found with nonce: %d | 0x%x\n", miner_id, nonce, nonce); valids++; Serial.printf("[WORKER] %d Submitted work valid!\n", miner_id); - // STEP 3: Submit mining job - tx_mining_submit(client, mWorker, mJob, nonce); - client.stop(); - // exit - nonce = MAX_NONCE; + // wait for new job break; } // increment nonce diff --git a/src/utils.cpp b/src/utils.cpp index 8216ef7..642e3f9 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -113,15 +113,18 @@ double diff_from_target(void *target) bool checkValid(unsigned char* hash, unsigned char* target) { bool valid = true; + unsigned char diff_target[32]; + memcpy(diff_target, &target, 32); + //convert target to little endian for comparison + reverse_bytes(diff_target, 32); + for(uint8_t i=31; i>=0; i--) { - if(hash[i] > target[i]) { + if(hash[i] > diff_target[i]) { valid = false; break; - } else if (hash[i] < target[i]) { - valid = true; - break; } } + #ifdef DEBUG_MINING if (valid) { Serial.print("\tvalid : ");