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 --- main/mint_health.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- main/tollgate_api.c | 29 ++++++++++++++++++++++++++++- main/tollgate_api.h | 1 + 3 files changed, 78 insertions(+), 2 deletions(-) (limited to 'main') 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 -- cgit v1.2.3