-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathstratum_manager.h
More file actions
189 lines (138 loc) · 5.36 KB
/
Copy pathstratum_manager.h
File metadata and controls
189 lines (138 loc) · 5.36 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#pragma once
#include <pthread.h>
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "lwip/inet.h"
#include "ArduinoJson.h"
#include "stratum_task.h"
#include "../tasks/ping_task.h"
#define DIFF_STRING_SIZE 12
/**
* @brief StratumManager handles pool selection, connection management, and failover.
*/
class StratumTaskV2;
class StratumManager {
friend StratumTaskBase;
friend StratumTaskV1;
friend StratumTaskV2;
friend PingTask;
public:
enum Selected
{
PRIMARY = 0,
SECONDARY = 1
};
enum PoolMode
{
FAILOVER = 0,
DUAL = 1
};
protected:
const char *m_tag = "stratum-manager"; ///< Debug tag for logging
pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER; ///< Mutex for thread safety
StratumApiV1Message m_stratum_api_v1_message; ///< API message handler
PoolMode m_poolmode; // default FAILOVER
uint64_t m_lastSubmitResponseTimestamp = 0; ///< Timestamp of last submitted share response
StratumTaskBase *m_stratumTasks[2]{}; ///< Primary and secondary Stratum tasks
PingTask *m_pingTasks[2]{};
StratumConfig *m_stratumConfig[2]{};
uint32_t m_totalFoundBlocks = 0;
uint32_t m_foundBlocks = 0;
// Difficulty tracking (compatibility)
uint64_t m_totalBestDiff = 0; // Best nonce difficulty found
char m_totalBestDiffString[DIFF_STRING_SIZE]{}; // String representation of the best difficulty
char m_bestSessionDiffString[DIFF_STRING_SIZE]{}; // String representation of the best session difficulty
bool m_initialized = false;
PoolMode getPoolMode() const
{
return m_poolmode;
}
void copyConfigInto(int pool, StratumConfig *dst);
// Helper methods for connection management
void connect(int index); ///< Connect to a specified pool (0 = primary, 1 = secondary)
void disconnect(int index); ///< Disconnect from a specified pool
bool isConnected(int index); ///< Check if a pool is connected
// Handles incoming Stratum responses
void dispatch(int pool, JsonDocument &doc);
// Factory method for creating protocol-specific tasks
virtual StratumTaskBase* createTask(int index);
// Core Stratum management task
void task();
// Clears queued mining jobs
void cleanQueue();
void freeStratumV1Message(StratumApiV1Message *message);
// abstract methods
// reconnect logic for failover mode
virtual void reconnectTimerCallback(int index) = 0;
virtual void connectedCallback(int index) = 0;
virtual void disconnectedCallback(int index) = 0;
virtual bool acceptsNotifyFrom(int pool) = 0;
virtual void acceptedShare(int pool) = 0;
virtual void rejectedShare(int pool) = 0;
virtual void setPoolDifficulty(int pool, uint32_t diff) = 0;
virtual void setNetworkDifficulty(int pool, uint32_t nbits) {}
virtual int getPoolMode() = 0;
// reset stats for a single pool (called from loadSettings with mutex held)
virtual void resetPoolSessionStats(int pool) {
}
public:
virtual void resetSessionStats() {
PThreadGuard lock(m_mutex);
m_foundBlocks = 0;
}
StratumManager(PoolMode mode);
static void taskWrapper(void *pvParameters); ///< Wrapper function for task execution
// Submit shares to the active Stratum pool
// version_rolled = full rolled version (base | rolled bits)
// version_base = original block template version
void submitShare(int pool, const char *jobid, const char *extranonce_2, const uint32_t ntime, const uint32_t nonce,
const uint32_t version_rolled, const uint32_t version_base);
void checkForFoundBlock(int pool, double diff, uint32_t nbits);
bool isAnyConnected();
int getNumConnectedPools();
virtual bool isDualPool() const { return false; }
virtual bool isFallback() const { return false; }
virtual void getManagerInfoJson(JsonObject &obj);
virtual void loadSettings() = 0;
virtual void loadSettings(bool reconnect);
virtual void saveSettings(const JsonDocument &doc);
virtual bool isUsingFallback() {
return false;
}
// abstract
virtual const char *getResolvedIpForPool(int pool) const;
virtual int getNextActivePool() = 0;
virtual uint32_t selectAsicDiff(int pool, uint32_t poolDiff) = 0;
virtual void checkForBestDiff(int pool, double diff, uint32_t nbits) = 0;
bool isInitialized() {
return m_initialized;
}
// compatibility
virtual uint64_t getSharesAccepted() = 0;
virtual uint64_t getSharesRejected() = 0;
uint32_t getTotalFoundBlocks() {
return m_totalFoundBlocks;
}
uint32_t getFoundBlocks() {
return m_foundBlocks;
}
const char* getBestDiffString() {
return m_totalBestDiffString;
}
uint64_t getBestDiff() {
return m_totalBestDiff;
}
virtual uint64_t getBestSessionDiff() = 0;
const char *getBestSessionDiffString() {
return m_bestSessionDiffString;
}
virtual int getPoolErrors() = 0;
virtual uint32_t getPoolDifficulty() = 0;
virtual double getNetworkDifficulty() { return 0; }
virtual int getCompatPingPoolIndex() = 0;
PingTask *getPingTask(int i) {
return m_pingTasks[i];
}
};
double get_last_ping_rtt();
double get_recent_ping_loss();