/
masternodemanager.cpp
394 lines (336 loc) · 13.2 KB
/
masternodemanager.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "masternodemanager.h"
#include "ui_masternodemanager.h"
#include "addeditadrenalinenode.h"
#include "adrenalinenodeconfigdialog.h"
#include "sync.h"
#include "clientmodel.h"
#include "walletmodel.h"
#include "activemasternode.h"
#include "masternodeconfig.h"
#include "masternode.h"
#include "walletdb.h"
#include "wallet.h"
#include "init.h"
#include "rpcserver.h"
#include <boost/lexical_cast.hpp>
#include <fstream>
using namespace json_spirit;
using namespace std;
#include <QAbstractItemDelegate>
#include <QPainter>
#include <QTimer>
#include <QDebug>
#include <QScrollArea>
#include <QDateTime>
#include <QApplication>
#include <QClipboard>
#include <QMessageBox>
#include <QThread>
#include <QScrollBar>
MasternodeManager::MasternodeManager(QWidget *parent) :
QWidget(parent),
ui(new Ui::MasternodeManager),
clientModel(0),
walletModel(0)
{
ui->setupUi(this);
ui->editButton->setEnabled(false);
ui->editButton->setVisible(false);
ui->getConfigButton->setEnabled(false);
ui->startButton->setEnabled(false);
ui->stopButton->setEnabled(false);
ui->copyAddressButton->setEnabled(false);
subscribeToCoreSignals();
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateNodeList()));
if(!GetBoolArg("-reindexaddr", false))
timer->start(1000);
fFilterUpdated = true;
nTimeFilterUpdated = GetTime();
updateNodeList();
}
MasternodeManager::~MasternodeManager()
{
delete ui;
}
static void NotifyAdrenalineNodeUpdated(MasternodeManager *page, CAdrenalineNodeConfig nodeConfig)
{
// alias, address, privkey, collateral address
QString alias = QString::fromStdString(nodeConfig.sAlias);
QString addr = QString::fromStdString(nodeConfig.sAddress);
QString privkey = QString::fromStdString(nodeConfig.sMasternodePrivKey);
QString collateral = QString::fromStdString(nodeConfig.sCollateralAddress);
QMetaObject::invokeMethod(page, "updateAdrenalineNode", Qt::QueuedConnection,
Q_ARG(QString, alias),
Q_ARG(QString, addr),
Q_ARG(QString, privkey),
Q_ARG(QString, collateral)
);
}
void MasternodeManager::subscribeToCoreSignals()
{
// Connect signals to core
uiInterface.NotifyAdrenalineNodeChanged.connect(boost::bind(&NotifyAdrenalineNodeUpdated, this, _1));
}
void MasternodeManager::on_tableWidget_2_itemSelectionChanged()
{
if(ui->tableWidget_2->selectedItems().count() > 0)
{
ui->editButton->setEnabled(true);
ui->editButton->setVisible(false);
ui->getConfigButton->setEnabled(true);
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(true);
ui->copyAddressButton->setEnabled(true);
}
}
void MasternodeManager::updateAdrenalineNode(QString alias, QString addr, QString privkey, QString collateral, QString status)
{
LOCK(cs_adrenaline);
bool bFound = false;
int nodeRow = 0;
for(int i=0; i < ui->tableWidget_2->rowCount(); i++)
{
if(ui->tableWidget_2->item(i, 0)->text() == alias)
{
bFound = true;
nodeRow = i;
break;
}
}
if(nodeRow == 0 && !bFound)
ui->tableWidget_2->insertRow(0);
QTableWidgetItem *aliasItem = new QTableWidgetItem(alias);
QTableWidgetItem *addrItem = new QTableWidgetItem(addr);
QTableWidgetItem *statusItem = new QTableWidgetItem(status);
QTableWidgetItem *collateralItem = new QTableWidgetItem(collateral);
ui->tableWidget_2->setItem(nodeRow, 0, aliasItem);
ui->tableWidget_2->setItem(nodeRow, 1, addrItem);
ui->tableWidget_2->setItem(nodeRow, 2, statusItem);
ui->tableWidget_2->setItem(nodeRow, 3, collateralItem);
ui->tableWidget_2->setSortingEnabled(true);
}
static QString seconds_to_DHMS(quint32 duration)
{
QString res;
int seconds = (int) (duration % 60);
duration /= 60;
int minutes = (int) (duration % 60);
duration /= 60;
int hours = (int) (duration % 24);
int days = (int) (duration / 24);
if((hours == 0)&&(days == 0))
return res.sprintf("%02dm:%02ds", minutes, seconds);
if (days == 0)
return res.sprintf("%02dh:%02dm:%02ds", hours, minutes, seconds);
return res.sprintf("%dd %02dh:%02dm:%02ds", days, hours, minutes, seconds);
}
void MasternodeManager::updateNodeList()
{
TRY_LOCK(cs_masternodes, lockMasternodes);
if(!lockMasternodes)
return;
ui->countLabel->setText("Updating...");
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
BOOST_FOREACH(CMasterNode mn, vecMasternodes)
{
int mnRow = 0;
ui->tableWidget->insertRow(0);
// populate list
// Address, Rank, Active, Active Seconds, Last Seen, Pub Key
QTableWidgetItem *activeItem = new QTableWidgetItem(QString::number(mn.IsEnabled()));
QTableWidgetItem *addressItem = new QTableWidgetItem(QString::fromStdString(mn.addr.ToString()));
QTableWidgetItem *rankItem = new QTableWidgetItem(QString::number(GetMasternodeRank(mn.vin, pindexBest->nHeight)));
QTableWidgetItem *activeSecondsItem = new QTableWidgetItem(seconds_to_DHMS((qint64)(mn.lastTimeSeen - mn.now)));
QTableWidgetItem *lastSeenItem = new QTableWidgetItem(QString::fromStdString(DateTimeStrFormat(mn.lastTimeSeen)));
CScript pubkey;
pubkey =GetScriptForDestination(mn.pubkey.GetID());
CTxDestination address1;
ExtractDestination(pubkey, address1);
CBitcoinAddress address2(address1);
QTableWidgetItem *pubkeyItem = new QTableWidgetItem(QString::fromStdString(address2.ToString()));
ui->tableWidget->setItem(mnRow, 0, addressItem);
ui->tableWidget->setItem(mnRow, 1, rankItem);
ui->tableWidget->setItem(mnRow, 2, activeItem);
ui->tableWidget->setItem(mnRow, 3, activeSecondsItem);
ui->tableWidget->setItem(mnRow, 4, lastSeenItem);
ui->tableWidget->setItem(mnRow, 5, pubkeyItem);
ui->tableWidget->setSortingEnabled(true);
ui->countLabel->setText(QString::number(ui->tableWidget->rowCount()));
}
if(pwalletMain)
{
LOCK(cs_adrenaline);
BOOST_FOREACH(PAIRTYPE(std::string, CAdrenalineNodeConfig) adrenaline, pwalletMain->mapMyAdrenalineNodes)
{
updateAdrenalineNode(QString::fromStdString(adrenaline.second.sAlias), QString::fromStdString(adrenaline.second.sAddress), QString::fromStdString(adrenaline.second.sMasternodePrivKey), QString::fromStdString(adrenaline.second.sCollateralAddress), QString::fromStdString(adrenaline.second.sstatus));
}
}
}
void MasternodeManager::setClientModel(ClientModel *model)
{
this->clientModel = model;
if(model)
{
}
}
void MasternodeManager::setWalletModel(WalletModel *model)
{
this->walletModel = model;
if(model && model->getOptionsModel())
{
}
}
void MasternodeManager::on_createButton_clicked()
{
AddEditAdrenalineNode* aenode = new AddEditAdrenalineNode();
aenode->exec();
}
void MasternodeManager::on_copyAddressButton_clicked()
{
QItemSelectionModel* selectionModel = ui->tableWidget_2->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
if(selected.count() == 0)
return;
QModelIndex index = selected.at(0);
int r = index.row();
std::string sCollateralAddress = ui->tableWidget_2->item(r, 3)->text().toStdString();
QApplication::clipboard()->setText(QString::fromStdString(sCollateralAddress));
}
void MasternodeManager::on_editButton_clicked()
{
QItemSelectionModel* selectionModel = ui->tableWidget_2->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
if(selected.count() == 0)
return;
QModelIndex index = selected.at(0);
int r = index.row();
std::string sAddress = ui->tableWidget_2->item(r, 1)->text().toStdString();
// get existing config entry
}
void MasternodeManager::on_getConfigButton_clicked()
{
QItemSelectionModel* selectionModel = ui->tableWidget_2->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
if(selected.count() == 0)
return;
QModelIndex index = selected.at(0);
int r = index.row();
std::string sAddress = ui->tableWidget_2->item(r, 1)->text().toStdString();
CAdrenalineNodeConfig c = pwalletMain->mapMyAdrenalineNodes[sAddress];
std::string sPrivKey = c.sMasternodePrivKey;
AdrenalineNodeConfigDialog* d = new AdrenalineNodeConfigDialog(this, QString::fromStdString(sAddress), QString::fromStdString(sPrivKey));
d->exec();
}
void MasternodeManager::on_removeButton_clicked()
{
QItemSelectionModel* selectionModel = ui->tableWidget_2->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
if(selected.count() == 0)
return;
QMessageBox::StandardButton confirm;
confirm = QMessageBox::question(this, "Delete Adrenaline Node?", "Are you sure you want to delete this adrenaline node configuration?", QMessageBox::Yes|QMessageBox::No);
if(confirm == QMessageBox::Yes)
{
QModelIndex index = selected.at(0);
int r = index.row();
std::string sAddress = ui->tableWidget_2->item(r, 1)->text().toStdString();
CAdrenalineNodeConfig c = pwalletMain->mapMyAdrenalineNodes[sAddress];
CWalletDB walletdb(pwalletMain->strWalletFile);
pwalletMain->mapMyAdrenalineNodes.erase(sAddress);
walletdb.EraseAdrenalineNodeConfig(c.sAddress);
ui->tableWidget_2->clearContents();
ui->tableWidget_2->setRowCount(0);
BOOST_FOREACH(PAIRTYPE(std::string, CAdrenalineNodeConfig) adrenaline, pwalletMain->mapMyAdrenalineNodes)
{
updateAdrenalineNode(QString::fromStdString(adrenaline.second.sAlias), QString::fromStdString(adrenaline.second.sAddress), QString::fromStdString(adrenaline.second.sMasternodePrivKey), QString::fromStdString(adrenaline.second.sCollateralAddress), QString::fromStdString(adrenaline.second.sstatus));
}
}
}
void MasternodeManager::on_startButton_clicked()
{
// start the node
QItemSelectionModel* selectionModel = ui->tableWidget_2->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
if(selected.count() == 0)
return;
QModelIndex index = selected.at(0);
int r = index.row();
std::string sAddress = ui->tableWidget_2->item(r, 1)->text().toStdString();
CAdrenalineNodeConfig c = pwalletMain->mapMyAdrenalineNodes[sAddress];
std::string errorMessage;
bool result = activeMasternode.RegisterByPubKey(c.sAddress, c.sMasternodePrivKey, c.sCollateralAddress, errorMessage);
QMessageBox msg;
if(result)
msg.setText("Adrenaline Node at " + QString::fromStdString(c.sAddress) + " started.");
else
msg.setText("Error: " + QString::fromStdString(errorMessage));
msg.exec();
}
void MasternodeManager::on_stopButton_clicked()
{
// start the node
QItemSelectionModel* selectionModel = ui->tableWidget_2->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
if(selected.count() == 0)
return;
QModelIndex index = selected.at(0);
int r = index.row();
std::string sAddress = ui->tableWidget_2->item(r, 1)->text().toStdString();
CAdrenalineNodeConfig c = pwalletMain->mapMyAdrenalineNodes[sAddress];
std::string errorMessage;
bool result = activeMasternode.StopMasterNode(c.sAddress, c.sMasternodePrivKey, errorMessage);
QMessageBox msg;
if(result)
{
msg.setText("Adrenaline Node at " + QString::fromStdString(c.sAddress) + " stopped.");
}
else
{
msg.setText("Error: " + QString::fromStdString(errorMessage));
}
msg.exec();
}
void MasternodeManager::on_startAllButton_clicked()
{
std::string results;
BOOST_FOREACH(PAIRTYPE(std::string, CAdrenalineNodeConfig) adrenaline, pwalletMain->mapMyAdrenalineNodes)
{
CAdrenalineNodeConfig c = adrenaline.second;
std::string errorMessage;
bool result = activeMasternode.RegisterByPubKey(c.sAddress, c.sMasternodePrivKey, c.sCollateralAddress, errorMessage);
if(result)
{
results += c.sAddress + ": STARTED\n";
}
else
{
results += c.sAddress + ": ERROR: " + errorMessage + "\n";
}
}
QMessageBox msg;
msg.setText(QString::fromStdString(results));
msg.exec();
}
void MasternodeManager::on_stopAllButton_clicked()
{
std::string results;
BOOST_FOREACH(PAIRTYPE(std::string, CAdrenalineNodeConfig) adrenaline, pwalletMain->mapMyAdrenalineNodes)
{
CAdrenalineNodeConfig c = adrenaline.second;
std::string errorMessage;
bool result = activeMasternode.StopMasterNode(c.sAddress, c.sMasternodePrivKey, errorMessage);
if(result)
{
results += c.sAddress + ": STOPPED\n";
}
else
{
results += c.sAddress + ": ERROR: " + errorMessage + "\n";
}
}
QMessageBox msg;
msg.setText(QString::fromStdString(results));
msg.exec();
}