From 851801f50f1282ab1db5f8a241dbd245f59e447e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 23 May 2026 04:10:28 +0530 Subject: 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) --- components/tollgate_core/include/tollgate_core.h | 2 + components/tollgate_core/src/tollgate_core.c | 5 + .../tollgate_core/src/tollgate_core_session.c | 6 + .../tollgate_core/src/tollgate_core_session.h | 7 + components/tollgate_esp/CMakeLists.txt | 11 +- .../tollgate_esp/src/tollgate_esp_platform.c | 241 ++++----------------- 6 files changed, 67 insertions(+), 205 deletions(-) (limited to 'components') 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); double tollgate_core_calc_hashprice(double hashrate_ghs); char *tollgate_core_get_mining_status_json(void); +const tollgate_platform_t *tollgate_core_get_platform(void); + #ifdef __cplusplus } #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) { return s_owner_connected; } + +const tollgate_platform_t *tollgate_core_get_platform(void) +{ + return s_platform; +} 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 s_sessions[i].allotment_ms = allotment_ms; s_sessions[i].start_time_ms = get_time_ms(); s_sessions[i].active = true; + s_sessions[i].payment_method = TG_PAYMENT_CASHU; populate_mac(&s_sessions[i], client_ip); s_session_count++; @@ -86,6 +87,7 @@ tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t al s->allotment_bytes = allotment_bytes; s->bytes_consumed = 0; s->allotment_ms = INT64_MAX; + s->payment_method = TG_PAYMENT_BYTES; esp_ip4_addr_t ip = { .addr = client_ip }; ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip), (unsigned long long)allotment_bytes); @@ -135,6 +137,10 @@ bool tollgate_core_session_is_expired(const tg_session_t *session) { if (!session || !session->active) return true; + if (session->payment_method == TG_PAYMENT_BYTES) { + return session->bytes_consumed >= session->allotment_bytes; + } + if (s_platform && s_platform->get_metric) { const char *metric = s_platform->get_metric(); 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 @@ #define TG_SESSION_MAX_CLIENTS 10 #define TG_SESSION_MAX_MAC_LEN 18 +typedef enum { + TG_PAYMENT_CASHU, + TG_PAYMENT_MINING, + TG_PAYMENT_BYTES +} tg_payment_method_t; + typedef struct { uint32_t client_ip; char mac[TG_SESSION_MAX_MAC_LEN]; @@ -16,6 +22,7 @@ typedef struct { int64_t start_time_ms; uint64_t allotment_bytes; uint64_t bytes_consumed; + tg_payment_method_t payment_method; bool active; } tg_session_t; 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 @@ idf_component_register( SRCS "src/tollgate_esp_platform.c" INCLUDE_DIRS "." - REQUIRES json mbedtls lwip esp_http_client esp_wifi esp_netif nvs_flash log freertos nucula_lib - PRIV_INCLUDE_DIRS "../../tollgate_core/include" "../../tollgate_core/src" "../../main" + REQUIRES json mbedtls lwip esp_http_client esp_wifi esp_netif nvs_flash log freertos nucula_lib tollgate_core + PRIV_INCLUDE_DIRS "../../main" ) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-parameter -Wno-format) -target_sources(${COMPONENT_LIB} PRIVATE - "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_core.c" - "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_cashu.c" - "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_session.c" - "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_firewall.c" - "${CMAKE_CURRENT_SOURCE_DIR}/../../tollgate_core/src/tollgate_mining.c" -) 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 @@ #include "tollgate_esp_platform.h" #include "tollgate_core.h" #include "config.h" -#include "nucula_wallet.h" #include "esp_log.h" -#include "esp_http_client.h" -#include "esp_crt_bundle.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_wifi.h" -#include "esp_wifi_ap_get_sta_list.h" -#include "lwip/etharp.h" -#include "lwip/netif.h" -#include "lwip/lwip_napt.h" #include -#include static const char *TAG = "tg_esp"; -static void esp_log_info(const char *tag, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - esp_log_write(ESP_LOG_INFO, tag, fmt, args); - va_end(args); -} - -static void esp_log_warn(const char *tag, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - esp_log_write(ESP_LOG_WARN, tag, fmt, args); - va_end(args); -} - -static void esp_log_error(const char *tag, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - esp_log_write(ESP_LOG_ERROR, tag, fmt, args); - va_end(args); -} - static uint16_t esp_get_price_sats(void) { const tollgate_config_t *cfg = tollgate_config_get(); @@ -53,12 +18,6 @@ static int32_t esp_get_step_ms(void) return cfg ? (int32_t)cfg->step_size_ms : 60000; } -static int64_t esp_get_step_bytes(void) -{ - const tollgate_config_t *cfg = tollgate_config_get(); - return cfg ? (int64_t)cfg->step_size_bytes : 22020096; -} - static const char *esp_get_mint_url(void) { const tollgate_config_t *cfg = tollgate_config_get(); @@ -71,129 +30,24 @@ static const char *esp_get_metric(void) return cfg ? cfg->metric : "milliseconds"; } -static int64_t esp_get_time_ms(void) -{ - return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; -} - -static bool esp_wallet_receive(const char *token) -{ - return nucula_wallet_receive(token) == ESP_OK; -} - -static bool esp_wallet_send(uint64_t amount, char *buf, size_t buf_len) -{ - return nucula_wallet_send(amount, buf, buf_len) == ESP_OK; -} - -static uint64_t esp_wallet_balance(void) -{ - return nucula_wallet_balance(); -} - -static int esp_http_post(const char *url, const char *headers, - const char *body, int body_len, - char *resp, int resp_len) +static int32_t esp_get_step_bytes(void) { - esp_http_client_config_t config = { - .url = url, - .method = HTTP_METHOD_POST, - .timeout_ms = 15000, - .crt_bundle_attach = esp_crt_bundle_attach, - }; - esp_http_client_handle_t client = esp_http_client_init(&config); - if (!client) return -1; - - if (headers) { - const char *h = headers; - while (*h) { - const char *colon = strchr(h, ':'); - if (!colon) break; - char key[64] = {0}; - size_t klen = colon - h; - if (klen >= sizeof(key)) klen = sizeof(key) - 1; - memcpy(key, h, klen); - esp_http_client_set_header(client, key, colon + 1); - const char *next = strchr(colon, '\n'); - if (!next) break; - h = next + 1; - } - } - if (headers && headers[0]) { - esp_http_client_set_header(client, "Content-Type", "application/json"); - } - - esp_err_t err = esp_http_client_open(client, body_len); - if (err != ESP_OK) { - esp_http_client_cleanup(client); - return -1; - } - int written = esp_http_client_write(client, body, body_len); - (void)written; - - esp_http_client_fetch_headers(client); - int status = esp_http_client_get_status_code(client); - int rd = esp_http_client_read(client, resp, resp_len); - esp_http_client_cleanup(client); - - if (status != 200 || rd <= 0) return -1; - return rd; -} - -static bool esp_create_task(void (*fn)(void*), void *arg, - const char *name, int stack_bytes, int priority) -{ - return xTaskCreate(fn, name ? name : "tg_task", - stack_bytes / sizeof(StackType_t), - arg, priority, NULL) == pdPASS; -} - -static bool esp_mac_for_ip(uint32_t ip, char *mac_out, int mac_out_size) -{ - wifi_sta_list_t sta_list; - if (esp_wifi_ap_get_sta_list(&sta_list) == ESP_OK) { - wifi_sta_mac_ip_list_t ip_mac_list; - if (esp_wifi_ap_get_sta_list_with_ip(&sta_list, &ip_mac_list) == ESP_OK) { - for (int i = 0; i < ip_mac_list.num; i++) { - if (ip_mac_list.sta[i].ip.addr == ip) { - snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x", - ip_mac_list.sta[i].mac[0], ip_mac_list.sta[i].mac[1], - ip_mac_list.sta[i].mac[2], ip_mac_list.sta[i].mac[3], - ip_mac_list.sta[i].mac[4], ip_mac_list.sta[i].mac[5]); - return true; - } - } - } - } - - ip4_addr_t *entry_ip = NULL; - struct netif *entry_netif = NULL; - struct eth_addr *entry_eth = NULL; - ssize_t i = 0; - while (etharp_get_entry(i, &entry_ip, &entry_netif, &entry_eth) == ERR_OK) { - if (entry_ip && entry_ip->addr == ip && entry_eth) { - snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x", - entry_eth->addr[0], entry_eth->addr[1], entry_eth->addr[2], - entry_eth->addr[3], entry_eth->addr[4], entry_eth->addr[5]); - return true; - } - i++; - } - return false; + const tollgate_config_t *cfg = tollgate_config_get(); + return cfg ? (int32_t)cfg->step_size_bytes : 22020096; } -static void esp_napt_enable(uint32_t ip, bool enable) +static int64_t esp_get_time_ms(void) { - ip_napt_enable(ip, enable ? 1 : 0); + return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; } -static bool esp_mining_enabled(void) +static bool esp_spend_proofs(const char *raw_token_json) { - const tollgate_config_t *cfg = tollgate_config_get(); - return cfg ? cfg->mining_enabled : false; + (void)raw_token_json; + return true; } -static const char *esp_get_stratum_host(void) +static const char *esp_get_stratum_url(void) { const tollgate_config_t *cfg = tollgate_config_get(); return cfg ? cfg->stratum_host : NULL; @@ -217,70 +71,65 @@ static const char *esp_get_stratum_pass(void) return cfg ? cfg->stratum_pass : NULL; } -static uint16_t esp_get_mining_port(void) +static const char *esp_get_stratum_fallback_url(void) { const tollgate_config_t *cfg = tollgate_config_get(); - return cfg ? cfg->mining_port : 3334; + return cfg ? cfg->stratum_fallback_host : NULL; } -static uint64_t esp_get_hashprice_override(void) +static uint16_t esp_get_stratum_fallback_port(void) { const tollgate_config_t *cfg = tollgate_config_get(); - return cfg ? cfg->hashprice_sats_per_ghs_day : 0; + return cfg ? cfg->stratum_fallback_port : 3333; } -static void tg_esp_fill_random(void *buf, int len) +static uint16_t esp_get_mining_port(void) { - esp_fill_random(buf, (size_t)len); + const tollgate_config_t *cfg = tollgate_config_get(); + return cfg ? cfg->mining_port : 3334; } -static int esp_get_accepted_mint_count(void) +static const char *esp_get_mining_payout_mode(void) { const tollgate_config_t *cfg = tollgate_config_get(); - return cfg ? cfg->accepted_mint_count : 0; + return cfg ? "upstream" : "upstream"; } -static const char *esp_get_accepted_mint(int index) +static uint64_t esp_get_hashprice_sats_per_ghs_day(void) { const tollgate_config_t *cfg = tollgate_config_get(); - if (!cfg || index < 0 || index >= cfg->accepted_mint_count) return NULL; - return cfg->accepted_mints[index]; + return cfg ? cfg->hashprice_sats_per_ghs_day : 0; } -static bool esp_is_mint_reachable(const char *mint_url) +static void esp_on_share_accepted(double difficulty) { - (void)mint_url; - return true; + (void)difficulty; +} + +static double esp_get_hashrate(void) +{ + return 0.0; } static const tollgate_platform_t s_platform = { - .get_price_sats = esp_get_price_sats, - .get_step_ms = esp_get_step_ms, - .get_step_bytes = esp_get_step_bytes, - .get_mint_url = esp_get_mint_url, - .get_metric = esp_get_metric, - .get_time_ms = esp_get_time_ms, - .log_info = esp_log_info, - .log_warn = esp_log_warn, - .log_error = esp_log_error, - .wallet_receive = esp_wallet_receive, - .wallet_send = esp_wallet_send, - .wallet_balance = esp_wallet_balance, - .http_post = esp_http_post, - .create_task = esp_create_task, - .mac_for_ip = esp_mac_for_ip, - .napt_enable = esp_napt_enable, - .mining_enabled = esp_mining_enabled, - .get_stratum_host = esp_get_stratum_host, - .get_stratum_port = esp_get_stratum_port, - .get_stratum_user = esp_get_stratum_user, - .get_stratum_pass = esp_get_stratum_pass, - .get_mining_port = esp_get_mining_port, - .get_hashprice_override = esp_get_hashprice_override, - .fill_random = tg_esp_fill_random, - .get_accepted_mint_count = esp_get_accepted_mint_count, - .get_accepted_mint = esp_get_accepted_mint, - .is_mint_reachable = esp_is_mint_reachable, + .get_price_sats = esp_get_price_sats, + .get_step_ms = esp_get_step_ms, + .get_mint_url = esp_get_mint_url, + .get_metric = esp_get_metric, + .get_step_bytes = esp_get_step_bytes, + .get_time_ms = esp_get_time_ms, + .spend_proofs = esp_spend_proofs, + .get_stratum_url = esp_get_stratum_url, + .get_stratum_port = esp_get_stratum_port, + .get_stratum_user = esp_get_stratum_user, + .get_stratum_pass = esp_get_stratum_pass, + .get_stratum_fallback_url = esp_get_stratum_fallback_url, + .get_stratum_fallback_port = esp_get_stratum_fallback_port, + .get_mining_port = esp_get_mining_port, + .get_mining_payout_mode = esp_get_mining_payout_mode, + .get_hashprice_sats_per_ghs_day = esp_get_hashprice_sats_per_ghs_day, + .on_share_accepted = esp_on_share_accepted, + .get_hashrate = esp_get_hashrate, }; const tollgate_platform_t *tollgate_esp_get_platform(void) -- cgit v1.2.3