PRCYCoin  2.0.0.7rc1
P2P Digital Currency
masternodelist.cpp
Go to the documentation of this file.
1 #include "masternodelist.h"
2 #include "ui_masternodelist.h"
3 
4 #include "activemasternode.h"
5 #include "clientmodel.h"
6 #include "guiutil.h"
7 #include "init.h"
8 #include "masternode-sync.h"
9 #include "masternodeconfig.h"
10 #include "masternodeman.h"
11 #include "sync.h"
12 #include "wallet/wallet.h"
13 #include "walletmodel.h"
14 #include "askpassphrasedialog.h"
15 
16 #include <QMessageBox>
17 #include <QErrorMessage>
18 #include <QTimer>
19 
21 
22 MasternodeList::MasternodeList(QWidget* parent) : QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
23  ui(new Ui::MasternodeList),
24  clientModel(0),
25  // m_SizeGrip(this),
26  walletModel(0)
27 {
28  ui->setupUi(this);
29 
30  ui->startButton->setEnabled(false);
31 
32  int columnAliasWidth = 100;
33  int columnAddressWidth = 200;
34  int columnStatusWidth = 80;
35  int columnActiveWidth = 130;
36  int columnLastSeenWidth = 130;
37 
38  ui->tableWidgetMyMasternodes->setColumnWidth(0, columnAliasWidth);
39  ui->tableWidgetMyMasternodes->setColumnWidth(1, columnAddressWidth);
40  ui->tableWidgetMyMasternodes->setColumnWidth(3, columnStatusWidth);
41  ui->tableWidgetMyMasternodes->setColumnWidth(4, columnActiveWidth);
42  ui->tableWidgetMyMasternodes->setColumnWidth(5, columnLastSeenWidth);
43 
44  ui->tableWidgetMyMasternodes->setContextMenuPolicy(Qt::CustomContextMenu);
45 
46  QAction* startAliasAction = new QAction(tr("Start alias"), this);
47  contextMenu = new QMenu();
48  contextMenu->addAction(startAliasAction);
49  connect(ui->tableWidgetMyMasternodes, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
50  connect(startAliasAction, SIGNAL(triggered()), this, SLOT(on_startButton_clicked()));
51 
52  timer = new QTimer(this);
53  connect(timer, SIGNAL(timeout()), this, SLOT(updateMyNodeList()));
54  timer->start(1000);
55 
56  // Fill MN list
57  fFilterUpdated = true;
59 }
60 
62 {
63  delete ui;
64 }
65 
67 {
68  this->clientModel = model;
69 }
70 
72 {
73  this->walletModel = model;
74 }
75 
76 void MasternodeList::showContextMenu(const QPoint& point)
77 {
78  QTableWidgetItem* item = ui->tableWidgetMyMasternodes->itemAt(point);
79  if (item) contextMenu->exec(QCursor::pos());
80 }
81 
82 void MasternodeList::resizeEvent(QResizeEvent* event)
83 {
84  QWidget::resizeEvent(event);
85 }
86 
87 void MasternodeList::StartAlias(std::string strAlias)
88 {
90  {
91  std::string strStatusHtml;
92  strStatusHtml += "<center>Alias: " + strAlias;
93 
95  if (mne.getAlias() == strAlias) {
96  std::string strError;
98 
99  bool fSuccess = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), strError, mnb);
100 
101  if (fSuccess) {
102  strStatusHtml += "<br>Successfully started masternode.";
104  mnb.Relay();
105  } else {
106  strStatusHtml += "<br>Failed to start masternode.<br>Error: " + strError;
107  }
108  break;
109  }
110  }
111  strStatusHtml += "</center>";
112 
113  QMessageBox msg;
114  msg.setText(strStatusHtml.c_str());
115  msg.setStyleSheet(GUIUtil::loadStyleSheet());
116  msg.exec();
117  }
118  updateMyNodeList(true);
119 }
120 
121 void MasternodeList::StartAll(std::string strCommand)
122 {
124  {
125  int nCountSuccessful = 0;
126  int nCountFailed = 0;
127  std::string strFailedHtml;
128 
130  std::string strError;
132 
133  int nIndex;
134  if(!mne.castOutputIndex(nIndex))
135  continue;
136 
137  CTxIn txin = CTxIn(uint256S(mne.getTxHash()), uint32_t(nIndex));
138  CMasternode* pmn = mnodeman.Find(txin);
139 
140  if (strCommand == "start-missing" && pmn) continue;
141 
142  bool fSuccess = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), strError, mnb);
143 
144  if (fSuccess) {
145  nCountSuccessful++;
147  mnb.Relay();
148  } else {
149  nCountFailed++;
150  strFailedHtml += "\nFailed to start " + mne.getAlias() + ". Error: " + strError;
151  }
152  }
153 
154  std::string returnObj;
155  returnObj = strprintf("Successfully started %d masternodes, failed to start %d, total %d", nCountSuccessful, nCountFailed, nCountFailed + nCountSuccessful);
156  if (nCountFailed > 0) {
157  returnObj += strFailedHtml;
158  }
159 
160  QMessageBox msg;
161  msg.setText(QString::fromStdString(returnObj));
162  msg.setStyleSheet(GUIUtil::loadStyleSheet());
163  msg.exec();
164  }
165  updateMyNodeList(true);
166 }
167 
168 void MasternodeList::updateMyMasternodeInfo(QString strAlias, QString strAddr, CMasternode* pmn)
169 {
171  bool fOldRowFound = false;
172  int nNewRow = 0;
173 
174  for (int i = 0; i < ui->tableWidgetMyMasternodes->rowCount(); i++) {
175  if (ui->tableWidgetMyMasternodes->item(i, 0)->text() == strAlias) {
176  fOldRowFound = true;
177  nNewRow = i;
178  break;
179  }
180  }
181 
182  if (nNewRow == 0 && !fOldRowFound) {
183  nNewRow = ui->tableWidgetMyMasternodes->rowCount();
184  ui->tableWidgetMyMasternodes->insertRow(nNewRow);
185  }
186 
187  QTableWidgetItem* aliasItem = new QTableWidgetItem(strAlias);
188  QTableWidgetItem* addrItem = new QTableWidgetItem(pmn ? QString::fromStdString(pmn->addr.ToString()) : strAddr);
189  QTableWidgetItem* statusItem = new QTableWidgetItem(QString::fromStdString(pmn ? pmn->GetStatus() : "MISSING"));
190  GUIUtil::DHMSTableWidgetItem* activeSecondsItem = new GUIUtil::DHMSTableWidgetItem(pmn ? (pmn->lastPing.sigTime - pmn->sigTime) : 0);
191  QTableWidgetItem* lastSeenItem = new QTableWidgetItem(QString::fromStdString(pmn ? DateTimeStrFormat("%Y-%m-%d %H:%M", pmn->lastPing.sigTime) : ""));
192  QTableWidgetItem* pubkeyItem = new QTableWidgetItem(QString::fromStdString(pmn ? pmn->pubKeyCollateralAddress.GetHex() : ""));
193 
194  ui->tableWidgetMyMasternodes->setItem(nNewRow, 0, aliasItem);
195  ui->tableWidgetMyMasternodes->setItem(nNewRow, 1, addrItem);
196  ui->tableWidgetMyMasternodes->setItem(nNewRow, 2, statusItem);
197  ui->tableWidgetMyMasternodes->setItem(nNewRow, 3, activeSecondsItem);
198  ui->tableWidgetMyMasternodes->setItem(nNewRow, 4, lastSeenItem);
199  ui->tableWidgetMyMasternodes->setItem(nNewRow, 5, pubkeyItem);
200 }
201 
203 {
204  {
205  static int64_t nTimeMyListUpdated = 0;
206 
207  // automatically update my masternode list only once in MY_MASTERNODELIST_UPDATE_SECONDS seconds,
208  // this update still can be triggered manually at any time via button click
209  int64_t nSecondsTillUpdate = nTimeMyListUpdated + MY_MASTERNODELIST_UPDATE_SECONDS - GetTime();
210 
211  if (nSecondsTillUpdate > 0 && !fForce) return;
212  nTimeMyListUpdated = GetTime();
213 
214  ui->tableWidgetMyMasternodes->setSortingEnabled(false);
216  int nIndex;
217  if(!mne.castOutputIndex(nIndex))
218  continue;
219 
220  CTxIn txin = CTxIn(uint256S(mne.getTxHash()), uint32_t(nIndex));
221  CMasternode* pmn = mnodeman.Find(txin);
222  updateMyMasternodeInfo(QString::fromStdString(mne.getAlias()), QString::fromStdString(mne.getIp()), pmn);
223  }
224  ui->tableWidgetMyMasternodes->setSortingEnabled(true);
225  }
226 }
227 
229 {
230  // Find selected node alias
231  QItemSelectionModel* selectionModel = ui->tableWidgetMyMasternodes->selectionModel();
232  QModelIndexList selected = selectionModel->selectedRows();
233 
234  if (selected.count() == 0) return;
235 
236  QModelIndex index = selected.at(0);
237  int nSelectedRow = index.row();
238  std::string strAlias = ui->tableWidgetMyMasternodes->item(nSelectedRow, 0)->text().toStdString();
239 
240  // Display message box
241  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm masternode start"),
242  tr("Are you sure you want to start masternode %1?").arg(QString::fromStdString(strAlias)),
243  QMessageBox::Yes | QMessageBox::Cancel,
244  QMessageBox::Cancel);
245 
246  if (retval != QMessageBox::Yes) return;
247 
249 
250  if (encStatus == walletModel->Locked || encStatus == walletModel->UnlockedForStakingOnly) {
252 
253  if (!ctx.isValid()) return; // Unlock wallet was cancelled
254 
255  StartAlias(strAlias);
256  return;
257  }
258 
259  StartAlias(strAlias);
260 }
261 
263 {
264  // Display message box
265  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm all masternodes start"),
266  tr("Are you sure you want to start ALL masternodes?"),
267  QMessageBox::Yes | QMessageBox::Cancel,
268  QMessageBox::Cancel);
269 
270  if (retval != QMessageBox::Yes) return;
271 
273 
274  if (encStatus == walletModel->Locked || encStatus == walletModel->UnlockedForStakingOnly) {
276 
277  if (!ctx.isValid()) return; // Unlock wallet was cancelled
278 
279  StartAll();
280  return;
281  }
282 
283  StartAll();
284 }
285 
287 {
289  QMessageBox::critical(this, tr("Command is not available right now"),
290  tr("You can't use this command until masternode list is synced"));
291  return;
292  }
293 
294  // Display message box
295  QMessageBox::StandardButton retval = QMessageBox::question(this,
296  tr("Confirm missing masternodes start"),
297  tr("Are you sure you want to start MISSING masternodes?"),
298  QMessageBox::Yes | QMessageBox::Cancel,
299  QMessageBox::Cancel);
300 
301  if (retval != QMessageBox::Yes) return;
302 
304 
305  if (encStatus == walletModel->Locked || encStatus == walletModel->UnlockedForStakingOnly) {
307 
308  if (!ctx.isValid()) return; // Unlock wallet was cancelled
309 
310  StartAll("start-missing");
311  return;
312  }
313 
314  StartAll("start-missing");
315 }
316 
318 {
319  if (ui->tableWidgetMyMasternodes->selectedItems().count() > 0) {
320  ui->startButton->setEnabled(true);
321  }
322 }
323 
325 {
326  updateMyNodeList(true);
327 }
MasternodeList::on_startAllButton_clicked
void on_startAllButton_clicked()
Definition: masternodelist.cpp:262
CTxIn
An input of a transaction.
Definition: transaction.h:83
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:183
MasternodeList::on_tableWidgetMyMasternodes_itemSelectionChanged
void on_tableWidgetMyMasternodes_itemSelectionChanged()
Definition: masternodelist.cpp:317
MasternodeList::updateMyNodeList
void updateMyNodeList(bool fForce=false)
Definition: masternodelist.cpp:202
GetTime
int64_t GetTime()
For unit testing.
Definition: utiltime.cpp:19
WalletModel::EncryptionStatus
EncryptionStatus
Definition: walletmodel.h:124
WalletModel::UnlockContext::isValid
bool isValid() const
Definition: walletmodel.h:195
CMasternodeMan::UpdateMasternodeList
void UpdateMasternodeList(CMasternodeBroadcast mnb)
Update masternode list and maps using provided CMasternodeBroadcast.
Definition: masternodeman.cpp:819
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:102
CMasternodeBroadcast::Create
static bool Create(CTxIn vin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string &strErrorRet, CMasternodeBroadcast &mnbRet)
Create Masternode broadcast, needs to be relayed manually after that.
Definition: masternode.cpp:467
MasternodeList::timer
QTimer * timer
Definition: masternodelist.h:61
sync.h
walletmodel.h
CMasternodeMan::Find
CMasternode * Find(const CScript &payee)
Find an entry.
Definition: masternodeman.cpp:440
MasternodeList::clientModel
ClientModel * clientModel
Definition: masternodelist.h:63
wallet.h
MasternodeList::setWalletModel
void setWalletModel(WalletModel *walletModel)
Definition: masternodelist.cpp:71
masternode-sync.h
CMasternodeBroadcast::Relay
void Relay()
Definition: masternode.cpp:694
MasternodeList::showContextMenu
void showContextMenu(const QPoint &)
Definition: masternodelist.cpp:76
AnnotatedMixin< std::recursive_mutex >
MasternodeList::cs_mnlistupdate
RecursiveMutex cs_mnlistupdate
Definition: masternodelist.h:65
CMasternodePing::sigTime
int64_t sigTime
Definition: masternode.h:44
DateTimeStrFormat
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:50
masternodeman.h
CPubKey::GetHex
std::string GetHex() const
Definition: pubkey.h:193
CMasternode::sigTime
int64_t sigTime
Definition: masternode.h:135
MasternodeList::MasternodeList
MasternodeList(QWidget *parent=0)
Definition: masternodelist.cpp:22
masternodeSync
CMasternodeSync masternodeSync
Definition: masternode-sync.cpp:19
cs_main
RecursiveMutex cs_main
Global state.
Definition: main.cpp:65
MasternodeList::on_startButton_clicked
void on_startButton_clicked()
Definition: masternodelist.cpp:228
mnodeman
CMasternodeMan mnodeman
Masternode manager.
Definition: masternodeman.cpp:22
CService::ToString
std::string ToString() const
Definition: netaddress.cpp:568
masternodelist.h
MasternodeList::on_UpdateButton_clicked
void on_UpdateButton_clicked()
Definition: masternodelist.cpp:324
MasternodeList
Masternode Manager page widget.
Definition: masternodelist.h:36
init.h
MasternodeList::updateMyMasternodeInfo
void updateMyMasternodeInfo(QString strAlias, QString strAddr, CMasternode *pmn)
Definition: masternodelist.cpp:168
cs_masternodes
RecursiveMutex cs_masternodes
Definition: masternodelist.cpp:20
AskPassphraseDialog::Context::Unlock_Full
@ Unlock_Full
Unlock wallet from menu
WalletModel::getEncryptionStatus
EncryptionStatus getEncryptionStatus() const
Definition: walletmodel.cpp:373
GUIUtil::DHMSTableWidgetItem
Extension to QTableWidgetItem that facilitates proper ordering for "DHMS" strings (primarily used in ...
Definition: guiutil.h:212
guiutil.h
uint256S
uint256 uint256S(const char *str)
Definition: uint256.h:99
WalletModel::Locked
@ Locked
Definition: walletmodel.h:126
MasternodeList::~MasternodeList
~MasternodeList()
Definition: masternodelist.cpp:61
MasternodeList::contextMenu
QMenu * contextMenu
Definition: masternodelist.h:50
MasternodeList::StartAll
void StartAll(std::string strCommand="start-all")
Definition: masternodelist.cpp:121
MasternodeList::nTimeFilterUpdated
int64_t nTimeFilterUpdated
Definition: masternodelist.h:51
activemasternode.h
CMasternode::addr
CService addr
Definition: masternode.h:128
masternodeConfig
CMasternodeConfig masternodeConfig
Definition: masternodeconfig.cpp:13
CMasternodeSync::IsMasternodeListSynced
bool IsMasternodeListSynced()
Definition: masternode-sync.h:78
strprintf
#define strprintf
Definition: tinyformat.h:1056
ClientModel
Model for PRCY network client.
Definition: clientmodel.h:44
CMasternode
Definition: masternode.h:107
Ui
Definition: 2faconfirmdialog.h:7
CMasternode::GetStatus
std::string GetStatus()
Definition: masternode.cpp:307
WalletModel::UnlockContext
Definition: walletmodel.h:189
MasternodeList::StartAlias
void StartAlias(std::string strAlias)
Definition: masternodelist.cpp:87
LOCK
#define LOCK(cs)
Definition: sync.h:182
CMasternodeConfig::CMasternodeEntry
Definition: masternodeconfig.h:22
MasternodeList::on_startMissingButton_clicked
void on_startMissingButton_clicked()
Definition: masternodelist.cpp:286
MasternodeList::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
Definition: masternodelist.cpp:82
MY_MASTERNODELIST_UPDATE_SECONDS
#define MY_MASTERNODELIST_UPDATE_SECONDS
Definition: masternodelist.h:17
CWallet::cs_wallet
RecursiveMutex cs_wallet
Definition: wallet.h:301
MasternodeList::walletModel
WalletModel * walletModel
Definition: masternodelist.h:64
WalletModel::UnlockedForStakingOnly
@ UnlockedForStakingOnly
Definition: walletmodel.h:128
GUIUtil::loadStyleSheet
QString loadStyleSheet()
Load global CSS theme.
Definition: guiutil.cpp:721
MasternodeList::setClientModel
void setClientModel(ClientModel *clientModel)
Definition: masternodelist.cpp:66
CMasternodeBroadcast
Definition: masternode.h:292
pwalletMain
CWallet * pwalletMain
Definition: wallet.cpp:49
MasternodeList::ui
Ui::MasternodeList * ui
Definition: masternodelist.h:62
WalletModel::requestUnlock
UnlockContext requestUnlock(AskPassphraseDialog::Context context, bool relock=false)
Definition: walletmodel.cpp:546
askpassphrasedialog.h
masternodeconfig.h
CMasternode::pubKeyCollateralAddress
CPubKey pubKeyCollateralAddress
Definition: masternode.h:129
clientmodel.h
CMasternode::lastPing
CMasternodePing lastPing
Definition: masternode.h:145
MasternodeList::fFilterUpdated
bool fFilterUpdated
Definition: masternodelist.h:52
CMasternodeConfig::getEntries
std::vector< CMasternodeEntry > & getEntries()
Definition: masternodeconfig.h:103