Bitcoin Core  26.99.0
P2P Digital Currency
rpcconsole.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <qt/rpcconsole.h>
10 #include <qt/forms/ui_debugwindow.h>
11 
12 #include <chainparams.h>
13 #include <common/system.h>
14 #include <interfaces/node.h>
15 #include <node/connection_types.h>
16 #include <qt/bantablemodel.h>
17 #include <qt/clientmodel.h>
18 #include <qt/guiutil.h>
19 #include <qt/peertablesortproxy.h>
20 #include <qt/platformstyle.h>
21 #include <qt/walletmodel.h>
22 #include <rpc/client.h>
23 #include <rpc/server.h>
24 #include <util/strencodings.h>
25 #include <util/string.h>
26 #include <util/threadnames.h>
27 
28 #include <univalue.h>
29 
30 #include <QAbstractButton>
31 #include <QAbstractItemModel>
32 #include <QDateTime>
33 #include <QFont>
34 #include <QKeyEvent>
35 #include <QKeySequence>
36 #include <QLatin1String>
37 #include <QLocale>
38 #include <QMenu>
39 #include <QMessageBox>
40 #include <QScreen>
41 #include <QScrollBar>
42 #include <QSettings>
43 #include <QString>
44 #include <QStringList>
45 #include <QStyledItemDelegate>
46 #include <QTime>
47 #include <QTimer>
48 #include <QVariant>
49 
50 #include <chrono>
51 
52 const int CONSOLE_HISTORY = 50;
54 const QSize FONT_RANGE(4, 40);
55 const char fontSizeSettingsKey[] = "consoleFontSize";
56 
57 const struct {
58  const char *url;
59  const char *source;
60 } ICON_MAPPING[] = {
61  {"cmd-request", ":/icons/tx_input"},
62  {"cmd-reply", ":/icons/tx_output"},
63  {"cmd-error", ":/icons/tx_output"},
64  {"misc", ":/icons/tx_inout"},
65  {nullptr, nullptr}
66 };
67 
68 namespace {
69 
70 // don't add private key handling cmd's to the history
71 const QStringList historyFilter = QStringList()
72  << "importprivkey"
73  << "importmulti"
74  << "sethdseed"
75  << "signmessagewithprivkey"
76  << "signrawtransactionwithkey"
77  << "walletpassphrase"
78  << "walletpassphrasechange"
79  << "encryptwallet";
80 
81 }
82 
83 /* Object for executing console RPC commands in a separate thread.
84 */
85 class RPCExecutor : public QObject
86 {
87  Q_OBJECT
88 public:
90 
91 public Q_SLOTS:
92  void request(const QString &command, const WalletModel* wallet_model);
93 
94 Q_SIGNALS:
95  void reply(int category, const QString &command);
96 
97 private:
99 };
100 
104 class QtRPCTimerBase: public QObject, public RPCTimerBase
105 {
106  Q_OBJECT
107 public:
108  QtRPCTimerBase(std::function<void()>& _func, int64_t millis):
109  func(_func)
110  {
111  timer.setSingleShot(true);
112  connect(&timer, &QTimer::timeout, [this]{ func(); });
113  timer.start(millis);
114  }
115  ~QtRPCTimerBase() = default;
116 private:
117  QTimer timer;
118  std::function<void()> func;
119 };
120 
122 {
123 public:
124  ~QtRPCTimerInterface() = default;
125  const char *Name() override { return "Qt"; }
126  RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) override
127  {
128  return new QtRPCTimerBase(func, millis);
129  }
130 };
131 
132 class PeerIdViewDelegate : public QStyledItemDelegate
133 {
134  Q_OBJECT
135 public:
136  explicit PeerIdViewDelegate(QObject* parent = nullptr)
137  : QStyledItemDelegate(parent) {}
138 
139  QString displayText(const QVariant& value, const QLocale& locale) const override
140  {
141  // Additional spaces should visually separate right-aligned content
142  // from the next column to the right.
143  return value.toString() + QLatin1String(" ");
144  }
145 };
146 
147 #include <qt/rpcconsole.moc>
148 
169 bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const WalletModel* wallet_model)
170 {
171  std::vector< std::vector<std::string> > stack;
172  stack.emplace_back();
173 
174  enum CmdParseState
175  {
176  STATE_EATING_SPACES,
177  STATE_EATING_SPACES_IN_ARG,
178  STATE_EATING_SPACES_IN_BRACKETS,
179  STATE_ARGUMENT,
180  STATE_SINGLEQUOTED,
181  STATE_DOUBLEQUOTED,
182  STATE_ESCAPE_OUTER,
183  STATE_ESCAPE_DOUBLEQUOTED,
184  STATE_COMMAND_EXECUTED,
185  STATE_COMMAND_EXECUTED_INNER
186  } state = STATE_EATING_SPACES;
187  std::string curarg;
188  UniValue lastResult;
189  unsigned nDepthInsideSensitive = 0;
190  size_t filter_begin_pos = 0, chpos;
191  std::vector<std::pair<size_t, size_t>> filter_ranges;
192 
193  auto add_to_current_stack = [&](const std::string& strArg) {
194  if (stack.back().empty() && (!nDepthInsideSensitive) && historyFilter.contains(QString::fromStdString(strArg), Qt::CaseInsensitive)) {
195  nDepthInsideSensitive = 1;
196  filter_begin_pos = chpos;
197  }
198  // Make sure stack is not empty before adding something
199  if (stack.empty()) {
200  stack.emplace_back();
201  }
202  stack.back().push_back(strArg);
203  };
204 
205  auto close_out_params = [&]() {
206  if (nDepthInsideSensitive) {
207  if (!--nDepthInsideSensitive) {
208  assert(filter_begin_pos);
209  filter_ranges.emplace_back(filter_begin_pos, chpos);
210  filter_begin_pos = 0;
211  }
212  }
213  stack.pop_back();
214  };
215 
216  std::string strCommandTerminated = strCommand;
217  if (strCommandTerminated.back() != '\n')
218  strCommandTerminated += "\n";
219  for (chpos = 0; chpos < strCommandTerminated.size(); ++chpos)
220  {
221  char ch = strCommandTerminated[chpos];
222  switch(state)
223  {
224  case STATE_COMMAND_EXECUTED_INNER:
225  case STATE_COMMAND_EXECUTED:
226  {
227  bool breakParsing = true;
228  switch(ch)
229  {
230  case '[': curarg.clear(); state = STATE_COMMAND_EXECUTED_INNER; break;
231  default:
232  if (state == STATE_COMMAND_EXECUTED_INNER)
233  {
234  if (ch != ']')
235  {
236  // append char to the current argument (which is also used for the query command)
237  curarg += ch;
238  break;
239  }
240  if (curarg.size() && fExecute)
241  {
242  // if we have a value query, query arrays with index and objects with a string key
243  UniValue subelement;
244  if (lastResult.isArray())
245  {
246  const auto parsed{ToIntegral<size_t>(curarg)};
247  if (!parsed) {
248  throw std::runtime_error("Invalid result query");
249  }
250  subelement = lastResult[parsed.value()];
251  }
252  else if (lastResult.isObject())
253  subelement = lastResult.find_value(curarg);
254  else
255  throw std::runtime_error("Invalid result query"); //no array or object: abort
256  lastResult = subelement;
257  }
258 
259  state = STATE_COMMAND_EXECUTED;
260  break;
261  }
262  // don't break parsing when the char is required for the next argument
263  breakParsing = false;
264 
265  // pop the stack and return the result to the current command arguments
266  close_out_params();
267 
268  // don't stringify the json in case of a string to avoid doublequotes
269  if (lastResult.isStr())
270  curarg = lastResult.get_str();
271  else
272  curarg = lastResult.write(2);
273 
274  // if we have a non empty result, use it as stack argument otherwise as general result
275  if (curarg.size())
276  {
277  if (stack.size())
278  add_to_current_stack(curarg);
279  else
280  strResult = curarg;
281  }
282  curarg.clear();
283  // assume eating space state
284  state = STATE_EATING_SPACES;
285  }
286  if (breakParsing)
287  break;
288  [[fallthrough]];
289  }
290  case STATE_ARGUMENT: // In or after argument
291  case STATE_EATING_SPACES_IN_ARG:
292  case STATE_EATING_SPACES_IN_BRACKETS:
293  case STATE_EATING_SPACES: // Handle runs of whitespace
294  switch(ch)
295  {
296  case '"': state = STATE_DOUBLEQUOTED; break;
297  case '\'': state = STATE_SINGLEQUOTED; break;
298  case '\\': state = STATE_ESCAPE_OUTER; break;
299  case '(': case ')': case '\n':
300  if (state == STATE_EATING_SPACES_IN_ARG)
301  throw std::runtime_error("Invalid Syntax");
302  if (state == STATE_ARGUMENT)
303  {
304  if (ch == '(' && stack.size() && stack.back().size() > 0)
305  {
306  if (nDepthInsideSensitive) {
307  ++nDepthInsideSensitive;
308  }
309  stack.emplace_back();
310  }
311 
312  // don't allow commands after executed commands on baselevel
313  if (!stack.size())
314  throw std::runtime_error("Invalid Syntax");
315 
316  add_to_current_stack(curarg);
317  curarg.clear();
318  state = STATE_EATING_SPACES_IN_BRACKETS;
319  }
320  if ((ch == ')' || ch == '\n') && stack.size() > 0)
321  {
322  if (fExecute) {
323  // Convert argument list to JSON objects in method-dependent way,
324  // and pass it along with the method name to the dispatcher.
325  UniValue params = RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end()));
326  std::string method = stack.back()[0];
327  std::string uri;
328 #ifdef ENABLE_WALLET
329  if (wallet_model) {
330  QByteArray encodedName = QUrl::toPercentEncoding(wallet_model->getWalletName());
331  uri = "/wallet/"+std::string(encodedName.constData(), encodedName.length());
332  }
333 #endif
334  assert(node);
335  lastResult = node->executeRpc(method, params, uri);
336  }
337 
338  state = STATE_COMMAND_EXECUTED;
339  curarg.clear();
340  }
341  break;
342  case ' ': case ',': case '\t':
343  if(state == STATE_EATING_SPACES_IN_ARG && curarg.empty() && ch == ',')
344  throw std::runtime_error("Invalid Syntax");
345 
346  else if(state == STATE_ARGUMENT) // Space ends argument
347  {
348  add_to_current_stack(curarg);
349  curarg.clear();
350  }
351  if ((state == STATE_EATING_SPACES_IN_BRACKETS || state == STATE_ARGUMENT) && ch == ',')
352  {
353  state = STATE_EATING_SPACES_IN_ARG;
354  break;
355  }
356  state = STATE_EATING_SPACES;
357  break;
358  default: curarg += ch; state = STATE_ARGUMENT;
359  }
360  break;
361  case STATE_SINGLEQUOTED: // Single-quoted string
362  switch(ch)
363  {
364  case '\'': state = STATE_ARGUMENT; break;
365  default: curarg += ch;
366  }
367  break;
368  case STATE_DOUBLEQUOTED: // Double-quoted string
369  switch(ch)
370  {
371  case '"': state = STATE_ARGUMENT; break;
372  case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
373  default: curarg += ch;
374  }
375  break;
376  case STATE_ESCAPE_OUTER: // '\' outside quotes
377  curarg += ch; state = STATE_ARGUMENT;
378  break;
379  case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
380  if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
381  curarg += ch; state = STATE_DOUBLEQUOTED;
382  break;
383  }
384  }
385  if (pstrFilteredOut) {
386  if (STATE_COMMAND_EXECUTED == state) {
387  assert(!stack.empty());
388  close_out_params();
389  }
390  *pstrFilteredOut = strCommand;
391  for (auto i = filter_ranges.rbegin(); i != filter_ranges.rend(); ++i) {
392  pstrFilteredOut->replace(i->first, i->second - i->first, "(…)");
393  }
394  }
395  switch(state) // final state
396  {
397  case STATE_COMMAND_EXECUTED:
398  if (lastResult.isStr())
399  strResult = lastResult.get_str();
400  else
401  strResult = lastResult.write(2);
402  [[fallthrough]];
403  case STATE_ARGUMENT:
404  case STATE_EATING_SPACES:
405  return true;
406  default: // ERROR to end in one of the other states
407  return false;
408  }
409 }
410 
411 void RPCExecutor::request(const QString &command, const WalletModel* wallet_model)
412 {
413  try
414  {
415  std::string result;
416  std::string executableCommand = command.toStdString() + "\n";
417 
418  // Catch the console-only-help command before RPC call is executed and reply with help text as-if a RPC reply.
419  if(executableCommand == "help-console\n") {
420  Q_EMIT reply(RPCConsole::CMD_REPLY, QString(("\n"
421  "This console accepts RPC commands using the standard syntax.\n"
422  " example: getblockhash 0\n\n"
423 
424  "This console can also accept RPC commands using the parenthesized syntax.\n"
425  " example: getblockhash(0)\n\n"
426 
427  "Commands may be nested when specified with the parenthesized syntax.\n"
428  " example: getblock(getblockhash(0) 1)\n\n"
429 
430  "A space or a comma can be used to delimit arguments for either syntax.\n"
431  " example: getblockhash 0\n"
432  " getblockhash,0\n\n"
433 
434  "Named results can be queried with a non-quoted key string in brackets using the parenthesized syntax.\n"
435  " example: getblock(getblockhash(0) 1)[tx]\n\n"
436 
437  "Results without keys can be queried with an integer in brackets using the parenthesized syntax.\n"
438  " example: getblock(getblockhash(0),1)[tx][0]\n\n")));
439  return;
440  }
441  if (!RPCConsole::RPCExecuteCommandLine(m_node, result, executableCommand, nullptr, wallet_model)) {
442  Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
443  return;
444  }
445 
446  Q_EMIT reply(RPCConsole::CMD_REPLY, QString::fromStdString(result));
447  }
448  catch (UniValue& objError)
449  {
450  try // Nice formatting for standard-format error
451  {
452  int code = objError.find_value("code").getInt<int>();
453  std::string message = objError.find_value("message").get_str();
454  Q_EMIT reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
455  }
456  catch (const std::runtime_error&) // raised when converting to invalid type, i.e. missing code or message
457  { // Show raw JSON object
458  Q_EMIT reply(RPCConsole::CMD_ERROR, QString::fromStdString(objError.write()));
459  }
460  }
461  catch (const std::exception& e)
462  {
463  Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
464  }
465 }
466 
467 RPCConsole::RPCConsole(interfaces::Node& node, const PlatformStyle *_platformStyle, QWidget *parent) :
468  QWidget(parent),
469  m_node(node),
470  ui(new Ui::RPCConsole),
471  platformStyle(_platformStyle)
472 {
473  ui->setupUi(this);
474  QSettings settings;
475 #ifdef ENABLE_WALLET
477  // RPCConsole widget is a window.
478  if (!restoreGeometry(settings.value("RPCConsoleWindowGeometry").toByteArray())) {
479  // Restore failed (perhaps missing setting), center the window
480  move(QGuiApplication::primaryScreen()->availableGeometry().center() - frameGeometry().center());
481  }
482  ui->splitter->restoreState(settings.value("RPCConsoleWindowPeersTabSplitterSizes").toByteArray());
483  } else
484 #endif // ENABLE_WALLET
485  {
486  // RPCConsole is a child widget.
487  ui->splitter->restoreState(settings.value("RPCConsoleWidgetPeersTabSplitterSizes").toByteArray());
488  }
489 
490  m_peer_widget_header_state = settings.value("PeersTabPeerHeaderState").toByteArray();
491  m_banlist_widget_header_state = settings.value("PeersTabBanlistHeaderState").toByteArray();
492 
493  constexpr QChar nonbreaking_hyphen(8209);
494  const std::vector<QString> CONNECTION_TYPE_DOC{
495  //: Explanatory text for an inbound peer connection.
496  tr("Inbound: initiated by peer"),
497  /*: Explanatory text for an outbound peer connection that
498  relays all network information. This is the default behavior for
499  outbound connections. */
500  tr("Outbound Full Relay: default"),
501  /*: Explanatory text for an outbound peer connection that relays
502  network information about blocks and not transactions or addresses. */
503  tr("Outbound Block Relay: does not relay transactions or addresses"),
504  /*: Explanatory text for an outbound peer connection that was
505  established manually through one of several methods. The numbered
506  arguments are stand-ins for the methods available to establish
507  manual connections. */
508  tr("Outbound Manual: added using RPC %1 or %2/%3 configuration options")
509  .arg("addnode")
510  .arg(QString(nonbreaking_hyphen) + "addnode")
511  .arg(QString(nonbreaking_hyphen) + "connect"),
512  /*: Explanatory text for a short-lived outbound peer connection that
513  is used to test the aliveness of known addresses. */
514  tr("Outbound Feeler: short-lived, for testing addresses"),
515  /*: Explanatory text for a short-lived outbound peer connection that is used
516  to request addresses from a peer. */
517  tr("Outbound Address Fetch: short-lived, for soliciting addresses")};
518  const QString connection_types_list{"<ul><li>" + Join(CONNECTION_TYPE_DOC, QString("</li><li>")) + "</li></ul>"};
519  ui->peerConnectionTypeLabel->setToolTip(ui->peerConnectionTypeLabel->toolTip().arg(connection_types_list));
520  const std::vector<QString> TRANSPORT_TYPE_DOC{
521  //: Explanatory text for "detecting" transport type.
522  tr("detecting: peer could be v1 or v2"),
523  //: Explanatory text for v1 transport type.
524  tr("v1: unencrypted, plaintext transport protocol"),
525  //: Explanatory text for v2 transport type.
526  tr("v2: BIP324 encrypted transport protocol")};
527  const QString transport_types_list{"<ul><li>" + Join(TRANSPORT_TYPE_DOC, QString("</li><li>")) + "</li></ul>"};
528  ui->peerTransportTypeLabel->setToolTip(ui->peerTransportTypeLabel->toolTip().arg(transport_types_list));
529  const QString hb_list{"<ul><li>\""
530  + ts.to + "\" – " + tr("we selected the peer for high bandwidth relay") + "</li><li>\""
531  + ts.from + "\" – " + tr("the peer selected us for high bandwidth relay") + "</li><li>\""
532  + ts.no + "\" – " + tr("no high bandwidth relay selected") + "</li></ul>"};
533  ui->peerHighBandwidthLabel->setToolTip(ui->peerHighBandwidthLabel->toolTip().arg(hb_list));
534  ui->dataDir->setToolTip(ui->dataDir->toolTip().arg(QString(nonbreaking_hyphen) + "datadir"));
535  ui->blocksDir->setToolTip(ui->blocksDir->toolTip().arg(QString(nonbreaking_hyphen) + "blocksdir"));
536  ui->openDebugLogfileButton->setToolTip(ui->openDebugLogfileButton->toolTip().arg(PACKAGE_NAME));
537 
539  ui->openDebugLogfileButton->setIcon(platformStyle->SingleColorIcon(":/icons/export"));
540  }
541  ui->clearButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
542 
543  ui->fontBiggerButton->setIcon(platformStyle->SingleColorIcon(":/icons/fontbigger"));
544  //: Main shortcut to increase the RPC console font size.
545  ui->fontBiggerButton->setShortcut(tr("Ctrl++"));
546  //: Secondary shortcut to increase the RPC console font size.
547  GUIUtil::AddButtonShortcut(ui->fontBiggerButton, tr("Ctrl+="));
548 
549  ui->fontSmallerButton->setIcon(platformStyle->SingleColorIcon(":/icons/fontsmaller"));
550  //: Main shortcut to decrease the RPC console font size.
551  ui->fontSmallerButton->setShortcut(tr("Ctrl+-"));
552  //: Secondary shortcut to decrease the RPC console font size.
553  GUIUtil::AddButtonShortcut(ui->fontSmallerButton, tr("Ctrl+_"));
554 
555  ui->promptIcon->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/prompticon")));
556 
557  // Install event filter for up and down arrow
558  ui->lineEdit->installEventFilter(this);
559  ui->lineEdit->setMaxLength(16 * 1024 * 1024);
560  ui->messagesWidget->installEventFilter(this);
561 
562  connect(ui->clearButton, &QAbstractButton::clicked, [this] { clear(); });
563  connect(ui->fontBiggerButton, &QAbstractButton::clicked, this, &RPCConsole::fontBigger);
564  connect(ui->fontSmallerButton, &QAbstractButton::clicked, this, &RPCConsole::fontSmaller);
565  connect(ui->btnClearTrafficGraph, &QPushButton::clicked, ui->trafficGraph, &TrafficGraphWidget::clear);
566 
567  // disable the wallet selector by default
568  ui->WalletSelector->setVisible(false);
569  ui->WalletSelectorLabel->setVisible(false);
570 
571  // Register RPC timer interface
573  // avoid accidentally overwriting an existing, non QTThread
574  // based timer interface
576 
579 
580  consoleFontSize = settings.value(fontSizeSettingsKey, QFont().pointSize()).toInt();
581  clear();
582 
584 }
585 
587 {
588  QSettings settings;
589 #ifdef ENABLE_WALLET
591  // RPCConsole widget is a window.
592  settings.setValue("RPCConsoleWindowGeometry", saveGeometry());
593  settings.setValue("RPCConsoleWindowPeersTabSplitterSizes", ui->splitter->saveState());
594  } else
595 #endif // ENABLE_WALLET
596  {
597  // RPCConsole is a child widget.
598  settings.setValue("RPCConsoleWidgetPeersTabSplitterSizes", ui->splitter->saveState());
599  }
600 
601  settings.setValue("PeersTabPeerHeaderState", m_peer_widget_header_state);
602  settings.setValue("PeersTabBanlistHeaderState", m_banlist_widget_header_state);
603 
605  delete rpcTimerInterface;
606  delete ui;
607 }
608 
609 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
610 {
611  if(event->type() == QEvent::KeyPress) // Special key handling
612  {
613  QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
614  int key = keyevt->key();
615  Qt::KeyboardModifiers mod = keyevt->modifiers();
616  switch(key)
617  {
618  case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
619  case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
620  case Qt::Key_PageUp: /* pass paging keys to messages widget */
621  case Qt::Key_PageDown:
622  if (obj == ui->lineEdit) {
623  QApplication::sendEvent(ui->messagesWidget, keyevt);
624  return true;
625  }
626  break;
627  case Qt::Key_Return:
628  case Qt::Key_Enter:
629  // forward these events to lineEdit
630  if (obj == autoCompleter->popup()) {
631  QApplication::sendEvent(ui->lineEdit, keyevt);
632  autoCompleter->popup()->hide();
633  return true;
634  }
635  break;
636  default:
637  // Typing in messages widget brings focus to line edit, and redirects key there
638  // Exclude most combinations and keys that emit no text, except paste shortcuts
639  if(obj == ui->messagesWidget && (
640  (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
641  ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
642  ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
643  {
644  ui->lineEdit->setFocus();
645  QApplication::sendEvent(ui->lineEdit, keyevt);
646  return true;
647  }
648  }
649  }
650  return QWidget::eventFilter(obj, event);
651 }
652 
653 void RPCConsole::setClientModel(ClientModel *model, int bestblock_height, int64_t bestblock_date, double verification_progress)
654 {
655  clientModel = model;
656 
657  bool wallet_enabled{false};
658 #ifdef ENABLE_WALLET
659  wallet_enabled = WalletModel::isWalletEnabled();
660 #endif // ENABLE_WALLET
661  if (model && !wallet_enabled) {
662  // Show warning, for example if this is a prerelease version
663  connect(model, &ClientModel::alertsChanged, this, &RPCConsole::updateAlerts);
665  }
666 
667  ui->trafficGraph->setClientModel(model);
669  // Keep up to date with client
672 
673  setNumBlocks(bestblock_height, QDateTime::fromSecsSinceEpoch(bestblock_date), verification_progress, SyncType::BLOCK_SYNC);
675 
678 
680  updateTrafficStats(node.getTotalBytesRecv(), node.getTotalBytesSent());
682 
684 
685  // set up peer table
686  ui->peerWidget->setModel(model->peerTableSortProxy());
687  ui->peerWidget->verticalHeader()->hide();
688  ui->peerWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
689  ui->peerWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
690  ui->peerWidget->setContextMenuPolicy(Qt::CustomContextMenu);
691 
692  if (!ui->peerWidget->horizontalHeader()->restoreState(m_peer_widget_header_state)) {
693  ui->peerWidget->setColumnWidth(PeerTableModel::Address, ADDRESS_COLUMN_WIDTH);
694  ui->peerWidget->setColumnWidth(PeerTableModel::Subversion, SUBVERSION_COLUMN_WIDTH);
695  ui->peerWidget->setColumnWidth(PeerTableModel::Ping, PING_COLUMN_WIDTH);
696  }
697  ui->peerWidget->horizontalHeader()->setSectionResizeMode(PeerTableModel::Age, QHeaderView::ResizeToContents);
698  ui->peerWidget->horizontalHeader()->setStretchLastSection(true);
699  ui->peerWidget->setItemDelegateForColumn(PeerTableModel::NetNodeId, new PeerIdViewDelegate(this));
700 
701  // create peer table context menu
702  peersTableContextMenu = new QMenu(this);
703  //: Context menu action to copy the address of a peer.
704  peersTableContextMenu->addAction(tr("&Copy address"), [this] {
705  GUIUtil::copyEntryData(ui->peerWidget, PeerTableModel::Address, Qt::DisplayRole);
706  });
707  peersTableContextMenu->addSeparator();
708  peersTableContextMenu->addAction(tr("&Disconnect"), this, &RPCConsole::disconnectSelectedNode);
709  peersTableContextMenu->addAction(ts.ban_for + " " + tr("1 &hour"), [this] { banSelectedNode(60 * 60); });
710  peersTableContextMenu->addAction(ts.ban_for + " " + tr("1 d&ay"), [this] { banSelectedNode(60 * 60 * 24); });
711  peersTableContextMenu->addAction(ts.ban_for + " " + tr("1 &week"), [this] { banSelectedNode(60 * 60 * 24 * 7); });
712  peersTableContextMenu->addAction(ts.ban_for + " " + tr("1 &year"), [this] { banSelectedNode(60 * 60 * 24 * 365); });
713  connect(ui->peerWidget, &QTableView::customContextMenuRequested, this, &RPCConsole::showPeersTableContextMenu);
714 
715  // peer table signal handling - update peer details when selecting new node
716  connect(ui->peerWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RPCConsole::updateDetailWidget);
717  connect(model->getPeerTableModel(), &QAbstractItemModel::dataChanged, [this] { updateDetailWidget(); });
718 
719  // set up ban table
720  ui->banlistWidget->setModel(model->getBanTableModel());
721  ui->banlistWidget->verticalHeader()->hide();
722  ui->banlistWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
723  ui->banlistWidget->setSelectionMode(QAbstractItemView::SingleSelection);
724  ui->banlistWidget->setContextMenuPolicy(Qt::CustomContextMenu);
725 
726  if (!ui->banlistWidget->horizontalHeader()->restoreState(m_banlist_widget_header_state)) {
727  ui->banlistWidget->setColumnWidth(BanTableModel::Address, BANSUBNET_COLUMN_WIDTH);
728  ui->banlistWidget->setColumnWidth(BanTableModel::Bantime, BANTIME_COLUMN_WIDTH);
729  }
730  ui->banlistWidget->horizontalHeader()->setSectionResizeMode(BanTableModel::Address, QHeaderView::ResizeToContents);
731  ui->banlistWidget->horizontalHeader()->setStretchLastSection(true);
732 
733  // create ban table context menu
734  banTableContextMenu = new QMenu(this);
735  /*: Context menu action to copy the IP/Netmask of a banned peer.
736  IP/Netmask is the combination of a peer's IP address and its Netmask.
737  For IP address, see: https://en.wikipedia.org/wiki/IP_address. */
738  banTableContextMenu->addAction(tr("&Copy IP/Netmask"), [this] {
739  GUIUtil::copyEntryData(ui->banlistWidget, BanTableModel::Address, Qt::DisplayRole);
740  });
741  banTableContextMenu->addSeparator();
742  banTableContextMenu->addAction(tr("&Unban"), this, &RPCConsole::unbanSelectedNode);
743  connect(ui->banlistWidget, &QTableView::customContextMenuRequested, this, &RPCConsole::showBanTableContextMenu);
744 
745  // ban table signal handling - clear peer details when clicking a peer in the ban table
746  connect(ui->banlistWidget, &QTableView::clicked, this, &RPCConsole::clearSelectedNode);
747  // ban table signal handling - ensure ban table is shown or hidden (if empty)
748  connect(model->getBanTableModel(), &BanTableModel::layoutChanged, this, &RPCConsole::showOrHideBanTableIfRequired);
750 
751  // Provide initial values
752  ui->clientVersion->setText(model->formatFullVersion());
753  ui->clientUserAgent->setText(model->formatSubVersion());
754  ui->dataDir->setText(model->dataDir());
755  ui->blocksDir->setText(model->blocksDir());
756  ui->startupTime->setText(model->formatClientStartupTime());
757  ui->networkName->setText(QString::fromStdString(Params().GetChainTypeString()));
758 
759  //Setup autocomplete and attach it
760  QStringList wordList;
761  std::vector<std::string> commandList = m_node.listRpcCommands();
762  for (size_t i = 0; i < commandList.size(); ++i)
763  {
764  wordList << commandList[i].c_str();
765  wordList << ("help " + commandList[i]).c_str();
766  }
767 
768  wordList << "help-console";
769  wordList.sort();
770  autoCompleter = new QCompleter(wordList, this);
771  autoCompleter->setModelSorting(QCompleter::CaseSensitivelySortedModel);
772  // ui->lineEdit is initially disabled because running commands is only
773  // possible from now on.
774  ui->lineEdit->setEnabled(true);
775  ui->lineEdit->setCompleter(autoCompleter);
776  autoCompleter->popup()->installEventFilter(this);
777  // Start thread to execute RPC commands.
778  startExecutor();
779  }
780  if (!model) {
781  // Client model is being set to 0, this means shutdown() is about to be called.
782  thread.quit();
783  thread.wait();
784  }
785 }
786 
787 #ifdef ENABLE_WALLET
788 void RPCConsole::addWallet(WalletModel * const walletModel)
789 {
790  // use name for text and wallet model for internal data object (to allow to move to a wallet id later)
791  ui->WalletSelector->addItem(walletModel->getDisplayName(), QVariant::fromValue(walletModel));
792  if (ui->WalletSelector->count() == 2) {
793  // First wallet added, set to default to match wallet RPC behavior
794  ui->WalletSelector->setCurrentIndex(1);
795  }
796  if (ui->WalletSelector->count() > 2) {
797  ui->WalletSelector->setVisible(true);
798  ui->WalletSelectorLabel->setVisible(true);
799  }
800 }
801 
802 void RPCConsole::removeWallet(WalletModel * const walletModel)
803 {
804  ui->WalletSelector->removeItem(ui->WalletSelector->findData(QVariant::fromValue(walletModel)));
805  if (ui->WalletSelector->count() == 2) {
806  ui->WalletSelector->setVisible(false);
807  ui->WalletSelectorLabel->setVisible(false);
808  }
809 }
810 
811 void RPCConsole::setCurrentWallet(WalletModel* const wallet_model)
812 {
813  QVariant data = QVariant::fromValue(wallet_model);
814  ui->WalletSelector->setCurrentIndex(ui->WalletSelector->findData(data));
815 }
816 #endif
817 
818 static QString categoryClass(int category)
819 {
820  switch(category)
821  {
822  case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
823  case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
824  case RPCConsole::CMD_ERROR: return "cmd-error"; break;
825  default: return "misc";
826  }
827 }
828 
830 {
832 }
833 
835 {
837 }
838 
839 void RPCConsole::setFontSize(int newSize)
840 {
841  QSettings settings;
842 
843  //don't allow an insane font size
844  if (newSize < FONT_RANGE.width() || newSize > FONT_RANGE.height())
845  return;
846 
847  // temp. store the console content
848  QString str = ui->messagesWidget->toHtml();
849 
850  // replace font tags size in current content
851  str.replace(QString("font-size:%1pt").arg(consoleFontSize), QString("font-size:%1pt").arg(newSize));
852 
853  // store the new font size
854  consoleFontSize = newSize;
855  settings.setValue(fontSizeSettingsKey, consoleFontSize);
856 
857  // clear console (reset icon sizes, default stylesheet) and re-add the content
858  float oldPosFactor = 1.0 / ui->messagesWidget->verticalScrollBar()->maximum() * ui->messagesWidget->verticalScrollBar()->value();
859  clear(/*keep_prompt=*/true);
860  ui->messagesWidget->setHtml(str);
861  ui->messagesWidget->verticalScrollBar()->setValue(oldPosFactor * ui->messagesWidget->verticalScrollBar()->maximum());
862 }
863 
864 void RPCConsole::clear(bool keep_prompt)
865 {
866  ui->messagesWidget->clear();
867  if (!keep_prompt) ui->lineEdit->clear();
868  ui->lineEdit->setFocus();
869 
870  // Add smoothly scaled icon images.
871  // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
872  for(int i=0; ICON_MAPPING[i].url; ++i)
873  {
874  ui->messagesWidget->document()->addResource(
875  QTextDocument::ImageResource,
876  QUrl(ICON_MAPPING[i].url),
877  platformStyle->SingleColorImage(ICON_MAPPING[i].source).scaled(QSize(consoleFontSize*2, consoleFontSize*2), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
878  }
879 
880  // Set default style sheet
881 #ifdef Q_OS_MACOS
882  QFontInfo fixedFontInfo(GUIUtil::fixedPitchFont(/*use_embedded_font=*/true));
883 #else
884  QFontInfo fixedFontInfo(GUIUtil::fixedPitchFont());
885 #endif
886  ui->messagesWidget->document()->setDefaultStyleSheet(
887  QString(
888  "table { }"
889  "td.time { color: #808080; font-size: %2; padding-top: 3px; } "
890  "td.message { font-family: %1; font-size: %2; white-space:pre-wrap; } "
891  "td.cmd-request { color: #006060; } "
892  "td.cmd-error { color: red; } "
893  ".secwarning { color: red; }"
894  "b { color: #006060; } "
895  ).arg(fixedFontInfo.family(), QString("%1pt").arg(consoleFontSize))
896  );
897 
898  static const QString welcome_message =
899  /*: RPC console welcome message.
900  Placeholders %7 and %8 are style tags for the warning content, and
901  they are not space separated from the rest of the text intentionally. */
902  tr("Welcome to the %1 RPC console.\n"
903  "Use up and down arrows to navigate history, and %2 to clear screen.\n"
904  "Use %3 and %4 to increase or decrease the font size.\n"
905  "Type %5 for an overview of available commands.\n"
906  "For more information on using this console, type %6.\n"
907  "\n"
908  "%7WARNING: Scammers have been active, telling users to type"
909  " commands here, stealing their wallet contents. Do not use this console"
910  " without fully understanding the ramifications of a command.%8")
911  .arg(PACKAGE_NAME,
912  "<b>" + ui->clearButton->shortcut().toString(QKeySequence::NativeText) + "</b>",
913  "<b>" + ui->fontBiggerButton->shortcut().toString(QKeySequence::NativeText) + "</b>",
914  "<b>" + ui->fontSmallerButton->shortcut().toString(QKeySequence::NativeText) + "</b>",
915  "<b>help</b>",
916  "<b>help-console</b>",
917  "<span class=\"secwarning\">",
918  "<span>");
919 
920  message(CMD_REPLY, welcome_message, true);
921 }
922 
923 void RPCConsole::keyPressEvent(QKeyEvent *event)
924 {
925  if (windowType() != Qt::Widget && GUIUtil::IsEscapeOrBack(event->key())) {
926  close();
927  }
928 }
929 
930 void RPCConsole::changeEvent(QEvent* e)
931 {
932  if (e->type() == QEvent::PaletteChange) {
933  ui->clearButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/remove")));
934  ui->fontBiggerButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/fontbigger")));
935  ui->fontSmallerButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/fontsmaller")));
936  ui->promptIcon->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/prompticon")));
937 
938  for (int i = 0; ICON_MAPPING[i].url; ++i) {
939  ui->messagesWidget->document()->addResource(
940  QTextDocument::ImageResource,
941  QUrl(ICON_MAPPING[i].url),
942  platformStyle->SingleColorImage(ICON_MAPPING[i].source).scaled(QSize(consoleFontSize * 2, consoleFontSize * 2), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
943  }
944  }
945 
946  QWidget::changeEvent(e);
947 }
948 
949 void RPCConsole::message(int category, const QString &message, bool html)
950 {
951  QTime time = QTime::currentTime();
952  QString timeString = time.toString();
953  QString out;
954  out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
955  out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
956  out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
957  if(html)
958  out += message;
959  else
960  out += GUIUtil::HtmlEscape(message, false);
961  out += "</td></tr></table>";
962  ui->messagesWidget->append(out);
963 }
964 
966 {
967  QString connections = QString::number(clientModel->getNumConnections()) + " (";
968  connections += tr("In:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / ";
969  connections += tr("Out:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")";
970 
971  if(!clientModel->node().getNetworkActive()) {
972  connections += " (" + tr("Network activity disabled") + ")";
973  }
974 
975  ui->numberOfConnections->setText(connections);
976 }
977 
979 {
980  if (!clientModel)
981  return;
982 
984 }
985 
986 void RPCConsole::setNetworkActive(bool networkActive)
987 {
989 }
990 
991 void RPCConsole::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, SyncType synctype)
992 {
993  if (synctype == SyncType::BLOCK_SYNC) {
994  ui->numberOfBlocks->setText(QString::number(count));
995  ui->lastBlockTime->setText(blockDate.toString());
996  }
997 }
998 
999 void RPCConsole::setMempoolSize(long numberOfTxs, size_t dynUsage)
1000 {
1001  ui->mempoolNumberTxs->setText(QString::number(numberOfTxs));
1002 
1003  if (dynUsage < 1000000) {
1004  ui->mempoolSize->setText(QObject::tr("%1 kB").arg(dynUsage / 1000.0, 0, 'f', 2));
1005  } else {
1006  ui->mempoolSize->setText(QObject::tr("%1 MB").arg(dynUsage / 1000000.0, 0, 'f', 2));
1007  }
1008 }
1009 
1011 {
1012  QString cmd = ui->lineEdit->text().trimmed();
1013 
1014  if (cmd.isEmpty()) {
1015  return;
1016  }
1017 
1018  std::string strFilteredCmd;
1019  try {
1020  std::string dummy;
1021  if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) {
1022  // Failed to parse command, so we cannot even filter it for the history
1023  throw std::runtime_error("Invalid command line");
1024  }
1025  } catch (const std::exception& e) {
1026  QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what()));
1027  return;
1028  }
1029 
1030  // A special case allows to request shutdown even a long-running command is executed.
1031  if (cmd == QLatin1String("stop")) {
1032  std::string dummy;
1033  RPCExecuteCommandLine(m_node, dummy, cmd.toStdString());
1034  return;
1035  }
1036 
1037  if (m_is_executing) {
1038  return;
1039  }
1040 
1041  ui->lineEdit->clear();
1042 
1043  WalletModel* wallet_model{nullptr};
1044 #ifdef ENABLE_WALLET
1045  wallet_model = ui->WalletSelector->currentData().value<WalletModel*>();
1046 
1047  if (m_last_wallet_model != wallet_model) {
1048  if (wallet_model) {
1049  message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
1050  } else {
1051  message(CMD_REQUEST, tr("Executing command without any wallet"));
1052  }
1053  m_last_wallet_model = wallet_model;
1054  }
1055 #endif // ENABLE_WALLET
1056 
1057  message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
1058  //: A console message indicating an entered command is currently being executed.
1059  message(CMD_REPLY, tr("Executing…"));
1060  m_is_executing = true;
1061 
1062  QMetaObject::invokeMethod(m_executor, [this, cmd, wallet_model] {
1063  m_executor->request(cmd, wallet_model);
1064  });
1065 
1066  cmd = QString::fromStdString(strFilteredCmd);
1067 
1068  // Remove command, if already in history
1069  history.removeOne(cmd);
1070  // Append command to history
1071  history.append(cmd);
1072  // Enforce maximum history size
1073  while (history.size() > CONSOLE_HISTORY) {
1074  history.removeFirst();
1075  }
1076  // Set pointer to end of history
1077  historyPtr = history.size();
1078 
1079  // Scroll console view to end
1080  scrollToEnd();
1081 }
1082 
1084 {
1085  // store current text when start browsing through the history
1086  if (historyPtr == history.size()) {
1087  cmdBeforeBrowsing = ui->lineEdit->text();
1088  }
1089 
1090  historyPtr += offset;
1091  if(historyPtr < 0)
1092  historyPtr = 0;
1093  if(historyPtr > history.size())
1094  historyPtr = history.size();
1095  QString cmd;
1096  if(historyPtr < history.size())
1097  cmd = history.at(historyPtr);
1098  else if (!cmdBeforeBrowsing.isNull()) {
1100  }
1101  ui->lineEdit->setText(cmd);
1102 }
1103 
1105 {
1106  m_executor = new RPCExecutor(m_node);
1107  m_executor->moveToThread(&thread);
1108 
1109  // Replies from executor object must go to this object
1110  connect(m_executor, &RPCExecutor::reply, this, [this](int category, const QString& command) {
1111  // Remove "Executing…" message.
1112  ui->messagesWidget->undo();
1113  message(category, command);
1114  scrollToEnd();
1115  m_is_executing = false;
1116  });
1117 
1118  // Make sure executor object is deleted in its own thread
1119  connect(&thread, &QThread::finished, m_executor, &RPCExecutor::deleteLater);
1120 
1121  // Default implementation of QThread::run() simply spins up an event loop in the thread,
1122  // which is what we want.
1123  thread.start();
1124  QTimer::singleShot(0, m_executor, []() {
1125  util::ThreadRename("qt-rpcconsole");
1126  });
1127 }
1128 
1130 {
1131  if (ui->tabWidget->widget(index) == ui->tab_console) {
1132  ui->lineEdit->setFocus();
1133  }
1134 }
1135 
1137 {
1139 }
1140 
1142 {
1143  QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
1144  scrollbar->setValue(scrollbar->maximum());
1145 }
1146 
1148 {
1149  const int multiplier = 5; // each position on the slider represents 5 min
1150  int mins = value * multiplier;
1151  setTrafficGraphRange(mins);
1152 }
1153 
1155 {
1156  ui->trafficGraph->setGraphRange(std::chrono::minutes{mins});
1157  ui->lblGraphRange->setText(GUIUtil::formatDurationStr(std::chrono::minutes{mins}));
1158 }
1159 
1160 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
1161 {
1162  ui->lblBytesIn->setText(GUIUtil::formatBytes(totalBytesIn));
1163  ui->lblBytesOut->setText(GUIUtil::formatBytes(totalBytesOut));
1164 }
1165 
1167 {
1168  const QList<QModelIndex> selected_peers = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
1169  if (!clientModel || !clientModel->getPeerTableModel() || selected_peers.size() != 1) {
1170  ui->peersTabRightPanel->hide();
1171  ui->peerHeading->setText(tr("Select a peer to view detailed information."));
1172  return;
1173  }
1174  const auto stats = selected_peers.first().data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
1175  // update the detail ui with latest node information
1176  QString peerAddrDetails(QString::fromStdString(stats->nodeStats.m_addr_name) + " ");
1177  peerAddrDetails += tr("(peer: %1)").arg(QString::number(stats->nodeStats.nodeid));
1178  if (!stats->nodeStats.addrLocal.empty())
1179  peerAddrDetails += "<br />" + tr("via %1").arg(QString::fromStdString(stats->nodeStats.addrLocal));
1180  ui->peerHeading->setText(peerAddrDetails);
1181  QString bip152_hb_settings;
1182  if (stats->nodeStats.m_bip152_highbandwidth_to) bip152_hb_settings = ts.to;
1183  if (stats->nodeStats.m_bip152_highbandwidth_from) bip152_hb_settings += (bip152_hb_settings.isEmpty() ? ts.from : QLatin1Char('/') + ts.from);
1184  if (bip152_hb_settings.isEmpty()) bip152_hb_settings = ts.no;
1185  ui->peerHighBandwidth->setText(bip152_hb_settings);
1186  const auto time_now{GetTime<std::chrono::seconds>()};
1187  ui->peerConnTime->setText(GUIUtil::formatDurationStr(time_now - stats->nodeStats.m_connected));
1188  ui->peerLastBlock->setText(TimeDurationField(time_now, stats->nodeStats.m_last_block_time));
1189  ui->peerLastTx->setText(TimeDurationField(time_now, stats->nodeStats.m_last_tx_time));
1190  ui->peerLastSend->setText(TimeDurationField(time_now, stats->nodeStats.m_last_send));
1191  ui->peerLastRecv->setText(TimeDurationField(time_now, stats->nodeStats.m_last_recv));
1192  ui->peerBytesSent->setText(GUIUtil::formatBytes(stats->nodeStats.nSendBytes));
1193  ui->peerBytesRecv->setText(GUIUtil::formatBytes(stats->nodeStats.nRecvBytes));
1194  ui->peerPingTime->setText(GUIUtil::formatPingTime(stats->nodeStats.m_last_ping_time));
1195  ui->peerMinPing->setText(GUIUtil::formatPingTime(stats->nodeStats.m_min_ping_time));
1196  ui->timeoffset->setText(GUIUtil::formatTimeOffset(stats->nodeStats.nTimeOffset));
1197  if (stats->nodeStats.nVersion) {
1198  ui->peerVersion->setText(QString::number(stats->nodeStats.nVersion));
1199  }
1200  if (!stats->nodeStats.cleanSubVer.empty()) {
1201  ui->peerSubversion->setText(QString::fromStdString(stats->nodeStats.cleanSubVer));
1202  }
1203  ui->peerConnectionType->setText(GUIUtil::ConnectionTypeToQString(stats->nodeStats.m_conn_type, /*prepend_direction=*/true));
1204  ui->peerTransportType->setText(QString::fromStdString(TransportTypeAsString(stats->nodeStats.m_transport_type)));
1205  if (stats->nodeStats.m_transport_type == TransportProtocolType::V2) {
1206  ui->peerSessionIdLabel->setVisible(true);
1207  ui->peerSessionId->setVisible(true);
1208  ui->peerSessionId->setText(QString::fromStdString(stats->nodeStats.m_session_id));
1209  } else {
1210  ui->peerSessionIdLabel->setVisible(false);
1211  ui->peerSessionId->setVisible(false);
1212  }
1213  ui->peerNetwork->setText(GUIUtil::NetworkToQString(stats->nodeStats.m_network));
1214  if (stats->nodeStats.m_permission_flags == NetPermissionFlags::None) {
1215  ui->peerPermissions->setText(ts.na);
1216  } else {
1217  QStringList permissions;
1218  for (const auto& permission : NetPermissions::ToStrings(stats->nodeStats.m_permission_flags)) {
1219  permissions.append(QString::fromStdString(permission));
1220  }
1221  ui->peerPermissions->setText(permissions.join(" & "));
1222  }
1223  ui->peerMappedAS->setText(stats->nodeStats.m_mapped_as != 0 ? QString::number(stats->nodeStats.m_mapped_as) : ts.na);
1224 
1225  // This check fails for example if the lock was busy and
1226  // nodeStateStats couldn't be fetched.
1227  if (stats->fNodeStateStatsAvailable) {
1228  ui->peerServices->setText(GUIUtil::formatServicesStr(stats->nodeStateStats.their_services));
1229  // Sync height is init to -1
1230  if (stats->nodeStateStats.nSyncHeight > -1) {
1231  ui->peerSyncHeight->setText(QString("%1").arg(stats->nodeStateStats.nSyncHeight));
1232  } else {
1233  ui->peerSyncHeight->setText(ts.unknown);
1234  }
1235  // Common height is init to -1
1236  if (stats->nodeStateStats.nCommonHeight > -1) {
1237  ui->peerCommonHeight->setText(QString("%1").arg(stats->nodeStateStats.nCommonHeight));
1238  } else {
1239  ui->peerCommonHeight->setText(ts.unknown);
1240  }
1241  ui->peerHeight->setText(QString::number(stats->nodeStateStats.m_starting_height));
1242  ui->peerPingWait->setText(GUIUtil::formatPingTime(stats->nodeStateStats.m_ping_wait));
1243  ui->peerAddrRelayEnabled->setText(stats->nodeStateStats.m_addr_relay_enabled ? ts.yes : ts.no);
1244  ui->peerAddrProcessed->setText(QString::number(stats->nodeStateStats.m_addr_processed));
1245  ui->peerAddrRateLimited->setText(QString::number(stats->nodeStateStats.m_addr_rate_limited));
1246  ui->peerRelayTxes->setText(stats->nodeStateStats.m_relay_txs ? ts.yes : ts.no);
1247  }
1248 
1249  ui->peersTabRightPanel->show();
1250 }
1251 
1252 void RPCConsole::resizeEvent(QResizeEvent *event)
1253 {
1254  QWidget::resizeEvent(event);
1255 }
1256 
1257 void RPCConsole::showEvent(QShowEvent *event)
1258 {
1259  QWidget::showEvent(event);
1260 
1262  return;
1263 
1264  // start PeerTableModel auto refresh
1266 }
1267 
1268 void RPCConsole::hideEvent(QHideEvent *event)
1269 {
1270  // It is too late to call QHeaderView::saveState() in ~RPCConsole(), as all of
1271  // the columns of QTableView child widgets will have zero width at that moment.
1272  m_peer_widget_header_state = ui->peerWidget->horizontalHeader()->saveState();
1273  m_banlist_widget_header_state = ui->banlistWidget->horizontalHeader()->saveState();
1274 
1275  QWidget::hideEvent(event);
1276 
1278  return;
1279 
1280  // stop PeerTableModel auto refresh
1282 }
1283 
1284 void RPCConsole::showPeersTableContextMenu(const QPoint& point)
1285 {
1286  QModelIndex index = ui->peerWidget->indexAt(point);
1287  if (index.isValid())
1288  peersTableContextMenu->exec(QCursor::pos());
1289 }
1290 
1291 void RPCConsole::showBanTableContextMenu(const QPoint& point)
1292 {
1293  QModelIndex index = ui->banlistWidget->indexAt(point);
1294  if (index.isValid())
1295  banTableContextMenu->exec(QCursor::pos());
1296 }
1297 
1299 {
1300  // Get selected peer addresses
1301  QList<QModelIndex> nodes = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
1302  for(int i = 0; i < nodes.count(); i++)
1303  {
1304  // Get currently selected peer address
1305  NodeId id = nodes.at(i).data().toLongLong();
1306  // Find the node, disconnect it and clear the selected node
1307  if(m_node.disconnectById(id))
1309  }
1310 }
1311 
1313 {
1314  if (!clientModel)
1315  return;
1316 
1317  for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
1318  // Find possible nodes, ban it and clear the selected node
1319  const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
1320  if (stats) {
1321  m_node.ban(stats->nodeStats.addr, bantime);
1322  m_node.disconnectByAddress(stats->nodeStats.addr);
1323  }
1324  }
1327 }
1328 
1330 {
1331  if (!clientModel)
1332  return;
1333 
1334  // Get selected ban addresses
1335  QList<QModelIndex> nodes = GUIUtil::getEntryData(ui->banlistWidget, BanTableModel::Address);
1336  BanTableModel* ban_table_model{clientModel->getBanTableModel()};
1337  bool unbanned{false};
1338  for (const auto& node_index : nodes) {
1339  unbanned |= ban_table_model->unban(node_index);
1340  }
1341  if (unbanned) {
1342  ban_table_model->refresh();
1343  }
1344 }
1345 
1347 {
1348  ui->peerWidget->selectionModel()->clearSelection();
1349  cachedNodeids.clear();
1351 }
1352 
1354 {
1355  if (!clientModel)
1356  return;
1357 
1358  bool visible = clientModel->getBanTableModel()->shouldShow();
1359  ui->banlistWidget->setVisible(visible);
1360  ui->banHeading->setVisible(visible);
1361 }
1362 
1364 {
1365  ui->tabWidget->setCurrentIndex(int(tabType));
1366 }
1367 
1368 QString RPCConsole::tabTitle(TabTypes tab_type) const
1369 {
1370  return ui->tabWidget->tabText(int(tab_type));
1371 }
1372 
1373 QKeySequence RPCConsole::tabShortcut(TabTypes tab_type) const
1374 {
1375  switch (tab_type) {
1376  case TabTypes::INFO: return QKeySequence(tr("Ctrl+I"));
1377  case TabTypes::CONSOLE: return QKeySequence(tr("Ctrl+T"));
1378  case TabTypes::GRAPH: return QKeySequence(tr("Ctrl+N"));
1379  case TabTypes::PEERS: return QKeySequence(tr("Ctrl+P"));
1380  } // no default case, so the compiler can warn about missing cases
1381 
1382  assert(false);
1383 }
1384 
1385 void RPCConsole::updateAlerts(const QString& warnings)
1386 {
1387  this->ui->label_alerts->setVisible(!warnings.isEmpty());
1388  this->ui->label_alerts->setText(warnings);
1389 }
#define PACKAGE_NAME
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
const auto cmd
const auto command
const CChainParams & Params()
Return the currently selected parameters.
Qt model providing information about banned peers, similar to the "getpeerinfo" RPC call.
Definition: bantablemodel.h:44
bool unban(const QModelIndex &index)
Model for Bitcoin network client.
Definition: clientmodel.h:54
void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut)
QString blocksDir() const
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
interfaces::Node & node() const
Definition: clientmodel.h:61
PeerTableModel * getPeerTableModel()
PeerTableSortProxy * peerTableSortProxy()
void numConnectionsChanged(int count)
QString formatClientStartupTime() const
int getNumConnections(unsigned int flags=CONNECTIONS_ALL) const
Return number of connections, default is in- and outbound (total)
Definition: clientmodel.cpp:77
BanTableModel * getBanTableModel()
void numBlocksChanged(int count, const QDateTime &blockDate, double nVerificationProgress, SyncType header, SynchronizationState sync_state)
void alertsChanged(const QString &warnings)
QString dataDir() const
QString formatFullVersion() const
QString formatSubVersion() const
void mempoolSizeChanged(long count, size_t mempoolSizeInBytes)
void networkActiveChanged(bool networkActive)
static std::vector< std::string > ToStrings(NetPermissionFlags flags)
QString displayText(const QVariant &value, const QLocale &locale) const override
Definition: rpcconsole.cpp:139
PeerIdViewDelegate(QObject *parent=nullptr)
Definition: rpcconsole.cpp:136
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
bool getImagesOnButtons() const
Definition: platformstyle.h:21
QImage SingleColorImage(const QString &filename) const
Colorize an image (given filename) with the icon color.
Class for handling RPC timers (used for e.g.
Definition: rpcconsole.cpp:105
std::function< void()> func
Definition: rpcconsole.cpp:118
QtRPCTimerBase(std::function< void()> &_func, int64_t millis)
Definition: rpcconsole.cpp:108
~QtRPCTimerBase()=default
const char * Name() override
Implementation name.
Definition: rpcconsole.cpp:125
RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis) override
Factory function for timers.
Definition: rpcconsole.cpp:126
~QtRPCTimerInterface()=default
Local Bitcoin RPC console.
Definition: rpcconsole.h:44
QMenu * peersTableContextMenu
Definition: rpcconsole.h:172
RPCConsole(interfaces::Node &node, const PlatformStyle *platformStyle, QWidget *parent)
Definition: rpcconsole.cpp:467
struct RPCConsole::TranslatedStrings ts
void browseHistory(int offset)
Go forward or back in history.
QByteArray m_banlist_widget_header_state
Definition: rpcconsole.h:181
void fontSmaller()
Definition: rpcconsole.cpp:834
RPCTimerInterface * rpcTimerInterface
Definition: rpcconsole.h:171
QString TimeDurationField(std::chrono::seconds time_now, std::chrono::seconds time_at_event) const
Helper for the output of a time duration field.
Definition: rpcconsole.h:187
void on_lineEdit_returnPressed()
QStringList history
Definition: rpcconsole.h:166
void message(int category, const QString &msg)
Append the message to the message widget.
Definition: rpcconsole.h:117
void setFontSize(int newSize)
Definition: rpcconsole.cpp:839
void updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
update traffic statistics
void setTrafficGraphRange(int mins)
static bool RPCParseCommandLine(interfaces::Node *node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string *const pstrFilteredOut=nullptr, const WalletModel *wallet_model=nullptr)
Split shell command line into a list of arguments and optionally execute the command(s).
Definition: rpcconsole.cpp:169
const PlatformStyle *const platformStyle
Definition: rpcconsole.h:170
void updateDetailWidget()
show detailed information on ui about selected node
void showEvent(QShowEvent *event) override
void resizeEvent(QResizeEvent *event) override
static bool RPCExecuteCommandLine(interfaces::Node &node, std::string &strResult, const std::string &strCommand, std::string *const pstrFilteredOut=nullptr, const WalletModel *wallet_model=nullptr)
Definition: rpcconsole.h:52
QString tabTitle(TabTypes tab_type) const
void updateNetworkState()
Update UI with latest network info from model.
Definition: rpcconsole.cpp:965
void clear(bool keep_prompt=false)
Definition: rpcconsole.cpp:864
void disconnectSelectedNode()
Disconnect a selected node on the Peers tab.
@ BANTIME_COLUMN_WIDTH
Definition: rpcconsole.h:159
@ ADDRESS_COLUMN_WIDTH
Definition: rpcconsole.h:155
@ SUBVERSION_COLUMN_WIDTH
Definition: rpcconsole.h:156
@ PING_COLUMN_WIDTH
Definition: rpcconsole.h:157
@ BANSUBNET_COLUMN_WIDTH
Definition: rpcconsole.h:158
QCompleter * autoCompleter
Definition: rpcconsole.h:175
void setMempoolSize(long numberOfTxs, size_t dynUsage)
Set size (number of transactions and memory usage) of the mempool in the UI.
Definition: rpcconsole.cpp:999
void hideEvent(QHideEvent *event) override
QKeySequence tabShortcut(TabTypes tab_type) const
void showPeersTableContextMenu(const QPoint &point)
Show custom context menu on Peers tab.
QList< NodeId > cachedNodeids
Definition: rpcconsole.h:169
bool m_is_executing
Definition: rpcconsole.h:179
interfaces::Node & m_node
Definition: rpcconsole.h:163
void unbanSelectedNode()
Unban a selected node on the Bans tab.
void updateAlerts(const QString &warnings)
void clearSelectedNode()
clear the selected node
void on_sldGraphRange_valueChanged(int value)
change the time range of the network traffic graph
int consoleFontSize
Definition: rpcconsole.h:174
void setNumConnections(int count)
Set number of connections shown in the UI.
Definition: rpcconsole.cpp:978
void setNumBlocks(int count, const QDateTime &blockDate, double nVerificationProgress, SyncType synctype)
Set number of blocks and last block date shown in the UI.
Definition: rpcconsole.cpp:991
ClientModel * clientModel
Definition: rpcconsole.h:165
void banSelectedNode(int bantime)
Ban a selected node on the Peers tab.
int historyPtr
Definition: rpcconsole.h:167
void scrollToEnd()
Scroll console view to end.
void keyPressEvent(QKeyEvent *) override
Definition: rpcconsole.cpp:923
void on_tabWidget_currentChanged(int index)
Ui::RPCConsole *const ui
Definition: rpcconsole.h:164
void startExecutor()
void setNetworkActive(bool networkActive)
Set network state shown in the UI.
Definition: rpcconsole.cpp:986
void fontBigger()
Definition: rpcconsole.cpp:829
QString cmdBeforeBrowsing
Definition: rpcconsole.h:168
virtual bool eventFilter(QObject *obj, QEvent *event) override
Definition: rpcconsole.cpp:609
void on_openDebugLogfileButton_clicked()
open the debug.log from the current datadir
void showBanTableContextMenu(const QPoint &point)
Show custom context menu on Bans tab.
void setClientModel(ClientModel *model=nullptr, int bestblock_height=0, int64_t bestblock_date=0, double verification_progress=0.0)
Definition: rpcconsole.cpp:653
void setTabFocus(enum TabTypes tabType)
set which tab has the focus (is visible)
QByteArray m_peer_widget_header_state
Definition: rpcconsole.h:180
void changeEvent(QEvent *e) override
Definition: rpcconsole.cpp:930
WalletModel * m_last_wallet_model
Definition: rpcconsole.h:178
void showOrHideBanTableIfRequired()
Hides ban table if no bans are present.
RPCExecutor * m_executor
Definition: rpcconsole.h:177
QMenu * banTableContextMenu
Definition: rpcconsole.h:173
QThread thread
Definition: rpcconsole.h:176
void reply(int category, const QString &command)
RPCExecutor(interfaces::Node &node)
Definition: rpcconsole.cpp:89
interfaces::Node & m_node
Definition: rpcconsole.cpp:98
void request(const QString &command, const WalletModel *wallet_model)
Definition: rpcconsole.cpp:411
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:51
RPC timer "driver".
Definition: server.h:60
void push_back(UniValue val)
Definition: univalue.cpp:104
const std::string & get_str() const
bool isArray() const
Definition: univalue.h:84
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:233
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool isStr() const
Definition: univalue.h:82
Int getInt() const
Definition: univalue.h:137
bool isObject() const
Definition: univalue.h:85
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:52
QString getDisplayName() const
static bool isWalletEnabled()
QString getWalletName() const
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:70
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface)=0
Set RPC timer interface if unset.
virtual bool disconnectById(NodeId id)=0
Disconnect node by id.
virtual std::vector< std::string > listRpcCommands()=0
List rpc commands.
virtual bool ban(const CNetAddr &net_addr, int64_t ban_time_offset)=0
Ban node.
virtual void rpcUnsetTimerInterface(RPCTimerInterface *iface)=0
Unset RPC timer interface.
virtual bool getNetworkActive()=0
Get network active.
virtual bool disconnectByAddress(const CNetAddr &net_addr)=0
Disconnect node by address.
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:348
SyncType
Definition: clientmodel.h:39
@ CONNECTIONS_IN
Definition: clientmodel.h:47
@ CONNECTIONS_OUT
Definition: clientmodel.h:48
std::string TransportTypeAsString(TransportProtocolType transport_type)
Convert TransportProtocolType enum to a string value.
@ V2
BIP324 protocol.
QString NetworkToQString(Network net)
Convert enum Network to QString.
Definition: guiutil.cpp:675
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:244
QList< QModelIndex > getEntryData(const QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:272
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:100
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:813
QString formatDurationStr(std::chrono::seconds dur)
Convert seconds into a QString with days, hours, mins, secs.
Definition: guiutil.cpp:724
void AddButtonShortcut(QAbstractButton *button, const QKeySequence &shortcut)
Connects an additional shortcut to a QAbstractButton.
Definition: guiutil.cpp:139
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:419
void copyEntryData(const QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
Definition: guiutil.cpp:259
QString formatPingTime(std::chrono::microseconds ping_time)
Format a CNodeStats.m_last_ping_time into a user-readable string or display N/A, if 0.
Definition: guiutil.cpp:764
void openDebugLogfile()
Definition: guiutil.cpp:424
QString formatTimeOffset(int64_t nTimeOffset)
Format a CNodeCombinedStats.nTimeOffset into a user-readable string.
Definition: guiutil.cpp:771
QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction)
Convert enum ConnectionType to QString.
Definition: guiutil.cpp:695
QString formatServicesStr(quint64 mask)
Format CNodeStats.nServices bitmask into a user-readable string.
Definition: guiutil.cpp:750
bool IsEscapeOrBack(int key)
Definition: guiutil.h:430
Definition: init.h:25
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:59
int64_t NodeId
Definition: net.h:100
const std::vector< std::string > CONNECTION_TYPE_DOC
Definition: net.cpp:41
const std::vector< std::string > TRANSPORT_TYPE_DOC
Definition: net.cpp:50
const int INITIAL_TRAFFIC_GRAPH_MINS
Definition: rpcconsole.cpp:53
const struct @8 ICON_MAPPING[]
const QSize FONT_RANGE(4, 40)
const int CONSOLE_HISTORY
Definition: rpcconsole.cpp:52
static QString categoryClass(int category)
Definition: rpcconsole.cpp:818
const char fontSizeSettingsKey[]
Definition: rpcconsole.cpp:55
const char * url
Definition: rpcconsole.cpp:58
const char * source
Definition: rpcconsole.cpp:59
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:68
static int count
assert(!tx.IsCoinBase())