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_mining_payment.c | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/unit/test_mining_payment.c (limited to 'tests/unit/test_mining_payment.c') diff --git a/tests/unit/test_mining_payment.c b/tests/unit/test_mining_payment.c new file mode 100644 index 0000000..c3834fd --- /dev/null +++ b/tests/unit/test_mining_payment.c @@ -0,0 +1,92 @@ +#include "test_framework.h" +#include "../../main/mining_payment.h" +#include +#include +#include + +int main(void) +{ + printf("=== test_mining_payment ===\n"); + + mining_payment_init(); + + printf("\n--- mining_nbits_to_difficulty ---\n"); + uint64_t d1 = mining_nbits_to_difficulty(0); + ASSERT(d1 == UINT64_MAX, "nbits=0 returns max"); + + uint64_t d2 = mining_nbits_to_difficulty(0x170309E2); + ASSERT(d2 > 0, "mainnet nbits gives positive difficulty"); + printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2); + + uint64_t d3 = mining_nbits_to_difficulty(0x1d00ffff); + ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1"); + + printf("\n--- mining_calculate_hashprice ---\n"); + double hp1 = mining_calculate_hashprice(0); + ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0"); + + double hp2 = mining_calculate_hashprice(0x170309E2); + ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice"); + printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2); + + printf("\n--- mining_calculate_hashprice_override ---\n"); + double hp3 = mining_calculate_hashprice_override(1000); + ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value"); + + printf("\n--- mining_set_current_nbits ---\n"); + mining_set_current_nbits(0x170309E2); + double hp4 = mining_get_current_hashprice(); + ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated"); + + printf("\n--- mining_get_or_create_client ---\n"); + mining_client_stats_t *c1 = mining_get_or_create_client(0x0A010203); + ASSERT(c1 != NULL, "created client 1"); + ASSERT(c1->ip == 0x0A010203, "client 1 IP matches"); + ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares"); + + mining_client_stats_t *c2 = mining_get_or_create_client(0x0A010204); + ASSERT(c2 != NULL, "created client 2"); + ASSERT(c2->ip == 0x0A010204, "client 2 IP matches"); + + mining_client_stats_t *c1_again = mining_get_or_create_client(0x0A010203); + ASSERT(c1_again == c1, "same IP returns same client"); + + printf("\n--- mining_update_hashrate ---\n"); + mining_update_hashrate(0x0A010203, true); + mining_update_hashrate(0x0A010203, true); + mining_update_hashrate(0x0A010203, false); + + const mining_client_stats_t *c1_stats = mining_get_client_stats(0x0A010203); + ASSERT(c1_stats != NULL, "client 1 stats found"); + ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted"); + ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected"); + + printf("\n--- mining_get_client_stats ---\n"); + const mining_client_stats_t *notfound = mining_get_client_stats(0xFFFFFFFF); + ASSERT(notfound == NULL, "nonexistent client returns NULL"); + + printf("\n--- mining_shares_to_allotment_ms ---\n"); + uint64_t allot1 = mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000); + ASSERT(allot1 == 0, "zero hashrate = zero allotment"); + + uint64_t allot2 = mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000); + ASSERT(allot2 > 0, "positive hashrate gives positive allotment"); + printf(" INFO: 1 GH/s at 100 sat/GH/s/day, 21 sats/60s => %llu ms\n", (unsigned long long)allot2); + + uint64_t allot3 = mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000); + ASSERT(allot3 > 0, "10 GH/s at 50 sat gives positive allotment"); + printf(" INFO: 10 GH/s at 50 sat/GH/s/day, 10 sats/30s => %llu ms\n", (unsigned long long)allot3); + + printf("\n--- mining_shares_to_allotment_bytes ---\n"); + uint64_t ab1 = mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576); + ASSERT(ab1 == 0, "zero hashrate = zero bytes"); + + uint64_t ab2 = mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576); + ASSERT(ab2 > 0, "positive hashrate gives positive bytes"); + + printf("\n--- mining_validate_share (stub) ---\n"); + esp_err_t vr = mining_validate_share(NULL, 0, NULL, 0); + ASSERT(vr == ESP_OK, "validate share stub returns OK"); + + TEST_SUMMARY(); +} -- cgit v1.2.3