diff options
| author | Your Name <you@example.com> | 2026-05-19 14:25:18 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-19 14:25:18 +0530 |
| commit | e366ceb336550a72c76efea4c98a2a08cca27bce (patch) | |
| tree | 4b45ac6f6e97b6763f81aa6d4a9b968d23e41235 /tests/unit/test_mining_payment.c | |
| parent | 163b8badec9359373a8fc016c2b1fe9ee38e6406 (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_mining_payment.c')
| -rw-r--r-- | tests/unit/test_mining_payment.c | 92 |
1 files changed, 92 insertions, 0 deletions
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 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/mining_payment.h" | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <math.h> | ||
| 6 | |||
| 7 | int main(void) | ||
| 8 | { | ||
| 9 | printf("=== test_mining_payment ===\n"); | ||
| 10 | |||
| 11 | mining_payment_init(); | ||
| 12 | |||
| 13 | printf("\n--- mining_nbits_to_difficulty ---\n"); | ||
| 14 | uint64_t d1 = mining_nbits_to_difficulty(0); | ||
| 15 | ASSERT(d1 == UINT64_MAX, "nbits=0 returns max"); | ||
| 16 | |||
| 17 | uint64_t d2 = mining_nbits_to_difficulty(0x170309E2); | ||
| 18 | ASSERT(d2 > 0, "mainnet nbits gives positive difficulty"); | ||
| 19 | printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2); | ||
| 20 | |||
| 21 | uint64_t d3 = mining_nbits_to_difficulty(0x1d00ffff); | ||
| 22 | ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1"); | ||
| 23 | |||
| 24 | printf("\n--- mining_calculate_hashprice ---\n"); | ||
| 25 | double hp1 = mining_calculate_hashprice(0); | ||
| 26 | ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0"); | ||
| 27 | |||
| 28 | double hp2 = mining_calculate_hashprice(0x170309E2); | ||
| 29 | ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice"); | ||
| 30 | printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2); | ||
| 31 | |||
| 32 | printf("\n--- mining_calculate_hashprice_override ---\n"); | ||
| 33 | double hp3 = mining_calculate_hashprice_override(1000); | ||
| 34 | ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value"); | ||
| 35 | |||
| 36 | printf("\n--- mining_set_current_nbits ---\n"); | ||
| 37 | mining_set_current_nbits(0x170309E2); | ||
| 38 | double hp4 = mining_get_current_hashprice(); | ||
| 39 | ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated"); | ||
| 40 | |||
| 41 | printf("\n--- mining_get_or_create_client ---\n"); | ||
| 42 | mining_client_stats_t *c1 = mining_get_or_create_client(0x0A010203); | ||
| 43 | ASSERT(c1 != NULL, "created client 1"); | ||
| 44 | ASSERT(c1->ip == 0x0A010203, "client 1 IP matches"); | ||
| 45 | ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares"); | ||
| 46 | |||
| 47 | mining_client_stats_t *c2 = mining_get_or_create_client(0x0A010204); | ||
| 48 | ASSERT(c2 != NULL, "created client 2"); | ||
| 49 | ASSERT(c2->ip == 0x0A010204, "client 2 IP matches"); | ||
| 50 | |||
| 51 | mining_client_stats_t *c1_again = mining_get_or_create_client(0x0A010203); | ||
| 52 | ASSERT(c1_again == c1, "same IP returns same client"); | ||
| 53 | |||
| 54 | printf("\n--- mining_update_hashrate ---\n"); | ||
| 55 | mining_update_hashrate(0x0A010203, true); | ||
| 56 | mining_update_hashrate(0x0A010203, true); | ||
| 57 | mining_update_hashrate(0x0A010203, false); | ||
| 58 | |||
| 59 | const mining_client_stats_t *c1_stats = mining_get_client_stats(0x0A010203); | ||
| 60 | ASSERT(c1_stats != NULL, "client 1 stats found"); | ||
| 61 | ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted"); | ||
| 62 | ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected"); | ||
| 63 | |||
| 64 | printf("\n--- mining_get_client_stats ---\n"); | ||
| 65 | const mining_client_stats_t *notfound = mining_get_client_stats(0xFFFFFFFF); | ||
| 66 | ASSERT(notfound == NULL, "nonexistent client returns NULL"); | ||
| 67 | |||
| 68 | printf("\n--- mining_shares_to_allotment_ms ---\n"); | ||
| 69 | uint64_t allot1 = mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000); | ||
| 70 | ASSERT(allot1 == 0, "zero hashrate = zero allotment"); | ||
| 71 | |||
| 72 | uint64_t allot2 = mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000); | ||
| 73 | ASSERT(allot2 > 0, "positive hashrate gives positive allotment"); | ||
| 74 | printf(" INFO: 1 GH/s at 100 sat/GH/s/day, 21 sats/60s => %llu ms\n", (unsigned long long)allot2); | ||
| 75 | |||
| 76 | uint64_t allot3 = mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000); | ||
| 77 | ASSERT(allot3 > 0, "10 GH/s at 50 sat gives positive allotment"); | ||
| 78 | printf(" INFO: 10 GH/s at 50 sat/GH/s/day, 10 sats/30s => %llu ms\n", (unsigned long long)allot3); | ||
| 79 | |||
| 80 | printf("\n--- mining_shares_to_allotment_bytes ---\n"); | ||
| 81 | uint64_t ab1 = mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576); | ||
| 82 | ASSERT(ab1 == 0, "zero hashrate = zero bytes"); | ||
| 83 | |||
| 84 | uint64_t ab2 = mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576); | ||
| 85 | ASSERT(ab2 > 0, "positive hashrate gives positive bytes"); | ||
| 86 | |||
| 87 | printf("\n--- mining_validate_share (stub) ---\n"); | ||
| 88 | esp_err_t vr = mining_validate_share(NULL, 0, NULL, 0); | ||
| 89 | ASSERT(vr == ESP_OK, "validate share stub returns OK"); | ||
| 90 | |||
| 91 | TEST_SUMMARY(); | ||
| 92 | } | ||