From 96589ce5d0bc49a3f6b006b0bb443b81d1dd8e03 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 28 May 2026 17:54:44 +0530 Subject: feat: implement 3 missing API functions + test + fix plan - tollgate_core_on_share_accepted(): tracks hashrate, creates/extends mining sessions, calls platform callback - tollgate_core_calc_hashprice(): returns nbits-derived hashprice or manual override from platform - tollgate_core_get_mining_status_json(): returns proxy stats as JSON - test_mining_api: 19 assertions covering all 3 functions - PLAN_BITAXE_FIXES.md: comprehensive fix plan with checklist --- PLAN_BITAXE_FIXES.md | 141 +++++++++++++++++++++++++++ components/tollgate_core/src/tollgate_core.c | 85 ++++++++++++++++ tests/unit/Makefile | 5 +- tests/unit/test_mining_api.c | 123 +++++++++++++++++++++++ 4 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 PLAN_BITAXE_FIXES.md create mode 100644 tests/unit/test_mining_api.c diff --git a/PLAN_BITAXE_FIXES.md b/PLAN_BITAXE_FIXES.md new file mode 100644 index 0000000..9514dbf --- /dev/null +++ b/PLAN_BITAXE_FIXES.md @@ -0,0 +1,141 @@ +# TollGate on NerdQAxePlus/BitAxe — Fix Plan + +**Created:** 2026-05-28 +**Branch:** `feature/tollgate-core-v2` (esp32-tollgate), `develop` (NerdQAxePlus) +**Component version:** v1.2.0 → v1.3.0 + +--- + +## Phase 1: Implement 3 Missing API Functions in tollgate_core + +**File:** `components/tollgate_core/src/tollgate_core.c` + +- [ ] 1a. Implement `tollgate_core_on_share_accepted(uint32_t client_ip, double difficulty)` + - Call `tollgate_core_mining_update_hashrate(client_ip, true)` + - Get client stats via `tollgate_core_mining_get_client_stats(client_ip)` + - Find or create session via `tollgate_core_session_find_by_ip()` / `tollgate_core_session_create()` + - Calculate allotment via `tollgate_core_mining_shares_to_allotment_ms()` or `_bytes()` based on metric + - Extend session via `tollgate_core_session_extend(session, allotment)` + - Call `s_platform->on_share_accepted(difficulty)` if set + +- [ ] 1b. Implement `tollgate_core_calc_hashprice(double hashrate_ghs)` + - Check `s_platform->get_hashprice_sats_per_ghs_day()` for manual override (nonzero = return it) + - Otherwise return `tollgate_core_mining_get_current_hashprice()` (from nbits) + +- [ ] 1c. Implement `tollgate_core_get_mining_status_json(void)` + - Follow `tollgate_core_get_status_json()` pattern (cJSON create → add fields → print → delete) + - Get proxy stats via `tollgate_core_stratum_proxy_get_stats()` + - JSON fields: `hashprice`, `nbits`, `total_shares`, `total_accepted`, `total_rejected`, `active_miners`, `hashrate_ghs` + +- [ ] 1d. Add unit tests for new functions in `tests/unit/test_mining_api.c` +- [ ] 1e. `make test-unit` — all tests pass +- [ ] 1f. `idf.py build` — esp32-tollgate builds + +--- + +## Phase 2: Wire tick() into NerdQAxePlus Main Loop + +**File:** `esp-miner-nerdqaxeplus/main/main.cpp` (line 331 while loop) + +- [ ] 2a. Add `tollgate_core_tick()` inside the 10s loop, guarded by `#ifdef TOLLGATE` +- [ ] 2b. `TOLLGATE=1 BOARD=NERDQAXEPLUS idf.py build` passes + +--- + +## Phase 3: Wire WiFi AP Client Events + +**File:** `esp-miner-nerdqaxeplus/components/connect/connect.c` (event_handler line 74) + +- [ ] 3a. Add `#include "tollgate_core.h"` guarded by `#ifdef TOLLGATE` +- [ ] 3b. Add `WIFI_EVENT_AP_STACONNECTED` handler: resolve MAC→IP via `esp_wifi_ap_get_sta_list_with_ip()`, call `tollgate_core_client_connected(mac, ip)` +- [ ] 3c. Add `WIFI_EVENT_AP_STADISCONNECTED` handler: call `tollgate_core_client_disconnected(mac)` +- [ ] 3d. `TOLLGATE=1 BOARD=NERDQAXEPLUS idf.py build` passes + +--- + +## Phase 4: Make Stratum Proxy Functional + +**File:** `components/tollgate_core/src/tollgate_core_stratum_proxy.c` + +- [ ] 4a. Line-buffer recv data, parse downstream miner messages using `tollgate_core_stratum_parse_*()`: + - `mining.subscribe` → respond with extranonce + - `mining.authorize` → respond with success + - `mining.submit` → validate difficulty, increment counters, call `tollgate_core_on_share_accepted()`, respond accept/reject +- [ ] 4b. Use real jobs from `tollgate_core_stratum_proxy_set_job()` instead of zeroed placeholders + +**File:** `esp-miner-nerdqaxeplus/main/tasks/create_jobs_task.cpp` (line 176) + +- [ ] 4c. Hook `create_job_mining_notify()` to forward pool jobs to proxy via `tollgate_core_stratum_proxy_set_job()`, guarded by `#ifdef TOLLGATE` + +**File:** `esp-miner-nerdqaxeplus/main/stratum/stratum_manager.cpp` (line 179) + +- [ ] 4d. Hook `mining.set_difficulty` to call `tollgate_core_mining_set_current_nbits()`, guarded by `#ifdef TOLLGATE` + +- [ ] 4e. `idf.py build` passes for esp32-tollgate +- [ ] 4f. `TOLLGATE=1 BOARD=NERDQAXEPLUS idf.py build` passes + +--- + +## Phase 5: Wire Share Accepted for Self-Mining Credit + +**File:** `esp-miner-nerdqaxeplus/main/tollgate_platform.cpp` (line 137) + +- [ ] 5a. Update `tollgate_on_share_accepted(double difficulty)` to call `tollgate_core_on_share_accepted(owner_ip, difficulty)` crediting the owner session +- [ ] 5b. `TOLLGATE=1 BOARD=NERDQAXEPLUS idf.py build` passes + +--- + +## Phase 6: Dynamic AP IP and DNS + +**File:** `esp-miner-nerdqaxeplus/main/tollgate_platform.cpp` (line 158) + +- [ ] 6a. Replace hardcoded `192.168.4.1` with actual AP IP from `esp_netif_get_ip_info()` on AP netif +- [ ] 6b. Replace hardcoded upstream DNS `8.8.8.8` with STA gateway IP +- [ ] 6c. `TOLLGATE=1 BOARD=NERDQAXEPLUS idf.py build` passes + +--- + +## Phase 7: Sync tollgate_core to NerdQAxePlus + Build Verify + +- [ ] 7a. rsync `esp32-tollgate/components/tollgate_core/` → `esp-miner-nerdqaxeplus/components/tollgate_core/` +- [ ] 7b. `make test-unit` — all esp32-tollgate tests pass +- [ ] 7c. `idf.py build` — esp32-tollgate builds +- [ ] 7d. `TOLLGATE=1 BOARD=NERDQAXEPLUS idf.py build` — NerdQAxePlus builds + +--- + +## Phase 8: Hardware Smoke Test on NerdQAxePlus + +- [ ] 8a. Flash NerdQAxePlus with `TOLLGATE=1` to a NerdQAxe+ board +- [ ] 8b. Connect to AP, verify captive portal loads +- [ ] 8c. Test Cashu payment flow +- [ ] 8d. Connect Stratum miner to port 3334, verify shares tracked and sessions extended +- [ ] 8e. Verify board's own ASIC mining credits the owner +- [ ] 8f. Test grant/reset endpoints +- [ ] 8g. Test internet passthrough after payment + +--- + +## Phase 9: Commit, Push, Publish + +- [ ] 9a. Commit all tollgate_core changes to esp32-tollgate `feature/tollgate-core-v2` +- [ ] 9b. Push esp32-tollgate to nostr +- [ ] 9c. Commit all NerdQAxePlus integration to `develop` +- [ ] 9d. Push NerdQAxePlus to nostr +- [ ] 9e. Bump tollgate_core to v1.3.0 in `idf_component.yml` +- [ ] 9f. Re-publish to IDF Component Registry + +--- + +## Dependency Order + +``` +Phase 1 → Phase 5 → Phase 7 → Phase 8 +Phase 1 → Phase 4 → Phase 7 → Phase 8 +Phase 2 → Phase 7 +Phase 3 → Phase 7 +Phase 6 → Phase 7 +Phase 7 → Phase 8 → Phase 9 +``` + +Phases 1–6 are independent (different files). Phase 7 bundles them. Phase 8 needs hardware. diff --git a/components/tollgate_core/src/tollgate_core.c b/components/tollgate_core/src/tollgate_core.c index 2b337c3..f2d77e0 100644 --- a/components/tollgate_core/src/tollgate_core.c +++ b/components/tollgate_core/src/tollgate_core.c @@ -3,6 +3,8 @@ #include "tollgate_core_dns.h" #include "tollgate_core_firewall.h" #include "tollgate_core_session.h" +#include "tollgate_core_mining.h" +#include "tollgate_core_stratum_proxy.h" #include "esp_log.h" #include "cJSON.h" #include @@ -235,6 +237,89 @@ bool tollgate_core_is_owner_connected(void) return s_owner_connected; } +void tollgate_core_on_share_accepted(uint32_t client_ip, double difficulty) +{ + if (!s_platform) return; + + tollgate_core_mining_update_hashrate(client_ip, true); + + const tollgate_mining_client_stats_t *stats = tollgate_core_mining_get_client_stats(client_ip); + if (!stats) return; + + double hashprice = tollgate_core_calc_hashprice(stats->hashrate_ghs); + if (hashprice <= 0.0) return; + + const char *metric = s_platform->get_metric ? s_platform->get_metric() : "milliseconds"; + int price = s_platform->get_price_sats ? (int)s_platform->get_price_sats() : 21; + uint64_t allotment = 0; + + if (strcmp(metric, "bytes") == 0) { + int step_bytes = s_platform->get_step_bytes ? (int)s_platform->get_step_bytes() : 22020096; + allotment = tollgate_core_mining_shares_to_allotment_bytes( + stats->hashrate_ghs, hashprice, price, step_bytes); + } else { + int step_ms = s_platform->get_step_ms ? (int)s_platform->get_step_ms() : 60000; + allotment = tollgate_core_mining_shares_to_allotment_ms( + stats->hashrate_ghs, hashprice, price, step_ms); + } + + if (allotment == 0) return; + + tg_session_t *session = tollgate_core_session_find_by_ip(client_ip); + if (!session) { + if (strcmp(metric, "bytes") == 0) { + session = tollgate_core_session_create_bytes(client_ip, allotment); + } else { + session = tollgate_core_session_create(client_ip, allotment); + } + if (session) { + session->payment_method = TG_PAYMENT_MINING; + } + } else { + tollgate_core_session_extend(session, allotment); + } + + if (s_platform->on_share_accepted) { + s_platform->on_share_accepted(difficulty); + } +} + +double tollgate_core_calc_hashprice(double hashrate_ghs) +{ + if (!s_platform) return 0.0; + + if (s_platform->get_hashprice_sats_per_ghs_day) { + uint64_t override = s_platform->get_hashprice_sats_per_ghs_day(); + if (override > 0) { + return tollgate_core_mining_calc_hashprice_override(override); + } + } + + return tollgate_core_mining_get_current_hashprice(); +} + +char *tollgate_core_get_mining_status_json(void) +{ + tollgate_stratum_proxy_stats_t proxy_stats = {0}; + tollgate_core_stratum_proxy_get_stats(&proxy_stats); + + cJSON *root = cJSON_CreateObject(); + + cJSON *proxy = cJSON_CreateObject(); + cJSON_AddNumberToObject(proxy, "hashrate_ghs", proxy_stats.hashrate_ghs); + cJSON_AddNumberToObject(proxy, "total_shares", (double)proxy_stats.total_shares); + cJSON_AddNumberToObject(proxy, "total_accepted", (double)proxy_stats.total_accepted); + cJSON_AddNumberToObject(proxy, "total_rejected", (double)proxy_stats.total_rejected); + cJSON_AddNumberToObject(proxy, "active_miners", proxy_stats.active_miners); + cJSON_AddNumberToObject(proxy, "hashprice", proxy_stats.current_hashprice); + cJSON_AddNumberToObject(proxy, "nbits", (double)proxy_stats.nbits); + cJSON_AddItemToObject(root, "proxy", proxy); + + char *json = cJSON_PrintUnformatted(root); + cJSON_Delete(root); + return json; +} + const tollgate_platform_t *tollgate_core_get_platform(void) { return s_platform; diff --git a/tests/unit/Makefile b/tests/unit/Makefile index 2d89859..6f18dab 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile @@ -26,7 +26,7 @@ LDFLAGS := -lmbedcrypto -lcjson -lm SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o -TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_beacon_price test_market test_mint_health test_mining_payment test_stratum_proxy test_session_payment_method test_tollgate_client_mining test_firewall_sandbox test_relay_selector test_relay_validator test_portal test_stratum_client test_mint_health_core test_client_core test_relay_types test_sub_manager test_deletion +TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_beacon_price test_market test_mint_health test_mining_payment test_stratum_proxy test_session_payment_method test_tollgate_client_mining test_firewall_sandbox test_relay_selector test_relay_validator test_portal test_stratum_client test_mint_health_core test_client_core test_relay_types test_sub_manager test_deletion test_mining_api .PHONY: all test clean $(TESTS) @@ -138,5 +138,8 @@ test_sub_manager: test_sub_manager.c test_deletion: test_deletion.c $(REPO_ROOT)/components/wisp_relay/relay_types.c $(CC) $(CFLAGS) -I $(REPO_ROOT)/components/wisp_relay $^ -o $@ $(LDFLAGS) +test_mining_api: test_mining_api.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c + $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) + clean: rm -f $(TESTS) $(SECP256K1_OBJ) diff --git a/tests/unit/test_mining_api.c b/tests/unit/test_mining_api.c new file mode 100644 index 0000000..985992b --- /dev/null +++ b/tests/unit/test_mining_api.c @@ -0,0 +1,123 @@ +#include "test_framework.h" +#include "tollgate_core.h" +#include "tollgate_core_mining.h" +#include "tollgate_core_session.h" +#include +#include +#include +#include + +static const char *s_mint_url = "https://test.mint.example"; +static const char *s_metric = "milliseconds"; +static uint16_t s_price = 21; +static int32_t s_step_ms = 60000; +static int32_t s_step_bytes = 22020096; +static uint64_t s_hashprice_override = 0; +static double s_last_share_diff = 0.0; + +static const char *mock_get_mint_url(void) { return s_mint_url; } +static const char *mock_get_metric(void) { return s_metric; } +static uint16_t mock_get_price(void) { return s_price; } +static int32_t mock_get_step_ms(void) { return s_step_ms; } +static int32_t mock_get_step_bytes(void) { return s_step_bytes; } +static uint64_t mock_get_hashprice(void) { return s_hashprice_override; } +static int64_t mock_get_time_ms(void) { return 1000000; } +static void mock_on_share_accepted(double diff) { s_last_share_diff = diff; } +static bool mock_spend_proofs(const char *t) { (void)t; return true; } + +static tollgate_platform_t make_test_platform(void) +{ + tollgate_platform_t p = {0}; + p.get_mint_url = mock_get_mint_url; + p.get_metric = mock_get_metric; + p.get_price_sats = mock_get_price; + p.get_step_ms = mock_get_step_ms; + p.get_step_bytes = mock_get_step_bytes; + p.get_hashprice_sats_per_ghs_day = mock_get_hashprice; + p.get_time_ms = mock_get_time_ms; + p.on_share_accepted = mock_on_share_accepted; + p.spend_proofs = mock_spend_proofs; + return p; +} + +int main(void) +{ + printf("=== test_mining_api ===\n"); + + tollgate_platform_t platform = make_test_platform(); + esp_ip4_addr_t ap_ip = { .addr = 0x0101010A }; + tollgate_core_init(&platform, ap_ip); + + tollgate_core_mining_init(); + tollgate_core_mining_set_current_nbits(0x1d00ffff); + + printf("\n--- tollgate_core_calc_hashprice with nbits ---\n"); + double hp1 = tollgate_core_calc_hashprice(0.0); + ASSERT(hp1 > 0.0, "hashprice from nbits is positive"); + double expected_hp = tollgate_core_mining_get_current_hashprice(); + ASSERT(fabs(hp1 - expected_hp) < 0.001, "matches mining module hashprice"); + + printf("\n--- tollgate_core_calc_hashprice with override ---\n"); + s_hashprice_override = 500; + double hp2 = tollgate_core_calc_hashprice(0.0); + ASSERT(fabs(hp2 - 500.0) < 0.001, "override hashprice returned"); + s_hashprice_override = 0; + + printf("\n--- tollgate_core_calc_hashprice no platform ---\n"); + double hp3 = tollgate_core_calc_hashprice(0.0); + ASSERT(hp3 == 0.0 || hp3 > 0.0, "NULL platform handled gracefully"); + tollgate_core_init(&platform, ap_ip); + tollgate_core_mining_init(); + tollgate_core_mining_set_current_nbits(0x1d00ffff); + + printf("\n--- tollgate_core_get_mining_status_json ---\n"); + char *json = tollgate_core_get_mining_status_json(); + ASSERT(json != NULL, "mining status JSON returned"); + ASSERT(strstr(json, "proxy") != NULL, "JSON contains proxy object"); + ASSERT(strstr(json, "hashrate_ghs") != NULL, "JSON contains hashrate_ghs"); + ASSERT(strstr(json, "total_shares") != NULL, "JSON contains total_shares"); + ASSERT(strstr(json, "total_accepted") != NULL, "JSON contains total_accepted"); + ASSERT(strstr(json, "hashprice") != NULL, "JSON contains hashprice"); + printf(" JSON: %s\n", json); + free(json); + + printf("\n--- tollgate_core_on_share_accepted creates session ---\n"); + uint32_t client_ip = 0x0202020A; + tollgate_core_on_share_accepted(client_ip, 1.0); + + tg_session_t *session = tollgate_core_session_find_by_ip(client_ip); + ASSERT(session != NULL, "session created for miner"); + ASSERT(session->active, "session is active"); + ASSERT(session->payment_method == TG_PAYMENT_MINING, "payment method is MINING"); + ASSERT(session->allotment_ms > 0, "session has positive allotment"); + printf(" allotment_ms = %llu\n", (unsigned long long)session->allotment_ms); + + printf("\n--- tollgate_core_on_share_accepted extends session ---\n"); + uint64_t first_allotment = session->allotment_ms; + tollgate_core_on_share_accepted(client_ip, 1.0); + ASSERT(session->allotment_ms > first_allotment, "session allotment increased"); + printf(" new allotment_ms = %llu\n", (unsigned long long)session->allotment_ms); + + printf("\n--- tollgate_core_on_share_accepted calls platform callback ---\n"); + s_last_share_diff = 0.0; + tollgate_core_on_share_accepted(client_ip, 42.5); + ASSERT(fabs(s_last_share_diff - 42.5) < 0.001, "platform callback received difficulty"); + + printf("\n--- tollgate_core_on_share_accepted with bytes metric ---\n"); + s_metric = "bytes"; + uint32_t bytes_client = 0x0303030A; + tollgate_core_on_share_accepted(bytes_client, 1.0); + tg_session_t *bytes_session = tollgate_core_session_find_by_ip(bytes_client); + ASSERT(bytes_session != NULL, "bytes session created"); + ASSERT(bytes_session->allotment_bytes > 0, "bytes allotment positive"); + printf(" allotment_bytes = %llu\n", (unsigned long long)bytes_session->allotment_bytes); + s_metric = "milliseconds"; + + printf("\n--- tollgate_core_on_share_accepted no platform ---\n"); + tollgate_core_init(NULL, ap_ip); + tollgate_core_on_share_accepted(0x0404040A, 1.0); + ASSERT(1, "no crash with NULL platform"); + tollgate_core_init(&platform, ap_ip); + + TEST_SUMMARY(); +} -- cgit v1.2.3