diff options
Diffstat (limited to 'main/sw_miner.c')
| -rw-r--r-- | main/sw_miner.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/main/sw_miner.c b/main/sw_miner.c new file mode 100644 index 0000000..cdd98a0 --- /dev/null +++ b/main/sw_miner.c | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #include "sw_miner.h" | ||
| 2 | #include "stratum_proxy.h" | ||
| 3 | #include "stratum_client.h" | ||
| 4 | #include "mining_payment.h" | ||
| 5 | #include "config.h" | ||
| 6 | #include "esp_log.h" | ||
| 7 | #include "esp_random.h" | ||
| 8 | #include "mbedtls/sha256.h" | ||
| 9 | #include "freertos/FreeRTOS.h" | ||
| 10 | #include "freertos/task.h" | ||
| 11 | #include <string.h> | ||
| 12 | |||
| 13 | static const char *TAG = "sw_miner"; | ||
| 14 | static bool s_running = false; | ||
| 15 | static TaskHandle_t s_task_handle = NULL; | ||
| 16 | static double s_hashrate = 0.0; | ||
| 17 | |||
| 18 | static void sha256d(const uint8_t *data, size_t len, uint8_t *hash) | ||
| 19 | { | ||
| 20 | uint8_t tmp[32]; | ||
| 21 | mbedtls_sha256(data, len, tmp, 0); | ||
| 22 | mbedtls_sha256(tmp, 32, hash, 0); | ||
| 23 | } | ||
| 24 | |||
| 25 | static void sw_miner_task(void *arg) | ||
| 26 | { | ||
| 27 | ESP_LOGI(TAG, "Software miner started"); | ||
| 28 | |||
| 29 | uint64_t hashes = 0; | ||
| 30 | int64_t start_time = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; | ||
| 31 | |||
| 32 | uint8_t header[80]; | ||
| 33 | uint8_t hash[32]; | ||
| 34 | |||
| 35 | while (s_running) { | ||
| 36 | const stratum_job_t *job = stratum_proxy_get_current_job(); | ||
| 37 | if (!job || !job->valid) { | ||
| 38 | vTaskDelay(pdMS_TO_TICKS(1000)); | ||
| 39 | continue; | ||
| 40 | } | ||
| 41 | |||
| 42 | stratum_job_t local_job; | ||
| 43 | memcpy(&local_job, job, sizeof(stratum_job_t)); | ||
| 44 | |||
| 45 | memcpy(header, local_job.prevhash, 32); | ||
| 46 | memcpy(header + 32, local_job.merkle_root, 32); | ||
| 47 | |||
| 48 | uint32_t start_nonce = esp_random(); | ||
| 49 | uint32_t end_nonce = start_nonce + 1000; | ||
| 50 | |||
| 51 | for (uint32_t nonce = start_nonce; nonce < end_nonce && s_running; nonce++) { | ||
| 52 | header[76] = (nonce >> 0) & 0xFF; | ||
| 53 | header[77] = (nonce >> 8) & 0xFF; | ||
| 54 | header[78] = (nonce >> 16) & 0xFF; | ||
| 55 | header[79] = (nonce >> 24) & 0xFF; | ||
| 56 | |||
| 57 | sha256d(header, 80, hash); | ||
| 58 | hashes++; | ||
| 59 | |||
| 60 | if (memcmp(hash, local_job.target, local_job.target_len) <= 0) { | ||
| 61 | ESP_LOGI(TAG, "Valid share found! nonce=%08lx", (unsigned long)nonce); | ||
| 62 | stratum_client_submit_share(local_job.job_id, nonce, local_job.ntime, local_job.version); | ||
| 63 | mining_update_hashrate(0, true); | ||
| 64 | break; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | int64_t now = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; | ||
| 69 | int64_t elapsed_s = (now - start_time) / 1000; | ||
| 70 | if (elapsed_s > 0) { | ||
| 71 | s_hashrate = (double)hashes / (double)elapsed_s / 1e6; | ||
| 72 | } | ||
| 73 | |||
| 74 | taskYIELD(); | ||
| 75 | } | ||
| 76 | |||
| 77 | vTaskDelete(NULL); | ||
| 78 | } | ||
| 79 | |||
| 80 | esp_err_t sw_miner_start(void) | ||
| 81 | { | ||
| 82 | if (s_running) return ESP_OK; | ||
| 83 | s_running = true; | ||
| 84 | s_hashrate = 0.0; | ||
| 85 | |||
| 86 | BaseType_t ret = xTaskCreate(sw_miner_task, "sw_miner", 8192, NULL, 2, &s_task_handle); | ||
| 87 | if (ret != pdPASS) { | ||
| 88 | ESP_LOGE(TAG, "Failed to create sw_miner task"); | ||
| 89 | s_running = false; | ||
| 90 | return ESP_FAIL; | ||
| 91 | } | ||
| 92 | return ESP_OK; | ||
| 93 | } | ||
| 94 | |||
| 95 | void sw_miner_stop(void) | ||
| 96 | { | ||
| 97 | s_running = false; | ||
| 98 | if (s_task_handle) { | ||
| 99 | vTaskDelay(pdMS_TO_TICKS(500)); | ||
| 100 | s_task_handle = NULL; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | bool sw_miner_is_running(void) | ||
| 105 | { | ||
| 106 | return s_running; | ||
| 107 | } | ||
| 108 | |||
| 109 | double sw_miner_get_hashrate(void) | ||
| 110 | { | ||
| 111 | return s_hashrate; | ||
| 112 | } | ||