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 14:25:18 +0530
committerYour Name <you@example.com>2026-05-19 14:25:18 +0530
commite366ceb336550a72c76efea4c98a2a08cca27bce (patch)
tree4b45ac6f6e97b6763f81aa6d4a9b968d23e41235 /main/sw_miner.c
parent163b8badec9359373a8fc016c2b1fe9ee38e6406 (diff)
feat(mining): Bitcoin mining-for-bandwidth payment system
New modules: - mining_payment.c/h: hashprice calc (nbits->difficulty->sat/GH/s/day), share validation, client stats, allotment conversion (ms + bytes) - stratum_client.c/h: SV1 upstream pool connection (subscribe/authorize/submit) - stratum_proxy.c/h: Local SV1 TCP server for downstream miners, job broadcast - sw_miner.c/h: Software SHA256d miner (ESP32 CPU fallback) - asic_miner.c/h: ASIC detection stub (BM1366/BM1368 SPI) Config: - config.h/c: mining_payout_mode_t enum (auto/pool/upstream/proxy_only), stratum pool settings, mining port, hashprice override, sandbox mint access - Defaults fill nostr_seed_relays (8/8) and nostr_relays (4/4) with fast relays Integration into existing modules: - session.h/c: payment_method_t enum (CASHU/MINING/BYTES) - firewall.h/c: firewall_set_mining_port(), firewall_set_sandbox_mint_access() - tollgate_api.c: GET /mining/job, POST /mining/share, GET /mining/stats - tollgate_client.h/c: TG_CLIENT_MINING state, mining discovery tag parsing - tollgate_main.c: mining init in start_services(), stratum_client_tick() in loop - captive_portal.c: tabbed Cashu/Mine UI with live hashrate polling Unit tests (69 new assertions across 4 suites): - test_mining_payment (23 tests): nbits->difficulty, hashprice, client stats, allotment - test_stratum_proxy (21 tests): job set/get, stats, type validation - test_session_payment_method (12 tests): PAYMENT_METHOD enum, bytes/cashu methods - test_tollgate_client_mining (20 tests): mining tag parsing, discovery struct - test_firewall_sandbox (16 tests): client grant/revoke, max clients, setters Enhanced test stubs: - BaseType_t/pdPASS in freertos/task.h - lwip: sockets.h, etharp.h, prot/ip.h, prot/ip4.h, prot/tcp.h, netif.h - dns_server.h, esp_wifi_ap_get_sta_list.h Build fixes: - cvm_server.c: replace esp_timer_get_time() with xTaskGetTickCount(), fix process_relay_message() 3-arg call to 2-arg, add WS keepalive ping - stratum_proxy.c: widen task_name buffer 16->20 - sw_miner.c: add missing #include esp_random.h - nucula_src: save_proofs() moved to public in wallet.hpp Nostr relay updates: - nostr_seed_relays: +relay.anzenkodo.workers.dev, +nostr.koning-degraaf.nl, +knostr.neutrine.com, +nostr.einundzwanzig.space (8/8 slots) - nostr_relays: +relay.anzenkodo.workers.dev, +nostr.koning-degraaf.nl (4/4 slots) Squash-merge of feature/mining-payment (5 commits: c75230e..9d98ba1)
Diffstat (limited to 'main/sw_miner.c')
-rw-r--r--main/sw_miner.c112
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
13static const char *TAG = "sw_miner";
14static bool s_running = false;
15static TaskHandle_t s_task_handle = NULL;
16static double s_hashrate = 0.0;
17
18static 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
25static 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
80esp_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
95void 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
104bool sw_miner_is_running(void)
105{
106 return s_running;
107}
108
109double sw_miner_get_hashrate(void)
110{
111 return s_hashrate;
112}