upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/unit/test_stratum_proxy.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 /tests/unit/test_stratum_proxy.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 'tests/unit/test_stratum_proxy.c')
-rw-r--r--tests/unit/test_stratum_proxy.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/unit/test_stratum_proxy.c b/tests/unit/test_stratum_proxy.c
new file mode 100644
index 0000000..7788911
--- /dev/null
+++ b/tests/unit/test_stratum_proxy.c
@@ -0,0 +1,95 @@
1#include "test_framework.h"
2#include "../../main/stratum_proxy.h"
3#include "../../main/mining_payment.h"
4#include <stdio.h>
5#include <string.h>
6
7int main(void)
8{
9 printf("=== test_stratum_proxy ===\n");
10
11 mining_payment_init();
12
13 printf("\n--- stratum_proxy_set_job / get_current_job ---\n");
14 {
15 stratum_job_t job = {0};
16 job.job_id = 42;
17 job.nbits = 0x170309E2;
18 job.ntime = 0x6789ABCD;
19 job.version = 0x20000000;
20 job.valid = true;
21 memset(job.prevhash, 0xAA, 32);
22 memset(job.merkle_root, 0xBB, 32);
23
24 stratum_proxy_set_job(&job);
25
26 const stratum_job_t *cur = stratum_proxy_get_current_job();
27 ASSERT(cur != NULL, "current job not NULL");
28 ASSERT_EQ_INT(42, (int)cur->job_id, "job_id=42");
29 ASSERT_EQ_INT(0x170309E2, (int)cur->nbits, "nbits preserved");
30 ASSERT_EQ_INT(0x6789ABCD, (int)cur->ntime, "ntime preserved");
31 ASSERT_EQ_INT(0x20000000, (int)cur->version, "version preserved");
32 ASSERT(cur->valid, "job is valid");
33 ASSERT_MEM_EQ(job.prevhash, cur->prevhash, 32, "prevhash preserved");
34 }
35
36 printf("\n--- stratum_proxy_set_job (NULL) ---\n");
37 {
38 stratum_proxy_set_job(NULL);
39 const stratum_job_t *cur = stratum_proxy_get_current_job();
40 ASSERT_EQ_INT(42, (int)cur->job_id, "job unchanged after NULL set");
41 }
42
43 printf("\n--- stratum_proxy_get_stats ---\n");
44 {
45 mining_set_current_nbits(0x170309E2);
46 stratum_proxy_set_job(&(stratum_job_t){
47 .job_id = 1,
48 .nbits = 0x170309E2,
49 .valid = true
50 });
51
52 stratum_proxy_stats_t stats;
53 memset(&stats, 0xFF, sizeof(stats));
54 stratum_proxy_get_stats(&stats);
55
56 ASSERT(stats.current_hashprice > 0.0, "hashprice populated from mining_payment");
57 ASSERT_EQ_INT(0x170309E2, (int)stats.nbits, "nbits in stats");
58 }
59
60 printf("\n--- stratum_proxy_get_stats (NULL) ---\n");
61 {
62 stratum_proxy_get_stats(NULL);
63 ASSERT(true, "get_stats with NULL does not crash");
64 }
65
66 printf("\n--- stratum_job_t initialization ---\n");
67 {
68 stratum_job_t zero = {0};
69 ASSERT(!zero.valid, "zero-initialized job is invalid");
70 ASSERT_EQ_INT(0, (int)zero.job_id, "zero job_id");
71 ASSERT_EQ_INT(0, (int)zero.nbits, "zero nbits");
72 }
73
74 printf("\n--- stratum_proxy_stats_t initialization ---\n");
75 {
76 stratum_proxy_stats_t zero = {0};
77 ASSERT(zero.hashrate_ghs == 0.0, "zero hashrate");
78 ASSERT_EQ_INT(0, (int)zero.active_miners, "zero active miners");
79 ASSERT_EQ_UINT64(0, zero.total_shares, "zero total shares");
80 ASSERT_EQ_UINT64(0, zero.total_accepted, "zero total accepted");
81 ASSERT_EQ_UINT64(0, zero.total_rejected, "zero total rejected");
82 }
83
84 printf("\n--- STRATUM_MAX_JOBS constant ---\n");
85 {
86 ASSERT(STRATUM_MAX_JOBS >= 1, "STRATUM_MAX_JOBS >= 1");
87 }
88
89 printf("\n--- STRATUM_MAX_JOB_ID_LEN constant ---\n");
90 {
91 ASSERT(STRATUM_MAX_JOB_ID_LEN >= 16, "STRATUM_MAX_JOB_ID_LEN >= 16");
92 }
93
94 TEST_SUMMARY();
95}