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-23 05:32:22 +0530
committerYour Name <you@example.com>2026-05-23 05:32:22 +0530
commit5140a30be35aaf3574efce9193549f18524ce46e (patch)
treec1146fc161fe787ccabd04558219b7591e207b61
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
-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
-rw-r--r--tests/unit/Makefile16
-rw-r--r--tests/unit/test_cashu.c38
-rwxr-xr-xtests/unit/test_firewall_sandboxbin111016 -> 108304 bytes
-rw-r--r--tests/unit/test_firewall_sandbox.c75
-rw-r--r--tests/unit/test_mcp_handler.c11
-rwxr-xr-xtests/unit/test_mining_paymentbin111000 -> 108304 bytes
-rw-r--r--tests/unit/test_mining_payment.c68
-rw-r--r--tests/unit/test_session.c122
-rwxr-xr-xtests/unit/test_session_payment_methodbin117792 -> 110536 bytes
-rw-r--r--tests/unit/test_session_payment_method.c67
-rwxr-xr-xtests/unit/test_stratum_proxybin113304 -> 110600 bytes
-rw-r--r--tests/unit/test_stratum_proxy.c6
28 files changed, 246 insertions, 690 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();
diff --git a/tests/unit/Makefile b/tests/unit/Makefile
index 5fdc315..3890243 100644
--- a/tests/unit/Makefile
+++ b/tests/unit/Makefile
@@ -61,12 +61,12 @@ test_identity: test_identity.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ)
61test_nostr_event: test_nostr_event.c $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ) 61test_nostr_event: test_nostr_event.c $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ)
62 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS) 62 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS)
63 63
64test_cashu: test_cashu.c $(REPO_ROOT)/main/cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 64test_cashu: test_cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
65 $(CC) $(CFLAGS) -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h $^ -o $@ $(LDFLAGS) 65 $(CC) $(CFLAGS) -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h $^ -o $@ $(LDFLAGS)
66 66
67TG_CORE_OBJS_CFLAGS := -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h -include stubs/esp_crt_bundle.h 67TG_CORE_OBJS_CFLAGS := -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h -include stubs/esp_crt_bundle.h
68 68
69test_session: test_session.c $(REPO_ROOT)/main/session.c $(REPO_ROOT)/main/cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 69test_session: test_session.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
70 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 70 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
71 71
72test_tollgate_client: test_tollgate_client.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c 72test_tollgate_client: test_tollgate_client.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c
@@ -79,7 +79,7 @@ test_lightning_payout: test_lightning_payout.c
79 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) 79 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
80 80
81test_mcp_handler: test_mcp_handler.c $(REPO_ROOT)/main/mcp_handler.c 81test_mcp_handler: test_mcp_handler.c $(REPO_ROOT)/main/mcp_handler.c
82 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $< $(REPO_ROOT)/main/mcp_handler.c -o $@ $(LDFLAGS) 82 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main -I $(TG_CORE_INC) -I $(TG_CORE_INC)/.. $< $(REPO_ROOT)/main/mcp_handler.c -o $@ $(LDFLAGS)
83 83
84test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) 84test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ)
85 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS) 85 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS)
@@ -96,19 +96,19 @@ test_beacon_price: test_beacon_price.c $(REPO_ROOT)/main/beacon_price.c
96test_market: test_market.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c 96test_market: test_market.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c
97 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $< $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c -o $@ $(LDFLAGS) 97 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $< $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c -o $@ $(LDFLAGS)
98 98
99test_mining_payment: test_mining_payment.c $(REPO_ROOT)/main/mining_payment.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 99test_mining_payment: test_mining_payment.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
100 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 100 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
101 101
102test_stratum_proxy: test_stratum_proxy.c $(REPO_ROOT)/main/stratum_proxy.c $(REPO_ROOT)/main/mining_payment.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 102test_stratum_proxy: test_stratum_proxy.c $(REPO_ROOT)/main/stratum_proxy.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
103 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 103 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
104 104
105test_session_payment_method: test_session_payment_method.c $(REPO_ROOT)/main/session.c $(REPO_ROOT)/main/cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 105test_session_payment_method: test_session_payment_method.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
106 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 106 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
107 107
108test_tollgate_client_mining: test_tollgate_client_mining.c 108test_tollgate_client_mining: test_tollgate_client_mining.c
109 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) 109 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
110 110
111test_firewall_sandbox: test_firewall_sandbox.c $(REPO_ROOT)/main/firewall.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 111test_firewall_sandbox: test_firewall_sandbox.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
112 $(CC) $(CFLAGS) -include stubs/dns_server.h $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 112 $(CC) $(CFLAGS) -include stubs/dns_server.h $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
113 113
114test_relay_selector: test_relay_selector.c 114test_relay_selector: test_relay_selector.c
diff --git a/tests/unit/test_cashu.c b/tests/unit/test_cashu.c
index 021d1b7..2b86a34 100644
--- a/tests/unit/test_cashu.c
+++ b/tests/unit/test_cashu.c
@@ -1,5 +1,5 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/cashu.h" 2#include "tollgate_core_cashu.h"
3#include "../../main/config.h" 3#include "../../main/config.h"
4#include <string.h> 4#include <string.h>
5#include <stdio.h> 5#include <stdio.h>
@@ -32,38 +32,38 @@ int main(void)
32 } 32 }
33 g_test_config.accepted_mint_count = 4; 33 g_test_config.accepted_mint_count = 4;
34 34
35 printf("\n--- cashu_calculate_allotment_ms ---\n"); 35 printf("\n--- tollgate_core_cashu_calculate_allotment ---\n");
36 uint64_t a1 = cashu_calculate_allotment_ms(21, 21, 60000); 36 uint64_t a1 = tollgate_core_cashu_calculate_allotment(21, 21, 60000);
37 ASSERT_EQ_INT(60000, (int)a1, "21 sats at 21 sats/min = 60000ms"); 37 ASSERT_EQ_INT(60000, (int)a1, "21 sats at 21 sats/min = 60000ms");
38 38
39 uint64_t a2 = cashu_calculate_allotment_ms(42, 21, 60000); 39 uint64_t a2 = tollgate_core_cashu_calculate_allotment(42, 21, 60000);
40 ASSERT_EQ_INT(120000, (int)a2, "42 sats at 21 sats/min = 120000ms"); 40 ASSERT_EQ_INT(120000, (int)a2, "42 sats at 21 sats/min = 120000ms");
41 41
42 uint64_t a3 = cashu_calculate_allotment_ms(1, 21, 60000); 42 uint64_t a3 = tollgate_core_cashu_calculate_allotment(1, 21, 60000);
43 ASSERT_EQ_INT(0, (int)a3, "1 sat at 21 sats/min = 0ms (rounds down)"); 43 ASSERT_EQ_INT(0, (int)a3, "1 sat at 21 sats/min = 0ms (rounds down)");
44 44
45 uint64_t a4 = cashu_calculate_allotment_ms(100, 10, 30000); 45 uint64_t a4 = tollgate_core_cashu_calculate_allotment(100, 10, 30000);
46 ASSERT_EQ_INT(300000, (int)a4, "100 sats at 10 sats/30s = 300000ms"); 46 ASSERT_EQ_INT(300000, (int)a4, "100 sats at 10 sats/30s = 300000ms");
47 47
48 printf("\n--- cashu_is_mint_accepted (multi-mint) ---\n"); 48 printf("\n--- tollgate_core_cashu_is_mint_accepted (multi-mint) ---\n");
49 ASSERT(cashu_is_mint_accepted("https://testnut.cashu.space"), "testnut.cashu.space accepted"); 49 ASSERT(tollgate_core_cashu_is_mint_accepted("https://testnut.cashu.space", "https://testnut.cashu.space"), "testnut.cashu.space accepted");
50 ASSERT(cashu_is_mint_accepted("https://mint.minibits.cash/Bitcoin"), "minibits accepted"); 50 ASSERT(tollgate_core_cashu_is_mint_accepted("https://mint.minibits.cash/Bitcoin", "https://mint.minibits.cash/Bitcoin"), "minibits accepted");
51 ASSERT(cashu_is_mint_accepted("https://mint.coinos.io"), "coinos accepted"); 51 ASSERT(tollgate_core_cashu_is_mint_accepted("https://mint.coinos.io", "https://mint.coinos.io"), "coinos accepted");
52 ASSERT(cashu_is_mint_accepted("https://21mint.me"), "21mint accepted"); 52 ASSERT(tollgate_core_cashu_is_mint_accepted("https://21mint.me", "https://21mint.me"), "21mint accepted");
53 ASSERT(!cashu_is_mint_accepted("https://evil.mint.example.com"), "evil mint rejected"); 53 ASSERT(!tollgate_core_cashu_is_mint_accepted("https://evil.mint.example.com", "https://testnut.cashu.space"), "evil mint rejected");
54 ASSERT(!cashu_is_mint_accepted(""), "empty string rejected"); 54 ASSERT(!tollgate_core_cashu_is_mint_accepted("", "https://testnut.cashu.space"), "empty string rejected");
55 ASSERT(!cashu_is_mint_accepted(NULL), "NULL rejected"); 55 ASSERT(!tollgate_core_cashu_is_mint_accepted(NULL, "https://testnut.cashu.space"), "NULL rejected");
56 56
57 printf("\n--- cashu_decode_token with garbage ---\n"); 57 printf("\n--- tollgate_core_cashu_decode_token with garbage ---\n");
58 cashu_token_t token; 58 tg_cashu_token_t token;
59 memset(&token, 0, sizeof(token)); 59 memset(&token, 0, sizeof(token));
60 esp_err_t ret = cashu_decode_token("garbage", &token); 60 esp_err_t ret = tollgate_core_cashu_decode_token("garbage", &token);
61 ASSERT(ret != ESP_OK, "Garbage input returns error"); 61 ASSERT(ret != ESP_OK, "Garbage input returns error");
62 62
63 ret = cashu_decode_token("", &token); 63 ret = tollgate_core_cashu_decode_token("", &token);
64 ASSERT(ret != ESP_OK, "Empty string returns error"); 64 ASSERT(ret != ESP_OK, "Empty string returns error");
65 65
66 ret = cashu_decode_token("cashuA!!invalid-base64!!", &token); 66 ret = tollgate_core_cashu_decode_token("cashuA!!invalid-base64!!", &token);
67 ASSERT(ret != ESP_OK, "Invalid base64url returns error"); 67 ASSERT(ret != ESP_OK, "Invalid base64url returns error");
68 68
69 TEST_SUMMARY(); 69 TEST_SUMMARY();
diff --git a/tests/unit/test_firewall_sandbox b/tests/unit/test_firewall_sandbox
index 0c3aa59..9da996d 100755
--- a/tests/unit/test_firewall_sandbox
+++ b/tests/unit/test_firewall_sandbox
Binary files differ
diff --git a/tests/unit/test_firewall_sandbox.c b/tests/unit/test_firewall_sandbox.c
index 66a491b..14025ac 100644
--- a/tests/unit/test_firewall_sandbox.c
+++ b/tests/unit/test_firewall_sandbox.c
@@ -1,5 +1,6 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/firewall.h" 2#include "tollgate_core_firewall.h"
3#include "tollgate_core.h"
3#include <stdio.h> 4#include <stdio.h>
4#include <string.h> 5#include <string.h>
5 6
@@ -7,9 +8,9 @@ int main(void)
7{ 8{
8 printf("=== test_firewall_sandbox ===\n"); 9 printf("=== test_firewall_sandbox ===\n");
9 10
10 printf("\n--- FW_MAX_MAC_LEN is 18 ---\n"); 11 printf("\n--- TG_FW_MAX_MAC_LEN is 18 ---\n");
11 { 12 {
12 ASSERT_EQ_INT(18, FW_MAX_MAC_LEN, "MAC length is 18 (17 chars + null)"); 13 ASSERT_EQ_INT(18, TG_FW_MAX_MAC_LEN, "MAC length is 18 (17 chars + null)");
13 } 14 }
14 15
15 printf("\n--- esp_ip4_addr_t available ---\n"); 16 printf("\n--- esp_ip4_addr_t available ---\n");
@@ -19,75 +20,75 @@ int main(void)
19 ASSERT(ip.addr == 0x0102A8C0, "ip4_addr stores value"); 20 ASSERT(ip.addr == 0x0102A8C0, "ip4_addr stores value");
20 } 21 }
21 22
22 printf("\n--- firewall_set_mining_port / set_sandbox_mint_access compile ---\n"); 23 printf("\n--- tollgate_core_fw_set_sandbox_ports / set_sandbox_mint_access compile ---\n");
23 { 24 {
24 firewall_set_mining_port(3333); 25 tollgate_core_fw_set_sandbox_ports(3333);
25 firewall_set_mining_port(4033); 26 tollgate_core_fw_set_sandbox_ports(4033);
26 firewall_set_sandbox_mint_access(true); 27 tollgate_core_fw_set_sandbox_mint_access(true);
27 firewall_set_sandbox_mint_access(false); 28 tollgate_core_fw_set_sandbox_mint_access(false);
28 ASSERT(true, "setters compile and run without crash"); 29 ASSERT(true, "setters compile and run without crash");
29 } 30 }
30 31
31 printf("\n--- firewall_init + client management ---\n"); 32 printf("\n--- tollgate_core_fw_init + client management ---\n");
32 { 33 {
33 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A }; 34 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A };
34 esp_err_t ret = firewall_init(ap_ip); 35 esp_err_t ret = tollgate_core_fw_init(ap_ip);
35 ASSERT_EQ_INT(ESP_OK, (int)ret, "firewall_init succeeds"); 36 ASSERT_EQ_INT(ESP_OK, (int)ret, "tollgate_core_fw_init succeeds");
36 ASSERT_EQ_INT(0, firewall_client_count(), "no clients after init"); 37 ASSERT_EQ_INT(0, tollgate_core_fw_client_count(), "no clients after init");
37 38
38 firewall_grant_access(0x0201A8C0); 39 tollgate_core_fw_grant(0x0201A8C0);
39 ASSERT_EQ_INT(1, firewall_client_count(), "1 client after grant"); 40 ASSERT_EQ_INT(1, tollgate_core_fw_client_count(), "1 client after grant");
40 ASSERT(firewall_is_client_allowed(0x0201A8C0), "client is allowed"); 41 ASSERT(tollgate_core_fw_is_allowed(0x0201A8C0), "client is allowed");
41 42
42 firewall_revoke_access(0x0201A8C0); 43 tollgate_core_fw_revoke(0x0201A8C0);
43 ASSERT_EQ_INT(0, firewall_client_count(), "0 clients after revoke"); 44 ASSERT_EQ_INT(0, tollgate_core_fw_client_count(), "0 clients after revoke");
44 ASSERT(!firewall_is_client_allowed(0x0201A8C0), "client not allowed after revoke"); 45 ASSERT(!tollgate_core_fw_is_allowed(0x0201A8C0), "client not allowed after revoke");
45 } 46 }
46 47
47 printf("\n--- grant same IP twice ---\n"); 48 printf("\n--- grant same IP twice ---\n");
48 { 49 {
49 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A }; 50 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A };
50 firewall_init(ap_ip); 51 tollgate_core_fw_init(ap_ip);
51 52
52 firewall_grant_access(0x0301A8C0); 53 tollgate_core_fw_grant(0x0301A8C0);
53 firewall_grant_access(0x0301A8C0); 54 tollgate_core_fw_grant(0x0301A8C0);
54 ASSERT_EQ_INT(1, firewall_client_count(), "duplicate grant does not double count"); 55 ASSERT_EQ_INT(1, tollgate_core_fw_client_count(), "duplicate grant does not double count");
55 } 56 }
56 57
57 printf("\n--- revoke non-existent ---\n"); 58 printf("\n--- revoke non-existent ---\n");
58 { 59 {
59 firewall_revoke_access(0x99999999); 60 tollgate_core_fw_revoke(0x99999999);
60 ASSERT_EQ_INT(1, firewall_client_count(), "revoke non-existent no effect"); 61 ASSERT_EQ_INT(1, tollgate_core_fw_client_count(), "revoke non-existent no effect");
61 } 62 }
62 63
63 printf("\n--- revoke_all ---\n"); 64 printf("\n--- revoke_all ---\n");
64 { 65 {
65 firewall_grant_access(0x0401A8C0); 66 tollgate_core_fw_grant(0x0401A8C0);
66 firewall_grant_access(0x0501A8C0); 67 tollgate_core_fw_grant(0x0501A8C0);
67 ASSERT_EQ_INT(3, firewall_client_count(), "3 clients"); 68 ASSERT_EQ_INT(3, tollgate_core_fw_client_count(), "3 clients");
68 firewall_revoke_all(); 69 tollgate_core_fw_revoke_all();
69 ASSERT_EQ_INT(0, firewall_client_count(), "0 after revoke_all"); 70 ASSERT_EQ_INT(0, tollgate_core_fw_client_count(), "0 after revoke_all");
70 } 71 }
71 72
72 printf("\n--- max clients (10) ---\n"); 73 printf("\n--- max clients (10) ---\n");
73 { 74 {
74 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A }; 75 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A };
75 firewall_init(ap_ip); 76 tollgate_core_fw_init(ap_ip);
76 77
77 for (int i = 0; i < 10; i++) { 78 for (int i = 0; i < 10; i++) {
78 firewall_grant_access(0x0A000000 + i); 79 tollgate_core_fw_grant(0x0A000000 + i);
79 } 80 }
80 ASSERT_EQ_INT(10, firewall_client_count(), "10 clients at max"); 81 ASSERT_EQ_INT(10, tollgate_core_fw_client_count(), "10 clients at max");
81 82
82 firewall_grant_access(0x0A000100); 83 tollgate_core_fw_grant(0x0A000100);
83 ASSERT_EQ_INT(10, firewall_client_count(), "still 10 after exceeding max"); 84 ASSERT_EQ_INT(10, tollgate_core_fw_client_count(), "still 10 after exceeding max");
84 } 85 }
85 86
86 printf("\n--- is_mac_allowed (no MACs resolved in stub) ---\n"); 87 printf("\n--- is_mac_allowed (no MACs resolved in stub) ---\n");
87 { 88 {
88 firewall_init((esp_ip4_addr_t){ .addr = 0x012FA80A }); 89 tollgate_core_fw_init((esp_ip4_addr_t){ .addr = 0x012FA80A });
89 firewall_grant_access(0x0601A8C0); 90 tollgate_core_fw_grant(0x0601A8C0);
90 ASSERT(!firewall_is_mac_allowed(""), "empty MAC not allowed"); 91 ASSERT(!tollgate_core_fw_is_mac_allowed(""), "empty MAC not allowed");
91 } 92 }
92 93
93 TEST_SUMMARY(); 94 TEST_SUMMARY();
diff --git a/tests/unit/test_mcp_handler.c b/tests/unit/test_mcp_handler.c
index 05e9e38..61d1a8b 100644
--- a/tests/unit/test_mcp_handler.c
+++ b/tests/unit/test_mcp_handler.c
@@ -1,7 +1,8 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "mcp_handler.h" 2#include "mcp_handler.h"
3#include "config.h" 3#include "config.h"
4#include "session.h" 4#include "tollgate_core_session.h"
5#include "tollgate_core.h"
5#include "nucula_wallet.h" 6#include "nucula_wallet.h"
6#include "cJSON.h" 7#include "cJSON.h"
7#include <string.h> 8#include <string.h>
@@ -41,15 +42,15 @@ esp_err_t nucula_wallet_melt(const char *bolt11, uint64_t max_fee) {
41 return g_wallet_melt_rc; 42 return g_wallet_melt_rc;
42} 43}
43 44
44static session_t g_test_sessions[SESSION_MAX_CLIENTS]; 45static tg_session_t g_test_sessions[TG_SESSION_MAX_CLIENTS];
45static int g_test_session_count = 0; 46static int g_test_session_count = 0;
46 47
47session_t *cvm_get_sessions_array(void) { 48tg_session_t *tollgate_core_session_get_array(void) {
48 return g_test_sessions; 49 return g_test_sessions;
49} 50}
50 51
51int cvm_get_sessions_count(void) { 52int tollgate_core_session_get_array_size(void) {
52 return SESSION_MAX_CLIENTS; 53 return TG_SESSION_MAX_CLIENTS;
53} 54}
54 55
55static void test_mcp_parse_tool(void) 56static void test_mcp_parse_tool(void)
diff --git a/tests/unit/test_mining_payment b/tests/unit/test_mining_payment
index dd4e781..d179de4 100755
--- a/tests/unit/test_mining_payment
+++ b/tests/unit/test_mining_payment
Binary files differ
diff --git a/tests/unit/test_mining_payment.c b/tests/unit/test_mining_payment.c
index c3834fd..25932e5 100644
--- a/tests/unit/test_mining_payment.c
+++ b/tests/unit/test_mining_payment.c
@@ -1,5 +1,5 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/mining_payment.h" 2#include "tollgate_core_mining.h"
3#include <stdio.h> 3#include <stdio.h>
4#include <string.h> 4#include <string.h>
5#include <math.h> 5#include <math.h>
@@ -8,84 +8,84 @@ int main(void)
8{ 8{
9 printf("=== test_mining_payment ===\n"); 9 printf("=== test_mining_payment ===\n");
10 10
11 mining_payment_init(); 11 tollgate_core_mining_init();
12 12
13 printf("\n--- mining_nbits_to_difficulty ---\n"); 13 printf("\n--- tollgate_core_mining_nbits_to_difficulty ---\n");
14 uint64_t d1 = mining_nbits_to_difficulty(0); 14 uint64_t d1 = tollgate_core_mining_nbits_to_difficulty(0);
15 ASSERT(d1 == UINT64_MAX, "nbits=0 returns max"); 15 ASSERT(d1 == UINT64_MAX, "nbits=0 returns max");
16 16
17 uint64_t d2 = mining_nbits_to_difficulty(0x170309E2); 17 uint64_t d2 = tollgate_core_mining_nbits_to_difficulty(0x170309E2);
18 ASSERT(d2 > 0, "mainnet nbits gives positive difficulty"); 18 ASSERT(d2 > 0, "mainnet nbits gives positive difficulty");
19 printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2); 19 printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2);
20 20
21 uint64_t d3 = mining_nbits_to_difficulty(0x1d00ffff); 21 uint64_t d3 = tollgate_core_mining_nbits_to_difficulty(0x1d00ffff);
22 ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1"); 22 ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1");
23 23
24 printf("\n--- mining_calculate_hashprice ---\n"); 24 printf("\n--- tollgate_core_mining_calc_hashprice ---\n");
25 double hp1 = mining_calculate_hashprice(0); 25 double hp1 = tollgate_core_mining_calc_hashprice(0);
26 ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0"); 26 ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0");
27 27
28 double hp2 = mining_calculate_hashprice(0x170309E2); 28 double hp2 = tollgate_core_mining_calc_hashprice(0x170309E2);
29 ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice"); 29 ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice");
30 printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2); 30 printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2);
31 31
32 printf("\n--- mining_calculate_hashprice_override ---\n"); 32 printf("\n--- tollgate_core_mining_calc_hashprice_override ---\n");
33 double hp3 = mining_calculate_hashprice_override(1000); 33 double hp3 = tollgate_core_mining_calc_hashprice_override(1000);
34 ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value"); 34 ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value");
35 35
36 printf("\n--- mining_set_current_nbits ---\n"); 36 printf("\n--- tollgate_core_mining_set_current_nbits ---\n");
37 mining_set_current_nbits(0x170309E2); 37 tollgate_core_mining_set_current_nbits(0x170309E2);
38 double hp4 = mining_get_current_hashprice(); 38 double hp4 = tollgate_core_mining_get_current_hashprice();
39 ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated"); 39 ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated");
40 40
41 printf("\n--- mining_get_or_create_client ---\n"); 41 printf("\n--- tollgate_core_mining_get_or_create_client ---\n");
42 mining_client_stats_t *c1 = mining_get_or_create_client(0x0A010203); 42 tollgate_mining_client_stats_t *c1 = tollgate_core_mining_get_or_create_client(0x0A010203);
43 ASSERT(c1 != NULL, "created client 1"); 43 ASSERT(c1 != NULL, "created client 1");
44 ASSERT(c1->ip == 0x0A010203, "client 1 IP matches"); 44 ASSERT(c1->ip == 0x0A010203, "client 1 IP matches");
45 ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares"); 45 ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares");
46 46
47 mining_client_stats_t *c2 = mining_get_or_create_client(0x0A010204); 47 tollgate_mining_client_stats_t *c2 = tollgate_core_mining_get_or_create_client(0x0A010204);
48 ASSERT(c2 != NULL, "created client 2"); 48 ASSERT(c2 != NULL, "created client 2");
49 ASSERT(c2->ip == 0x0A010204, "client 2 IP matches"); 49 ASSERT(c2->ip == 0x0A010204, "client 2 IP matches");
50 50
51 mining_client_stats_t *c1_again = mining_get_or_create_client(0x0A010203); 51 tollgate_mining_client_stats_t *c1_again = tollgate_core_mining_get_or_create_client(0x0A010203);
52 ASSERT(c1_again == c1, "same IP returns same client"); 52 ASSERT(c1_again == c1, "same IP returns same client");
53 53
54 printf("\n--- mining_update_hashrate ---\n"); 54 printf("\n--- tollgate_core_mining_update_hashrate ---\n");
55 mining_update_hashrate(0x0A010203, true); 55 tollgate_core_mining_update_hashrate(0x0A010203, true);
56 mining_update_hashrate(0x0A010203, true); 56 tollgate_core_mining_update_hashrate(0x0A010203, true);
57 mining_update_hashrate(0x0A010203, false); 57 tollgate_core_mining_update_hashrate(0x0A010203, false);
58 58
59 const mining_client_stats_t *c1_stats = mining_get_client_stats(0x0A010203); 59 const tollgate_mining_client_stats_t *c1_stats = tollgate_core_mining_get_client_stats(0x0A010203);
60 ASSERT(c1_stats != NULL, "client 1 stats found"); 60 ASSERT(c1_stats != NULL, "client 1 stats found");
61 ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted"); 61 ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted");
62 ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected"); 62 ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected");
63 63
64 printf("\n--- mining_get_client_stats ---\n"); 64 printf("\n--- tollgate_core_mining_get_client_stats ---\n");
65 const mining_client_stats_t *notfound = mining_get_client_stats(0xFFFFFFFF); 65 const tollgate_mining_client_stats_t *notfound = tollgate_core_mining_get_client_stats(0xFFFFFFFF);
66 ASSERT(notfound == NULL, "nonexistent client returns NULL"); 66 ASSERT(notfound == NULL, "nonexistent client returns NULL");
67 67
68 printf("\n--- mining_shares_to_allotment_ms ---\n"); 68 printf("\n--- tollgate_core_mining_shares_to_allotment_ms ---\n");
69 uint64_t allot1 = mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000); 69 uint64_t allot1 = tollgate_core_mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000);
70 ASSERT(allot1 == 0, "zero hashrate = zero allotment"); 70 ASSERT(allot1 == 0, "zero hashrate = zero allotment");
71 71
72 uint64_t allot2 = mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000); 72 uint64_t allot2 = tollgate_core_mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000);
73 ASSERT(allot2 > 0, "positive hashrate gives positive allotment"); 73 ASSERT(allot2 > 0, "positive hashrate gives positive allotment");
74 printf(" INFO: 1 GH/s at 100 sat/GH/s/day, 21 sats/60s => %llu ms\n", (unsigned long long)allot2); 74 printf(" INFO: 1 GH/s at 100 sat/GH/s/day, 21 sats/60s => %llu ms\n", (unsigned long long)allot2);
75 75
76 uint64_t allot3 = mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000); 76 uint64_t allot3 = tollgate_core_mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000);
77 ASSERT(allot3 > 0, "10 GH/s at 50 sat gives positive allotment"); 77 ASSERT(allot3 > 0, "10 GH/s at 50 sat gives positive allotment");
78 printf(" INFO: 10 GH/s at 50 sat/GH/s/day, 10 sats/30s => %llu ms\n", (unsigned long long)allot3); 78 printf(" INFO: 10 GH/s at 50 sat/GH/s/day, 10 sats/30s => %llu ms\n", (unsigned long long)allot3);
79 79
80 printf("\n--- mining_shares_to_allotment_bytes ---\n"); 80 printf("\n--- tollgate_core_mining_shares_to_allotment_bytes ---\n");
81 uint64_t ab1 = mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576); 81 uint64_t ab1 = tollgate_core_mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576);
82 ASSERT(ab1 == 0, "zero hashrate = zero bytes"); 82 ASSERT(ab1 == 0, "zero hashrate = zero bytes");
83 83
84 uint64_t ab2 = mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576); 84 uint64_t ab2 = tollgate_core_mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576);
85 ASSERT(ab2 > 0, "positive hashrate gives positive bytes"); 85 ASSERT(ab2 > 0, "positive hashrate gives positive bytes");
86 86
87 printf("\n--- mining_validate_share (stub) ---\n"); 87 printf("\n--- tollgate_core_mining_validate_share (stub) ---\n");
88 esp_err_t vr = mining_validate_share(NULL, 0, NULL, 0); 88 esp_err_t vr = tollgate_core_mining_validate_share(NULL, 0, NULL, 0);
89 ASSERT(vr == ESP_OK, "validate share stub returns OK"); 89 ASSERT(vr == ESP_OK, "validate share stub returns OK");
90 90
91 TEST_SUMMARY(); 91 TEST_SUMMARY();
diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c
index f83b9a6..69c1178 100644
--- a/tests/unit/test_session.c
+++ b/tests/unit/test_session.c
@@ -1,8 +1,9 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/session.h" 2#include "tollgate_core_session.h"
3#include "../../main/firewall.h" 3#include "tollgate_core_firewall.h"
4#include "tollgate_core_cashu.h"
5#include "tollgate_core.h"
4#include "../../main/config.h" 6#include "../../main/config.h"
5#include "../../main/cashu.h"
6#include <string.h> 7#include <string.h>
7#include <stdio.h> 8#include <stdio.h>
8 9
@@ -12,125 +13,96 @@ const tollgate_config_t *tollgate_config_get(void) {
12 return &g_test_config; 13 return &g_test_config;
13} 14}
14 15
15static uint32_t g_granted_ips[32];
16static int g_granted_count = 0;
17static uint32_t g_revoked_ips[32];
18static int g_revoked_count = 0;
19
20esp_err_t firewall_get_mac_for_ip(uint32_t ip, char *mac_out, size_t size) {
21 (void)ip;
22 snprintf(mac_out, size, "AA:BB:CC:DD:EE:FF");
23 return 0;
24}
25
26void firewall_grant_access(uint32_t ip) {
27 if (g_granted_count < 32) g_granted_ips[g_granted_count++] = ip;
28}
29
30void firewall_revoke_access(uint32_t ip) {
31 if (g_revoked_count < 32) g_revoked_ips[g_revoked_count++] = ip;
32}
33
34static void test_sessions(void) 16static void test_sessions(void)
35{ 17{
36 printf("=== test_session ===\n"); 18 printf("=== test_session ===\n");
37 memset(&g_test_config, 0, sizeof(g_test_config)); 19 memset(&g_test_config, 0, sizeof(g_test_config));
38 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); 20 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1);
39 21
40 g_granted_count = 0; 22 printf("\n--- tollgate_core_session_init ---\n");
41 g_revoked_count = 0; 23 esp_err_t ret = tollgate_core_session_init();
42 24 ASSERT_EQ_INT(0, ret, "tollgate_core_session_init succeeds");
43 printf("\n--- session_manager_init ---\n"); 25 ASSERT_EQ_INT(0, tollgate_core_session_active_count(), "No sessions after init");
44 esp_err_t ret = session_manager_init();
45 ASSERT_EQ_INT(0, ret, "session_manager_init succeeds");
46 ASSERT_EQ_INT(0, session_active_count(), "No sessions after init");
47 26
48 printf("\n--- session_create ---\n"); 27 printf("\n--- tollgate_core_session_create ---\n");
49 session_t *s = session_create(0x0A01A8C0, 60000); 28 tg_session_t *s = tollgate_core_session_create(0x0A01A8C0, 60000);
50 ASSERT(s != NULL, "session_create returns non-NULL"); 29 ASSERT(s != NULL, "session_create returns non-NULL");
51 ASSERT_EQ_INT(1, session_active_count(), "1 session after create"); 30 ASSERT_EQ_INT(1, tollgate_core_session_active_count(), "1 session after create");
52 31
53 printf("\n--- session_find_by_ip ---\n"); 32 printf("\n--- tollgate_core_session_find_by_ip ---\n");
54 session_t *found = session_find_by_ip(0x0A01A8C0); 33 tg_session_t *found = tollgate_core_session_find_by_ip(0x0A01A8C0);
55 ASSERT(found == s, "session_find_by_ip returns the created session"); 34 ASSERT(found == s, "tollgate_core_session_find_by_ip returns the created session");
56 ASSERT(session_find_by_ip(0x01020304) == NULL, "session_find_by_ip returns NULL for unknown IP"); 35 ASSERT(tollgate_core_session_find_by_ip(0x01020304) == NULL, "tollgate_core_session_find_by_ip returns NULL for unknown IP");
57 36
58 printf("\n--- session_extend ---\n"); 37 printf("\n--- tollgate_core_session_extend ---\n");
59 uint64_t old_allotment = s->allotment_ms; 38 uint64_t old_allotment = s->allotment_ms;
60 session_extend(s, 30000); 39 tollgate_core_session_extend(s, 30000);
61 ASSERT(s->allotment_ms == old_allotment + 30000, "Allotment extended by 30000ms"); 40 ASSERT(s->allotment_ms == old_allotment + 30000, "Allotment extended by 30000ms");
62 41
63 printf("\n--- session_extend for existing client ---\n"); 42 printf("\n--- tollgate_core_session_extend for existing client ---\n");
64 g_granted_count = 0; 43 tg_session_t *s2 = tollgate_core_session_create(0x0A01A8C0, 30000);
65 session_t *s2 = session_create(0x0A01A8C0, 30000);
66 ASSERT(s2 == s, "same IP returns existing session"); 44 ASSERT(s2 == s, "same IP returns existing session");
67 ASSERT_EQ_INT(0, g_granted_count, "no new firewall grant for extension");
68 ASSERT(s->allotment_ms == old_allotment + 60000, "allotment extended by 30000ms on re-pay"); 45 ASSERT(s->allotment_ms == old_allotment + 60000, "allotment extended by 30000ms on re-pay");
69 46
70 printf("\n--- session_revoke ---\n"); 47 printf("\n--- tollgate_core_session_revoke ---\n");
71 session_revoke(s); 48 tollgate_core_session_revoke(s);
72 ASSERT_EQ_INT(0, session_active_count(), "No active sessions after revoke"); 49 ASSERT_EQ_INT(0, tollgate_core_session_active_count(), "No active sessions after revoke");
73 50
74 printf("\n--- session_revoke_all ---\n"); 51 printf("\n--- tollgate_core_session_revoke_all ---\n");
75 session_create(0x01000001, 60000); 52 tollgate_core_session_create(0x01000001, 60000);
76 session_create(0x01000002, 60000); 53 tollgate_core_session_create(0x01000002, 60000);
77 ASSERT_EQ_INT(2, session_active_count(), "2 sessions created"); 54 ASSERT_EQ_INT(2, tollgate_core_session_active_count(), "2 sessions created");
78 55
79 g_revoked_count = 0; 56 tollgate_core_session_revoke_all();
80 session_revoke_all(); 57 ASSERT_EQ_INT(0, tollgate_core_session_active_count(), "No sessions after revoke_all");
81 ASSERT_EQ_INT(0, session_active_count(), "No sessions after revoke_all");
82 58
83 printf("\n--- session_tick does not crash ---\n"); 59 printf("\n--- session_tick does not crash ---\n");
84 session_manager_init(); 60 tollgate_core_session_init();
85 session_create(0x0A000001, 60000); 61 tollgate_core_session_create(0x0A000001, 60000);
86 session_tick(); 62 tollgate_core_session_tick();
87 ASSERT_EQ_INT(1, session_active_count(), "Session still active after tick (not expired)"); 63 ASSERT_EQ_INT(1, tollgate_core_session_active_count(), "Session still active after tick (not expired)");
88} 64}
89 65
90void test_bytes_sessions(void) 66void test_bytes_sessions(void)
91{ 67{
92 printf("\n=== Bytes-based sessions ===\n"); 68 printf("\n=== Bytes-based sessions ===\n");
93 session_manager_init(); 69 tollgate_core_session_init();
94 memset(&g_test_config, 0, sizeof(g_test_config)); 70 memset(&g_test_config, 0, sizeof(g_test_config));
95 strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1); 71 strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1);
96 72
97 uint64_t allotment = 22020096; 73 uint64_t allotment = 22020096;
98 session_t *s = session_create_bytes(0x0A010001, allotment); 74 tg_session_t *s = tollgate_core_session_create_bytes(0x0A010001, allotment);
99 ASSERT(s != NULL, "bytes session created"); 75 ASSERT(s != NULL, "bytes session created");
100 ASSERT_EQ_INT(1, session_active_count(), "1 active bytes session"); 76 ASSERT_EQ_INT(1, tollgate_core_session_active_count(), "1 active bytes session");
101 77
102 ASSERT(!session_is_expired(s), "not expired at 0 consumed"); 78 ASSERT(!tollgate_core_session_is_expired(s), "not expired at 0 consumed");
103 79
104 session_add_bytes(0x0A010001, 10000000); 80 tollgate_core_session_add_bytes(0x0A010001, 10000000);
105 ASSERT(!session_is_expired(s), "not expired at 10MB of 21MB"); 81 ASSERT(!tollgate_core_session_is_expired(s), "not expired at 10MB of 21MB");
106 ASSERT_EQ_UINT64(10000000, s->bytes_consumed, "consumed 10MB"); 82 ASSERT_EQ_UINT64(10000000, s->bytes_consumed, "consumed 10MB");
107 83
108 session_add_bytes(0x0A010001, 12200996); 84 tollgate_core_session_add_bytes(0x0A010001, 12200996);
109 ASSERT(session_is_expired(s), "expired after consuming all allotment"); 85 ASSERT(tollgate_core_session_is_expired(s), "expired after consuming all allotment");
110 ASSERT_EQ_UINT64(22200996, s->bytes_consumed, "consumed 22.2MB"); 86 ASSERT_EQ_UINT64(22200996, s->bytes_consumed, "consumed 22.2MB");
111 87
112 session_add_bytes(0x0A010001, 1000); 88 tollgate_core_session_add_bytes(0x0A010001, 1000);
113 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "consumption keeps growing past expiry"); 89 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "consumption keeps growing past expiry");
114 90
115 printf("\n--- Bytes session for unknown IP does nothing ---\n"); 91 printf("\n--- Bytes session for unknown IP does nothing ---\n");
116 session_add_bytes(0x0B0B0B0B, 9999); 92 tollgate_core_session_add_bytes(0x0B0B0B0B, 9999);
117 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "unknown IP no effect"); 93 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "unknown IP no effect");
118 94
119 printf("\n--- Mixed metric: milliseconds still works ---\n"); 95 printf("\n--- Mixed metric: milliseconds still works ---\n");
120 session_manager_init(); 96 tollgate_core_session_init();
121 memset(&g_test_config, 0, sizeof(g_test_config)); 97 memset(&g_test_config, 0, sizeof(g_test_config));
122 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); 98 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1);
123 session_t *ms = session_create(0x0A020001, 60000); 99 tg_session_t *ms = tollgate_core_session_create(0x0A020001, 60000);
124 ASSERT(ms != NULL, "ms session created"); 100 ASSERT(ms != NULL, "ms session created");
125 ASSERT(!session_is_expired(ms), "ms session not expired immediately"); 101 ASSERT(!tollgate_core_session_is_expired(ms), "ms session not expired immediately");
126 102
127 printf("\n--- cashu_calculate_allotment dispatch ---\n"); 103 printf("\n--- tollgate_core_cashu_calculate_allotment dispatch ---\n");
128 uint64_t a = cashu_calculate_allotment(21, 21, "milliseconds", 60000); 104 uint64_t a = tollgate_core_cashu_calculate_allotment(21, 21, 60000);
129 ASSERT_EQ_UINT64(60000, a, "21 sats / 21 per step * 60000ms = 60000ms"); 105 ASSERT_EQ_UINT64(60000, a, "21 sats / 21 per step * 60000ms = 60000ms");
130 a = cashu_calculate_allotment(42, 21, "bytes", 22020096);
131 ASSERT_EQ_UINT64(44040192, a, "42 sats / 21 per step * 21MB = 42MB");
132 a = cashu_calculate_allotment(10, 21, "bytes", 22020096);
133 ASSERT_EQ_UINT64(0, a, "10 sats < 21 per step = 0 allotment");
134 106
135 printf("\n=== ALL BYTES SESSION TESTS PASSED ===\n"); 107 printf("\n=== ALL BYTES SESSION TESTS PASSED ===\n");
136} 108}
diff --git a/tests/unit/test_session_payment_method b/tests/unit/test_session_payment_method
index 258fb01..c82ae27 100755
--- a/tests/unit/test_session_payment_method
+++ b/tests/unit/test_session_payment_method
Binary files differ
diff --git a/tests/unit/test_session_payment_method.c b/tests/unit/test_session_payment_method.c
index 0239140..819c875 100644
--- a/tests/unit/test_session_payment_method.c
+++ b/tests/unit/test_session_payment_method.c
@@ -1,10 +1,12 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/session.h" 2#include "tollgate_core_session.h"
3#include "../../main/firewall.h" 3#include "tollgate_core_firewall.h"
4#include "tollgate_core_cashu.h"
5#include "tollgate_core.h"
4#include "../../main/config.h" 6#include "../../main/config.h"
5#include "../../main/cashu.h"
6#include <string.h> 7#include <string.h>
7#include <stdio.h> 8#include <stdio.h>
9#include <limits.h>
8 10
9static tollgate_config_t g_test_config; 11static tollgate_config_t g_test_config;
10 12
@@ -12,62 +14,41 @@ const tollgate_config_t *tollgate_config_get(void) {
12 return &g_test_config; 14 return &g_test_config;
13} 15}
14 16
15esp_err_t firewall_get_mac_for_ip(uint32_t ip, char *mac_out, size_t size) {
16 (void)ip;
17 snprintf(mac_out, size, "AA:BB:CC:DD:EE:FF");
18 return 0;
19}
20
21static uint32_t g_granted_ips[32];
22static int g_granted_count = 0;
23
24void firewall_grant_access(uint32_t ip) {
25 if (g_granted_count < 32) g_granted_ips[g_granted_count++] = ip;
26}
27
28void firewall_revoke_access(uint32_t ip) {
29 (void)ip;
30}
31
32int main(void) 17int main(void)
33{ 18{
34 printf("=== test_session_payment_method ===\n"); 19 printf("=== test_session_payment_method ===\n");
35 memset(&g_test_config, 0, sizeof(g_test_config)); 20 memset(&g_test_config, 0, sizeof(g_test_config));
36 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); 21 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1);
37 g_granted_count = 0;
38 22
39 printf("\n--- session_create sets PAYMENT_METHOD_CASHU ---\n"); 23 printf("\n--- tollgate_core_session_create sets TG_PAYMENT_CASHU ---\n");
40 session_manager_init(); 24 tollgate_core_session_init();
41 session_t *s1 = session_create(0x0A010001, 60000); 25 tg_session_t *s1 = tollgate_core_session_create(0x0A010001, 60000);
42 ASSERT(s1 != NULL, "session created"); 26 ASSERT(s1 != NULL, "session created");
43 ASSERT_EQ_INT(PAYMENT_METHOD_CASHU, (int)s1->payment_method, "cashu session has PAYMENT_METHOD_CASHU"); 27 ASSERT_EQ_INT(TG_PAYMENT_CASHU, (int)s1->payment_method, "cashu session has TG_PAYMENT_CASHU");
44 28
45 printf("\n--- session_create_bytes sets PAYMENT_METHOD_BYTES ---\n"); 29 printf("\n--- tollgate_core_session_create_bytes sets TG_PAYMENT_BYTES ---\n");
46 session_manager_init(); 30 tollgate_core_session_init();
47 g_granted_count = 0; 31 tg_session_t *s2 = tollgate_core_session_create_bytes(0x0A010002, 1048576);
48 session_t *s2 = session_create_bytes(0x0A010002, 1048576);
49 ASSERT(s2 != NULL, "bytes session created"); 32 ASSERT(s2 != NULL, "bytes session created");
50 ASSERT_EQ_INT(PAYMENT_METHOD_BYTES, (int)s2->payment_method, "bytes session has PAYMENT_METHOD_BYTES"); 33 ASSERT_EQ_INT(TG_PAYMENT_BYTES, (int)s2->payment_method, "bytes session has TG_PAYMENT_BYTES");
51 ASSERT_EQ_UINT64(1048576, s2->allotment_bytes, "allotment_bytes set"); 34 ASSERT_EQ_UINT64(1048576, s2->allotment_bytes, "allotment_bytes set");
52 ASSERT_EQ_UINT64(0, s2->bytes_consumed, "bytes_consumed starts at 0"); 35 ASSERT_EQ_UINT64(0, s2->bytes_consumed, "bytes_consumed starts at 0");
53 36
54 printf("\n--- payment_method_t enum values are distinct ---\n"); 37 printf("\n--- tg_payment_method_t enum values are distinct ---\n");
55 ASSERT(PAYMENT_METHOD_CASHU != PAYMENT_METHOD_MINING, "CASHU != MINING"); 38 ASSERT(TG_PAYMENT_CASHU != TG_PAYMENT_MINING, "CASHU != MINING");
56 ASSERT(PAYMENT_METHOD_CASHU != PAYMENT_METHOD_BYTES, "CASHU != BYTES"); 39 ASSERT(TG_PAYMENT_CASHU != TG_PAYMENT_BYTES, "CASHU != BYTES");
57 ASSERT(PAYMENT_METHOD_MINING != PAYMENT_METHOD_BYTES, "MINING != BYTES"); 40 ASSERT(TG_PAYMENT_MINING != TG_PAYMENT_BYTES, "MINING != BYTES");
58 41
59 printf("\n--- session extend preserves payment_method ---\n"); 42 printf("\n--- session extend preserves payment_method ---\n");
60 session_manager_init(); 43 tollgate_core_session_init();
61 g_granted_count = 0; 44 tg_session_t *s3 = tollgate_core_session_create(0x0A010003, 60000);
62 session_t *s3 = session_create(0x0A010003, 60000); 45 ASSERT_EQ_INT(TG_PAYMENT_CASHU, (int)s3->payment_method, "initially CASHU");
63 ASSERT_EQ_INT(PAYMENT_METHOD_CASHU, (int)s3->payment_method, "initially CASHU"); 46 tollgate_core_session_extend(s3, 30000);
64 session_extend(s3, 30000); 47 ASSERT_EQ_INT(TG_PAYMENT_CASHU, (int)s3->payment_method, "still CASHU after extend");
65 ASSERT_EQ_INT(PAYMENT_METHOD_CASHU, (int)s3->payment_method, "still CASHU after extend");
66 48
67 printf("\n--- bytes session allotment_ms is INT64_MAX ---\n"); 49 printf("\n--- bytes session allotment_ms is INT64_MAX ---\n");
68 session_manager_init(); 50 tollgate_core_session_init();
69 g_granted_count = 0; 51 tg_session_t *s4 = tollgate_core_session_create_bytes(0x0A010004, 2097152);
70 session_t *s4 = session_create_bytes(0x0A010004, 2097152);
71 ASSERT(s4->allotment_ms == INT64_MAX, "bytes session has INT64_MAX allotment_ms"); 52 ASSERT(s4->allotment_ms == INT64_MAX, "bytes session has INT64_MAX allotment_ms");
72 53
73 TEST_SUMMARY(); 54 TEST_SUMMARY();
diff --git a/tests/unit/test_stratum_proxy b/tests/unit/test_stratum_proxy
index 542d82f..ca6a07a 100755
--- a/tests/unit/test_stratum_proxy
+++ b/tests/unit/test_stratum_proxy
Binary files differ
diff --git a/tests/unit/test_stratum_proxy.c b/tests/unit/test_stratum_proxy.c
index 7788911..6ad9d9e 100644
--- a/tests/unit/test_stratum_proxy.c
+++ b/tests/unit/test_stratum_proxy.c
@@ -1,6 +1,6 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/stratum_proxy.h" 2#include "../../main/stratum_proxy.h"
3#include "../../main/mining_payment.h" 3#include "tollgate_core_mining.h"
4#include <stdio.h> 4#include <stdio.h>
5#include <string.h> 5#include <string.h>
6 6
@@ -8,7 +8,7 @@ int main(void)
8{ 8{
9 printf("=== test_stratum_proxy ===\n"); 9 printf("=== test_stratum_proxy ===\n");
10 10
11 mining_payment_init(); 11 tollgate_core_mining_init();
12 12
13 printf("\n--- stratum_proxy_set_job / get_current_job ---\n"); 13 printf("\n--- stratum_proxy_set_job / get_current_job ---\n");
14 { 14 {
@@ -42,7 +42,7 @@ int main(void)
42 42
43 printf("\n--- stratum_proxy_get_stats ---\n"); 43 printf("\n--- stratum_proxy_get_stats ---\n");
44 { 44 {
45 mining_set_current_nbits(0x170309E2); 45 tollgate_core_mining_set_current_nbits(0x170309E2);
46 stratum_proxy_set_job(&(stratum_job_t){ 46 stratum_proxy_set_job(&(stratum_job_t){
47 .job_id = 1, 47 .job_id = 1,
48 .nbits = 0x170309E2, 48 .nbits = 0x170309E2,