From 0931960ac10af32b3159cb8ca89ce57de6c2a90f Mon Sep 17 00:00:00 2001 From: shufps Date: Mon, 9 Sep 2024 07:38:02 +0200 Subject: [PATCH] proper job management --- src/drivers/nerd-nos/bm1397.cpp | 15 +++++-------- src/drivers/nerd-nos/bm1397.h | 4 ++-- src/drivers/nerd-nos/mining.cpp | 15 +++++++++---- src/drivers/nerd-nos/mining.h | 7 +++--- src/mining.cpp | 40 ++++++++++++++++++++++++--------- src/stratum.cpp | 6 ++--- src/stratum.h | 2 +- 7 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/drivers/nerd-nos/bm1397.cpp b/src/drivers/nerd-nos/bm1397.cpp index fab8912..fe1dc87 100644 --- a/src/drivers/nerd-nos/bm1397.cpp +++ b/src/drivers/nerd-nos/bm1397.cpp @@ -337,17 +337,15 @@ void BM1397_set_job_difficulty_mask(int difficulty) _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, BM1937_SERIALTX_DEBUG); } -static uint8_t id = 0; -uint8_t BM1397_send_work(bm_job_t *next_bm_job) +void BM1397_send_work(bm_job_t *next_bm_job, uint8_t job_id) { job_packet job; // max job number is 128 // there is still some really weird logic with the job id bits for the asic to sort out // so we have it limited to 128 and it has to increment by 4 - id = (id + 4) % 128; - job.job_id = id; + job.job_id = (job_id * 4) % 128; job.num_midstates = next_bm_job->num_midstates; memcpy(&job.starting_nonce, &next_bm_job->starting_nonce, 4); memcpy(&job.nbits, &next_bm_job->target, 4); @@ -363,8 +361,6 @@ uint8_t BM1397_send_work(bm_job_t *next_bm_job) } _send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t*) &job, sizeof(job_packet), BM1397_DEBUG_WORK); - - return id; } asic_result *BM1397_receive_work(uint16_t timeout) @@ -394,7 +390,7 @@ asic_result *BM1397_receive_work(uint16_t timeout) return (asic_result *)asic_response_buffer; } -task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout) +task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout) { asic_result *asic_result = BM1397_receive_work(timeout); @@ -408,10 +404,10 @@ task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout) uint8_t nonce_found = 0; uint32_t first_nonce = 0; - uint8_t rx_job_id = asic_result->job_id & 0xfc; + uint8_t rx_job_id = (asic_result->job_id & 0xfc) >> 2; uint8_t rx_midstate_index = asic_result->job_id & 0x03; - uint32_t rolled_version = job->version; + uint32_t rolled_version = version; for (int i = 0; i < rx_midstate_index; i++) { rolled_version = increment_bitmask(rolled_version, 0x1fffe000); @@ -446,3 +442,4 @@ task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout) return &result; } + diff --git a/src/drivers/nerd-nos/bm1397.h b/src/drivers/nerd-nos/bm1397.h index 7c638c9..5304723 100644 --- a/src/drivers/nerd-nos/bm1397.h +++ b/src/drivers/nerd-nos/bm1397.h @@ -46,10 +46,10 @@ typedef struct __attribute__((__packed__)) uint8_t BM1397_init(uint64_t frequency, uint16_t asic_count); -uint8_t BM1397_send_work(bm_job_t * next_bm_job); +void BM1397_send_work(bm_job_t * next_bm_job, uint8_t job_id); void BM1397_set_job_difficulty_mask(int); int BM1397_set_max_baud(void); int BM1397_set_default_baud(void); void BM1397_send_hash_frequency(float frequency); -task_result *BM1397_proccess_work(bm_job_t *job, uint16_t timeout); +task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout); diff --git a/src/drivers/nerd-nos/mining.cpp b/src/drivers/nerd-nos/mining.cpp index 5072795..b742f8d 100644 --- a/src/drivers/nerd-nos/mining.cpp +++ b/src/drivers/nerd-nos/mining.cpp @@ -159,10 +159,17 @@ void asic_create_job(mining_subscribe *mWorker, mining_job *job, bm_job_t *next_ //next_job->pool_diff = stratum_difficulty; } -uint8_t asic_send_work(bm_job_t *next_bm_job) { - return BM1397_send_work(next_bm_job); +void asic_send_work(bm_job_t *next_bm_job, uint8_t job_id) { + BM1397_send_work(next_bm_job, job_id); } -task_result *asic_proccess_work(bm_job_t *job, uint16_t timeout) { - return BM1397_proccess_work(job, timeout); +task_result *asic_proccess_work(uint32_t version, uint16_t timeout) { + return BM1397_proccess_work(version, timeout); +} + +void asic_free_bm_job(bm_job_t *job) { + free(job->jobid); + free(job->extranonce2); + // mark as free + job->ntime = 0; } \ No newline at end of file diff --git a/src/drivers/nerd-nos/mining.h b/src/drivers/nerd-nos/mining.h index f25ad71..4c1e666 100644 --- a/src/drivers/nerd-nos/mining.h +++ b/src/drivers/nerd-nos/mining.h @@ -26,7 +26,8 @@ typedef struct } bm_job_t; -uint8_t asic_send_work(bm_job_t *next_bm_job); -task_result *asic_proccess_work(bm_job_t *job, uint16_t timeout); +void asic_send_work(bm_job_t *next_bm_job, uint8_t job_id); +task_result *asic_proccess_work(uint32_t version, uint16_t timeout); -double asic_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const uint32_t rolled_version); \ No newline at end of file +double asic_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const uint32_t rolled_version); +void asic_free_bm_job(bm_job_t *job); \ No newline at end of file diff --git a/src/mining.cpp b/src/mining.cpp index 20be8f9..76c31dd 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -374,11 +374,13 @@ void runMiner(void * task_id) { void asic_create_job(mining_subscribe *mWorker, mining_job *job, bm_job_t *next_job, uint32_t extranonce_2); +// we can have 32 different job ids +bm_job_t asic_jobs[32]; + void runASIC(void * task_id) { Serial.printf("[MINER] Started runASIC Task!\n"); uint32_t extranonce_2 = 0; - bm_job_t asic_job; while(1) { // wait for new job while(1) { @@ -396,33 +398,49 @@ void runASIC(void * task_id) { Serial.println(">>> STARTING TO HASH NONCES"); uint32_t startT = micros(); + memset(asic_jobs, 0, sizeof(asic_jobs)); + + // we are assuming the version doesn't change from job to job + uint32_t version = strtoul(mJob.version.c_str(), NULL, 16); + + mMonitor.NerdStatus = NM_hashing; + while (mMiner.inRun) { - mMonitor.NerdStatus = NM_hashing; extranonce_2++; + // use extranonce2 as job id + uint8_t asic_job_id = (uint8_t) (extranonce_2 % 32); + + // if it was used before, we have to free the pointers + if (asic_jobs[asic_job_id].ntime) { + asic_free_bm_job(&asic_jobs[asic_job_id]); + } + // create the next asic job - asic_create_job(&mWorker, &mJob, &asic_job, extranonce_2); + asic_create_job(&mWorker, &mJob, &asic_jobs[asic_job_id], extranonce_2); // send the job and - uint8_t asic_job_id = asic_send_work(&asic_job); + asic_send_work(&asic_jobs[asic_job_id], asic_job_id); // wait 30ms for the response - task_result *result = asic_proccess_work(&asic_job, 30); + // the pointer returned is the RS232 receive buffer :shushing-face: + // but we only have a single thread so it should be okay + task_result *result = asic_proccess_work(version, 30); + // if we haven't received anything in time, so send a new job if (!result) { - // we haven't received anything in time, so send a new job continue; } - if (result->job_id != asic_job_id) { - // job id mismatch - Serial.printf("ID mismatch, expected %02x, got %02x\n", asic_job_id, result->job_id); + // if we have received a job we don't know + if (!asic_jobs[result->job_id].ntime) { + Serial.printf("No Job found for received ID %02x\n", result->job_id); continue; } // check the nonce difficulty double diff_hash = asic_test_nonce_value( - &asic_job, + &asic_jobs[result->job_id], result->nonce, result->rolled_version); @@ -435,7 +453,7 @@ void runASIC(void * task_id) { if(diff_hash > mMiner.poolDifficulty) { - tx_mining_submit_with_version(client, mWorker, &asic_job, extranonce_2, result->nonce, result->rolled_version); + tx_mining_submit_asic(client, mWorker, &asic_jobs[result->job_id], result); Serial.println("valid share!"); /* Serial.print(" - Current diff share: "); Serial.println(diff_hash,12); diff --git a/src/stratum.cpp b/src/stratum.cpp index d5bcabf..d63619f 100644 --- a/src/stratum.cpp +++ b/src/stratum.cpp @@ -230,7 +230,7 @@ bool tx_mining_submit(WiFiClient& client, mining_subscribe mWorker, mining_job m return true; } -bool tx_mining_submit_with_version(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, uint32_t extranonce2, unsigned long nonce, uint32_t version) +bool tx_mining_submit_asic(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, task_result *result) { char payload[BUFFER] = {0}; @@ -242,8 +242,8 @@ bool tx_mining_submit_with_version(WiFiClient& client, mining_subscribe mWorker, asic_job->jobid, asic_job->extranonce2, asic_job->ntime, - nonce, - version ^ asic_job->version + result->nonce, + result->rolled_version ^ asic_job->version ); Serial.print(" Sending : "); Serial.print(payload); client.print(payload); diff --git a/src/stratum.h b/src/stratum.h index 4d5ffc3..b6951e3 100644 --- a/src/stratum.h +++ b/src/stratum.h @@ -62,7 +62,7 @@ bool parse_mining_notify(String line, mining_job& mJob); //Method Mining.submit bool tx_mining_submit(WiFiClient& client, mining_subscribe mWorker, mining_job mJob, unsigned long nonce); -bool tx_mining_submit_with_version(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, uint32_t extranonce2, unsigned long nonce, uint32_t version); +bool tx_mining_submit_asic(WiFiClient& client, mining_subscribe mWorker, const bm_job_t* asic_job, task_result *result); //Difficulty Methods bool tx_suggest_difficulty(WiFiClient& client, double difficulty);