From 19e175b1c1dea54efb61e8040ffdfa973fbac5d5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 21 May 2026 16:46:51 +0530 Subject: Wallet receive via health task queue (no separate TLS worker) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Removed standalone TLS worker task (couldn't allocate 16KB internal stack) - Added wallet receive queue to mint_health task (already has 16KB stack + working TLS) - health_task processes wallet tokens between probe intervals (1s poll) - tls_worker_set_queue() registers queue from mint_health_start() - Fallback to synchronous receive if queue not available - Added freertos/queue.h and tollgate_api.h stubs for unit tests - Added BaseType_t/UBaseType_t/TickType_t/pdFALSE to FreeRTOS stub - Full payment round-trip confirmed: 2 payments → 41 sat balance --- components/nucula_lib/nucula_wallet.cpp | 2 +- main/mint_health.c | 50 +++++++++++++++++++++++++++++++- main/tollgate_api.c | 29 +++++++++++++++++- main/tollgate_api.h | 1 + tests/unit/stubs/freertos/FreeRTOS.h | 5 ++++ tests/unit/stubs/freertos/queue.h | 37 +++++++++++++++++++++++ tests/unit/stubs/tollgate_api.h | 11 +++++++ tests/unit/test_beacon_price | Bin 0 -> 35744 bytes tests/unit/test_display | Bin 0 -> 24816 bytes tests/unit/test_firewall_sandbox | Bin 0 -> 30568 bytes tests/unit/test_keyboard | Bin 0 -> 35416 bytes tests/unit/test_market | Bin 0 -> 49456 bytes tests/unit/test_mining_payment | Bin 0 -> 28664 bytes tests/unit/test_mint_health.c | 5 ++++ tests/unit/test_negentropy_adapter | Bin 0 -> 21216 bytes tests/unit/test_session_payment_method | Bin 0 -> 54680 bytes tests/unit/test_stratum_proxy | Bin 0 -> 40784 bytes tests/unit/test_tollgate_client_mining | Bin 0 -> 48448 bytes tests/unit/test_touch | Bin 0 -> 30304 bytes tests/unit/test_wifi_setup | Bin 0 -> 33336 bytes 20 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 tests/unit/stubs/freertos/queue.h create mode 100644 tests/unit/stubs/tollgate_api.h create mode 100755 tests/unit/test_beacon_price create mode 100755 tests/unit/test_display create mode 100755 tests/unit/test_firewall_sandbox create mode 100755 tests/unit/test_keyboard create mode 100755 tests/unit/test_market create mode 100755 tests/unit/test_mining_payment create mode 100755 tests/unit/test_negentropy_adapter create mode 100755 tests/unit/test_session_payment_method create mode 100755 tests/unit/test_stratum_proxy create mode 100755 tests/unit/test_tollgate_client_mining create mode 100755 tests/unit/test_touch create mode 100755 tests/unit/test_wifi_setup diff --git a/components/nucula_lib/nucula_wallet.cpp b/components/nucula_lib/nucula_wallet.cpp index 278c9bc..cd63741 100644 --- a/components/nucula_lib/nucula_wallet.cpp +++ b/components/nucula_lib/nucula_wallet.cpp @@ -188,7 +188,7 @@ esp_err_t nucula_wallet_receive(const char *token_str) return ESP_FAIL; } - vTaskDelay(pdMS_TO_TICKS(2000)); + vTaskDelay(pdMS_TO_TICKS(100)); std::vector proofs_out; if (!w->receive(tok, proofs_out)) { diff --git a/main/mint_health.c b/main/mint_health.c index a551295..4ed8a19 100644 --- a/main/mint_health.c +++ b/main/mint_health.c @@ -5,11 +5,17 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" +#include "freertos/queue.h" +#include "nucula_wallet.h" +#include "tollgate_api.h" #include #include static const char *TAG = "mint_health"; +#define WALLET_QUEUE_LEN 8 +static QueueHandle_t s_wallet_queue = NULL; + static int s_last_probe_err = 0; static mint_status_t s_mints[MINT_HEALTH_MAX]; @@ -163,16 +169,54 @@ static void run_initial_probes(void) fire_callbacks(); } +static void process_wallet_queue(void) +{ + char *token; + while (s_wallet_queue && xQueueReceive(s_wallet_queue, &token, 0) == pdTRUE) { + if (!token) continue; + ESP_LOGI(TAG, "Processing wallet receive (%zu bytes)", strlen(token)); + esp_err_t err = nucula_wallet_receive(token); + if (err == ESP_OK) { + ESP_LOGI(TAG, "Wallet receive OK, balance=%llu", + (unsigned long long)nucula_wallet_balance()); + } else { + ESP_LOGW(TAG, "Wallet receive failed"); + } + free(token); + } +} + static void health_task(void *pvParameters) { ESP_LOGI(TAG, "Health probe task started, waiting for DNS to stabilize..."); vTaskDelay(pdMS_TO_TICKS(5000)); run_initial_probes(); + process_wallet_queue(); while (s_running) { - vTaskDelay(pdMS_TO_TICKS(MINT_HEALTH_PROBE_INTERVAL_S * 1000)); + TickType_t start = xTaskGetTickCount(); + while (s_running) { + TickType_t elapsed = (xTaskGetTickCount() - start) * portTICK_PERIOD_MS; + if (elapsed >= MINT_HEALTH_PROBE_INTERVAL_S * 1000) break; + + char *token = NULL; + if (s_wallet_queue && xQueueReceive(s_wallet_queue, &token, pdMS_TO_TICKS(1000)) == pdTRUE) { + if (token) { + ESP_LOGI(TAG, "Processing wallet receive (%zu bytes)", strlen(token)); + esp_err_t err = nucula_wallet_receive(token); + if (err == ESP_OK) { + ESP_LOGI(TAG, "Wallet receive OK, balance=%llu", + (unsigned long long)nucula_wallet_balance()); + } else { + ESP_LOGW(TAG, "Wallet receive failed"); + } + free(token); + } + } + } if (!s_running) break; run_probes(); + process_wallet_queue(); } s_task_handle = NULL; @@ -183,6 +227,10 @@ void mint_health_start(void) { if (s_running) return; s_running = true; + + s_wallet_queue = xQueueCreate(WALLET_QUEUE_LEN, sizeof(char *)); + tls_worker_set_queue(s_wallet_queue); + xTaskCreate(health_task, "mint_health", 16384, NULL, 3, &s_task_handle); } diff --git a/main/tollgate_api.c b/main/tollgate_api.c index 7f2102e..be753ea 100644 --- a/main/tollgate_api.c +++ b/main/tollgate_api.c @@ -19,11 +19,37 @@ #include "lwip/sockets.h" #include "lwip/netdb.h" #include "freertos/task.h" +#include "freertos/queue.h" #include static const char *TAG = "tollgate_api"; static httpd_handle_t s_api_server = NULL; +static QueueHandle_t s_wallet_queue = NULL; + +void tls_worker_set_queue(QueueHandle_t q) +{ + s_wallet_queue = q; +} + +static void tls_worker_submit(const char *token) +{ + if (!s_wallet_queue) { + ESP_LOGW(TAG, "No wallet queue, receiving synchronously"); + nucula_wallet_receive(token); + return; + } + + char *copy = strdup(token); + if (!copy) return; + + if (xQueueSend(s_wallet_queue, ©, pdMS_TO_TICKS(1000)) != pdTRUE) { + ESP_LOGW(TAG, "Wallet queue full, receiving synchronously"); + nucula_wallet_receive(copy); + free(copy); + } +} + static esp_err_t get_client_ip(httpd_req_t *req, uint32_t *ip_out) { int sockfd = httpd_req_to_sockfd(req); @@ -343,7 +369,7 @@ static esp_err_t api_post_payment(httpd_req_t *req) cJSON_free(json); cJSON_Delete(session_event); - nucula_wallet_receive(body_copy); + tls_worker_submit(body_copy); free(states); free(token); @@ -820,6 +846,7 @@ esp_err_t tollgate_api_start(void) } ESP_LOGI(TAG, "TollGate API started on port 2121"); + return ESP_OK; } diff --git a/main/tollgate_api.h b/main/tollgate_api.h index 23e0d75..2af4b8c 100644 --- a/main/tollgate_api.h +++ b/main/tollgate_api.h @@ -6,5 +6,6 @@ esp_err_t tollgate_api_start(void); void tollgate_api_stop(void); +void tls_worker_set_queue(QueueHandle_t q); #endif diff --git a/tests/unit/stubs/freertos/FreeRTOS.h b/tests/unit/stubs/freertos/FreeRTOS.h index 2d2b967..543d4e4 100644 --- a/tests/unit/stubs/freertos/FreeRTOS.h +++ b/tests/unit/stubs/freertos/FreeRTOS.h @@ -3,11 +3,16 @@ #include +typedef int32_t BaseType_t; +typedef uint32_t UBaseType_t; +typedef uint32_t TickType_t; + static inline uint32_t xTaskGetTickCount(void) { return 0; } static inline void vTaskDelay(uint32_t ticks) { (void)ticks; } #define pdMS_TO_TICKS(ms) ((ms) / 10) #define portTICK_PERIOD_MS 10 #define portMAX_DELAY 0xFFFFFFFF #define pdTRUE 1 +#define pdFALSE 0 #endif diff --git a/tests/unit/stubs/freertos/queue.h b/tests/unit/stubs/freertos/queue.h new file mode 100644 index 0000000..04aef49 --- /dev/null +++ b/tests/unit/stubs/freertos/queue.h @@ -0,0 +1,37 @@ +#ifndef STUB_FREERTOS_QUEUE_H +#define STUB_FREERTOS_QUEUE_H + +#include +#include "freertos/FreeRTOS.h" + +typedef void *QueueHandle_t; + +static inline QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength, UBaseType_t uxItemSize) +{ + (void)uxQueueLength; + (void)uxItemSize; + return (QueueHandle_t)1; +} + +static inline BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait) +{ + (void)xQueue; + (void)pvItemToQueue; + (void)xTicksToWait; + return pdTRUE; +} + +static inline BaseType_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait) +{ + (void)xQueue; + (void)pvBuffer; + (void)xTicksToWait; + return pdFALSE; +} + +static inline void vQueueDelete(QueueHandle_t xQueue) +{ + (void)xQueue; +} + +#endif diff --git a/tests/unit/stubs/tollgate_api.h b/tests/unit/stubs/tollgate_api.h new file mode 100644 index 0000000..7e20fce --- /dev/null +++ b/tests/unit/stubs/tollgate_api.h @@ -0,0 +1,11 @@ +#ifndef STUB_TOLLGATE_API_H +#define STUB_TOLLGATE_API_H + +#include "freertos/queue.h" + +static inline void tls_worker_set_queue(QueueHandle_t q) +{ + (void)q; +} + +#endif diff --git a/tests/unit/test_beacon_price b/tests/unit/test_beacon_price new file mode 100755 index 0000000..a9f6357 Binary files /dev/null and b/tests/unit/test_beacon_price differ diff --git a/tests/unit/test_display b/tests/unit/test_display new file mode 100755 index 0000000..9b8364e Binary files /dev/null and b/tests/unit/test_display differ diff --git a/tests/unit/test_firewall_sandbox b/tests/unit/test_firewall_sandbox new file mode 100755 index 0000000..3e2895b Binary files /dev/null and b/tests/unit/test_firewall_sandbox differ diff --git a/tests/unit/test_keyboard b/tests/unit/test_keyboard new file mode 100755 index 0000000..61cc9f5 Binary files /dev/null and b/tests/unit/test_keyboard differ diff --git a/tests/unit/test_market b/tests/unit/test_market new file mode 100755 index 0000000..8efac8e Binary files /dev/null and b/tests/unit/test_market differ diff --git a/tests/unit/test_mining_payment b/tests/unit/test_mining_payment new file mode 100755 index 0000000..d38bf9d Binary files /dev/null and b/tests/unit/test_mining_payment differ diff --git a/tests/unit/test_mint_health.c b/tests/unit/test_mint_health.c index d170d55..1cb05d9 100644 --- a/tests/unit/test_mint_health.c +++ b/tests/unit/test_mint_health.c @@ -1,8 +1,13 @@ #include #include #include +#include "esp_err.h" #include "mint_health.h" +esp_err_t nucula_wallet_receive(const char *token_str) { (void)token_str; return ESP_OK; } +uint64_t nucula_wallet_balance(void) { return 0; } +void tls_worker_set_queue(void *q) { (void)q; } + static int test_count = 0; static int pass_count = 0; diff --git a/tests/unit/test_negentropy_adapter b/tests/unit/test_negentropy_adapter new file mode 100755 index 0000000..64b6053 Binary files /dev/null and b/tests/unit/test_negentropy_adapter differ diff --git a/tests/unit/test_session_payment_method b/tests/unit/test_session_payment_method new file mode 100755 index 0000000..950a72f Binary files /dev/null and b/tests/unit/test_session_payment_method differ diff --git a/tests/unit/test_stratum_proxy b/tests/unit/test_stratum_proxy new file mode 100755 index 0000000..963df67 Binary files /dev/null and b/tests/unit/test_stratum_proxy differ diff --git a/tests/unit/test_tollgate_client_mining b/tests/unit/test_tollgate_client_mining new file mode 100755 index 0000000..d331bd1 Binary files /dev/null and b/tests/unit/test_tollgate_client_mining differ diff --git a/tests/unit/test_touch b/tests/unit/test_touch new file mode 100755 index 0000000..43c8790 Binary files /dev/null and b/tests/unit/test_touch differ diff --git a/tests/unit/test_wifi_setup b/tests/unit/test_wifi_setup new file mode 100755 index 0000000..aa0e0b4 Binary files /dev/null and b/tests/unit/test_wifi_setup differ -- cgit v1.2.3