This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
client_difficulty.cpp
99 lines (71 loc) · 2.46 KB
/
client_difficulty.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "stratum.h"
bool kawpow_set_difficulty(YAAMP_CLIENT* client, double difficulty);
double client_normalize_difficulty(double difficulty)
{
return difficulty;
}
void client_record_difficulty(YAAMP_CLIENT *client)
{
if(client->difficulty_remote)
{
client->last_submit_time = current_timestamp();
return;
}
int e = current_timestamp() - client->last_submit_time;
if(e < 500) e = 500;
int p = 5;
client->shares_per_minute = (client->shares_per_minute * (100 - p) + 60*1000*p/e) / 100;
client->last_submit_time = current_timestamp();
// debuglog("client->shares_per_minute %f\n", client->shares_per_minute);
}
void client_change_difficulty(YAAMP_CLIENT *client, double difficulty)
{
if(difficulty <= 0) return;
difficulty = client_normalize_difficulty(difficulty);
if(difficulty <= 0) return;
// debuglog("change diff to %f %f\n", difficulty, client->difficulty_actual);
if(difficulty == client->difficulty_actual) return;
uint64_t user_target = diff_to_target(difficulty);
if(user_target >= YAAMP_MINDIFF && user_target <= YAAMP_MAXDIFF)
{
client->difficulty_actual = difficulty;
client_send_difficulty(client, difficulty);
}
}
void client_adjust_difficulty(YAAMP_CLIENT *client)
{
if(client->shares_per_minute > 100)
kawpow_set_difficulty(client, client->difficulty_actual*4);
else if(client->difficulty_fixed)
return;
else if(client->shares_per_minute > 25)
kawpow_set_difficulty(client, client->difficulty_actual*2);
else if(client->shares_per_minute > 20)
kawpow_set_difficulty(client, client->difficulty_actual*1.5);
else if(client->shares_per_minute < 5)
kawpow_set_difficulty(client, client->difficulty_actual/2);
}
int client_send_difficulty(YAAMP_CLIENT *client, double difficulty)
{
// debuglog("%s diff %f\n", client->sock->ip, difficulty);
client->shares_per_minute = YAAMP_SHAREPERSEC;
if(difficulty >= 1)
client_call(client, "mining.set_difficulty", "[%.0f]", difficulty);
else
client_call(client, "mining.set_difficulty", "[%.3f]", difficulty);
return 0;
}
void client_initialize_difficulty(YAAMP_CLIENT *client)
{
char *p = strstr(client->password, "d=");
char *p2 = strstr(client->password, "decred=");
if(!p || p2) return;
double diff = client_normalize_difficulty(atof(p+2));
uint64_t user_target = diff_to_target(diff);
// debuglog("%016llx target\n", user_target);
if(user_target >= YAAMP_MINDIFF && user_target <= YAAMP_MAXDIFF)
{
client->difficulty_actual = diff;
client->difficulty_fixed = true;
}
}