upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/unit/test_mining_payment.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/test_mining_payment.c')
-rw-r--r--tests/unit/test_mining_payment.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/unit/test_mining_payment.c b/tests/unit/test_mining_payment.c
new file mode 100644
index 0000000..c3834fd
--- /dev/null
+++ b/tests/unit/test_mining_payment.c
@@ -0,0 +1,92 @@
1#include "test_framework.h"
2#include "../../main/mining_payment.h"
3#include <stdio.h>
4#include <string.h>
5#include <math.h>
6
7int main(void)
8{
9 printf("=== test_mining_payment ===\n");
10
11 mining_payment_init();
12
13 printf("\n--- mining_nbits_to_difficulty ---\n");
14 uint64_t d1 = mining_nbits_to_difficulty(0);
15 ASSERT(d1 == UINT64_MAX, "nbits=0 returns max");
16
17 uint64_t d2 = mining_nbits_to_difficulty(0x170309E2);
18 ASSERT(d2 > 0, "mainnet nbits gives positive difficulty");
19 printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2);
20
21 uint64_t d3 = mining_nbits_to_difficulty(0x1d00ffff);
22 ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1");
23
24 printf("\n--- mining_calculate_hashprice ---\n");
25 double hp1 = mining_calculate_hashprice(0);
26 ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0");
27
28 double hp2 = mining_calculate_hashprice(0x170309E2);
29 ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice");
30 printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2);
31
32 printf("\n--- mining_calculate_hashprice_override ---\n");
33 double hp3 = mining_calculate_hashprice_override(1000);
34 ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value");
35
36 printf("\n--- mining_set_current_nbits ---\n");
37 mining_set_current_nbits(0x170309E2);
38 double hp4 = mining_get_current_hashprice();
39 ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated");
40
41 printf("\n--- mining_get_or_create_client ---\n");
42 mining_client_stats_t *c1 = mining_get_or_create_client(0x0A010203);
43 ASSERT(c1 != NULL, "created client 1");
44 ASSERT(c1->ip == 0x0A010203, "client 1 IP matches");
45 ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares");
46
47 mining_client_stats_t *c2 = mining_get_or_create_client(0x0A010204);
48 ASSERT(c2 != NULL, "created client 2");
49 ASSERT(c2->ip == 0x0A010204, "client 2 IP matches");
50
51 mining_client_stats_t *c1_again = mining_get_or_create_client(0x0A010203);
52 ASSERT(c1_again == c1, "same IP returns same client");
53
54 printf("\n--- mining_update_hashrate ---\n");
55 mining_update_hashrate(0x0A010203, true);
56 mining_update_hashrate(0x0A010203, true);
57 mining_update_hashrate(0x0A010203, false);
58
59 const mining_client_stats_t *c1_stats = mining_get_client_stats(0x0A010203);
60 ASSERT(c1_stats != NULL, "client 1 stats found");
61 ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted");
62 ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected");
63
64 printf("\n--- mining_get_client_stats ---\n");
65 const mining_client_stats_t *notfound = mining_get_client_stats(0xFFFFFFFF);
66 ASSERT(notfound == NULL, "nonexistent client returns NULL");
67
68 printf("\n--- mining_shares_to_allotment_ms ---\n");
69 uint64_t allot1 = mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000);
70 ASSERT(allot1 == 0, "zero hashrate = zero allotment");
71
72 uint64_t allot2 = mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000);
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);
75
76 uint64_t allot3 = mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000);
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);
79
80 printf("\n--- mining_shares_to_allotment_bytes ---\n");
81 uint64_t ab1 = mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576);
82 ASSERT(ab1 == 0, "zero hashrate = zero bytes");
83
84 uint64_t ab2 = mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576);
85 ASSERT(ab2 > 0, "positive hashrate gives positive bytes");
86
87 printf("\n--- mining_validate_share (stub) ---\n");
88 esp_err_t vr = mining_validate_share(NULL, 0, NULL, 0);
89 ASSERT(vr == ESP_OK, "validate share stub returns OK");
90
91 TEST_SUMMARY();
92}