-
Notifications
You must be signed in to change notification settings - Fork 38
/
qtminer.cpp
259 lines (207 loc) · 7.07 KB
/
qtminer.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "QtMiner.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
QtMiner::~QtMiner()
{
}
void QtMiner::run()
{
QTimer::singleShot(0, this, [=](){
this->m_rateTimer = startTimer(2000);
this->m_pingTimer = startTimer(30000);
});
cnote << "Connecting to stratum server " << m_server.toStdString() << ":" << m_port;
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()),this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
socket->connectToHost(m_server, m_port);
if(!socket->waitForConnected(5000))
{
cnote << "Stratum connection error: " << socket->errorString().toStdString();
emit finished();
}
startMining();
}
void QtMiner::timerEvent(QTimerEvent* _e)
{
if (_e->timerId() == m_rateTimer)
{
auto mp = f.miningProgress();
f.resetMiningProgress();
if (current)
cnote << "Mining on PoWhash" << current.headerHash << ": " << mp;
else
cnote << "Waiting for work package...";
}
if (_e->timerId() == m_pingTimer) {
cnote << "Pinging server...";
sendGetWorkRequest();
}
}
void QtMiner::sendGetWorkRequest()
{
int requestId = requestCounter++;
// cnote << requestId;
requests[requestId] = "eth_getWork";
QJsonObject obj;
obj["jsonrpc"] = "2.0";
obj["method"] = "eth_getWork";
QJsonArray params = { };
obj["params"] = params;
obj["id"] = requestId;
QByteArray doc = QJsonDocument(obj).toJson(QJsonDocument::Compact);
doc.append("\n");
//qDebug() << doc;
socket->write(doc);
}
void QtMiner::connected()
{
cnote << "Connection to stratum server established!";
int requestId = requestCounter++;
QJsonObject obj;
obj["jsonrpc"] = "2.0";
obj["method"] = "eth_login";
QJsonArray params = { m_user, m_pass };
obj["params"] = params;
obj["id"] = requestId;
requests[requestId] = "eth_login";
QByteArray doc = QJsonDocument(obj).toJson(QJsonDocument::Compact);
doc.append("\n");
// qDebug() << doc;
socket->write(doc);
}
void QtMiner::disconnected()
{
cnote << "Connection to stratum server lost. Trying to reconnect!";
this_thread::sleep_for(chrono::milliseconds(5000));
while (true) {
socket->connectToHost(m_server, m_port);
if(!socket->waitForConnected(5000))
{
cnote << "Stratum connection error: " << socket->errorString().toStdString();
} else {
return;
}
}
}
void QtMiner::readyRead()
{
//qDebug() << "reading...";
// read the data from the socket
QByteArray jsonData = socket->readAll();
// qDebug() << jsonData;
QJsonDocument document = QJsonDocument::fromJson(jsonData);
QJsonObject object = document.object();
QJsonValue jsonValue = object.value("id");
double id = jsonValue.toDouble();
// qDebug() << "Id is: " << id;
QJsonValue result = object.value("result");
if (result.isBool() && !result.toBool()) {
cnote << "Stratum Server error:" << object.value("error").toString().toStdString();
sendGetWorkRequest();
return;
}
if (id == 0) {
cnote << "Push: New work package received";
QJsonArray workData = object["result"].toArray();
processWorkPackage(workData);
} else {
QString method = requests[id];
// qDebug() << method;
if (method == "eth_login") {
requests.erase(id);
cnote << "Login to stratum server successfull";
sendGetWorkRequest();
}
if (method == "eth_getWork") {
requests.erase(id);
cnote << "Work package received";
QJsonArray workData = object["result"].toArray();
processWorkPackage(workData);
}
if (method == "eth_submitWork") {
requests.erase(id);
cnote << "Share submitted to server!";
sendGetWorkRequest();
}
}
}
void QtMiner::processWorkPackage(QJsonArray workData) {
h256 hh(workData[0].toString().toStdString());
h256 newSeedHash(workData[1].toString().toStdString());
// cnote << current.seedHash;
// cout << newSeedHash << endl;
if (current.seedHash != newSeedHash)
cnote << "Grabbing DAG for" << newSeedHash;
if (!(dag = EthashAux::full(newSeedHash, true, [&](unsigned _pc){ cout << "\rCreating DAG. " << _pc << "% done..." << flush; return 0; })))
BOOST_THROW_EXCEPTION(DAGCreationFailure());
if (m_precompute)
EthashAux::computeFull(sha3(newSeedHash), true);
if (hh != current.headerHash)
{
current.headerHash = hh;
current.seedHash = newSeedHash;
current.boundary = h256(fromHex(workData[2].toString().toStdString()), h256::AlignRight);
cnote << "Got work package:";
cnote << " Header-hash:" << current.headerHash.hex();
cnote << " Seedhash:" << current.seedHash.hex();
cnote << " Target: " << h256(current.boundary).hex();
f.setWork(current);
}
}
void QtMiner::startMining() {
if (m_minerType == "cpu")
EthashCPUMiner::setNumInstances(m_miningThreads);
else if (m_minerType == "opencl")
{
if (!EthashGPUMiner::configureGPU(
m_localWorkSize,
m_globalWorkSizeMultiplier,
m_msPerBatch,
m_openclPlatform,
m_openclDevice,
m_clAllowCPU,
m_extraGPUMemory,
m_currentBlock
))
exit(1);
EthashGPUMiner::setNumInstances(m_miningThreads);
}
map<string, GenericFarm<EthashProofOfWork>::SealerDescriptor> sealers;
sealers["cpu"] = GenericFarm<EthashProofOfWork>::SealerDescriptor{&EthashCPUMiner::instances, [](GenericMiner<EthashProofOfWork>::ConstructionInfo ci){ return new EthashCPUMiner(ci); }};
sealers["opencl"] = GenericFarm<EthashProofOfWork>::SealerDescriptor{&EthashGPUMiner::instances, [](GenericMiner<EthashProofOfWork>::ConstructionInfo ci){ return new EthashGPUMiner(ci); }};
std::string _m = m_minerType;
unsigned _recheckPeriod = 500;
(void)_m;
(void)_recheckPeriod;
f.setSealers(sealers);
f.start(_m);
f.onSolutionFound([&](EthashProofOfWork::Solution sol)
{
cnote << "Solution found; Submitting ...";
cnote << " Nonce:" << sol.nonce.hex();
cnote << " Mixhash:" << sol.mixHash.hex();
cnote << " Header-hash:" << current.headerHash.hex();
cnote << " Seedhash:" << current.seedHash.hex();
cnote << " Target: " << h256(current.boundary).hex();
cnote << " Ethash: " << h256(EthashAux::eval(current.seedHash, current.headerHash, sol.nonce).value).hex();
emit solutionFound(QString::fromStdString("0x" + toString(sol.nonce)), QString::fromStdString("0x" + toString(current.headerHash)), QString::fromStdString("0x" + toString(sol.mixHash)));
return true;
});
}
void QtMiner::submitWork(QString nonce, QString headerHash, QString mixHash) {
int requestId = requestCounter++;
QJsonObject obj;
obj["jsonrpc"] = "2.0";
obj["method"] = "eth_submitWork";
QJsonArray params = { nonce, headerHash, mixHash };
obj["params"] = params;
obj["id"] = requestId;
requests[requestId] = "eth_submitWork";
QByteArray doc = QJsonDocument(obj).toJson(QJsonDocument::Compact);
doc.append("\n");
socket->write(doc);
current.reset();
}