From 6065ef05a37643e610640ad0236ace6bd54d1890 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 8 Jun 2023 17:48:26 -0400 Subject: [PATCH] Fixed issues with ckpool job id, parameter length, asic init --- components/bm1397/bm1397.c | 2 ++ components/stratum/stratum_api.c | 15 +++++++++------ main/miner.c | 9 +++++++-- main/system.c | 30 +++++++++++++++++++++++++++++- main/system.h | 2 ++ main/tasks/asic_task.c | 17 +++++++++++++---- main/tasks/create_jobs_task.c | 14 +++++++------- main/tasks/stratum_task.c | 16 +++++++++------- main/tasks/stratum_task.h | 4 ++++ 9 files changed, 82 insertions(+), 27 deletions(-) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index 2889493f..928dce6f 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -293,6 +293,8 @@ void BM1397_set_job_difficulty_mask(int difficulty){ job_difficulty_mask[5 - i] = _reverse_bits(value); } + ESP_LOGI(TAG, "Setting job ASIC mask to %d", difficulty); + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, false); } diff --git a/components/stratum/stratum_api.c b/components/stratum/stratum_api.c index e067f0da..ed9df0e0 100644 --- a/components/stratum/stratum_api.c +++ b/components/stratum/stratum_api.c @@ -120,6 +120,7 @@ void STRATUM_V1_parse(StratumApiV1Message* message, const char * stratum_json) } else if (strcmp("mining.set_version_mask", method_json->valuestring) == 0) { result = MINING_SET_VERSION_MASK; } + send_uid++; } else { //parse results cJSON * result_json = cJSON_GetObjectItem(json, "result"); @@ -169,7 +170,9 @@ void STRATUM_V1_parse(StratumApiV1Message* message, const char * stratum_json) message->mining_notification = new_work; - int value = cJSON_IsTrue(cJSON_GetArrayItem(params, 8)); + // params can be varible length + int paramsLength = cJSON_GetArraySize(params); + int value = cJSON_IsTrue(cJSON_GetArrayItem(params, paramsLength - 1)); message->should_abandon_work = value; @@ -241,7 +244,7 @@ int STRATUM_V1_subscribe(int socket, char ** extranonce, int * extranonce2_len) { // Subscribe char subscribe_msg[BUFFER_SIZE]; - sprintf(subscribe_msg, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"bitaxe v2.2\"]}\n", send_uid++); + sprintf(subscribe_msg, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"bitaxe v2.2\"]}\n", send_uid); debug_stratum_tx(subscribe_msg); write(socket, subscribe_msg, strlen(subscribe_msg)); char * line; @@ -259,7 +262,7 @@ int STRATUM_V1_subscribe(int socket, char ** extranonce, int * extranonce2_len) int STRATUM_V1_suggest_difficulty(int socket, uint32_t difficulty) { char difficulty_msg[BUFFER_SIZE]; - sprintf(difficulty_msg, "{\"id\": %d, \"method\": \"mining.STRATUM_V1_suggest_difficulty\", \"params\": [%d]}\n", send_uid++, difficulty); + sprintf(difficulty_msg, "{\"id\": %d, \"method\": \"mining.STRATUM_V1_suggest_difficulty\", \"params\": [%d]}\n", send_uid, difficulty); debug_stratum_tx(difficulty_msg); write(socket, difficulty_msg, strlen(difficulty_msg)); @@ -279,7 +282,7 @@ int STRATUM_V1_authenticate(int socket, const char * username) { char authorize_msg[BUFFER_SIZE]; sprintf(authorize_msg, "{\"id\": %d, \"method\": \"mining.authorize\", \"params\": [\"%s\", \"x\"]}\n", - send_uid++, username); + send_uid, username); debug_stratum_tx(authorize_msg); write(socket, authorize_msg, strlen(authorize_msg)); @@ -292,7 +295,7 @@ void STRATUM_V1_submit_share(int socket, const char * username, const char * job { char submit_msg[BUFFER_SIZE]; sprintf(submit_msg, "{\"id\": %d, \"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%08x\", \"%08x\"]}\n", - send_uid++, username, jobid, extranonce_2, ntime, nonce); + send_uid, username, jobid, extranonce_2, ntime, nonce); debug_stratum_tx(submit_msg); write(socket, submit_msg, strlen(submit_msg)); @@ -303,7 +306,7 @@ void STRATUM_V1_configure_version_rolling(int socket) { // Configure char configure_msg[BUFFER_SIZE * 2]; - sprintf(configure_msg, "{\"id\": %d, \"method\": \"mining.configure\", \"params\": [[\"version-rolling\"], {\"version-rolling.mask\": \"ffffffff\"}]}\n", send_uid++); + sprintf(configure_msg, "{\"id\": %d, \"method\": \"mining.configure\", \"params\": [[\"version-rolling\"], {\"version-rolling.mask\": \"ffffffff\"}]}\n", send_uid); ESP_LOGI(TAG, "tx: %s", configure_msg); write(socket, configure_msg, strlen(configure_msg)); diff --git a/main/miner.c b/main/miner.c index f8df8868..6ff4ebb3 100755 --- a/main/miner.c +++ b/main/miner.c @@ -10,12 +10,12 @@ #include "asic_task.h" #include "create_jobs_task.h" #include "global_state.h" +#include "serial.h" static GlobalState GLOBAL_STATE = { .extranonce_str = NULL, .extranonce_2_len = 0, - .abandon_work = 0, - .stratum_difficulty = 8192 + .abandon_work = 0 }; @@ -40,6 +40,11 @@ void app_main(void) queue_init(&GLOBAL_STATE.stratum_queue); queue_init(&GLOBAL_STATE.ASIC_jobs_queue); + + SERIAL_init(); + + BM1397_init(); + xTaskCreate(stratum_task, "stratum admin", 8192, (void*)&GLOBAL_STATE, 15, NULL); xTaskCreate(create_jobs_task, "stratum miner", 8192, (void*)&GLOBAL_STATE, 10, NULL); xTaskCreate(ASIC_task, "asic", 8192, (void*)&GLOBAL_STATE, 10, NULL); diff --git a/main/system.c b/main/system.c index ec301206..45047951 100644 --- a/main/system.c +++ b/main/system.c @@ -13,6 +13,7 @@ #include "oled.h" #include #include "system.h" +#include "math.h" static const char *TAG = "SystemModule"; @@ -29,7 +30,7 @@ static void _init_system(SystemModule* module) { module->screen_page = 0; module->shares_accepted = 0; module->shares_rejected = 0; - + module->best_nonce_diff = 0; module->start_time = time(NULL); //test the LEDs @@ -94,6 +95,16 @@ static void _update_shares(SystemModule* module){ OLED_writeString(0, 1, module->oled_buf); } +static void _update_best_diff(SystemModule* module){ + if(module->screen_page != 0){ + return; + } + OLED_clearLine(3); + memset(module->oled_buf, 0, 20); + snprintf(module->oled_buf, 20, "BD: %u", module->best_nonce_diff); + OLED_writeString(0, 3, module->oled_buf); +} + static void _clear_display(void){ OLED_clearLine(0); OLED_clearLine(1); @@ -172,6 +183,7 @@ static void _update_system_performance(SystemModule* module){ _update_hashrate(module); _update_shares(module); + _update_best_diff(module); memset(module->oled_buf, 0, 20); snprintf(module->oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes); @@ -204,8 +216,24 @@ void SYSTEM_task(void *pvParameters) { } } +double _calculate_network_difficultiy(uint32_t nBits) { + uint32_t mantissa = nBits & 0x007fffff; // Extract the mantissa from nBits + uint8_t exponent = (nBits >> 24) & 0xff; // Extract the exponent from nBits + + double target = (double)mantissa * pow(253,(exponent - 3)); // Calculate the target value + + double difficulty = (pow(2, 208) * 65535) / target; // Calculate the difficulty + + return difficulty; +} +void SYSTEM_notify_best_nonce_diff(SystemModule * module, uint32_t diff, uint32_t nbits){ + module->best_nonce_diff = diff; + uint32_t network_diff = _calculate_network_difficultiy(nbits); + ESP_LOGI(TAG, "Network diff: %u", network_diff); +} + void SYSTEM_notify_accepted_share(SystemModule* module){ module->shares_accepted++; _update_shares(module); diff --git a/main/system.h b/main/system.h index 3a633a33..b81f4267 100644 --- a/main/system.h +++ b/main/system.h @@ -15,6 +15,7 @@ typedef struct { uint16_t shares_rejected; int screen_page; char oled_buf[20]; + uint32_t best_nonce_diff; } SystemModule; void SYSTEM_task(void *parameters); @@ -24,5 +25,6 @@ void SYSTEM_notify_accepted_share(SystemModule* module); void SYSTEM_notify_rejected_share(SystemModule* module); void SYSTEM_notify_found_nonce(SystemModule* module, double nonce_diff); void SYSTEM_notify_mining_started(SystemModule* module); +void SYSTEM_notify_best_nonce_diff(SystemModule* module, uint32_t best_nonce_diff, uint32_t nbits); #endif /* SYSTEM_H_ */ diff --git a/main/tasks/asic_task.c b/main/tasks/asic_task.c index 39072403..3e6662aa 100644 --- a/main/tasks/asic_task.c +++ b/main/tasks/asic_task.c @@ -14,9 +14,6 @@ void ASIC_task(void * pvParameters) GlobalState *GLOBAL_STATE = (GlobalState*)pvParameters; - SERIAL_init(); - - BM1397_init(); uint8_t buf[CHUNK_SIZE]; memset(buf, 0, 1024); @@ -36,9 +33,17 @@ void ASIC_task(void * pvParameters) SERIAL_set_baud(baud); SYSTEM_notify_mining_started(&GLOBAL_STATE->SYSTEM_MODULE); - ESP_LOGI(TAG, "Mining!"); + ESP_LOGI(TAG, "ASIC Ready!"); while (1) { bm_job * next_bm_job = (bm_job *) queue_dequeue(&GLOBAL_STATE->ASIC_jobs_queue); + + if(next_bm_job->pool_diff != GLOBAL_STATE->stratum_difficulty){ + ESP_LOGI(TAG, "New difficulty %d", next_bm_job->pool_diff); + BM1397_set_job_difficulty_mask(next_bm_job->pool_diff); + GLOBAL_STATE->stratum_difficulty = next_bm_job->pool_diff; + } + + struct job_packet job; // max job number is 128 id = (id + 4) % 128; @@ -119,5 +124,9 @@ void ASIC_task(void * pvParameters) } + if(nonce_diff > GLOBAL_STATE->SYSTEM_MODULE.best_nonce_diff){ + SYSTEM_notify_best_nonce_diff(&GLOBAL_STATE->SYSTEM_MODULE, nonce_diff, next_bm_job->target); + } + } } diff --git a/main/tasks/create_jobs_task.c b/main/tasks/create_jobs_task.c index 781a41c7..9424f033 100644 --- a/main/tasks/create_jobs_task.c +++ b/main/tasks/create_jobs_task.c @@ -17,8 +17,8 @@ void create_jobs_task(void * pvParameters) while (1) { - mining_notify * params = (mining_notify *) queue_dequeue(&GLOBAL_STATE->stratum_queue); - ESP_LOGI(TAG, "New Work Dequeued %s", params->job_id); + mining_notify * mining_notification = (mining_notify *) queue_dequeue(&GLOBAL_STATE->stratum_queue); + ESP_LOGI(TAG, "New Work Dequeued %s", mining_notification->job_id); uint32_t extranonce_2 = 0; while (extranonce_2 < UINT_MAX && GLOBAL_STATE->abandon_work == 0) @@ -26,15 +26,15 @@ void create_jobs_task(void * pvParameters) char * extranonce_2_str = extranonce_2_generate(extranonce_2, GLOBAL_STATE->extranonce_2_len); - char *coinbase_tx = construct_coinbase_tx(params->coinbase_1, params->coinbase_2, GLOBAL_STATE->extranonce_str, extranonce_2_str); + char *coinbase_tx = construct_coinbase_tx(mining_notification->coinbase_1, mining_notification->coinbase_2, GLOBAL_STATE->extranonce_str, extranonce_2_str); - char *merkle_root = calculate_merkle_root_hash(coinbase_tx, (uint8_t(*)[32])params->merkle_branches, params->n_merkle_branches); - bm_job next_job = construct_bm_job(params, merkle_root); + char *merkle_root = calculate_merkle_root_hash(coinbase_tx, (uint8_t(*)[32])mining_notification->merkle_branches, mining_notification->n_merkle_branches); + bm_job next_job = construct_bm_job(mining_notification, merkle_root); bm_job * queued_next_job = malloc(sizeof(bm_job)); memcpy(queued_next_job, &next_job, sizeof(bm_job)); queued_next_job->extranonce2 = strdup(extranonce_2_str); - queued_next_job->jobid = strdup(params->job_id); + queued_next_job->jobid = strdup(mining_notification->job_id); queue_enqueue(&GLOBAL_STATE->ASIC_jobs_queue, queued_next_job); @@ -49,7 +49,7 @@ void create_jobs_task(void * pvParameters) ASIC_jobs_queue_clear(&GLOBAL_STATE->ASIC_jobs_queue); } - STRATUM_V1_free_mining_notify(params); + STRATUM_V1_free_mining_notify(mining_notification); } } diff --git a/main/tasks/stratum_task.c b/main/tasks/stratum_task.c index 623a7c08..0a3bf62c 100644 --- a/main/tasks/stratum_task.c +++ b/main/tasks/stratum_task.c @@ -5,7 +5,7 @@ #include "work_queue.h" #include "bm1397.h" #include "global_state.h" - +#include "stratum_task.h" #define PORT CONFIG_STRATUM_PORT #define STRATUM_URL CONFIG_STRATUM_URL @@ -19,6 +19,9 @@ static bool bDNSFound = false; static StratumApiV1Message stratum_api_v1_message = {}; +static SystemTaskModule SYSTEM_TASK_MODULE = { + .stratum_difficulty = 8192 +}; void dns_found_cb(const char * name, const ip_addr_t * ipaddr, void * callback_arg) { @@ -96,7 +99,7 @@ void stratum_task(void * pvParameters) if (stratum_api_v1_message.method == MINING_NOTIFY) { //ESP_LOGI(TAG, "Mining Notify"); - if (stratum_api_v1_message.should_abandon_work && GLOBAL_STATE->stratum_queue.count > 0) { + if (stratum_api_v1_message.should_abandon_work) { ESP_LOGI(TAG, "abandoning work"); GLOBAL_STATE->abandon_work = 1; @@ -114,14 +117,13 @@ void stratum_task(void * pvParameters) STRATUM_V1_free_mining_notify(next_notify_json_str); } - stratum_api_v1_message.mining_notification->difficulty = GLOBAL_STATE->stratum_difficulty; + stratum_api_v1_message.mining_notification->difficulty = SYSTEM_TASK_MODULE.stratum_difficulty; queue_enqueue(&GLOBAL_STATE->stratum_queue, stratum_api_v1_message.mining_notification); } else if (stratum_api_v1_message.method == MINING_SET_DIFFICULTY) { - if (stratum_api_v1_message.new_difficulty != GLOBAL_STATE->stratum_difficulty) { - GLOBAL_STATE->stratum_difficulty = stratum_api_v1_message.new_difficulty; - ESP_LOGI(TAG, "Set stratum difficulty: %d", GLOBAL_STATE->stratum_difficulty); - BM1397_set_job_difficulty_mask(GLOBAL_STATE->stratum_difficulty); + if (stratum_api_v1_message.new_difficulty != SYSTEM_TASK_MODULE.stratum_difficulty) { + SYSTEM_TASK_MODULE.stratum_difficulty = stratum_api_v1_message.new_difficulty; + ESP_LOGI(TAG, "Set stratum difficulty: %d", SYSTEM_TASK_MODULE.stratum_difficulty); } } else if (stratum_api_v1_message.method == MINING_SET_VERSION_MASK) { diff --git a/main/tasks/stratum_task.h b/main/tasks/stratum_task.h index 1956bca3..efe257a4 100644 --- a/main/tasks/stratum_task.h +++ b/main/tasks/stratum_task.h @@ -1,6 +1,10 @@ #ifndef STRATUM_TASK_H_ #define STRATUM_TASK_H_ +typedef struct { + uint32_t stratum_difficulty; +} SystemTaskModule; + void stratum_task(void * pvParameters); #endif \ No newline at end of file