upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-21 16:46:51 +0530
committerYour Name <you@example.com>2026-05-21 16:46:51 +0530
commit19e175b1c1dea54efb61e8040ffdfa973fbac5d5 (patch)
treef41ca977a993cc24f8bf136e96370c8097ceeaf1
parent243e4808246555f86d464d1c476681a902e644ff (diff)
Wallet receive via health task queue (no separate TLS worker)
- 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
-rw-r--r--components/nucula_lib/nucula_wallet.cpp2
-rw-r--r--main/mint_health.c50
-rw-r--r--main/tollgate_api.c29
-rw-r--r--main/tollgate_api.h1
-rw-r--r--tests/unit/stubs/freertos/FreeRTOS.h5
-rw-r--r--tests/unit/stubs/freertos/queue.h37
-rw-r--r--tests/unit/stubs/tollgate_api.h11
-rwxr-xr-xtests/unit/test_beacon_pricebin0 -> 35744 bytes
-rwxr-xr-xtests/unit/test_displaybin0 -> 24816 bytes
-rwxr-xr-xtests/unit/test_firewall_sandboxbin0 -> 30568 bytes
-rwxr-xr-xtests/unit/test_keyboardbin0 -> 35416 bytes
-rwxr-xr-xtests/unit/test_marketbin0 -> 49456 bytes
-rwxr-xr-xtests/unit/test_mining_paymentbin0 -> 28664 bytes
-rw-r--r--tests/unit/test_mint_health.c5
-rwxr-xr-xtests/unit/test_negentropy_adapterbin0 -> 21216 bytes
-rwxr-xr-xtests/unit/test_session_payment_methodbin0 -> 54680 bytes
-rwxr-xr-xtests/unit/test_stratum_proxybin0 -> 40784 bytes
-rwxr-xr-xtests/unit/test_tollgate_client_miningbin0 -> 48448 bytes
-rwxr-xr-xtests/unit/test_touchbin0 -> 30304 bytes
-rwxr-xr-xtests/unit/test_wifi_setupbin0 -> 33336 bytes
20 files changed, 137 insertions, 3 deletions
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)
188 return ESP_FAIL; 188 return ESP_FAIL;
189 } 189 }
190 190
191 vTaskDelay(pdMS_TO_TICKS(2000)); 191 vTaskDelay(pdMS_TO_TICKS(100));
192 192
193 std::vector<cashu::Proof> proofs_out; 193 std::vector<cashu::Proof> proofs_out;
194 if (!w->receive(tok, proofs_out)) { 194 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 @@
5#include "freertos/FreeRTOS.h" 5#include "freertos/FreeRTOS.h"
6#include "freertos/task.h" 6#include "freertos/task.h"
7#include "freertos/semphr.h" 7#include "freertos/semphr.h"
8#include "freertos/queue.h"
9#include "nucula_wallet.h"
10#include "tollgate_api.h"
8#include <string.h> 11#include <string.h>
9#include <stdlib.h> 12#include <stdlib.h>
10 13
11static const char *TAG = "mint_health"; 14static const char *TAG = "mint_health";
12 15
16#define WALLET_QUEUE_LEN 8
17static QueueHandle_t s_wallet_queue = NULL;
18
13static int s_last_probe_err = 0; 19static int s_last_probe_err = 0;
14 20
15static mint_status_t s_mints[MINT_HEALTH_MAX]; 21static mint_status_t s_mints[MINT_HEALTH_MAX];
@@ -163,16 +169,54 @@ static void run_initial_probes(void)
163 fire_callbacks(); 169 fire_callbacks();
164} 170}
165 171
172static void process_wallet_queue(void)
173{
174 char *token;
175 while (s_wallet_queue && xQueueReceive(s_wallet_queue, &token, 0) == pdTRUE) {
176 if (!token) continue;
177 ESP_LOGI(TAG, "Processing wallet receive (%zu bytes)", strlen(token));
178 esp_err_t err = nucula_wallet_receive(token);
179 if (err == ESP_OK) {
180 ESP_LOGI(TAG, "Wallet receive OK, balance=%llu",
181 (unsigned long long)nucula_wallet_balance());
182 } else {
183 ESP_LOGW(TAG, "Wallet receive failed");
184 }
185 free(token);
186 }
187}
188
166static void health_task(void *pvParameters) 189static void health_task(void *pvParameters)
167{ 190{
168 ESP_LOGI(TAG, "Health probe task started, waiting for DNS to stabilize..."); 191 ESP_LOGI(TAG, "Health probe task started, waiting for DNS to stabilize...");
169 vTaskDelay(pdMS_TO_TICKS(5000)); 192 vTaskDelay(pdMS_TO_TICKS(5000));
170 run_initial_probes(); 193 run_initial_probes();
194 process_wallet_queue();
171 195
172 while (s_running) { 196 while (s_running) {
173 vTaskDelay(pdMS_TO_TICKS(MINT_HEALTH_PROBE_INTERVAL_S * 1000)); 197 TickType_t start = xTaskGetTickCount();
198 while (s_running) {
199 TickType_t elapsed = (xTaskGetTickCount() - start) * portTICK_PERIOD_MS;
200 if (elapsed >= MINT_HEALTH_PROBE_INTERVAL_S * 1000) break;
201
202 char *token = NULL;
203 if (s_wallet_queue && xQueueReceive(s_wallet_queue, &token, pdMS_TO_TICKS(1000)) == pdTRUE) {
204 if (token) {
205 ESP_LOGI(TAG, "Processing wallet receive (%zu bytes)", strlen(token));
206 esp_err_t err = nucula_wallet_receive(token);
207 if (err == ESP_OK) {
208 ESP_LOGI(TAG, "Wallet receive OK, balance=%llu",
209 (unsigned long long)nucula_wallet_balance());
210 } else {
211 ESP_LOGW(TAG, "Wallet receive failed");
212 }
213 free(token);
214 }
215 }
216 }
174 if (!s_running) break; 217 if (!s_running) break;
175 run_probes(); 218 run_probes();
219 process_wallet_queue();
176 } 220 }
177 221
178 s_task_handle = NULL; 222 s_task_handle = NULL;
@@ -183,6 +227,10 @@ void mint_health_start(void)
183{ 227{
184 if (s_running) return; 228 if (s_running) return;
185 s_running = true; 229 s_running = true;
230
231 s_wallet_queue = xQueueCreate(WALLET_QUEUE_LEN, sizeof(char *));
232 tls_worker_set_queue(s_wallet_queue);
233
186 xTaskCreate(health_task, "mint_health", 16384, NULL, 3, &s_task_handle); 234 xTaskCreate(health_task, "mint_health", 16384, NULL, 3, &s_task_handle);
187} 235}
188 236
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 @@
19#include "lwip/sockets.h" 19#include "lwip/sockets.h"
20#include "lwip/netdb.h" 20#include "lwip/netdb.h"
21#include "freertos/task.h" 21#include "freertos/task.h"
22#include "freertos/queue.h"
22#include <string.h> 23#include <string.h>
23 24
24static const char *TAG = "tollgate_api"; 25static const char *TAG = "tollgate_api";
25static httpd_handle_t s_api_server = NULL; 26static httpd_handle_t s_api_server = NULL;
26 27
28static QueueHandle_t s_wallet_queue = NULL;
29
30void tls_worker_set_queue(QueueHandle_t q)
31{
32 s_wallet_queue = q;
33}
34
35static void tls_worker_submit(const char *token)
36{
37 if (!s_wallet_queue) {
38 ESP_LOGW(TAG, "No wallet queue, receiving synchronously");
39 nucula_wallet_receive(token);
40 return;
41 }
42
43 char *copy = strdup(token);
44 if (!copy) return;
45
46 if (xQueueSend(s_wallet_queue, &copy, pdMS_TO_TICKS(1000)) != pdTRUE) {
47 ESP_LOGW(TAG, "Wallet queue full, receiving synchronously");
48 nucula_wallet_receive(copy);
49 free(copy);
50 }
51}
52
27static esp_err_t get_client_ip(httpd_req_t *req, uint32_t *ip_out) 53static esp_err_t get_client_ip(httpd_req_t *req, uint32_t *ip_out)
28{ 54{
29 int sockfd = httpd_req_to_sockfd(req); 55 int sockfd = httpd_req_to_sockfd(req);
@@ -343,7 +369,7 @@ static esp_err_t api_post_payment(httpd_req_t *req)
343 cJSON_free(json); 369 cJSON_free(json);
344 cJSON_Delete(session_event); 370 cJSON_Delete(session_event);
345 371
346 nucula_wallet_receive(body_copy); 372 tls_worker_submit(body_copy);
347 373
348 free(states); 374 free(states);
349 free(token); 375 free(token);
@@ -820,6 +846,7 @@ esp_err_t tollgate_api_start(void)
820 } 846 }
821 847
822 ESP_LOGI(TAG, "TollGate API started on port 2121"); 848 ESP_LOGI(TAG, "TollGate API started on port 2121");
849
823 return ESP_OK; 850 return ESP_OK;
824} 851}
825 852
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 @@
6 6
7esp_err_t tollgate_api_start(void); 7esp_err_t tollgate_api_start(void);
8void tollgate_api_stop(void); 8void tollgate_api_stop(void);
9void tls_worker_set_queue(QueueHandle_t q);
9 10
10#endif 11#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 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5 5
6typedef int32_t BaseType_t;
7typedef uint32_t UBaseType_t;
8typedef uint32_t TickType_t;
9
6static inline uint32_t xTaskGetTickCount(void) { return 0; } 10static inline uint32_t xTaskGetTickCount(void) { return 0; }
7static inline void vTaskDelay(uint32_t ticks) { (void)ticks; } 11static inline void vTaskDelay(uint32_t ticks) { (void)ticks; }
8#define pdMS_TO_TICKS(ms) ((ms) / 10) 12#define pdMS_TO_TICKS(ms) ((ms) / 10)
9#define portTICK_PERIOD_MS 10 13#define portTICK_PERIOD_MS 10
10#define portMAX_DELAY 0xFFFFFFFF 14#define portMAX_DELAY 0xFFFFFFFF
11#define pdTRUE 1 15#define pdTRUE 1
16#define pdFALSE 0
12 17
13#endif 18#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 @@
1#ifndef STUB_FREERTOS_QUEUE_H
2#define STUB_FREERTOS_QUEUE_H
3
4#include <stddef.h>
5#include "freertos/FreeRTOS.h"
6
7typedef void *QueueHandle_t;
8
9static inline QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength, UBaseType_t uxItemSize)
10{
11 (void)uxQueueLength;
12 (void)uxItemSize;
13 return (QueueHandle_t)1;
14}
15
16static inline BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait)
17{
18 (void)xQueue;
19 (void)pvItemToQueue;
20 (void)xTicksToWait;
21 return pdTRUE;
22}
23
24static inline BaseType_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait)
25{
26 (void)xQueue;
27 (void)pvBuffer;
28 (void)xTicksToWait;
29 return pdFALSE;
30}
31
32static inline void vQueueDelete(QueueHandle_t xQueue)
33{
34 (void)xQueue;
35}
36
37#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 @@
1#ifndef STUB_TOLLGATE_API_H
2#define STUB_TOLLGATE_API_H
3
4#include "freertos/queue.h"
5
6static inline void tls_worker_set_queue(QueueHandle_t q)
7{
8 (void)q;
9}
10
11#endif
diff --git a/tests/unit/test_beacon_price b/tests/unit/test_beacon_price
new file mode 100755
index 0000000..a9f6357
--- /dev/null
+++ b/tests/unit/test_beacon_price
Binary files differ
diff --git a/tests/unit/test_display b/tests/unit/test_display
new file mode 100755
index 0000000..9b8364e
--- /dev/null
+++ b/tests/unit/test_display
Binary files differ
diff --git a/tests/unit/test_firewall_sandbox b/tests/unit/test_firewall_sandbox
new file mode 100755
index 0000000..3e2895b
--- /dev/null
+++ b/tests/unit/test_firewall_sandbox
Binary files differ
diff --git a/tests/unit/test_keyboard b/tests/unit/test_keyboard
new file mode 100755
index 0000000..61cc9f5
--- /dev/null
+++ b/tests/unit/test_keyboard
Binary files differ
diff --git a/tests/unit/test_market b/tests/unit/test_market
new file mode 100755
index 0000000..8efac8e
--- /dev/null
+++ b/tests/unit/test_market
Binary files differ
diff --git a/tests/unit/test_mining_payment b/tests/unit/test_mining_payment
new file mode 100755
index 0000000..d38bf9d
--- /dev/null
+++ b/tests/unit/test_mining_payment
Binary files 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 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <string.h> 2#include <string.h>
3#include <assert.h> 3#include <assert.h>
4#include "esp_err.h"
4#include "mint_health.h" 5#include "mint_health.h"
5 6
7esp_err_t nucula_wallet_receive(const char *token_str) { (void)token_str; return ESP_OK; }
8uint64_t nucula_wallet_balance(void) { return 0; }
9void tls_worker_set_queue(void *q) { (void)q; }
10
6static int test_count = 0; 11static int test_count = 0;
7static int pass_count = 0; 12static int pass_count = 0;
8 13
diff --git a/tests/unit/test_negentropy_adapter b/tests/unit/test_negentropy_adapter
new file mode 100755
index 0000000..64b6053
--- /dev/null
+++ b/tests/unit/test_negentropy_adapter
Binary files differ
diff --git a/tests/unit/test_session_payment_method b/tests/unit/test_session_payment_method
new file mode 100755
index 0000000..950a72f
--- /dev/null
+++ b/tests/unit/test_session_payment_method
Binary files differ
diff --git a/tests/unit/test_stratum_proxy b/tests/unit/test_stratum_proxy
new file mode 100755
index 0000000..963df67
--- /dev/null
+++ b/tests/unit/test_stratum_proxy
Binary files differ
diff --git a/tests/unit/test_tollgate_client_mining b/tests/unit/test_tollgate_client_mining
new file mode 100755
index 0000000..d331bd1
--- /dev/null
+++ b/tests/unit/test_tollgate_client_mining
Binary files differ
diff --git a/tests/unit/test_touch b/tests/unit/test_touch
new file mode 100755
index 0000000..43c8790
--- /dev/null
+++ b/tests/unit/test_touch
Binary files differ
diff --git a/tests/unit/test_wifi_setup b/tests/unit/test_wifi_setup
new file mode 100755
index 0000000..aa0e0b4
--- /dev/null
+++ b/tests/unit/test_wifi_setup
Binary files differ