upleb.uk

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

summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/tollgate_core/src/tollgate_core.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/components/tollgate_core/src/tollgate_core.c b/components/tollgate_core/src/tollgate_core.c
index 2b337c3..f2d77e0 100644
--- a/components/tollgate_core/src/tollgate_core.c
+++ b/components/tollgate_core/src/tollgate_core.c
@@ -3,6 +3,8 @@
3#include "tollgate_core_dns.h" 3#include "tollgate_core_dns.h"
4#include "tollgate_core_firewall.h" 4#include "tollgate_core_firewall.h"
5#include "tollgate_core_session.h" 5#include "tollgate_core_session.h"
6#include "tollgate_core_mining.h"
7#include "tollgate_core_stratum_proxy.h"
6#include "esp_log.h" 8#include "esp_log.h"
7#include "cJSON.h" 9#include "cJSON.h"
8#include <string.h> 10#include <string.h>
@@ -235,6 +237,89 @@ bool tollgate_core_is_owner_connected(void)
235 return s_owner_connected; 237 return s_owner_connected;
236} 238}
237 239
240void tollgate_core_on_share_accepted(uint32_t client_ip, double difficulty)
241{
242 if (!s_platform) return;
243
244 tollgate_core_mining_update_hashrate(client_ip, true);
245
246 const tollgate_mining_client_stats_t *stats = tollgate_core_mining_get_client_stats(client_ip);
247 if (!stats) return;
248
249 double hashprice = tollgate_core_calc_hashprice(stats->hashrate_ghs);
250 if (hashprice <= 0.0) return;
251
252 const char *metric = s_platform->get_metric ? s_platform->get_metric() : "milliseconds";
253 int price = s_platform->get_price_sats ? (int)s_platform->get_price_sats() : 21;
254 uint64_t allotment = 0;
255
256 if (strcmp(metric, "bytes") == 0) {
257 int step_bytes = s_platform->get_step_bytes ? (int)s_platform->get_step_bytes() : 22020096;
258 allotment = tollgate_core_mining_shares_to_allotment_bytes(
259 stats->hashrate_ghs, hashprice, price, step_bytes);
260 } else {
261 int step_ms = s_platform->get_step_ms ? (int)s_platform->get_step_ms() : 60000;
262 allotment = tollgate_core_mining_shares_to_allotment_ms(
263 stats->hashrate_ghs, hashprice, price, step_ms);
264 }
265
266 if (allotment == 0) return;
267
268 tg_session_t *session = tollgate_core_session_find_by_ip(client_ip);
269 if (!session) {
270 if (strcmp(metric, "bytes") == 0) {
271 session = tollgate_core_session_create_bytes(client_ip, allotment);
272 } else {
273 session = tollgate_core_session_create(client_ip, allotment);
274 }
275 if (session) {
276 session->payment_method = TG_PAYMENT_MINING;
277 }
278 } else {
279 tollgate_core_session_extend(session, allotment);
280 }
281
282 if (s_platform->on_share_accepted) {
283 s_platform->on_share_accepted(difficulty);
284 }
285}
286
287double tollgate_core_calc_hashprice(double hashrate_ghs)
288{
289 if (!s_platform) return 0.0;
290
291 if (s_platform->get_hashprice_sats_per_ghs_day) {
292 uint64_t override = s_platform->get_hashprice_sats_per_ghs_day();
293 if (override > 0) {
294 return tollgate_core_mining_calc_hashprice_override(override);
295 }
296 }
297
298 return tollgate_core_mining_get_current_hashprice();
299}
300
301char *tollgate_core_get_mining_status_json(void)
302{
303 tollgate_stratum_proxy_stats_t proxy_stats = {0};
304 tollgate_core_stratum_proxy_get_stats(&proxy_stats);
305
306 cJSON *root = cJSON_CreateObject();
307
308 cJSON *proxy = cJSON_CreateObject();
309 cJSON_AddNumberToObject(proxy, "hashrate_ghs", proxy_stats.hashrate_ghs);
310 cJSON_AddNumberToObject(proxy, "total_shares", (double)proxy_stats.total_shares);
311 cJSON_AddNumberToObject(proxy, "total_accepted", (double)proxy_stats.total_accepted);
312 cJSON_AddNumberToObject(proxy, "total_rejected", (double)proxy_stats.total_rejected);
313 cJSON_AddNumberToObject(proxy, "active_miners", proxy_stats.active_miners);
314 cJSON_AddNumberToObject(proxy, "hashprice", proxy_stats.current_hashprice);
315 cJSON_AddNumberToObject(proxy, "nbits", (double)proxy_stats.nbits);
316 cJSON_AddItemToObject(root, "proxy", proxy);
317
318 char *json = cJSON_PrintUnformatted(root);
319 cJSON_Delete(root);
320 return json;
321}
322
238const tollgate_platform_t *tollgate_core_get_platform(void) 323const tollgate_platform_t *tollgate_core_get_platform(void)
239{ 324{
240 return s_platform; 325 return s_platform;