/
ethereumminer.cpp
250 lines (214 loc) · 11 KB
/
ethereumminer.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
243
244
245
246
247
248
249
///////////////////////////////////////////////////////////////////////////////
// //
// This file is part of QtEthminer. //
// Copyright (C) 2015-2016 Jacob Dawid, jacob@omg-it.works //
// //
// QtEthminer is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// QtEthminer is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with QtEthminer. If not, see <http://www.gnu.org/licenses/>. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "ethereumminer.h"
// Qt includes
#include <QDebug>
EthereumMiner::EthereumMiner(QObject *parent) :
QObject(parent) {
_ethereumProtocol = new EthereumProtocol(this);
// When losing connection, handle this.
connect(_ethereumProtocol->stratumClient(), SIGNAL(disconnectedFromServer()), this, SLOT(handleDisconnect()));
// Once we login to the server, we want to login in right after that.
connect(_ethereumProtocol->stratumClient(), SIGNAL(connectedToServer()), this, SLOT(login()));
// Once we have logged in, query for work.
connect(_ethereumProtocol, SIGNAL(eth_login(bool)), _ethereumProtocol, SLOT(eth_getWork()));
// Evertime we receive a work package, work on it.
connect(_ethereumProtocol, SIGNAL(eth_getWork(QString,QString,QString)), this, SLOT(processWorkPackage(QString,QString,QString)));
setCurrentStep(Halted);
}
EthereumMiner::~EthereumMiner() {
}
QThread* EthereumMiner::processInBackground() {
QThread *workerThread = new QThread();
moveToThread(workerThread);
workerThread->start();
return workerThread;
}
void EthereumMiner::startMining() {
setCurrentStep(Starting);
QMetaObject::invokeMethod(this, "begin");
}
void EthereumMiner::stopMining() {
setCurrentStep(Halted);
QMetaObject::invokeMethod(this, "halt");
}
void EthereumMiner::restartMining() {
stopMining();
startMining();
}
void EthereumMiner::handleDisconnect() {
// In case we are mining, we obviously want to reconnect.
if(_powFarm.isMining()) {
connectToServer();
}
}
void EthereumMiner::connectToServer() {
setCurrentStep(Connecting);
_ethereumProtocol->stratumClient()->connectToServer(_configuration.server, _configuration.port);
}
void EthereumMiner::login() {
setCurrentStep(LoggingIn);
_ethereumProtocol->eth_login(_configuration.username, _configuration.password);
}
int EthereumMiner::hashrate() {
dev::eth::WorkingProgress wp = _powFarm.miningProgress();
_powFarm.resetMiningProgress();
return (int)wp.rate();
}
void EthereumMiner::saveSettings(QSettings* settings) {
switch(_configuration.minerType) {
case CPUMiner:
settings->setValue("minerType", "CPUMiner");
break;
case OpenCLMiner:
settings->setValue("minerType", "OpenCLMiner");
break;
}
settings->setValue("openclPlatform", _configuration.openclPlatform);
settings->setValue("openclDevice", _configuration.openclDevice);
settings->setValue("maxMiningThreads", _configuration.maxMiningThreads);
settings->setValue("currentBlock", (int)_configuration.currentBlock);
settings->setValue("recognizeCPUAsOpenCLDevice", _configuration.recognizeCPUAsOpenCLDevice);
settings->setValue("extraGPUMemory", _configuration.extraGPUMemory);
settings->setValue("precomputeNextDAG", _configuration.precomputeNextDAG);
settings->setValue("username", _configuration.username);
settings->setValue("password", _configuration.password);
settings->setValue("server", _configuration.server);
settings->setValue("port", _configuration.port);
settings->setValue("globalWorkSizeMultiplier", _configuration.globalWorkSizeMultiplier);
settings->setValue("localWorkSize", _configuration.localWorkSize);
settings->setValue("msPerBatch", _configuration.msPerBatch);
settings->sync();
}
void EthereumMiner::loadSettings(QSettings *settings) {
settings->sync();
QString minerTypeString = settings->value("minerType", "CPUMiner").toString();
if(minerTypeString == "CPUMiner") {
_configuration.minerType = CPUMiner;
} else
if(minerTypeString == "OpenCLMiner") {
_configuration.minerType = OpenCLMiner;
}
_configuration.openclPlatform = settings->value("openclPlatform", 0).toInt();
_configuration.openclDevice = settings->value("openclDevice", 0).toInt();
_configuration.maxMiningThreads = settings->value("maxMiningThreads", UINT_MAX).toInt();
_configuration.currentBlock = settings->value("currentBlock", 0).toInt();
_configuration.recognizeCPUAsOpenCLDevice = settings->value("recognizeCPUAsOpenCLDevice", false).toBool();
_configuration.extraGPUMemory = settings->value("extraGPUMemory", 64000000).toInt();
_configuration.precomputeNextDAG = settings->value("precomputeNextDAG", true).toBool();
_configuration.username = settings->value("username", "0x0.rig1").toString();
_configuration.password = settings->value("password", "unused").toString();
_configuration.server = settings->value("server", "eu1.ethermine.org").toString();
_configuration.port = settings->value("port", 4444).toInt();
_configuration.globalWorkSizeMultiplier = settings->value("globalWorkSizeMultiplier", ethash_cl_miner::c_defaultGlobalWorkSizeMultiplier).toInt();
_configuration.localWorkSize = settings->value("localWorkSize", ethash_cl_miner::c_defaultLocalWorkSize).toInt();
_configuration.msPerBatch = settings->value("msPerBatch", ethash_cl_miner::c_defaultMSPerBatch).toInt();
}
EthereumMiner::Step EthereumMiner::currentStep() {
return _currentStep;
}
void EthereumMiner::processWorkPackage(QString headerHash, QString seedHash, QString boundary) {
setCurrentStep(Mining);
emit receivedWorkPackage(headerHash, seedHash, boundary);
dev::h256 newHeaderHash(headerHash.toStdString());
dev::h256 newSeedHash(seedHash.toStdString());
if(_currentWorkPackage.seedHash != newSeedHash) {
qDebug() << "Grabbing DAG for" << seedHash;
}
if(!(dag = dev::eth::EthashAux::full(newSeedHash, true, [&](unsigned progressCount) {
emit dagCreationProgress(progressCount);
setCurrentStep(BuildingDAG);
return 0;
}))) {
emit dagCreationFailure();
}
if(_configuration.precomputeNextDAG) {
dev::eth::EthashAux::computeFull(dev::sha3(newSeedHash), true);
}
if(newHeaderHash != _currentWorkPackage.headerHash) {
_currentWorkPackage.headerHash = newHeaderHash;
_currentWorkPackage.seedHash = newSeedHash;
_currentWorkPackage.boundary = dev::h256(dev::fromHex(
boundary.toStdString()
), dev::h256::AlignRight);
_powFarm.setWork(_currentWorkPackage);
}
}
void EthereumMiner::begin() {
if(_configuration.minerType == CPUMiner) {
dev::eth::EthashCPUMiner::setNumInstances(_configuration.maxMiningThreads);
emit platformInfo(QString::fromStdString(dev::eth::EthashCPUMiner::platformInfo()));
} else if (_configuration.minerType == OpenCLMiner) {
if(!dev::eth::EthashGPUMiner::configureGPU(
_configuration.localWorkSize,
_configuration.globalWorkSizeMultiplier,
_configuration.msPerBatch,
_configuration.openclPlatform,
_configuration.openclDevice,
_configuration.recognizeCPUAsOpenCLDevice,
_configuration.extraGPUMemory,
_configuration.currentBlock
)) {
emit error("Failed to initialize GPU miner.");
}
dev::eth::EthashGPUMiner::setNumInstances(_configuration.maxMiningThreads);
emit platformInfo(QString::fromStdString(dev::eth::EthashGPUMiner::platformInfo()));
}
std::map<std::string, dev::eth::GenericFarm<dev::eth::EthashProofOfWork>::SealerDescriptor> sealers;
sealers[sealerString(CPUMiner)] = dev::eth::GenericFarm<dev::eth::EthashProofOfWork>::SealerDescriptor{&dev::eth::EthashCPUMiner::instances, [](dev::eth::GenericMiner<dev::eth::EthashProofOfWork>::ConstructionInfo ci){ return new dev::eth::EthashCPUMiner(ci); }};
sealers[sealerString(OpenCLMiner)] = dev::eth::GenericFarm<dev::eth::EthashProofOfWork>::SealerDescriptor{&dev::eth::EthashGPUMiner::instances, [](dev::eth::GenericMiner<dev::eth::EthashProofOfWork>::ConstructionInfo ci){ return new dev::eth::EthashGPUMiner(ci); }};
_powFarm.setSealers(sealers);
_powFarm.start(sealerString(_configuration.minerType));
_powFarm.onSolutionFound([&](dev::eth::EthashProofOfWork::Solution sol) {
QString nonce = "0x" + QString::fromStdString(sol.nonce.hex());
QString headerHash = "0x" + QString::fromStdString(_currentWorkPackage.headerHash.hex());
QString mixHash = "0x" + QString::fromStdString(sol.mixHash.hex());
qDebug() << "Solution found; Submitting ...";
qDebug() << " Nonce:" << nonce;
qDebug() << " Mixhash:" << mixHash;
qDebug() << " Header-hash:" << headerHash;
qDebug() << " Seedhash:" << QString::fromStdString(_currentWorkPackage.seedHash.hex());
qDebug() << " Target: " << QString::fromStdString(dev::h256(_currentWorkPackage.boundary).hex());
qDebug() << " Ethash: " << QString::fromStdString(dev::h256(dev::eth::EthashAux::eval(_currentWorkPackage.seedHash, _currentWorkPackage.headerHash, sol.nonce).value).hex());
_ethereumProtocol->eth_submitWork(nonce, headerHash, mixHash);
emit solutionFound(nonce, headerHash, mixHash);
_currentWorkPackage.reset();
_ethereumProtocol->eth_getWork();
return true;
});
connectToServer();
}
void EthereumMiner::halt() {
_powFarm.stop();
}
const char* EthereumMiner::sealerString(MinerType minerType) {
switch(minerType) {
case CPUMiner:
return "cpu";
case OpenCLMiner:
return "opencl";
default:
return "unknown";
}
}
void EthereumMiner::setCurrentStep(Step step) {
_currentStep = step;
emit currentStep(_currentStep);
}