From e366ceb336550a72c76efea4c98a2a08cca27bce Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 19 May 2026 14:25:18 +0530 Subject: 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) --- tests/unit/test_stratum_proxy.c | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/unit/test_stratum_proxy.c (limited to 'tests/unit/test_stratum_proxy.c') 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 @@ +#include "test_framework.h" +#include "../../main/stratum_proxy.h" +#include "../../main/mining_payment.h" +#include +#include + +int main(void) +{ + printf("=== test_stratum_proxy ===\n"); + + mining_payment_init(); + + printf("\n--- stratum_proxy_set_job / get_current_job ---\n"); + { + stratum_job_t job = {0}; + job.job_id = 42; + job.nbits = 0x170309E2; + job.ntime = 0x6789ABCD; + job.version = 0x20000000; + job.valid = true; + memset(job.prevhash, 0xAA, 32); + memset(job.merkle_root, 0xBB, 32); + + stratum_proxy_set_job(&job); + + const stratum_job_t *cur = stratum_proxy_get_current_job(); + ASSERT(cur != NULL, "current job not NULL"); + ASSERT_EQ_INT(42, (int)cur->job_id, "job_id=42"); + ASSERT_EQ_INT(0x170309E2, (int)cur->nbits, "nbits preserved"); + ASSERT_EQ_INT(0x6789ABCD, (int)cur->ntime, "ntime preserved"); + ASSERT_EQ_INT(0x20000000, (int)cur->version, "version preserved"); + ASSERT(cur->valid, "job is valid"); + ASSERT_MEM_EQ(job.prevhash, cur->prevhash, 32, "prevhash preserved"); + } + + printf("\n--- stratum_proxy_set_job (NULL) ---\n"); + { + stratum_proxy_set_job(NULL); + const stratum_job_t *cur = stratum_proxy_get_current_job(); + ASSERT_EQ_INT(42, (int)cur->job_id, "job unchanged after NULL set"); + } + + printf("\n--- stratum_proxy_get_stats ---\n"); + { + mining_set_current_nbits(0x170309E2); + stratum_proxy_set_job(&(stratum_job_t){ + .job_id = 1, + .nbits = 0x170309E2, + .valid = true + }); + + stratum_proxy_stats_t stats; + memset(&stats, 0xFF, sizeof(stats)); + stratum_proxy_get_stats(&stats); + + ASSERT(stats.current_hashprice > 0.0, "hashprice populated from mining_payment"); + ASSERT_EQ_INT(0x170309E2, (int)stats.nbits, "nbits in stats"); + } + + printf("\n--- stratum_proxy_get_stats (NULL) ---\n"); + { + stratum_proxy_get_stats(NULL); + ASSERT(true, "get_stats with NULL does not crash"); + } + + printf("\n--- stratum_job_t initialization ---\n"); + { + stratum_job_t zero = {0}; + ASSERT(!zero.valid, "zero-initialized job is invalid"); + ASSERT_EQ_INT(0, (int)zero.job_id, "zero job_id"); + ASSERT_EQ_INT(0, (int)zero.nbits, "zero nbits"); + } + + printf("\n--- stratum_proxy_stats_t initialization ---\n"); + { + stratum_proxy_stats_t zero = {0}; + ASSERT(zero.hashrate_ghs == 0.0, "zero hashrate"); + ASSERT_EQ_INT(0, (int)zero.active_miners, "zero active miners"); + ASSERT_EQ_UINT64(0, zero.total_shares, "zero total shares"); + ASSERT_EQ_UINT64(0, zero.total_accepted, "zero total accepted"); + ASSERT_EQ_UINT64(0, zero.total_rejected, "zero total rejected"); + } + + printf("\n--- STRATUM_MAX_JOBS constant ---\n"); + { + ASSERT(STRATUM_MAX_JOBS >= 1, "STRATUM_MAX_JOBS >= 1"); + } + + printf("\n--- STRATUM_MAX_JOB_ID_LEN constant ---\n"); + { + ASSERT(STRATUM_MAX_JOB_ID_LEN >= 16, "STRATUM_MAX_JOB_ID_LEN >= 16"); + } + + TEST_SUMMARY(); +} -- cgit v1.2.3