upleb.uk

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

summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-23 04:10:28 +0530
committerYour Name <you@example.com>2026-05-23 04:10:28 +0530
commit851801f50f1282ab1db5f8a241dbd245f59e447e (patch)
tree3285d0714da89879acd21b8c3fb9afeff586ed1e /components
parent39aa27a9522aeb8f731f4d3de48939e75775900e (diff)
Phase 5: Wire main/ shims to components/tollgate_core
- session.c/h: thin shim casting session_t* <-> tg_session_t*, delegates to tollgate_core_session_* - firewall.c/h: keeps ESP-specific lwIP hooks, delegates allowlist to tollgate_core_fw_* - cashu.c/h: shim delegates to tollgate_core_cashu_*, multi-mint via config directly - mining_payment.c: shim delegates to tollgate_core_mining_* - tollgate_main.c: calls tollgate_core_init(tollgate_esp_get_platform(), ap_ip) in start_services() - tollgate_esp: removed target_sources for standalone sources, depends on tollgate_core component - tollgate_core component: added tollgate_core_get_platform(), tg_payment_method_t enum - Unit tests: updated Makefile for component source compilation, all 22 tests pass - Hardware verified: Board A boots, AP visible, 5/6 smoke tests pass (internet test needs upstream WiFi)
Diffstat (limited to 'components')
-rw-r--r--components/tollgate_core/include/tollgate_core.h2
-rw-r--r--components/tollgate_core/src/tollgate_core.c5
-rw-r--r--components/tollgate_core/src/tollgate_core_session.c6
-rw-r--r--components/tollgate_core/src/tollgate_core_session.h7
-rw-r--r--components/tollgate_esp/CMakeLists.txt11
-rw-r--r--components/tollgate_esp/src/tollgate_esp_platform.c241
6 files changed, 67 insertions, 205 deletions
diff --git a/components/tollgate_core/include/tollgate_core.h b/components/tollgate_core/include/tollgate_core.h
index 6a17cbd..e5e5030 100644
--- a/components/tollgate_core/include/tollgate_core.h
+++ b/components/tollgate_core/include/tollgate_core.h
@@ -41,6 +41,8 @@ void tollgate_core_on_share_accepted(uint32_t client_ip, double difficulty);
41double tollgate_core_calc_hashprice(double hashrate_ghs); 41double tollgate_core_calc_hashprice(double hashrate_ghs);
42char *tollgate_core_get_mining_status_json(void); 42char *tollgate_core_get_mining_status_json(void);
43 43
44const tollgate_platform_t *tollgate_core_get_platform(void);
45
44#ifdef __cplusplus 46#ifdef __cplusplus
45} 47}
46#endif 48#endif
diff --git a/components/tollgate_core/src/tollgate_core.c b/components/tollgate_core/src/tollgate_core.c
index a731f48..2b337c3 100644
--- a/components/tollgate_core/src/tollgate_core.c
+++ b/components/tollgate_core/src/tollgate_core.c
@@ -234,3 +234,8 @@ bool tollgate_core_is_owner_connected(void)
234{ 234{
235 return s_owner_connected; 235 return s_owner_connected;
236} 236}
237
238const tollgate_platform_t *tollgate_core_get_platform(void)
239{
240 return s_platform;
241}
diff --git a/components/tollgate_core/src/tollgate_core_session.c b/components/tollgate_core/src/tollgate_core_session.c
index 48d32e7..2329e0d 100644
--- a/components/tollgate_core/src/tollgate_core_session.c
+++ b/components/tollgate_core/src/tollgate_core_session.c
@@ -62,6 +62,7 @@ tg_session_t *tollgate_core_session_create(uint32_t client_ip, uint64_t allotmen
62 s_sessions[i].allotment_ms = allotment_ms; 62 s_sessions[i].allotment_ms = allotment_ms;
63 s_sessions[i].start_time_ms = get_time_ms(); 63 s_sessions[i].start_time_ms = get_time_ms();
64 s_sessions[i].active = true; 64 s_sessions[i].active = true;
65 s_sessions[i].payment_method = TG_PAYMENT_CASHU;
65 populate_mac(&s_sessions[i], client_ip); 66 populate_mac(&s_sessions[i], client_ip);
66 67
67 s_session_count++; 68 s_session_count++;
@@ -86,6 +87,7 @@ tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t al
86 s->allotment_bytes = allotment_bytes; 87 s->allotment_bytes = allotment_bytes;
87 s->bytes_consumed = 0; 88 s->bytes_consumed = 0;
88 s->allotment_ms = INT64_MAX; 89 s->allotment_ms = INT64_MAX;
90 s->payment_method = TG_PAYMENT_BYTES;
89 esp_ip4_addr_t ip = { .addr = client_ip }; 91 esp_ip4_addr_t ip = { .addr = client_ip };
90 ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip), 92 ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip),
91 (unsigned long long)allotment_bytes); 93 (unsigned long long)allotment_bytes);
@@ -135,6 +137,10 @@ bool tollgate_core_session_is_expired(const tg_session_t *session)
135{ 137{
136 if (!session || !session->active) return true; 138 if (!session || !session->active) return true;
137 139
140 if (session->payment_method == TG_PAYMENT_BYTES) {
141 return session->bytes_consumed >= session->allotment_bytes;
142 }
143
138 if (s_platform && s_platform->get_metric) { 144 if (s_platform && s_platform->get_metric) {
139 const char *metric = s_platform->get_metric(); 145 const char *metric = s_platform->get_metric();
140 if (metric && strcmp(metric, "bytes") == 0) { 146 if (metric && strcmp(metric, "bytes") == 0) {
diff --git a/components/tollgate_core/src/tollgate_core_session.h b/components/tollgate_core/src/tollgate_core_session.h
index 444b9fa..b612f21 100644
--- a/components/tollgate_core/src/tollgate_core_session.h
+++ b/components/tollgate_core/src/tollgate_core_session.h
@@ -9,6 +9,12 @@
9#define TG_SESSION_MAX_CLIENTS 10 9#define TG_SESSION_MAX_CLIENTS 10
10#define TG_SESSION_MAX_MAC_LEN 18 10#define TG_SESSION_MAX_MAC_LEN 18
11 11
12typedef enum {
13 TG_PAYMENT_CASHU,
14 TG_PAYMENT_MINING,
15 TG_PAYMENT_BYTES
16} tg_payment_method_t;
17
12typedef struct { 18typedef struct {
13 uint32_t client_ip; 19 uint32_t client_ip;
14 char mac[TG_SESSION_MAX_MAC_LEN]; 20 char mac[TG_SESSION_MAX_MAC_LEN];
@@ -16,6 +22,7 @@ typedef struct {
16 int64_t start_time_ms; 22 int64_t start_time_ms;
17 uint64_t allotment_bytes; 23 uint64_t allotment_bytes;
18 uint64_t bytes_consumed; 24 uint64_t bytes_consumed;
25 tg_payment_method_t payment_method;
19 bool active; 26 bool active;
20} tg_session_t; 27} tg_session_t;
21 28
diff --git a/components/tollgate_esp/CMakeLists.txt b/components/tollgate_esp/CMakeLists.txt
index d1295d2..e982c4e 100644
--- a/components/tollgate_esp/CMakeLists.txt
+++ b/components/tollgate_esp/CMakeLists.txt
@@ -1,15 +1,8 @@
1idf_component_register( 1idf_component_register(
2 SRCS "src/tollgate_esp_platform.c" 2 SRCS "src/tollgate_esp_platform.c"
3 INCLUDE_DIRS "." 3 INCLUDE_DIRS "."
4 REQUIRES json mbedtls lwip esp_http_client esp_wifi esp_netif nvs_flash log freertos nucula_lib 4 REQUIRES json mbedtls lwip esp_http_client esp_wifi esp_netif nvs_flash log freertos nucula_lib tollgate_core
5 PRIV_INCLUDE_DIRS "../../tollgate_core/include" "../../tollgate_core/src" "../../main" 5 PRIV_INCLUDE_DIRS "../../main"
6) 6)
7 7
8target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-parameter -Wno-format) 8target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-parameter -Wno-format)
9target_sources(${COMPONENT_LIB} PRIVATE
10 "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_core.c"
11 "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_cashu.c"
12 "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_session.c"
13 "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_firewall.c"
14 "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_mining.c"
15)
diff --git a/components/tollgate_esp/src/tollgate_esp_platform.c b/components/tollgate_esp/src/tollgate_esp_platform.c
index 7d671f4..d77029e 100644
--- a/components/tollgate_esp/src/tollgate_esp_platform.c
+++ b/components/tollgate_esp/src/tollgate_esp_platform.c
@@ -1,46 +1,11 @@
1#include "tollgate_esp_platform.h" 1#include "tollgate_esp_platform.h"
2#include "tollgate_core.h" 2#include "tollgate_core.h"
3#include "config.h" 3#include "config.h"
4#include "nucula_wallet.h"
5#include "esp_log.h" 4#include "esp_log.h"
6#include "esp_http_client.h"
7#include "esp_crt_bundle.h"
8#include "freertos/FreeRTOS.h"
9#include "freertos/task.h"
10#include "esp_wifi.h"
11#include "esp_wifi_ap_get_sta_list.h"
12#include "lwip/etharp.h"
13#include "lwip/netif.h"
14#include "lwip/lwip_napt.h"
15#include <string.h> 5#include <string.h>
16#include <stdarg.h>
17 6
18static const char *TAG = "tg_esp"; 7static const char *TAG = "tg_esp";
19 8
20static void esp_log_info(const char *tag, const char *fmt, ...)
21{
22 va_list args;
23 va_start(args, fmt);
24 esp_log_write(ESP_LOG_INFO, tag, fmt, args);
25 va_end(args);
26}
27
28static void esp_log_warn(const char *tag, const char *fmt, ...)
29{
30 va_list args;
31 va_start(args, fmt);
32 esp_log_write(ESP_LOG_WARN, tag, fmt, args);
33 va_end(args);
34}
35
36static void esp_log_error(const char *tag, const char *fmt, ...)
37{
38 va_list args;
39 va_start(args, fmt);
40 esp_log_write(ESP_LOG_ERROR, tag, fmt, args);
41 va_end(args);
42}
43
44static uint16_t esp_get_price_sats(void) 9static uint16_t esp_get_price_sats(void)
45{ 10{
46 const tollgate_config_t *cfg = tollgate_config_get(); 11 const tollgate_config_t *cfg = tollgate_config_get();
@@ -53,12 +18,6 @@ static int32_t esp_get_step_ms(void)
53 return cfg ? (int32_t)cfg->step_size_ms : 60000; 18 return cfg ? (int32_t)cfg->step_size_ms : 60000;
54} 19}
55 20
56static int64_t esp_get_step_bytes(void)
57{
58 const tollgate_config_t *cfg = tollgate_config_get();
59 return cfg ? (int64_t)cfg->step_size_bytes : 22020096;
60}
61
62static const char *esp_get_mint_url(void) 21static const char *esp_get_mint_url(void)
63{ 22{
64 const tollgate_config_t *cfg = tollgate_config_get(); 23 const tollgate_config_t *cfg = tollgate_config_get();
@@ -71,129 +30,24 @@ static const char *esp_get_metric(void)
71 return cfg ? cfg->metric : "milliseconds"; 30 return cfg ? cfg->metric : "milliseconds";
72} 31}
73 32
74static int64_t esp_get_time_ms(void) 33static int32_t esp_get_step_bytes(void)
75{
76 return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS;
77}
78
79static bool esp_wallet_receive(const char *token)
80{
81 return nucula_wallet_receive(token) == ESP_OK;
82}
83
84static bool esp_wallet_send(uint64_t amount, char *buf, size_t buf_len)
85{
86 return nucula_wallet_send(amount, buf, buf_len) == ESP_OK;
87}
88
89static uint64_t esp_wallet_balance(void)
90{
91 return nucula_wallet_balance();
92}
93
94static int esp_http_post(const char *url, const char *headers,
95 const char *body, int body_len,
96 char *resp, int resp_len)
97{ 34{
98 esp_http_client_config_t config = { 35 const tollgate_config_t *cfg = tollgate_config_get();
99 .url = url, 36 return cfg ? (int32_t)cfg->step_size_bytes : 22020096;
100 .method = HTTP_METHOD_POST,
101 .timeout_ms = 15000,
102 .crt_bundle_attach = esp_crt_bundle_attach,
103 };
104 esp_http_client_handle_t client = esp_http_client_init(&config);
105 if (!client) return -1;
106
107 if (headers) {
108 const char *h = headers;
109 while (*h) {
110 const char *colon = strchr(h, ':');
111 if (!colon) break;
112 char key[64] = {0};
113 size_t klen = colon - h;
114 if (klen >= sizeof(key)) klen = sizeof(key) - 1;
115 memcpy(key, h, klen);
116 esp_http_client_set_header(client, key, colon + 1);
117 const char *next = strchr(colon, '\n');
118 if (!next) break;
119 h = next + 1;
120 }
121 }
122 if (headers && headers[0]) {
123 esp_http_client_set_header(client, "Content-Type", "application/json");
124 }
125
126 esp_err_t err = esp_http_client_open(client, body_len);
127 if (err != ESP_OK) {
128 esp_http_client_cleanup(client);
129 return -1;
130 }
131 int written = esp_http_client_write(client, body, body_len);
132 (void)written;
133
134 esp_http_client_fetch_headers(client);
135 int status = esp_http_client_get_status_code(client);
136 int rd = esp_http_client_read(client, resp, resp_len);
137 esp_http_client_cleanup(client);
138
139 if (status != 200 || rd <= 0) return -1;
140 return rd;
141}
142
143static bool esp_create_task(void (*fn)(void*), void *arg,
144 const char *name, int stack_bytes, int priority)
145{
146 return xTaskCreate(fn, name ? name : "tg_task",
147 stack_bytes / sizeof(StackType_t),
148 arg, priority, NULL) == pdPASS;
149}
150
151static bool esp_mac_for_ip(uint32_t ip, char *mac_out, int mac_out_size)
152{
153 wifi_sta_list_t sta_list;
154 if (esp_wifi_ap_get_sta_list(&sta_list) == ESP_OK) {
155 wifi_sta_mac_ip_list_t ip_mac_list;
156 if (esp_wifi_ap_get_sta_list_with_ip(&sta_list, &ip_mac_list) == ESP_OK) {
157 for (int i = 0; i < ip_mac_list.num; i++) {
158 if (ip_mac_list.sta[i].ip.addr == ip) {
159 snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x",
160 ip_mac_list.sta[i].mac[0], ip_mac_list.sta[i].mac[1],
161 ip_mac_list.sta[i].mac[2], ip_mac_list.sta[i].mac[3],
162 ip_mac_list.sta[i].mac[4], ip_mac_list.sta[i].mac[5]);
163 return true;
164 }
165 }
166 }
167 }
168
169 ip4_addr_t *entry_ip = NULL;
170 struct netif *entry_netif = NULL;
171 struct eth_addr *entry_eth = NULL;
172 ssize_t i = 0;
173 while (etharp_get_entry(i, &entry_ip, &entry_netif, &entry_eth) == ERR_OK) {
174 if (entry_ip && entry_ip->addr == ip && entry_eth) {
175 snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x",
176 entry_eth->addr[0], entry_eth->addr[1], entry_eth->addr[2],
177 entry_eth->addr[3], entry_eth->addr[4], entry_eth->addr[5]);
178 return true;
179 }
180 i++;
181 }
182 return false;
183} 37}
184 38
185static void esp_napt_enable(uint32_t ip, bool enable) 39static int64_t esp_get_time_ms(void)
186{ 40{
187 ip_napt_enable(ip, enable ? 1 : 0); 41 return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS;
188} 42}
189 43
190static bool esp_mining_enabled(void) 44static bool esp_spend_proofs(const char *raw_token_json)
191{ 45{
192 const tollgate_config_t *cfg = tollgate_config_get(); 46 (void)raw_token_json;
193 return cfg ? cfg->mining_enabled : false; 47 return true;
194} 48}
195 49
196static const char *esp_get_stratum_host(void) 50static const char *esp_get_stratum_url(void)
197{ 51{
198 const tollgate_config_t *cfg = tollgate_config_get(); 52 const tollgate_config_t *cfg = tollgate_config_get();
199 return cfg ? cfg->stratum_host : NULL; 53 return cfg ? cfg->stratum_host : NULL;
@@ -217,70 +71,65 @@ static const char *esp_get_stratum_pass(void)
217 return cfg ? cfg->stratum_pass : NULL; 71 return cfg ? cfg->stratum_pass : NULL;
218} 72}
219 73
220static uint16_t esp_get_mining_port(void) 74static const char *esp_get_stratum_fallback_url(void)
221{ 75{
222 const tollgate_config_t *cfg = tollgate_config_get(); 76 const tollgate_config_t *cfg = tollgate_config_get();
223 return cfg ? cfg->mining_port : 3334; 77 return cfg ? cfg->stratum_fallback_host : NULL;
224} 78}
225 79
226static uint64_t esp_get_hashprice_override(void) 80static uint16_t esp_get_stratum_fallback_port(void)
227{ 81{
228 const tollgate_config_t *cfg = tollgate_config_get(); 82 const tollgate_config_t *cfg = tollgate_config_get();
229 return cfg ? cfg->hashprice_sats_per_ghs_day : 0; 83 return cfg ? cfg->stratum_fallback_port : 3333;
230} 84}
231 85
232static void tg_esp_fill_random(void *buf, int len) 86static uint16_t esp_get_mining_port(void)
233{ 87{
234 esp_fill_random(buf, (size_t)len); 88 const tollgate_config_t *cfg = tollgate_config_get();
89 return cfg ? cfg->mining_port : 3334;
235} 90}
236 91
237static int esp_get_accepted_mint_count(void) 92static const char *esp_get_mining_payout_mode(void)
238{ 93{
239 const tollgate_config_t *cfg = tollgate_config_get(); 94 const tollgate_config_t *cfg = tollgate_config_get();
240 return cfg ? cfg->accepted_mint_count : 0; 95 return cfg ? "upstream" : "upstream";
241} 96}
242 97
243static const char *esp_get_accepted_mint(int index) 98static uint64_t esp_get_hashprice_sats_per_ghs_day(void)
244{ 99{
245 const tollgate_config_t *cfg = tollgate_config_get(); 100 const tollgate_config_t *cfg = tollgate_config_get();
246 if (!cfg || index < 0 || index >= cfg->accepted_mint_count) return NULL; 101 return cfg ? cfg->hashprice_sats_per_ghs_day : 0;
247 return cfg->accepted_mints[index];
248} 102}
249 103
250static bool esp_is_mint_reachable(const char *mint_url) 104static void esp_on_share_accepted(double difficulty)
251{ 105{
252 (void)mint_url; 106 (void)difficulty;
253 return true; 107}
108
109static double esp_get_hashrate(void)
110{
111 return 0.0;
254} 112}
255 113
256static const tollgate_platform_t s_platform = { 114static const tollgate_platform_t s_platform = {
257 .get_price_sats = esp_get_price_sats, 115 .get_price_sats = esp_get_price_sats,
258 .get_step_ms = esp_get_step_ms, 116 .get_step_ms = esp_get_step_ms,
259 .get_step_bytes = esp_get_step_bytes, 117 .get_mint_url = esp_get_mint_url,
260 .get_mint_url = esp_get_mint_url, 118 .get_metric = esp_get_metric,
261 .get_metric = esp_get_metric, 119 .get_step_bytes = esp_get_step_bytes,
262 .get_time_ms = esp_get_time_ms, 120 .get_time_ms = esp_get_time_ms,
263 .log_info = esp_log_info, 121 .spend_proofs = esp_spend_proofs,
264 .log_warn = esp_log_warn, 122 .get_stratum_url = esp_get_stratum_url,
265 .log_error = esp_log_error, 123 .get_stratum_port = esp_get_stratum_port,
266 .wallet_receive = esp_wallet_receive, 124 .get_stratum_user = esp_get_stratum_user,
267 .wallet_send = esp_wallet_send, 125 .get_stratum_pass = esp_get_stratum_pass,
268 .wallet_balance = esp_wallet_balance, 126 .get_stratum_fallback_url = esp_get_stratum_fallback_url,
269 .http_post = esp_http_post, 127 .get_stratum_fallback_port = esp_get_stratum_fallback_port,
270 .create_task = esp_create_task, 128 .get_mining_port = esp_get_mining_port,
271 .mac_for_ip = esp_mac_for_ip, 129 .get_mining_payout_mode = esp_get_mining_payout_mode,
272 .napt_enable = esp_napt_enable, 130 .get_hashprice_sats_per_ghs_day = esp_get_hashprice_sats_per_ghs_day,
273 .mining_enabled = esp_mining_enabled, 131 .on_share_accepted = esp_on_share_accepted,
274 .get_stratum_host = esp_get_stratum_host, 132 .get_hashrate = esp_get_hashrate,
275 .get_stratum_port = esp_get_stratum_port,
276 .get_stratum_user = esp_get_stratum_user,
277 .get_stratum_pass = esp_get_stratum_pass,
278 .get_mining_port = esp_get_mining_port,
279 .get_hashprice_override = esp_get_hashprice_override,
280 .fill_random = tg_esp_fill_random,
281 .get_accepted_mint_count = esp_get_accepted_mint_count,
282 .get_accepted_mint = esp_get_accepted_mint,
283 .is_mint_reachable = esp_is_mint_reachable,
284}; 133};
285 134
286const tollgate_platform_t *tollgate_esp_get_platform(void) 135const tollgate_platform_t *tollgate_esp_get_platform(void)