diff options
| author | Your Name <you@example.com> | 2026-05-23 02:32:24 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-23 02:32:24 +0530 |
| commit | 6771956f1647f377007ab61a59b116de6d536706 (patch) | |
| tree | e8b6c74164040339134e33b8616c27c23ec1ade6 /tollgate_core/src/tollgate_mining.h | |
| parent | 10c69b0ddc34a1fafada3f8b68a622b4aa2bea77 (diff) | |
feat: create tollgate_core standalone C library + tollgate_esp IDF component
Phase 1-4: Extract core business logic into reusable library
tollgate_core/ — standalone C library (zero ESP-IDF deps):
- tollgate_core.c — orchestrator (init, process_payment, process_share, tick)
- tollgate_cashu.c — V3 token decode, checkstate via platform callback, allotment math
- tollgate_session.c — session lifecycle state machine (create/find/revoke/extend)
- tollgate_firewall.c — client allowlist, NAT filter, packet filter
- tollgate_mining.c — hashprice, nbits→difficulty, shares-to-allotment, client stats
components/tollgate_esp/ — ESP-IDF component wrapper:
- tollgate_esp_platform.c — implements tollgate_platform_t using ESP-IDF APIs
- CMakeLists.txt — compiles tollgate_core + ESP wrapper, links to ESP-IDF
- idf_component.yml — for IDF Component Registry
Platform interface (tollgate_platform.h):
- Config access: price, step_size, mint_url, metric
- Time: get_time_ms (via FreeRTOS ticks on ESP)
- Logging: log_info/warn/error (via ESP_LOG on ESP)
- Wallet: receive/send/balance (via nucula on ESP)
- HTTP: http_post (via esp_http_client on ESP)
- WiFi/MAC: mac_for_ip, napt_enable
- Mining: stratum config, hashprice override
Build verification:
- tollgate_core compiles standalone: cmake + make (gcc, host)
- tollgate_esp + ESP-IDF full build: passes (idf.py build)
- Unit tests: 22/22 pass (make test-unit)
- No behavioral change to existing main/ code yet (Phase 5)
Diffstat (limited to 'tollgate_core/src/tollgate_mining.h')
| -rw-r--r-- | tollgate_core/src/tollgate_mining.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tollgate_core/src/tollgate_mining.h b/tollgate_core/src/tollgate_mining.h new file mode 100644 index 0000000..39681a5 --- /dev/null +++ b/tollgate_core/src/tollgate_mining.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_MINING_H | ||
| 2 | #define TOLLGATE_CORE_MINING_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | |||
| 7 | #define TG_MINING_SHARE_WINDOW_S 30 | ||
| 8 | #define TG_MINING_BLOCK_SUBSIDY_SATS 312500000ULL | ||
| 9 | #define TG_MINING_BLOCKS_PER_DAY 144ULL | ||
| 10 | #define TG_MINING_MAX_CLIENTS 10 | ||
| 11 | |||
| 12 | typedef struct { | ||
| 13 | uint32_t ip; | ||
| 14 | uint64_t shares_accepted; | ||
| 15 | uint64_t shares_rejected; | ||
| 16 | int64_t first_share_time_ms; | ||
| 17 | int64_t last_share_time_ms; | ||
| 18 | double hashrate_ghs; | ||
| 19 | } tg_mining_client_stats_t; | ||
| 20 | |||
| 21 | uint64_t tg_mining_nbits_to_difficulty(uint32_t nbits); | ||
| 22 | double tg_mining_calculate_hashprice(uint32_t nbits); | ||
| 23 | double tg_mining_calculate_hashprice_override(uint64_t sats_per_ghs_day); | ||
| 24 | int tg_mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len); | ||
| 25 | uint64_t tg_mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice, int price, int step_ms); | ||
| 26 | uint64_t tg_mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice, int price, int step_bytes); | ||
| 27 | tg_mining_client_stats_t *tg_mining_get_or_create_client(uint32_t client_ip); | ||
| 28 | void tg_mining_update_hashrate(uint32_t client_ip, bool accepted); | ||
| 29 | const tg_mining_client_stats_t *tg_mining_get_client_stats(uint32_t client_ip); | ||
| 30 | double tg_mining_get_current_hashprice(void); | ||
| 31 | void tg_mining_set_current_nbits(uint32_t nbits); | ||
| 32 | void tg_mining_init(void); | ||
| 33 | |||
| 34 | #endif | ||