Rework two-threaded miner task code

Distribute odd and even nonces between miner tasks based on task_id
This commit is contained in:
Stefan Berger 2023-07-11 21:58:53 +02:00
parent 146d0497d6
commit 57c60ed0f1
2 changed files with 14 additions and 16 deletions

View File

@ -141,8 +141,8 @@ void setup()
// Start mining tasks // Start mining tasks
//BaseType_t res = xTaskCreate(runWorker, name, 35000, (void*)name, 1, NULL); //BaseType_t res = xTaskCreate(runWorker, name, 35000, (void*)name, 1, NULL);
TaskHandle_t minerTask1, minerTask2 = NULL; TaskHandle_t minerTask1, minerTask2 = NULL;
xTaskCreate(runMiner, "Miner0", 15000, NULL, 1, &minerTask1); xTaskCreate(runMiner, "Miner0", 15000, (void*)0, 1, &minerTask1);
xTaskCreate(runMiner, "Miner1", 15000, NULL, 1, &minerTask2); xTaskCreate(runMiner, "Miner1", 15000, (void*)1, 1, &minerTask2);
esp_task_wdt_add(minerTask1); esp_task_wdt_add(minerTask1);
esp_task_wdt_add(minerTask2); esp_task_wdt_add(minerTask2);

View File

@ -214,9 +214,9 @@ void runStratumWorker(void *name) {
#include "shaTests/jadeSHA256.h" #include "shaTests/jadeSHA256.h"
#include "shaTests/customSHA256.h" #include "shaTests/customSHA256.h"
#include "mbedtls/sha256.h" #include "mbedtls/sha256.h"
void runMiner(void * name){ void runMiner(void * task_id) {
unsigned long nonce;
unsigned long max_nonce; unsigned int miner_id = (uint32_t)task_id;
while(1){ while(1){
@ -227,16 +227,10 @@ void runMiner(void * name){
} }
vTaskDelay(10 / portTICK_PERIOD_MS); //Small delay to join both mining threads vTaskDelay(10 / portTICK_PERIOD_MS); //Small delay to join both mining threads
if(mMiner.newJob) { if(mMiner.newJob)
mMiner.newJob = false; //Clear newJob flag mMiner.newJob = false; //Clear newJob flag
nonce = 0; else if(mMiner.newJob2)
max_nonce = MAX_NONCE;
}
else if(mMiner.newJob2){
mMiner.newJob2 = false; //Clear newJob flag mMiner.newJob2 = false; //Clear newJob flag
nonce = TARGET_NONCE - MAX_NONCE;
max_nonce = TARGET_NONCE;
}
mMiner.inRun = true; //Set inRun flag mMiner.inRun = true; //Set inRun flag
//Prepare Premining data //Prepare Premining data
@ -259,6 +253,9 @@ void runMiner(void * name){
Serial.println(""); Serial.println("");
*/ */
// search a valid nonce // search a valid nonce
unsigned long nonce = TARGET_NONCE - MAX_NONCE;
// split up odd/even nonces between miner tasks
nonce += miner_id;
uint32_t startT = micros(); uint32_t startT = micros();
unsigned char *header64 = mMiner.bytearray_blockheader + 64; unsigned char *header64 = mMiner.bytearray_blockheader + 64;
Serial.println(">>> STARTING TO HASH NONCES"); Serial.println(">>> STARTING TO HASH NONCES");
@ -284,7 +281,8 @@ void runMiner(void * name){
Serial.println(""); */ Serial.println(""); */
hashes++; hashes++;
if (nonce++> max_nonce) break; //exit nonce += 2;
if (nonce > TARGET_NONCE) break; //exit
if(!mMiner.inRun) { Serial.println ("MINER WORK ABORTED >> waiting new job"); break;} if(!mMiner.inRun) { Serial.println ("MINER WORK ABORTED >> waiting new job"); break;}
// check if 16bit share // check if 16bit share
@ -315,9 +313,9 @@ void runMiner(void * name){
// check if valid header // check if valid header
if(checkValid(hash, mMiner.bytearray_target)){ if(checkValid(hash, mMiner.bytearray_target)){
Serial.printf("[WORKER] %s CONGRATULATIONS! Valid completed with nonce: %d | 0x%x\n", (char *)name, nonce, nonce); Serial.printf("[WORKER] %d CONGRATULATIONS! Valid block found with nonce: %d | 0x%x\n", miner_id, nonce, nonce);
valids++; valids++;
Serial.printf("[WORKER] %s Submiting work valid!\n", (char *)name); Serial.printf("[WORKER] %d Submitted work valid!\n", miner_id);
// STEP 3: Submit mining job // STEP 3: Submit mining job
tx_mining_submit(client, mWorker, mJob, nonce); tx_mining_submit(client, mWorker, mJob, nonce);
client.stop(); client.stop();