From 3e340a0117336ab319cb020ce0f53698daa64c49 Mon Sep 17 00:00:00 2001 From: shufps Date: Fri, 16 Aug 2024 08:55:34 +0200 Subject: [PATCH 1/4] improved hashrate --- main/global_state.h | 7 ++-- main/system.c | 69 +++++++++++++++++++++-------------- main/tasks/asic_result_task.c | 11 +++--- 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/main/global_state.h b/main/global_state.h index 47945537..e551eedf 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -15,7 +15,8 @@ #define STRATUM_USER CONFIG_STRATUM_USER -#define HISTORY_LENGTH 100 +// best is power of 2 for ring buffers +#define HISTORY_LENGTH 512 #define DIFF_STRING_SIZE 10 typedef enum @@ -45,11 +46,9 @@ typedef struct typedef struct { - double duration_start; int historical_hashrate_rolling_index; - double historical_hashrate_time_stamps[HISTORY_LENGTH]; + uint64_t historical_hashrate_time_stamps[HISTORY_LENGTH]; double historical_hashrate[HISTORY_LENGTH]; - int historical_hashrate_init; double current_hashrate; int64_t start_time; uint64_t shares_accepted; diff --git a/main/system.c b/main/system.c index d46a54a5..e6da30f6 100644 --- a/main/system.c +++ b/main/system.c @@ -60,9 +60,10 @@ static void _init_system(GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; - module->duration_start = 0; module->historical_hashrate_rolling_index = 0; - module->historical_hashrate_init = 0; + memset(module->historical_hashrate, 0, sizeof(module->historical_hashrate)); + memset(module->historical_hashrate_time_stamps, 0, sizeof(module->historical_hashrate_time_stamps)); + module->current_hashrate = 0; module->screen_page = 0; module->shares_accepted = 0; @@ -602,9 +603,6 @@ void SYSTEM_notify_rejected_share(GlobalState * GLOBAL_STATE) void SYSTEM_notify_mining_started(GlobalState * GLOBAL_STATE) { - SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; - - module->duration_start = esp_timer_get_time(); } void SYSTEM_notify_new_ntime(GlobalState * GLOBAL_STATE, uint32_t ntime) @@ -627,38 +625,55 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; - // Calculate the time difference in seconds with sub-second precision // hashrate = (nonce_difficulty * 2^32) / time_to_find - module->historical_hashrate[module->historical_hashrate_rolling_index] = GLOBAL_STATE->initial_ASIC_difficulty; - module->historical_hashrate_time_stamps[module->historical_hashrate_rolling_index] = esp_timer_get_time(); + // let's calculate the 10min average hashrate + uint64_t time_period = 600 * 1e6; + uint64_t current_time = esp_timer_get_time(); - module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH; + int index = module->historical_hashrate_rolling_index; - // ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration, - // historical_hashrate[historical_hashrate_rolling_index]); + module->historical_hashrate[index] = found_diff; + module->historical_hashrate_time_stamps[index] = current_time; - if (module->historical_hashrate_init < HISTORY_LENGTH) { - module->historical_hashrate_init++; - } else { - module->duration_start = - module->historical_hashrate_time_stamps[(module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH]; - } double sum = 0; - for (int i = 0; i < module->historical_hashrate_init; i++) { - sum += module->historical_hashrate[i]; - } + uint64_t oldest_time = 0; + int valid_shares = 0; + for (int i = 0; i < HISTORY_LENGTH; i++) { + // sum backwards + int rindex = (index - i + HISTORY_LENGTH) % HISTORY_LENGTH; - double duration = (double) (esp_timer_get_time() - module->duration_start) / 1000000; + uint64_t timestamp = module->historical_hashrate_time_stamps[rindex]; - double rolling_rate = (sum * 4294967296) / (duration * 1000000000); - if (module->historical_hashrate_init < HISTORY_LENGTH) { - module->current_hashrate = rolling_rate; - } else { - // More smoothing - module->current_hashrate = ((module->current_hashrate * 9) + rolling_rate) / 10; + // zero timestamps indicate that the "slot" is not used + if (timestamp == 0) { + break; + } + + // out of scope? break + // avoid modulo of a negative number + if (current_time - timestamp > time_period) { + break; + } + + sum += module->historical_hashrate[rindex]; + oldest_time = timestamp; + valid_shares++; } + // increment rolling index + // can't be done before summation + index = (index + 1) % HISTORY_LENGTH; + module->historical_hashrate_rolling_index = index; + + double rolling_rate = (sum * 4.294967296e9) / (double) (time_period / 1e6); + double rolling_rate_gh = rolling_rate / 1.0e9; + + ESP_LOGI(TAG, "hashrate: %.3fGH%s shares: %d (historical buffer spans %ds)", rolling_rate_gh, + (current_time - oldest_time >= time_period) ? "" : "*", valid_shares, (int) ((current_time - oldest_time) / 1e6)); + + module->current_hashrate = rolling_rate_gh; + _update_hashrate(GLOBAL_STATE); // logArrayContents(historical_hashrate, HISTORY_LENGTH); diff --git a/main/tasks/asic_result_task.c b/main/tasks/asic_result_task.c index 6b9313ae..492da88d 100644 --- a/main/tasks/asic_result_task.c +++ b/main/tasks/asic_result_task.c @@ -39,10 +39,12 @@ void ASIC_result_task(void *pvParameters) asic_result->nonce, asic_result->rolled_version); + uint32_t pool_difficulty = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff; + //log the ASIC response - ESP_LOGI(TAG, "Ver: %08" PRIX32 " Nonce %08" PRIX32 " diff %.1f of %ld.", asic_result->rolled_version, asic_result->nonce, nonce_diff, GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff); + ESP_LOGI(TAG, "Ver: %08" PRIX32 " Nonce %08" PRIX32 " diff %.1f of %ld.", asic_result->rolled_version, asic_result->nonce, nonce_diff, pool_difficulty); - if (nonce_diff > GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff) + if (nonce_diff > pool_difficulty) { STRATUM_V1_submit_share( @@ -54,8 +56,7 @@ void ASIC_result_task(void *pvParameters) asic_result->nonce, asic_result->rolled_version ^ GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->version); + SYSTEM_notify_found_nonce(GLOBAL_STATE, (double) pool_difficulty, job_id); } - - SYSTEM_notify_found_nonce(GLOBAL_STATE, nonce_diff, job_id); } -} \ No newline at end of file +} From d3513e8d4bfbe4c39cd876d6023a323d28e79f5c Mon Sep 17 00:00:00 2001 From: shufps Date: Fri, 16 Aug 2024 09:04:06 +0200 Subject: [PATCH 2/4] removed old variable --- main/system.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main/system.c b/main/system.c index e6da30f6..c7bce281 100644 --- a/main/system.c +++ b/main/system.c @@ -74,7 +74,7 @@ static void _init_system(GlobalState * GLOBAL_STATE) module->lastClockSync = 0; module->FOUND_BLOCK = false; module->startup_done = false; - + // set the pool url module->pool_url = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, CONFIG_STRATUM_URL); @@ -148,7 +148,7 @@ void SYSTEM_update_overheat_mode(GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; uint16_t new_overheat_mode = nvs_config_get_u16(NVS_CONFIG_OVERHEAT_MODE, 0); - + if (new_overheat_mode != module->overheat_mode) { module->overheat_mode = new_overheat_mode; ESP_LOGI(TAG, "Overheat mode updated to: %d", module->overheat_mode); @@ -192,7 +192,7 @@ static void _update_hashrate(GlobalState * GLOBAL_STATE) float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0); OLED_clearLine(0); memset(module->oled_buf, 0, 20); - snprintf(module->oled_buf, 20, "Gh%s: %.1f J/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*" : "", + snprintf(module->oled_buf, 20, "Gh: %.1f J/Th: %.1f", module->current_hashrate, efficiency); OLED_writeString(0, 0, module->oled_buf); break; @@ -355,7 +355,7 @@ static void _update_connection(GlobalState * GLOBAL_STATE) strncpy(module->oled_buf, module->ssid, sizeof(module->oled_buf)); module->oled_buf[sizeof(module->oled_buf) - 1] = 0; OLED_writeString(0, 1, module->oled_buf); - + memset(module->oled_buf, 0, 20); snprintf(module->oled_buf, 20, "Configuration SSID:"); OLED_writeString(0, 2, module->oled_buf); @@ -578,7 +578,7 @@ void SYSTEM_task(void * pvParameters) break; } else if (strcmp(input_event, "LONG") == 0) { ESP_LOGI(TAG, "Long button press detected, toggling WiFi SoftAP"); - toggle_wifi_softap(); // Toggle AP + toggle_wifi_softap(); // Toggle AP } } } From c25806b3562306854d781ca50b6335401db7dea7 Mon Sep 17 00:00:00 2001 From: shufps Date: Fri, 16 Aug 2024 09:07:49 +0200 Subject: [PATCH 3/4] fixed comment --- main/system.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/system.c b/main/system.c index c7bce281..c49c5bf9 100644 --- a/main/system.c +++ b/main/system.c @@ -641,6 +641,7 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui int valid_shares = 0; for (int i = 0; i < HISTORY_LENGTH; i++) { // sum backwards + // avoid modulo of a negative number int rindex = (index - i + HISTORY_LENGTH) % HISTORY_LENGTH; uint64_t timestamp = module->historical_hashrate_time_stamps[rindex]; @@ -651,7 +652,6 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui } // out of scope? break - // avoid modulo of a negative number if (current_time - timestamp > time_period) { break; } From 5b0d53677de447957efc754198bcbf9860cfe96c Mon Sep 17 00:00:00 2001 From: shufps Date: Sat, 17 Aug 2024 07:04:24 +0200 Subject: [PATCH 4/4] fixed best difficulty --- main/system.c | 13 ++++++------- main/system.h | 3 ++- main/tasks/asic_result_task.c | 4 +++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/main/system.c b/main/system.c index c49c5bf9..06a0bb0a 100644 --- a/main/system.c +++ b/main/system.c @@ -621,7 +621,11 @@ void SYSTEM_notify_new_ntime(GlobalState * GLOBAL_STATE, uint32_t ntime) settimeofday(&tv, NULL); } -void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, uint8_t job_id) +void SYSTEM_check_for_best_diff(GlobalState * GLOBAL_STATE, double found_diff, uint8_t job_id) { + _check_for_best_diff(GLOBAL_STATE, found_diff, job_id); +} + +void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double pool_diff) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; @@ -633,7 +637,7 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui int index = module->historical_hashrate_rolling_index; - module->historical_hashrate[index] = found_diff; + module->historical_hashrate[index] = pool_diff; module->historical_hashrate_time_stamps[index] = current_time; double sum = 0; @@ -675,9 +679,4 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui module->current_hashrate = rolling_rate_gh; _update_hashrate(GLOBAL_STATE); - - // logArrayContents(historical_hashrate, HISTORY_LENGTH); - // logArrayContents(historical_hashrate_time_stamps, HISTORY_LENGTH); - - _check_for_best_diff(GLOBAL_STATE, found_diff, job_id); } diff --git a/main/system.h b/main/system.h index 27330ddc..ac14c7ba 100644 --- a/main/system.h +++ b/main/system.h @@ -7,7 +7,8 @@ void SYSTEM_task(void * parameters); void SYSTEM_notify_accepted_share(GlobalState * GLOBAL_STATE); void SYSTEM_notify_rejected_share(GlobalState * GLOBAL_STATE); -void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, uint8_t job_id); +void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double pool_diff); +void SYSTEM_check_for_best_diff(GlobalState * GLOBAL_STATE, double found_diff, uint8_t job_id); void SYSTEM_notify_mining_started(GlobalState * GLOBAL_STATE); void SYSTEM_notify_new_ntime(GlobalState * GLOBAL_STATE, uint32_t ntime); diff --git a/main/tasks/asic_result_task.c b/main/tasks/asic_result_task.c index 492da88d..0f19d343 100644 --- a/main/tasks/asic_result_task.c +++ b/main/tasks/asic_result_task.c @@ -56,7 +56,9 @@ void ASIC_result_task(void *pvParameters) asic_result->nonce, asic_result->rolled_version ^ GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->version); - SYSTEM_notify_found_nonce(GLOBAL_STATE, (double) pool_difficulty, job_id); + SYSTEM_notify_found_nonce(GLOBAL_STATE, (double) pool_difficulty); } + + SYSTEM_check_for_best_diff(GLOBAL_STATE, nonce_diff, job_id); } }