upleb.uk

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

summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-23 05:32:22 +0530
committerYour Name <you@example.com>2026-05-23 05:32:22 +0530
commit5140a30be35aaf3574efce9193549f18524ce46e (patch)
treec1146fc161fe787ccabd04558219b7591e207b61 /main
parentfb8558cbf964fae5a840f8f948b89a0d42045802 (diff)
Phase 7: eliminate shims - consumer files use component headers directly
- Deleted standalone shims: session.c/h, cashu.c/h, mining_payment.c/h, firewall.c/h - Updated captive_portal.c, tollgate_api.c, tollgate_main.c, mcp_handler.c, cvm_server.c, sw_miner.c, stratum_client.c to include component headers - Moved tollgate_ip4_canforward_filter() lwIP hook into tollgate_main.c - Fixed all unit tests to use component API (test_cashu, test_session, test_mining_payment, test_stratum_proxy, test_session_payment_method, test_firewall_sandbox, test_mcp_handler) - Removed deleted source files from Makefile targets - All 21 unit tests pass, ESP-IDF build passes
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt4
-rw-r--r--main/captive_portal.c16
-rw-r--r--main/cashu.c44
-rw-r--r--main/cashu.h41
-rw-r--r--main/cvm_server.c2
-rw-r--r--main/firewall.c66
-rw-r--r--main/firewall.h26
-rw-r--r--main/mcp_handler.c9
-rw-r--r--main/mining_payment.c66
-rw-r--r--main/mining_payment.h35
-rw-r--r--main/session.c86
-rw-r--r--main/session.h44
-rw-r--r--main/stratum_client.c4
-rw-r--r--main/sw_miner.c4
-rw-r--r--main/tollgate_api.c64
-rw-r--r--main/tollgate_main.c22
16 files changed, 67 insertions, 466 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 3a8613d..1f127c0 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -2,9 +2,6 @@ idf_component_register(SRCS "tollgate_main.c"
2 "config.c" 2 "config.c"
3 "dns_server.c" 3 "dns_server.c"
4 "captive_portal.c" 4 "captive_portal.c"
5 "firewall.c"
6 "cashu.c"
7 "session.c"
8 "tollgate_api.c" 5 "tollgate_api.c"
9 "identity.c" 6 "identity.c"
10 "nostr_event.c" 7 "nostr_event.c"
@@ -26,7 +23,6 @@ idf_component_register(SRCS "tollgate_main.c"
26 "beacon_price.c" 23 "beacon_price.c"
27 "market.c" 24 "market.c"
28 "negentropy_adapter.c" 25 "negentropy_adapter.c"
29 "mining_payment.c"
30 "stratum_client.c" 26 "stratum_client.c"
31 "stratum_proxy.c" 27 "stratum_proxy.c"
32 "sw_miner.c" 28 "sw_miner.c"
diff --git a/main/captive_portal.c b/main/captive_portal.c
index 9a0a5ff..f95a791 100644
--- a/main/captive_portal.c
+++ b/main/captive_portal.c
@@ -1,8 +1,8 @@
1#include "captive_portal.h" 1#include "captive_portal.h"
2#include "firewall.h" 2#include "tollgate_core.h"
3#include "session.h" 3#include "tollgate_core_firewall.h"
4#include "tollgate_core_session.h"
4#include "config.h" 5#include "config.h"
5#include "mining_payment.h"
6#include "stratum_proxy.h" 6#include "stratum_proxy.h"
7#include "esp_log.h" 7#include "esp_log.h"
8#include "esp_wifi.h" 8#include "esp_wifi.h"
@@ -253,7 +253,7 @@ static esp_err_t grant_access_handler(httpd_req_t *req)
253{ 253{
254 uint32_t client_ip; 254 uint32_t client_ip;
255 if (get_client_ip(req, &client_ip) == ESP_OK) { 255 if (get_client_ip(req, &client_ip) == ESP_OK) {
256 firewall_grant_access(client_ip); 256 tollgate_core_fw_grant(client_ip);
257 } 257 }
258 const char *resp = "{\"status\":\"granted\"}"; 258 const char *resp = "{\"status\":\"granted\"}";
259 httpd_resp_set_type(req, "application/json"); 259 httpd_resp_set_type(req, "application/json");
@@ -282,7 +282,7 @@ static esp_err_t whoami_handler(httpd_req_t *req)
282 if (get_client_ip(req, &client_ip) == ESP_OK) { 282 if (get_client_ip(req, &client_ip) == ESP_OK) {
283 char mac[18] = {0}; 283 char mac[18] = {0};
284 esp_ip4_addr_t ip = { .addr = client_ip }; 284 esp_ip4_addr_t ip = { .addr = client_ip };
285 if (firewall_get_mac_for_ip(client_ip, mac, sizeof(mac)) == ESP_OK) { 285 if (tollgate_core_fw_get_mac_for_ip(client_ip, mac, sizeof(mac)) == ESP_OK) {
286 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=%s", IP2STR(&ip), mac); 286 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=%s", IP2STR(&ip), mac);
287 } else { 287 } else {
288 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=unknown", IP2STR(&ip)); 288 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=unknown", IP2STR(&ip));
@@ -304,7 +304,7 @@ static esp_err_t usage_handler(httpd_req_t *req)
304 return ESP_OK; 304 return ESP_OK;
305 } 305 }
306 306
307 session_t *session = session_find_by_ip(client_ip); 307 tg_session_t *session = tollgate_core_session_find_by_ip(client_ip);
308 if (!session || !session->active) { 308 if (!session || !session->active) {
309 httpd_resp_set_type(req, "text/plain"); 309 httpd_resp_set_type(req, "text/plain");
310 httpd_resp_send(req, "-1/-1", 5); 310 httpd_resp_send(req, "-1/-1", 5);
@@ -332,8 +332,8 @@ static esp_err_t usage_handler(httpd_req_t *req)
332 332
333static esp_err_t reset_auth_handler(httpd_req_t *req) 333static esp_err_t reset_auth_handler(httpd_req_t *req)
334{ 334{
335 session_revoke_all(); 335 tollgate_core_session_revoke_all();
336 firewall_revoke_all(); 336 tollgate_core_fw_revoke_all();
337 const char *resp = "{\"status\":\"reset\"}"; 337 const char *resp = "{\"status\":\"reset\"}";
338 httpd_resp_set_type(req, "application/json"); 338 httpd_resp_set_type(req, "application/json");
339 httpd_resp_send(req, resp, strlen(resp)); 339 httpd_resp_send(req, resp, strlen(resp));
diff --git a/main/cashu.c b/main/cashu.c
deleted file mode 100644
index 2da6a05..0000000
--- a/main/cashu.c
+++ /dev/null
@@ -1,44 +0,0 @@
1#include "cashu.h"
2#include "tollgate_core_cashu.h"
3#include "tollgate_core.h"
4#include "config.h"
5#include "esp_log.h"
6
7static const char *TAG = "cashu";
8
9esp_err_t cashu_decode_token(const char *token_str, cashu_token_t *out)
10{
11 return tollgate_core_cashu_decode_token(token_str, (tg_cashu_token_t *)out);
12}
13
14esp_err_t cashu_check_proof_states(const char *mint_url, const cashu_token_t *token,
15 cashu_proof_state_t *states, int *state_count)
16{
17 return tollgate_core_cashu_check_proof_states(mint_url, (const tg_cashu_token_t *)token,
18 (tg_cashu_proof_state_t *)states, state_count);
19}
20
21uint64_t cashu_calculate_allotment_ms(uint64_t token_amount, uint64_t price_per_step,
22 uint64_t step_size_ms)
23{
24 return tollgate_core_cashu_calculate_allotment(token_amount, price_per_step, step_size_ms);
25}
26
27uint64_t cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step,
28 const char *metric, uint64_t step_size)
29{
30 (void)metric;
31 return tollgate_core_cashu_calculate_allotment(token_amount, price_per_step, step_size);
32}
33
34bool cashu_is_mint_accepted(const char *mint_url)
35{
36 const tollgate_config_t *cfg = tollgate_config_get();
37 if (cfg) {
38 for (int i = 0; i < cfg->accepted_mint_count; i++) {
39 if (tollgate_core_cashu_is_mint_accepted(mint_url, cfg->accepted_mints[i]))
40 return true;
41 }
42 }
43 return false;
44}
diff --git a/main/cashu.h b/main/cashu.h
deleted file mode 100644
index 43be033..0000000
--- a/main/cashu.h
+++ /dev/null
@@ -1,41 +0,0 @@
1#ifndef CASHU_H
2#define CASHU_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stdbool.h>
7
8#define CASHU_MAX_PROOFS 10
9#define CASHU_MAX_SECRET_LEN 128
10#define CASHU_MAX_ID_LEN 68
11#define CASHU_MAX_C_LEN 128
12
13typedef struct {
14 uint64_t amount;
15 char id[CASHU_MAX_ID_LEN];
16 char secret[CASHU_MAX_SECRET_LEN];
17 char c[CASHU_MAX_C_LEN];
18} cashu_proof_t;
19
20typedef struct {
21 cashu_proof_t proofs[CASHU_MAX_PROOFS];
22 int proof_count;
23 char mint_url[256];
24 uint64_t total_amount;
25} cashu_token_t;
26
27typedef struct {
28 char y_hex[65];
29 bool spent;
30} cashu_proof_state_t;
31
32esp_err_t cashu_decode_token(const char *token_str, cashu_token_t *out);
33esp_err_t cashu_check_proof_states(const char *mint_url, const cashu_token_t *token,
34 cashu_proof_state_t *states, int *state_count);
35uint64_t cashu_calculate_allotment_ms(uint64_t token_amount, uint64_t price_per_step,
36 uint64_t step_size_ms);
37uint64_t cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step,
38 const char *metric, uint64_t step_size);
39bool cashu_is_mint_accepted(const char *mint_url);
40
41#endif
diff --git a/main/cvm_server.c b/main/cvm_server.c
index 1ac5cb6..5f59a77 100644
--- a/main/cvm_server.c
+++ b/main/cvm_server.c
@@ -4,7 +4,7 @@
4#include "identity.h" 4#include "identity.h"
5#include "nostr_event.h" 5#include "nostr_event.h"
6#include "config.h" 6#include "config.h"
7#include "session.h" 7#include "tollgate_core_session.h"
8#include "nucula_wallet.h" 8#include "nucula_wallet.h"
9#include "cJSON.h" 9#include "cJSON.h"
10#include "esp_log.h" 10#include "esp_log.h"
diff --git a/main/firewall.c b/main/firewall.c
deleted file mode 100644
index 5ffdee0..0000000
--- a/main/firewall.c
+++ /dev/null
@@ -1,66 +0,0 @@
1#include "firewall.h"
2#include "tollgate_core.h"
3#include "tollgate_core_firewall.h"
4#include "esp_log.h"
5#include "lwip/prot/ip4.h"
6#include <string.h>
7
8static const char *TAG = "firewall";
9static esp_ip4_addr_t s_ap_ip;
10
11esp_err_t firewall_init(esp_ip4_addr_t ap_ip)
12{
13 s_ap_ip = ap_ip;
14 ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR, IP2STR(&s_ap_ip));
15 return ESP_OK;
16}
17
18void firewall_set_mining_port(uint16_t port)
19{
20 tollgate_core_fw_set_sandbox_ports(port);
21}
22
23void firewall_set_sandbox_mint_access(bool enabled)
24{
25 tollgate_core_fw_set_sandbox_mint_access(enabled);
26}
27
28esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size)
29{
30 return tollgate_core_fw_get_mac_for_ip(client_ip, mac_out, mac_out_size);
31}
32
33int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder)
34{
35 return tollgate_core_ip4_canforward_filter(p, dest_addr_hostorder);
36}
37
38void firewall_grant_access(uint32_t client_ip)
39{
40 tollgate_core_fw_grant(client_ip);
41}
42
43void firewall_revoke_access(uint32_t client_ip)
44{
45 tollgate_core_fw_revoke(client_ip);
46}
47
48void firewall_revoke_all(void)
49{
50 tollgate_core_fw_revoke_all();
51}
52
53bool firewall_is_client_allowed(uint32_t client_ip)
54{
55 return tollgate_core_is_client_allowed(client_ip);
56}
57
58bool firewall_is_mac_allowed(const char *mac)
59{
60 return tollgate_core_fw_is_mac_allowed(mac);
61}
62
63int firewall_client_count(void)
64{
65 return tollgate_core_allowed_client_count();
66}
diff --git a/main/firewall.h b/main/firewall.h
deleted file mode 100644
index 8d6e1c1..0000000
--- a/main/firewall.h
+++ /dev/null
@@ -1,26 +0,0 @@
1#ifndef FIREWALL_H
2#define FIREWALL_H
3
4#include "esp_err.h"
5#include "esp_netif.h"
6#include <stdbool.h>
7#include <stdint.h>
8
9struct pbuf;
10
11#define FW_MAX_MAC_LEN 18
12
13esp_err_t firewall_init(esp_ip4_addr_t ap_ip);
14void firewall_set_mining_port(uint16_t port);
15void firewall_set_sandbox_mint_access(bool enabled);
16void firewall_grant_access(uint32_t client_ip);
17void firewall_revoke_access(uint32_t client_ip);
18void firewall_revoke_all(void);
19bool firewall_is_client_allowed(uint32_t client_ip);
20bool firewall_is_mac_allowed(const char *mac);
21int firewall_client_count(void);
22esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size);
23
24int tollgate_ip4_canforward_filter(struct pbuf *p, uint32_t dest_addr_hostorder);
25
26#endif
diff --git a/main/mcp_handler.c b/main/mcp_handler.c
index 93bfba9..5f5d494 100644
--- a/main/mcp_handler.c
+++ b/main/mcp_handler.c
@@ -1,7 +1,8 @@
1#include "mcp_handler.h" 1#include "mcp_handler.h"
2#include "config.h" 2#include "config.h"
3#include "nucula_wallet.h" 3#include "nucula_wallet.h"
4#include "session.h" 4#include "tollgate_core_session.h"
5#include "tollgate_core.h"
5#include "cJSON.h" 6#include "cJSON.h"
6#include "lwip/ip4_addr.h" 7#include "lwip/ip4_addr.h"
7#include <string.h> 8#include <string.h>
@@ -157,12 +158,10 @@ mcp_response_t mcp_handle_wallet_send(const char *params_json)
157mcp_response_t mcp_handle_get_sessions(void) 158mcp_response_t mcp_handle_get_sessions(void)
158{ 159{
159 mcp_response_t resp = {0}; 160 mcp_response_t resp = {0};
160 extern session_t *cvm_get_sessions_array(void);
161 extern int cvm_get_sessions_count(void);
162 161
163 cJSON *arr = cJSON_CreateArray(); 162 cJSON *arr = cJSON_CreateArray();
164 int count = cvm_get_sessions_count(); 163 int count = tollgate_core_session_get_array_size();
165 session_t *sessions = cvm_get_sessions_array(); 164 tg_session_t *sessions = tollgate_core_session_get_array();
166 165
167 if (sessions && count > 0) { 166 if (sessions && count > 0) {
168 for (int i = 0; i < count; i++) { 167 for (int i = 0; i < count; i++) {
diff --git a/main/mining_payment.c b/main/mining_payment.c
deleted file mode 100644
index f9d01b6..0000000
--- a/main/mining_payment.c
+++ /dev/null
@@ -1,66 +0,0 @@
1#include "mining_payment.h"
2#include "tollgate_core_mining.h"
3#include "tollgate_core.h"
4#include "esp_log.h"
5
6static const char *TAG = "mining_payment";
7
8uint64_t mining_nbits_to_difficulty(uint32_t nbits)
9{
10 return tollgate_core_mining_nbits_to_difficulty(nbits);
11}
12
13double mining_calculate_hashprice(uint32_t nbits)
14{
15 return tollgate_core_mining_calc_hashprice(nbits);
16}
17
18double mining_calculate_hashprice_override(uint64_t sats_per_ghs_day)
19{
20 return tollgate_core_mining_calc_hashprice_override(sats_per_ghs_day);
21}
22
23esp_err_t mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len)
24{
25 return tollgate_core_mining_validate_share(header80, nonce, target, target_len);
26}
27
28uint64_t mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice_sats_per_ghs_s, int price_per_step, int step_size_ms)
29{
30 return tollgate_core_mining_shares_to_allotment_ms(hashrate_ghs, hashprice_sats_per_ghs_s, price_per_step, step_size_ms);
31}
32
33uint64_t mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice_sats_per_ghs_s, int price_per_step, int step_size_bytes)
34{
35 return tollgate_core_mining_shares_to_allotment_bytes(hashrate_ghs, hashprice_sats_per_ghs_s, price_per_step, step_size_bytes);
36}
37
38mining_client_stats_t *mining_get_or_create_client(uint32_t client_ip)
39{
40 return (mining_client_stats_t *)tollgate_core_mining_get_or_create_client(client_ip);
41}
42
43void mining_update_hashrate(uint32_t client_ip, bool accepted)
44{
45 tollgate_core_mining_update_hashrate(client_ip, accepted);
46}
47
48const mining_client_stats_t *mining_get_client_stats(uint32_t client_ip)
49{
50 return (const mining_client_stats_t *)tollgate_core_mining_get_client_stats(client_ip);
51}
52
53double mining_get_current_hashprice(void)
54{
55 return tollgate_core_mining_get_current_hashprice();
56}
57
58void mining_set_current_nbits(uint32_t nbits)
59{
60 tollgate_core_mining_set_current_nbits(nbits);
61}
62
63void mining_payment_init(void)
64{
65 ESP_LOGI(TAG, "Mining payment initialized (via tollgate_core)");
66}
diff --git a/main/mining_payment.h b/main/mining_payment.h
deleted file mode 100644
index c5ce0f2..0000000
--- a/main/mining_payment.h
+++ /dev/null
@@ -1,35 +0,0 @@
1#ifndef MINING_PAYMENT_H
2#define MINING_PAYMENT_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stdbool.h>
7
8#define MINING_SHARE_WINDOW_S 30
9#define MINING_BLOCK_SUBSIDY_SATS 312500000ULL
10#define MINING_BLOCKS_PER_DAY 144ULL
11#define MINING_MAX_CLIENTS 10
12
13typedef struct {
14 uint32_t ip;
15 uint64_t shares_accepted;
16 uint64_t shares_rejected;
17 int64_t first_share_time_ms;
18 int64_t last_share_time_ms;
19 double hashrate_ghs;
20} mining_client_stats_t;
21
22uint64_t mining_nbits_to_difficulty(uint32_t nbits);
23double mining_calculate_hashprice(uint32_t nbits);
24double mining_calculate_hashprice_override(uint64_t sats_per_ghs_day);
25esp_err_t mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len);
26uint64_t mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice_sats_per_ghs_s, int price_per_step, int step_size_ms);
27uint64_t mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice_sats_per_ghs_s, int price_per_step, int step_size_bytes);
28mining_client_stats_t *mining_get_or_create_client(uint32_t client_ip);
29void mining_update_hashrate(uint32_t client_ip, bool accepted);
30const mining_client_stats_t *mining_get_client_stats(uint32_t client_ip);
31double mining_get_current_hashprice(void);
32void mining_set_current_nbits(uint32_t nbits);
33void mining_payment_init(void);
34
35#endif
diff --git a/main/session.c b/main/session.c
deleted file mode 100644
index 3e3813a..0000000
--- a/main/session.c
+++ /dev/null
@@ -1,86 +0,0 @@
1#include "session.h"
2#include "tollgate_core_session.h"
3#include "tollgate_core_firewall.h"
4#include "tollgate_core.h"
5#include "esp_log.h"
6#include "freertos/FreeRTOS.h"
7#include "freertos/task.h"
8#include <string.h>
9
10static const char *TAG = "session";
11
12esp_err_t session_manager_init(void)
13{
14 tollgate_core_session_init();
15 ESP_LOGI(TAG, "Session manager initialized (via tollgate_core)");
16 return ESP_OK;
17}
18
19session_t *session_create(uint32_t client_ip, uint64_t allotment_ms)
20{
21 return (session_t *)tollgate_core_session_create(client_ip, allotment_ms);
22}
23
24session_t *session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes)
25{
26 return (session_t *)tollgate_core_session_create_bytes(client_ip, allotment_bytes);
27}
28
29void session_add_bytes(uint32_t client_ip, uint64_t bytes)
30{
31 tollgate_core_session_add_bytes(client_ip, bytes);
32}
33
34session_t *session_find_by_ip(uint32_t client_ip)
35{
36 return (session_t *)tollgate_core_session_find_by_ip(client_ip);
37}
38
39session_t *session_find_by_mac(const char *mac)
40{
41 return (session_t *)tollgate_core_session_find_by_mac(mac);
42}
43
44void session_extend(session_t *session, uint64_t additional_ms)
45{
46 tollgate_core_session_extend((tg_session_t *)session, additional_ms);
47}
48
49bool session_is_expired(const session_t *session)
50{
51 return tollgate_core_session_is_expired((const tg_session_t *)session);
52}
53
54void session_check_expiry(void)
55{
56}
57
58void session_revoke(session_t *session)
59{
60 tollgate_core_session_revoke((tg_session_t *)session);
61}
62
63void session_revoke_all(void)
64{
65 tollgate_core_session_revoke_all();
66}
67
68int session_active_count(void)
69{
70 return tollgate_core_active_session_count();
71}
72
73void session_tick(void)
74{
75 tollgate_core_tick();
76}
77
78session_t *cvm_get_sessions_array(void)
79{
80 return (session_t *)tollgate_core_session_get_array();
81}
82
83int cvm_get_sessions_count(void)
84{
85 return tollgate_core_session_get_array_size();
86}
diff --git a/main/session.h b/main/session.h
deleted file mode 100644
index 8f6b991..0000000
--- a/main/session.h
+++ /dev/null
@@ -1,44 +0,0 @@
1#ifndef SESSION_H
2#define SESSION_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stdbool.h>
7
8#define SESSION_MAX_CLIENTS 10
9#define SESSION_MAX_MAC_LEN 18
10
11typedef enum {
12 PAYMENT_METHOD_CASHU,
13 PAYMENT_METHOD_MINING,
14 PAYMENT_METHOD_BYTES
15} payment_method_t;
16
17typedef struct {
18 uint32_t client_ip;
19 char mac[SESSION_MAX_MAC_LEN];
20 uint64_t allotment_ms;
21 int64_t start_time_ms;
22 uint64_t allotment_bytes;
23 uint64_t bytes_consumed;
24 payment_method_t payment_method;
25 bool active;
26} session_t;
27
28esp_err_t session_manager_init(void);
29session_t *session_create(uint32_t client_ip, uint64_t allotment_ms);
30session_t *session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes);
31void session_add_bytes(uint32_t client_ip, uint64_t bytes);
32session_t *session_find_by_ip(uint32_t client_ip);
33session_t *session_find_by_mac(const char *mac);
34void session_extend(session_t *session, uint64_t additional_ms);
35bool session_is_expired(const session_t *session);
36void session_check_expiry(void);
37void session_revoke(session_t *session);
38void session_revoke_all(void);
39int session_active_count(void);
40void session_tick(void);
41session_t *cvm_get_sessions_array(void);
42int cvm_get_sessions_count(void);
43
44#endif
diff --git a/main/stratum_client.c b/main/stratum_client.c
index cf88daf..a308a5b 100644
--- a/main/stratum_client.c
+++ b/main/stratum_client.c
@@ -1,6 +1,6 @@
1#include "stratum_client.h" 1#include "stratum_client.h"
2#include "stratum_proxy.h" 2#include "stratum_proxy.h"
3#include "mining_payment.h" 3#include "tollgate_core_mining.h"
4#include "config.h" 4#include "config.h"
5#include "esp_log.h" 5#include "esp_log.h"
6#include "esp_transport.h" 6#include "esp_transport.h"
@@ -124,7 +124,7 @@ static void handle_mining_notify(cJSON *params)
124 memset(job.target, 0xFF, 32); 124 memset(job.target, 0xFF, 32);
125 job.target_len = 32; 125 job.target_len = 32;
126 126
127 mining_set_current_nbits(job.nbits); 127 tollgate_core_mining_set_current_nbits(job.nbits);
128 stratum_proxy_set_job(&job); 128 stratum_proxy_set_job(&job);
129 129
130 ESP_LOGI(TAG, "New mining job: id=%lu, nbits=0x%08lx", (unsigned long)job.job_id, (unsigned long)job.nbits); 130 ESP_LOGI(TAG, "New mining job: id=%lu, nbits=0x%08lx", (unsigned long)job.job_id, (unsigned long)job.nbits);
diff --git a/main/sw_miner.c b/main/sw_miner.c
index cdd98a0..9d7ea36 100644
--- a/main/sw_miner.c
+++ b/main/sw_miner.c
@@ -1,7 +1,7 @@
1#include "sw_miner.h" 1#include "sw_miner.h"
2#include "stratum_proxy.h" 2#include "stratum_proxy.h"
3#include "stratum_client.h" 3#include "stratum_client.h"
4#include "mining_payment.h" 4#include "tollgate_core_mining.h"
5#include "config.h" 5#include "config.h"
6#include "esp_log.h" 6#include "esp_log.h"
7#include "esp_random.h" 7#include "esp_random.h"
@@ -60,7 +60,7 @@ static void sw_miner_task(void *arg)
60 if (memcmp(hash, local_job.target, local_job.target_len) <= 0) { 60 if (memcmp(hash, local_job.target, local_job.target_len) <= 0) {
61 ESP_LOGI(TAG, "Valid share found! nonce=%08lx", (unsigned long)nonce); 61 ESP_LOGI(TAG, "Valid share found! nonce=%08lx", (unsigned long)nonce);
62 stratum_client_submit_share(local_job.job_id, nonce, local_job.ntime, local_job.version); 62 stratum_client_submit_share(local_job.job_id, nonce, local_job.ntime, local_job.version);
63 mining_update_hashrate(0, true); 63 tollgate_core_mining_update_hashrate(0, true);
64 break; 64 break;
65 } 65 }
66 } 66 }
diff --git a/main/tollgate_api.c b/main/tollgate_api.c
index 4e9de43..58c123c 100644
--- a/main/tollgate_api.c
+++ b/main/tollgate_api.c
@@ -1,16 +1,18 @@
1#include "tollgate_api.h" 1#include "tollgate_api.h"
2#include "cashu.h" 2#include "tollgate_core_cashu.h"
3#include "tollgate_core_session.h"
4#include "tollgate_core_firewall.h"
5#include "tollgate_core_mining.h"
6#include "tollgate_core.h"
3#include "config.h" 7#include "config.h"
4#include "identity.h" 8#include "identity.h"
5#include "session.h"
6#include "captive_portal.h" 9#include "captive_portal.h"
7#include "firewall.h"
8#include "lwip/dns.h" 10#include "lwip/dns.h"
9#include "esp_heap_caps.h" 11#include "esp_heap_caps.h"
10#include "nucula_wallet.h" 12#include "nucula_wallet.h"
11#include "mint_health.h" 13#include "mint_health.h"
12#include "market.h" 14#include "market.h"
13#include "mining_payment.h" 15
14#include "stratum_proxy.h" 16#include "stratum_proxy.h"
15#include "stratum_client.h" 17#include "stratum_client.h"
16#include "tls_worker.h" 18#include "tls_worker.h"
@@ -226,7 +228,7 @@ static esp_err_t api_post_payment(httpd_req_t *req)
226 228
227 ESP_LOGI(TAG, "Payment received: %d bytes", total); 229 ESP_LOGI(TAG, "Payment received: %d bytes", total);
228 230
229 cashu_token_t *token = malloc(sizeof(cashu_token_t)); 231 tg_cashu_token_t *token = malloc(sizeof(tg_cashu_token_t));
230 if (!token) { 232 if (!token) {
231 cJSON *notice = create_notice("error", "session-error", "Out of memory"); 233 cJSON *notice = create_notice("error", "session-error", "Out of memory");
232 char *json = cJSON_PrintUnformatted(notice); 234 char *json = cJSON_PrintUnformatted(notice);
@@ -237,7 +239,7 @@ static esp_err_t api_post_payment(httpd_req_t *req)
237 cJSON_Delete(notice); 239 cJSON_Delete(notice);
238 return ESP_OK; 240 return ESP_OK;
239 } 241 }
240 esp_err_t err = cashu_decode_token(body, token); 242 esp_err_t err = tollgate_core_cashu_decode_token(body, token);
241 char *body_copy = strdup(body); 243 char *body_copy = strdup(body);
242 free(body); 244 free(body);
243 245
@@ -254,7 +256,17 @@ static esp_err_t api_post_payment(httpd_req_t *req)
254 } 256 }
255 257
256 const char *mint_url = token->mint_url[0] ? token->mint_url : tollgate_config_get()->mint_url; 258 const char *mint_url = token->mint_url[0] ? token->mint_url : tollgate_config_get()->mint_url;
257 if (!cashu_is_mint_accepted(mint_url)) { 259 bool mint_accepted = false;
260 {
261 const tollgate_config_t *_cfg = tollgate_config_get();
262 for (int i = 0; i < _cfg->accepted_mint_count; i++) {
263 if (tollgate_core_cashu_is_mint_accepted(mint_url, _cfg->accepted_mints[i])) {
264 mint_accepted = true;
265 break;
266 }
267 }
268 }
269 if (!mint_accepted) {
258 free(token); 270 free(token);
259 cJSON *notice = create_notice("error", "payment-error-mint-not-accepted", "Mint not accepted"); 271 cJSON *notice = create_notice("error", "payment-error-mint-not-accepted", "Mint not accepted");
260 char *json = cJSON_PrintUnformatted(notice); 272 char *json = cJSON_PrintUnformatted(notice);
@@ -266,7 +278,7 @@ static esp_err_t api_post_payment(httpd_req_t *req)
266 return ESP_OK; 278 return ESP_OK;
267 } 279 }
268 280
269 cashu_proof_state_t *states = malloc(CASHU_MAX_PROOFS * sizeof(cashu_proof_state_t)); 281 tg_cashu_proof_state_t *states = malloc(TG_CASHU_MAX_PROOFS * sizeof(tg_cashu_proof_state_t));
270 if (!states) { 282 if (!states) {
271 free(token); 283 free(token);
272 cJSON *notice = create_notice("error", "session-error", "Out of memory"); 284 cJSON *notice = create_notice("error", "session-error", "Out of memory");
@@ -279,7 +291,7 @@ static esp_err_t api_post_payment(httpd_req_t *req)
279 return ESP_OK; 291 return ESP_OK;
280 } 292 }
281 int state_count = 0; 293 int state_count = 0;
282 err = cashu_check_proof_states(mint_url, token, states, &state_count); 294 err = tollgate_core_cashu_check_proof_states(mint_url, token, states, &state_count);
283 ESP_LOGI(TAG, "Stack HWM after checkstate: %u", uxTaskGetStackHighWaterMark(NULL)); 295 ESP_LOGI(TAG, "Stack HWM after checkstate: %u", uxTaskGetStackHighWaterMark(NULL));
284 if (err != ESP_OK) { 296 if (err != ESP_OK) {
285 ESP_LOGW(TAG, "Checkstate failed, proceeding without spend check (wallet swap will verify)"); 297 ESP_LOGW(TAG, "Checkstate failed, proceeding without spend check (wallet swap will verify)");
@@ -304,8 +316,8 @@ static esp_err_t api_post_payment(httpd_req_t *req)
304 const tollgate_config_t *cfg = tollgate_config_get(); 316 const tollgate_config_t *cfg = tollgate_config_get();
305 bool is_bytes = (strcmp(cfg->metric, "bytes") == 0); 317 bool is_bytes = (strcmp(cfg->metric, "bytes") == 0);
306 uint64_t step_size = is_bytes ? (uint64_t)cfg->step_size_bytes : (uint64_t)cfg->step_size_ms; 318 uint64_t step_size = is_bytes ? (uint64_t)cfg->step_size_bytes : (uint64_t)cfg->step_size_ms;
307 uint64_t allotment = cashu_calculate_allotment(token->total_amount, cfg->price_per_step, 319 uint64_t allotment = tollgate_core_cashu_calculate_allotment(token->total_amount, cfg->price_per_step,
308 cfg->metric, step_size); 320 step_size);
309 if (allotment == 0) { 321 if (allotment == 0) {
310 free(states); 322 free(states);
311 free(token); 323 free(token);
@@ -319,11 +331,11 @@ static esp_err_t api_post_payment(httpd_req_t *req)
319 return ESP_OK; 331 return ESP_OK;
320 } 332 }
321 333
322 session_t *session; 334 tg_session_t *session;
323 if (is_bytes) { 335 if (is_bytes) {
324 session = session_create_bytes(client_ip, allotment); 336 session = tollgate_core_session_create_bytes(client_ip, allotment);
325 } else { 337 } else {
326 session = session_create(client_ip, allotment); 338 session = tollgate_core_session_create(client_ip, allotment);
327 } 339 }
328 if (!session) { 340 if (!session) {
329 free(states); 341 free(states);
@@ -357,7 +369,7 @@ static esp_err_t api_get_usage(httpd_req_t *req)
357 uint32_t client_ip = 0; 369 uint32_t client_ip = 0;
358 get_client_ip(req, &client_ip); 370 get_client_ip(req, &client_ip);
359 371
360 session_t *session = session_find_by_ip(client_ip); 372 tg_session_t *session = tollgate_core_session_find_by_ip(client_ip);
361 if (!session || !session->active) { 373 if (!session || !session->active) {
362 httpd_resp_set_type(req, "text/plain"); 374 httpd_resp_set_type(req, "text/plain");
363 httpd_resp_send(req, "-1/-1", 5); 375 httpd_resp_send(req, "-1/-1", 5);
@@ -390,7 +402,7 @@ static esp_err_t api_get_whoami(httpd_req_t *req)
390 if (get_client_ip(req, &client_ip) == ESP_OK) { 402 if (get_client_ip(req, &client_ip) == ESP_OK) {
391 char mac[18] = {0}; 403 char mac[18] = {0};
392 esp_ip4_addr_t ip = { .addr = client_ip }; 404 esp_ip4_addr_t ip = { .addr = client_ip };
393 if (firewall_get_mac_for_ip(client_ip, mac, sizeof(mac)) == ESP_OK) { 405 if (tollgate_core_fw_get_mac_for_ip(client_ip, mac, sizeof(mac)) == ESP_OK) {
394 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=%s", IP2STR(&ip), mac); 406 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=%s", IP2STR(&ip), mac);
395 } else { 407 } else {
396 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=unknown", IP2STR(&ip)); 408 snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=unknown", IP2STR(&ip));
@@ -546,7 +558,7 @@ static esp_err_t api_get_mining_job(httpd_req_t *req)
546 cJSON_AddNumberToObject(root, "version", job->version); 558 cJSON_AddNumberToObject(root, "version", job->version);
547 cJSON_AddNumberToObject(root, "nbits", job->nbits); 559 cJSON_AddNumberToObject(root, "nbits", job->nbits);
548 cJSON_AddNumberToObject(root, "ntime", job->ntime); 560 cJSON_AddNumberToObject(root, "ntime", job->ntime);
549 cJSON_AddNumberToObject(root, "hashprice", mining_get_current_hashprice()); 561 cJSON_AddNumberToObject(root, "hashprice", tollgate_core_mining_get_current_hashprice());
550 562
551 char *json = cJSON_PrintUnformatted(root); 563 char *json = cJSON_PrintUnformatted(root);
552 httpd_resp_set_type(req, "application/json"); 564 httpd_resp_set_type(req, "application/json");
@@ -620,21 +632,21 @@ static esp_err_t api_post_mining_share(httpd_req_t *req)
620 esp_err_t share_err = stratum_client_submit_share(job_id, nonce, ntime, version); 632 esp_err_t share_err = stratum_client_submit_share(job_id, nonce, ntime, version);
621 bool accepted = (share_err == ESP_OK); 633 bool accepted = (share_err == ESP_OK);
622 634
623 mining_update_hashrate(client_ip, accepted); 635 tollgate_core_mining_update_hashrate(client_ip, accepted);
624 mining_client_stats_t *stats = mining_get_or_create_client(client_ip); 636 tollgate_mining_client_stats_t *stats = tollgate_core_mining_get_or_create_client(client_ip);
625 637
626 if (accepted) { 638 if (accepted) {
627 const tollgate_config_t *cfg = tollgate_config_get(); 639 const tollgate_config_t *cfg = tollgate_config_get();
628 double hashprice = mining_get_current_hashprice(); 640 double hashprice = tollgate_core_mining_get_current_hashprice();
629 uint64_t allotment_ms = mining_shares_to_allotment_ms( 641 uint64_t allotment_ms = tollgate_core_mining_shares_to_allotment_ms(
630 stats->hashrate_ghs, hashprice, cfg->price_per_step, cfg->step_size_ms); 642 stats->hashrate_ghs, hashprice, cfg->price_per_step, cfg->step_size_ms);
631 643
632 session_t *session = session_find_by_ip(client_ip); 644 tg_session_t *session = tollgate_core_session_find_by_ip(client_ip);
633 if (!session || !session->active || session->payment_method != PAYMENT_METHOD_MINING) { 645 if (!session || !session->active || session->payment_method != TG_PAYMENT_MINING) {
634 session = session_create(client_ip, allotment_ms); 646 session = tollgate_core_session_create(client_ip, allotment_ms);
635 if (session) session->payment_method = PAYMENT_METHOD_MINING; 647 if (session) session->payment_method = TG_PAYMENT_MINING;
636 } else { 648 } else {
637 session_extend(session, allotment_ms); 649 tollgate_core_session_extend(session, allotment_ms);
638 } 650 }
639 } 651 }
640 652
diff --git a/main/tollgate_main.c b/main/tollgate_main.c
index 2d1f657..dc7453d 100644
--- a/main/tollgate_main.c
+++ b/main/tollgate_main.c
@@ -13,13 +13,13 @@
13#include "esp_sntp.h" 13#include "esp_sntp.h"
14#include "dhcpserver/dhcpserver.h" 14#include "dhcpserver/dhcpserver.h"
15#include "tollgate_core.h" 15#include "tollgate_core.h"
16#include "tollgate_core_firewall.h"
17#include "tollgate_core_mining.h"
16#include "tollgate_esp_platform.h" 18#include "tollgate_esp_platform.h"
17#include "config.h" 19#include "config.h"
18#include "identity.h" 20#include "identity.h"
19#include "dns_server.h" 21#include "dns_server.h"
20#include "captive_portal.h" 22#include "captive_portal.h"
21#include "firewall.h"
22#include "session.h"
23#include "tollgate_api.h" 23#include "tollgate_api.h"
24#include "nucula_wallet.h" 24#include "nucula_wallet.h"
25#include "wifistr.h" 25#include "wifistr.h"
@@ -37,7 +37,12 @@
37#include "stratum_proxy.h" 37#include "stratum_proxy.h"
38#include "sw_miner.h" 38#include "sw_miner.h"
39#include "asic_miner.h" 39#include "asic_miner.h"
40#include "mining_payment.h" 40#include "lwip/prot/ip4.h"
41
42int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder)
43{
44 return tollgate_core_ip4_canforward_filter(p, dest_addr_hostorder);
45}
41 46
42#define MAX_STA_RETRY 5 47#define MAX_STA_RETRY 5
43static const char *TAG = "tollgate_main"; 48static const char *TAG = "tollgate_main";
@@ -236,8 +241,6 @@ static void start_services(void)
236 IP2STR(&(esp_ip4_addr_t){.addr=dns_getserver(2)->addr})); 241 IP2STR(&(esp_ip4_addr_t){.addr=dns_getserver(2)->addr}));
237 242
238 tollgate_core_init(tollgate_esp_get_platform(), ap_ip_info.ip); 243 tollgate_core_init(tollgate_esp_get_platform(), ap_ip_info.ip);
239 firewall_init(ap_ip_info.ip);
240 session_manager_init();
241 244
242 const tollgate_config_t *cfg = tollgate_config_get(); 245 const tollgate_config_t *cfg = tollgate_config_get();
243 246
@@ -256,8 +259,8 @@ static void start_services(void)
256 } 259 }
257 260
258 if (cfg->mining_enabled) { 261 if (cfg->mining_enabled) {
259 firewall_set_mining_port(cfg->mining_port); 262 tollgate_core_fw_set_sandbox_ports(cfg->mining_port);
260 firewall_set_sandbox_mint_access(cfg->mining_sandbox_mint_access); 263 tollgate_core_fw_set_sandbox_mint_access(cfg->mining_sandbox_mint_access);
261 } 264 }
262 lightning_payout_init(&cfg->payout); 265 lightning_payout_init(&cfg->payout);
263 266
@@ -279,7 +282,6 @@ static void start_services(void)
279 282
280 if (cfg->mining_enabled) { 283 if (cfg->mining_enabled) {
281 ESP_LOGI(TAG, "Mining subsystem enabled, initializing..."); 284 ESP_LOGI(TAG, "Mining subsystem enabled, initializing...");
282 mining_payment_init();
283 stratum_client_init(); 285 stratum_client_init();
284 stratum_proxy_init(cfg->mining_port); 286 stratum_proxy_init(cfg->mining_port);
285 287
@@ -327,7 +329,7 @@ static void stop_services(void)
327 sync_manager_stop(&s_sync_manager); 329 sync_manager_stop(&s_sync_manager);
328 local_relay_stop(); 330 local_relay_stop();
329 relay_selector_destroy(&s_relay_selector); 331 relay_selector_destroy(&s_relay_selector);
330 firewall_revoke_all(); 332 tollgate_core_fw_revoke_all();
331 s_services_running = false; 333 s_services_running = false;
332 if (s_services_mutex) xSemaphoreGive(s_services_mutex); 334 if (s_services_mutex) xSemaphoreGive(s_services_mutex);
333 ESP_LOGI(TAG, "=== TollGate services stopped ==="); 335 ESP_LOGI(TAG, "=== TollGate services stopped ===");
@@ -462,7 +464,7 @@ void app_main(void)
462 464
463 while (1) { 465 while (1) {
464 vTaskDelay(pdMS_TO_TICKS(1000)); 466 vTaskDelay(pdMS_TO_TICKS(1000));
465 session_tick(); 467 tollgate_core_tick();
466 tollgate_client_tick(); 468 tollgate_client_tick();
467 lightning_payout_tick(); 469 lightning_payout_tick();
468 market_tick(); 470 market_tick();