From 18d0f651e8343539295e2441ba4eba664869c651 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 4 Jun 2023 13:16:34 -0400 Subject: [PATCH] hashrate calc, WIP --- components/bm1397/bm1397.c | 3 +- components/stratum/include/mining.h | 3 - main/miner.c | 14 +-- main/system.c | 132 ++++++++++++++++++++++++---- main/system.h | 2 + 5 files changed, 120 insertions(+), 34 deletions(-) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index 627f6fbf..312ce39e 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -159,7 +159,7 @@ void set_job_difficulty_mask(int difficulty){ // The mask must be a power of 2 so there are no holes // Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111} // Incorrect: {0b00000000, 0b00000000, 0b11100111, 0b11111111} - difficulty = largestPowerOfTwo(difficulty) -1; + difficulty = largestPowerOfTwo(difficulty) -1; // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling // convert difficulty into char array // Ex: 256 = {0b00000000, 0b00000000, 0b00000000, 0b11111111}, {0x00, 0x00, 0x00, 0xff} @@ -169,6 +169,7 @@ void set_job_difficulty_mask(int difficulty){ //The char is read in backwards to the register so we need to reverse them //So a mask of 512 looks like 0b00000000 00000000 00000001 1111111 //and not 0b00000000 00000000 10000000 1111111 + job_difficulty_mask[5 - i] = reverseBits(value); } diff --git a/components/stratum/include/mining.h b/components/stratum/include/mining.h index 46d3061c..72236f25 100644 --- a/components/stratum/include/mining.h +++ b/components/stratum/include/mining.h @@ -2,7 +2,6 @@ #define MINING_H #include "stratum_api.h" -#include typedef struct { @@ -15,8 +14,6 @@ typedef struct { uint8_t midstate[32]; uint32_t pool_diff; - // is the pool diff a power of 2? - bool pool_diff_pow2; char * jobid; char * extranonce2; } bm_job; diff --git a/main/miner.c b/main/miner.c index 59af2fcd..03acdda6 100755 --- a/main/miner.c +++ b/main/miner.c @@ -91,6 +91,8 @@ static void ASIC_task(void * pvParameters) // TODO // set_max_baud(); + + ESP_LOGI(TAG, "Mining!"); while (1) { bm_job * next_bm_job = (bm_job *) queue_dequeue(&ASIC_jobs_queue); struct job_packet job; @@ -158,20 +160,11 @@ static void ASIC_task(void * pvParameters) prev_nonce = nonce.nonce; } - // If the pool diff is a power of 2, the ASIC won't return nonces lower than the diff - // hence we can skip all the checking - if(active_jobs[nonce.job_id]->pool_diff_pow2){ - submit_share(sock, STRATUM_USER, active_jobs[nonce.job_id]->jobid, active_jobs[nonce.job_id]->ntime, - active_jobs[nonce.job_id]->extranonce2, nonce.nonce); - notify_system_submitted_share(); - continue; - } - // check the nonce difficulty double nonce_diff = test_nonce_value(active_jobs[nonce.job_id], nonce.nonce); ESP_LOGI(TAG, "Nonce difficulty %.2f of %d.", nonce_diff, active_jobs[nonce.job_id]->pool_diff); - + notify_system_found_nonce(nonce_diff); if (nonce_diff > active_jobs[nonce.job_id]->pool_diff) { //print_hex((uint8_t *)&job, sizeof(struct job_packet), sizeof(struct job_packet), "job: "); @@ -206,7 +199,6 @@ static void create_jobs_task(void * pvParameters) bm_job next_job = construct_bm_job(¶ms, merkle_root); next_job.pool_diff = stratum_difficulty; //each job is tied to the _current_ difficulty - next_job.pool_diff_pow2 = (stratum_difficulty != 0 && (stratum_difficulty & (stratum_difficulty - 1)) == 0); //ESP_LOGI(TAG, "bm_job: "); // print_hex((uint8_t *) &next_job.target, 4, 4, "nbits: "); diff --git a/main/system.c b/main/system.c index 43fd41e7..046f6278 100644 --- a/main/system.c +++ b/main/system.c @@ -11,20 +11,110 @@ #include "INA260.h" #include "adc.h" #include "oled.h" -#include +#include static const char *TAG = "system"; #define BM1397_VOLTAGE CONFIG_BM1397_VOLTAGE +#define HISTORY_LENGTH 100 +#define HISTORY_WINDOW_SIZE 5 + +static char oled_buf[20]; +static int screen_page = 0; static int shares_submitted = 0; -static time_t start_time, current_time; +static time_t start_time; + +static double last_found_nonce_time = 0; +static int historical_hashrate_rolling_index = 0; +static double historical_hashrate[HISTORY_LENGTH] = {0.0}; +static int historical_hashrate_init = 0; +static double current_hashrate = 0; + + +void logArrayContents(const double* array, size_t length) { + char logMessage[1024]; // Adjust the buffer size as needed + int offset = 0; + + offset += snprintf(logMessage + offset, sizeof(logMessage) - offset, "Array Contents: ["); + + for (size_t i = 0; i < length; i++) { + offset += snprintf(logMessage + offset, sizeof(logMessage) - offset, "%.1f%s", array[i], (i < length - 1) ? ", " : "]"); + } + + ESP_LOGI(TAG, "%s", logMessage); +} + +void update_hashrate(void){ + + if(screen_page != 0){ + return; + } + + OLED_clearLine(1); + memset(oled_buf, 0, 20); + snprintf(oled_buf, 20, "GH/s%s: %.1f", historical_hashrate_init < HISTORY_LENGTH ? "*": "", current_hashrate); + OLED_writeString(0, 1, oled_buf); +} + +void update_shares(void){ + if(screen_page != 0){ + return; + } + OLED_clearLine(2); + memset(oled_buf, 0, 20); + snprintf(oled_buf, 20, "Shares: %i", shares_submitted); + OLED_writeString(0, 2, oled_buf); +} void notify_system_submitted_share(void){ shares_submitted++; + update_shares(); } + + +void notify_system_found_nonce(double nonce_diff){ + + //init + if(last_found_nonce_time == 0){ + last_found_nonce_time = esp_timer_get_time(); + return; + } + + // Calculate the time difference in seconds with sub-second precision + double time_to_find = (double)(esp_timer_get_time() - last_found_nonce_time) / 1000000; + + + // hashrate = (nonce_difficulty * 2^32) / time_to_find + historical_hashrate[historical_hashrate_rolling_index] = (nonce_diff * 4294967296) / (time_to_find * 1000000000); + + + if(historical_hashrate_init < HISTORY_LENGTH){ + historical_hashrate_init++; + } + double sum = 0; + for (int i = 0; i < historical_hashrate_init; i++) { + sum += historical_hashrate[i]; + } + current_hashrate = sum / historical_hashrate_init; + + + ESP_LOGI(TAG, "CH: %.1f", current_hashrate); + + // Increment all the stuff + historical_hashrate_rolling_index = (historical_hashrate_rolling_index + 1) % HISTORY_LENGTH; + + last_found_nonce_time = esp_timer_get_time(); + + update_hashrate(); + + logArrayContents(historical_hashrate, HISTORY_LENGTH); + +} + + void init_system(void) { start_time = time(NULL); @@ -93,42 +183,46 @@ void update_system_info(void) { } void update_system_performance(){ - char oled_buf[20]; - // Get the current system time as the current time - current_time = time(NULL); - // Calculate the uptime in seconds - double uptime_in_seconds = difftime(current_time, start_time); - - // Calculate the uptime in days and hours + double uptime_in_seconds = difftime(time(NULL), start_time); int uptime_in_days = uptime_in_seconds / (3600 * 24); int remaining_seconds = (int)uptime_in_seconds % (3600 * 24); - double uptime_in_hours = (double)remaining_seconds / 3600; + int uptime_in_hours = remaining_seconds / 3600; + remaining_seconds %= 3600; + int uptime_in_minutes = remaining_seconds / 60; + if (OLED_status()) { - OLED_clearLine(1); - OLED_clearLine(2); + + OLED_clearLine(3); - memset(oled_buf, 0, 20); - snprintf(oled_buf, 20, "Shares: %i", shares_submitted); - OLED_writeString(0, 1, oled_buf); + update_hashrate(); + update_shares(); + memset(oled_buf, 0, 20); - snprintf(oled_buf, 20, "Uptime: %dd %.2fh", uptime_in_days, uptime_in_hours); - OLED_writeString(0, 2, oled_buf); + snprintf(oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes); + OLED_writeString(0, 3, oled_buf); } } + + + void system_task(void *arg) { init_system(); while(1){ + screen_page = 0; + update_system_performance(); + vTaskDelay(30000 / portTICK_RATE_MS); + + screen_page = 1; update_system_info(); vTaskDelay(5000 / portTICK_RATE_MS); - update_system_performance(); - vTaskDelay(5000 / portTICK_RATE_MS); + } } \ No newline at end of file diff --git a/main/system.h b/main/system.h index eba76c8b..8d07fb8f 100644 --- a/main/system.h +++ b/main/system.h @@ -5,5 +5,7 @@ void system_task(void *arg); void init_system(void); void get_stats(void); void notify_system_submitted_share(void); +void notify_system_found_nonce(double nonce_diff); + #endif /* SYSTEM_H_ */ \ No newline at end of file