upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/sw_miner.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-19 04:07:16 +0530
committerYour Name <you@example.com>2026-05-19 04:07:16 +0530
commitc75230e551a778408b2e370b208aff76b74c6560 (patch)
tree0cf51ecae1c469b7e10272d44b37f69ff4cb611f /main/sw_miner.c
parentabee221b0f0e5a4513ab126afbdfddc2728df6be (diff)
feat(mining): add new mining source files and unit tests
- mining_payment.c/h: hashprice calc, share validation, per-client hashrate - stratum_client.c/h: SV1 upstream pool connection - stratum_proxy.c/h: local SV1 TCP server for downstream miners - sw_miner.c/h: software SHA256d miner using mbedtls - asic_miner.c/h: ASIC detection stub (software fallback) - test_mining_payment.c: 23 unit tests for mining payment module
Diffstat (limited to 'main/sw_miner.c')
-rw-r--r--main/sw_miner.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/main/sw_miner.c b/main/sw_miner.c
new file mode 100644
index 0000000..b45e7c5
--- /dev/null
+++ b/main/sw_miner.c
@@ -0,0 +1,111 @@
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 "mbedtls/sha256.h"
8#include "freertos/FreeRTOS.h"
9#include "freertos/task.h"
10#include <string.h>
11
12static const char *TAG = "sw_miner";
13static bool s_running = false;
14static TaskHandle_t s_task_handle = NULL;
15static double s_hashrate = 0.0;
16
17static void sha256d(const uint8_t *data, size_t len, uint8_t *hash)
18{
19 uint8_t tmp[32];
20 mbedtls_sha256(data, len, tmp, 0);
21 mbedtls_sha256(tmp, 32, hash, 0);
22}
23
24static void sw_miner_task(void *arg)
25{
26 ESP_LOGI(TAG, "Software miner started");
27
28 uint64_t hashes = 0;
29 int64_t start_time = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS;
30
31 uint8_t header[80];
32 uint8_t hash[32];
33
34 while (s_running) {
35 const stratum_job_t *job = stratum_proxy_get_current_job();
36 if (!job || !job->valid) {
37 vTaskDelay(pdMS_TO_TICKS(1000));
38 continue;
39 }
40
41 stratum_job_t local_job;
42 memcpy(&local_job, job, sizeof(stratum_job_t));
43
44 memcpy(header, local_job.prevhash, 32);
45 memcpy(header + 32, local_job.merkle_root, 32);
46
47 uint32_t start_nonce = esp_random();
48 uint32_t end_nonce = start_nonce + 1000;
49
50 for (uint32_t nonce = start_nonce; nonce < end_nonce && s_running; nonce++) {
51 header[76] = (nonce >> 0) & 0xFF;
52 header[77] = (nonce >> 8) & 0xFF;
53 header[78] = (nonce >> 16) & 0xFF;
54 header[79] = (nonce >> 24) & 0xFF;
55
56 sha256d(header, 80, hash);
57 hashes++;
58
59 if (memcmp(hash, local_job.target, local_job.target_len) <= 0) {
60 ESP_LOGI(TAG, "Valid share found! nonce=%08lx", (unsigned long)nonce);
61 stratum_client_submit_share(local_job.job_id, nonce, local_job.ntime, local_job.version);
62 mining_update_hashrate(0, true);
63 break;
64 }
65 }
66
67 int64_t now = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS;
68 int64_t elapsed_s = (now - start_time) / 1000;
69 if (elapsed_s > 0) {
70 s_hashrate = (double)hashes / (double)elapsed_s / 1e6;
71 }
72
73 taskYIELD();
74 }
75
76 vTaskDelete(NULL);
77}
78
79esp_err_t sw_miner_start(void)
80{
81 if (s_running) return ESP_OK;
82 s_running = true;
83 s_hashrate = 0.0;
84
85 BaseType_t ret = xTaskCreate(sw_miner_task, "sw_miner", 8192, NULL, 2, &s_task_handle);
86 if (ret != pdPASS) {
87 ESP_LOGE(TAG, "Failed to create sw_miner task");
88 s_running = false;
89 return ESP_FAIL;
90 }
91 return ESP_OK;
92}
93
94void sw_miner_stop(void)
95{
96 s_running = false;
97 if (s_task_handle) {
98 vTaskDelay(pdMS_TO_TICKS(500));
99 s_task_handle = NULL;
100 }
101}
102
103bool sw_miner_is_running(void)
104{
105 return s_running;
106}
107
108double sw_miner_get_hashrate(void)
109{
110 return s_hashrate;
111}