diff options
Diffstat (limited to 'tests/unit/test_mining_api.c')
| -rw-r--r-- | tests/unit/test_mining_api.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/unit/test_mining_api.c b/tests/unit/test_mining_api.c new file mode 100644 index 0000000..985992b --- /dev/null +++ b/tests/unit/test_mining_api.c | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "tollgate_core.h" | ||
| 3 | #include "tollgate_core_mining.h" | ||
| 4 | #include "tollgate_core_session.h" | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <string.h> | ||
| 8 | #include <math.h> | ||
| 9 | |||
| 10 | static const char *s_mint_url = "https://test.mint.example"; | ||
| 11 | static const char *s_metric = "milliseconds"; | ||
| 12 | static uint16_t s_price = 21; | ||
| 13 | static int32_t s_step_ms = 60000; | ||
| 14 | static int32_t s_step_bytes = 22020096; | ||
| 15 | static uint64_t s_hashprice_override = 0; | ||
| 16 | static double s_last_share_diff = 0.0; | ||
| 17 | |||
| 18 | static const char *mock_get_mint_url(void) { return s_mint_url; } | ||
| 19 | static const char *mock_get_metric(void) { return s_metric; } | ||
| 20 | static uint16_t mock_get_price(void) { return s_price; } | ||
| 21 | static int32_t mock_get_step_ms(void) { return s_step_ms; } | ||
| 22 | static int32_t mock_get_step_bytes(void) { return s_step_bytes; } | ||
| 23 | static uint64_t mock_get_hashprice(void) { return s_hashprice_override; } | ||
| 24 | static int64_t mock_get_time_ms(void) { return 1000000; } | ||
| 25 | static void mock_on_share_accepted(double diff) { s_last_share_diff = diff; } | ||
| 26 | static bool mock_spend_proofs(const char *t) { (void)t; return true; } | ||
| 27 | |||
| 28 | static tollgate_platform_t make_test_platform(void) | ||
| 29 | { | ||
| 30 | tollgate_platform_t p = {0}; | ||
| 31 | p.get_mint_url = mock_get_mint_url; | ||
| 32 | p.get_metric = mock_get_metric; | ||
| 33 | p.get_price_sats = mock_get_price; | ||
| 34 | p.get_step_ms = mock_get_step_ms; | ||
| 35 | p.get_step_bytes = mock_get_step_bytes; | ||
| 36 | p.get_hashprice_sats_per_ghs_day = mock_get_hashprice; | ||
| 37 | p.get_time_ms = mock_get_time_ms; | ||
| 38 | p.on_share_accepted = mock_on_share_accepted; | ||
| 39 | p.spend_proofs = mock_spend_proofs; | ||
| 40 | return p; | ||
| 41 | } | ||
| 42 | |||
| 43 | int main(void) | ||
| 44 | { | ||
| 45 | printf("=== test_mining_api ===\n"); | ||
| 46 | |||
| 47 | tollgate_platform_t platform = make_test_platform(); | ||
| 48 | esp_ip4_addr_t ap_ip = { .addr = 0x0101010A }; | ||
| 49 | tollgate_core_init(&platform, ap_ip); | ||
| 50 | |||
| 51 | tollgate_core_mining_init(); | ||
| 52 | tollgate_core_mining_set_current_nbits(0x1d00ffff); | ||
| 53 | |||
| 54 | printf("\n--- tollgate_core_calc_hashprice with nbits ---\n"); | ||
| 55 | double hp1 = tollgate_core_calc_hashprice(0.0); | ||
| 56 | ASSERT(hp1 > 0.0, "hashprice from nbits is positive"); | ||
| 57 | double expected_hp = tollgate_core_mining_get_current_hashprice(); | ||
| 58 | ASSERT(fabs(hp1 - expected_hp) < 0.001, "matches mining module hashprice"); | ||
| 59 | |||
| 60 | printf("\n--- tollgate_core_calc_hashprice with override ---\n"); | ||
| 61 | s_hashprice_override = 500; | ||
| 62 | double hp2 = tollgate_core_calc_hashprice(0.0); | ||
| 63 | ASSERT(fabs(hp2 - 500.0) < 0.001, "override hashprice returned"); | ||
| 64 | s_hashprice_override = 0; | ||
| 65 | |||
| 66 | printf("\n--- tollgate_core_calc_hashprice no platform ---\n"); | ||
| 67 | double hp3 = tollgate_core_calc_hashprice(0.0); | ||
| 68 | ASSERT(hp3 == 0.0 || hp3 > 0.0, "NULL platform handled gracefully"); | ||
| 69 | tollgate_core_init(&platform, ap_ip); | ||
| 70 | tollgate_core_mining_init(); | ||
| 71 | tollgate_core_mining_set_current_nbits(0x1d00ffff); | ||
| 72 | |||
| 73 | printf("\n--- tollgate_core_get_mining_status_json ---\n"); | ||
| 74 | char *json = tollgate_core_get_mining_status_json(); | ||
| 75 | ASSERT(json != NULL, "mining status JSON returned"); | ||
| 76 | ASSERT(strstr(json, "proxy") != NULL, "JSON contains proxy object"); | ||
| 77 | ASSERT(strstr(json, "hashrate_ghs") != NULL, "JSON contains hashrate_ghs"); | ||
| 78 | ASSERT(strstr(json, "total_shares") != NULL, "JSON contains total_shares"); | ||
| 79 | ASSERT(strstr(json, "total_accepted") != NULL, "JSON contains total_accepted"); | ||
| 80 | ASSERT(strstr(json, "hashprice") != NULL, "JSON contains hashprice"); | ||
| 81 | printf(" JSON: %s\n", json); | ||
| 82 | free(json); | ||
| 83 | |||
| 84 | printf("\n--- tollgate_core_on_share_accepted creates session ---\n"); | ||
| 85 | uint32_t client_ip = 0x0202020A; | ||
| 86 | tollgate_core_on_share_accepted(client_ip, 1.0); | ||
| 87 | |||
| 88 | tg_session_t *session = tollgate_core_session_find_by_ip(client_ip); | ||
| 89 | ASSERT(session != NULL, "session created for miner"); | ||
| 90 | ASSERT(session->active, "session is active"); | ||
| 91 | ASSERT(session->payment_method == TG_PAYMENT_MINING, "payment method is MINING"); | ||
| 92 | ASSERT(session->allotment_ms > 0, "session has positive allotment"); | ||
| 93 | printf(" allotment_ms = %llu\n", (unsigned long long)session->allotment_ms); | ||
| 94 | |||
| 95 | printf("\n--- tollgate_core_on_share_accepted extends session ---\n"); | ||
| 96 | uint64_t first_allotment = session->allotment_ms; | ||
| 97 | tollgate_core_on_share_accepted(client_ip, 1.0); | ||
| 98 | ASSERT(session->allotment_ms > first_allotment, "session allotment increased"); | ||
| 99 | printf(" new allotment_ms = %llu\n", (unsigned long long)session->allotment_ms); | ||
| 100 | |||
| 101 | printf("\n--- tollgate_core_on_share_accepted calls platform callback ---\n"); | ||
| 102 | s_last_share_diff = 0.0; | ||
| 103 | tollgate_core_on_share_accepted(client_ip, 42.5); | ||
| 104 | ASSERT(fabs(s_last_share_diff - 42.5) < 0.001, "platform callback received difficulty"); | ||
| 105 | |||
| 106 | printf("\n--- tollgate_core_on_share_accepted with bytes metric ---\n"); | ||
| 107 | s_metric = "bytes"; | ||
| 108 | uint32_t bytes_client = 0x0303030A; | ||
| 109 | tollgate_core_on_share_accepted(bytes_client, 1.0); | ||
| 110 | tg_session_t *bytes_session = tollgate_core_session_find_by_ip(bytes_client); | ||
| 111 | ASSERT(bytes_session != NULL, "bytes session created"); | ||
| 112 | ASSERT(bytes_session->allotment_bytes > 0, "bytes allotment positive"); | ||
| 113 | printf(" allotment_bytes = %llu\n", (unsigned long long)bytes_session->allotment_bytes); | ||
| 114 | s_metric = "milliseconds"; | ||
| 115 | |||
| 116 | printf("\n--- tollgate_core_on_share_accepted no platform ---\n"); | ||
| 117 | tollgate_core_init(NULL, ap_ip); | ||
| 118 | tollgate_core_on_share_accepted(0x0404040A, 1.0); | ||
| 119 | ASSERT(1, "no crash with NULL platform"); | ||
| 120 | tollgate_core_init(&platform, ap_ip); | ||
| 121 | |||
| 122 | TEST_SUMMARY(); | ||
| 123 | } | ||