Skip to content

Commit 5a0d524

Browse files
hebastoknst
authored andcommitted
Merge bitcoin-core/gui#123: rpc: Do not accept command while executing another one
38eb37c qt, rpc: Do not accept command while executing another one (Hennadii Stepanov) 0c32b9c qt, rpc: Accept stop RPC even another command is executing (Hennadii Stepanov) ccf7902 qt, rpc, refactor: Return early in RPCConsole::on_lineEdit_returnPressed (Hennadii Stepanov) 5b9c8c9 qt, rpc: Add "Executing…" message (Hennadii Stepanov) Pull request description: On master (3f512f3) it is possible to enter another command while the current command is still being executed. That makes a mess in the output. With this PR: ![Screenshot from 2020-10-29 20-48-55](https://user-images.githubusercontent.com/32963518/97619690-329c0880-1a29-11eb-9f5b-6ae3c02c13b2.png) Some previous context: bitcoin-core/gui#59 (comment) --- It is still possible to enter and execute the `stop` command any time. ACKs for top commit: jarolrod: ACK 38eb37c promag: Tested ACK 38eb37c. Tree-SHA512: 2b37a4b6838bf586b1b5c878192106721f713caeb6252514a6540356aab898986396e0777e73891d331b1be797a4926c20d3f9f38ba2c984ea90d55b0c34f664
1 parent 1931064 commit 5a0d524

3 files changed

Lines changed: 64 additions & 43 deletions

File tree

src/qt/rpcconsole.cpp

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,57 +1080,71 @@ void RPCConsole::showPage(int index)
10801080

10811081
void RPCConsole::on_lineEdit_returnPressed()
10821082
{
1083-
QString cmd = ui->lineEdit->text();
1083+
QString cmd = ui->lineEdit->text().trimmed();
10841084

1085-
if(!cmd.isEmpty())
1086-
{
1087-
std::string strFilteredCmd;
1088-
try {
1089-
std::string dummy;
1090-
if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) {
1091-
// Failed to parse command, so we cannot even filter it for the history
1092-
throw std::runtime_error("Invalid command line");
1093-
}
1094-
} catch (const std::exception& e) {
1095-
QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what()));
1096-
return;
1085+
if (cmd.isEmpty()) {
1086+
return;
1087+
}
1088+
1089+
std::string strFilteredCmd;
1090+
try {
1091+
std::string dummy;
1092+
if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) {
1093+
// Failed to parse command, so we cannot even filter it for the history
1094+
throw std::runtime_error("Invalid command line");
10971095
}
1096+
} catch (const std::exception& e) {
1097+
QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what()));
1098+
return;
1099+
}
10981100

1099-
ui->lineEdit->clear();
1101+
// A special case allows to request shutdown even a long-running command is executed.
1102+
if (cmd == QLatin1String("stop")) {
1103+
std::string dummy;
1104+
RPCExecuteCommandLine(m_node, dummy, cmd.toStdString());
1105+
return;
1106+
}
11001107

1101-
cmdBeforeBrowsing = QString();
1108+
if (m_is_executing) {
1109+
return;
1110+
}
1111+
1112+
ui->lineEdit->clear();
11021113

11031114
#ifdef ENABLE_WALLET
1104-
WalletModel* wallet_model = ui->WalletSelector->currentData().value<WalletModel*>();
1115+
WalletModel* wallet_model = ui->WalletSelector->currentData().value<WalletModel*>();
11051116

1106-
if (m_last_wallet_model != wallet_model) {
1107-
if (wallet_model) {
1108-
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
1109-
} else {
1110-
message(CMD_REQUEST, tr("Executing command without any wallet"));
1111-
}
1112-
m_last_wallet_model = wallet_model;
1117+
if (m_last_wallet_model != wallet_model) {
1118+
if (wallet_model) {
1119+
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
1120+
} else {
1121+
message(CMD_REQUEST, tr("Executing command without any wallet"));
11131122
}
1114-
#endif
1115-
1116-
message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
1117-
Q_EMIT cmdRequest(cmd, m_last_wallet_model);
1118-
1119-
cmd = QString::fromStdString(strFilteredCmd);
1120-
1121-
// Remove command, if already in history
1122-
history.removeOne(cmd);
1123-
// Append command to history
1124-
history.append(cmd);
1125-
// Enforce maximum history size
1126-
while(history.size() > CONSOLE_HISTORY)
1127-
history.removeFirst();
1128-
// Set pointer to end of history
1129-
historyPtr = history.size();
1123+
m_last_wallet_model = wallet_model;
1124+
}
1125+
#endif // ENABLE_WALLET
11301126

1131-
// Scroll console view to end
1132-
scrollToEnd();
1127+
message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
1128+
//: A console message indicating an entered command is currently being executed.
1129+
message(CMD_REPLY, tr("Executing…"));
1130+
m_is_executing = true;
1131+
Q_EMIT cmdRequest(cmd, m_last_wallet_model);
1132+
1133+
cmd = QString::fromStdString(strFilteredCmd);
1134+
1135+
// Remove command, if already in history
1136+
history.removeOne(cmd);
1137+
// Append command to history
1138+
history.append(cmd);
1139+
// Enforce maximum history size
1140+
while (history.size() > CONSOLE_HISTORY) {
1141+
history.removeFirst();
11331142
}
1143+
// Set pointer to end of history
1144+
historyPtr = history.size();
1145+
1146+
// Scroll console view to end
1147+
scrollToEnd();
11341148
}
11351149

11361150
void RPCConsole::browseHistory(int offset)
@@ -1160,7 +1174,13 @@ void RPCConsole::startExecutor()
11601174
executor->moveToThread(&thread);
11611175

11621176
// Replies from executor object must go to this object
1163-
connect(executor, &RPCExecutor::reply, this, qOverload<int, const QString&>(&RPCConsole::message));
1177+
connect(executor, &RPCExecutor::reply, this, [this](int category, const QString& command) {
1178+
// Remove "Executing…" message.
1179+
ui->messagesWidget->undo();
1180+
message(category, command);
1181+
scrollToEnd();
1182+
m_is_executing = false;
1183+
});
11641184

11651185
// Requests from this object must go to executor
11661186
connect(this, &RPCConsole::cmdRequest, executor, &RPCExecutor::request);

src/qt/rpcconsole.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public Q_SLOTS:
189189
QCompleter *autoCompleter = nullptr;
190190
QThread thread;
191191
WalletModel* m_last_wallet_model{nullptr};
192+
bool m_is_executing{false};
192193

193194
/** Update UI with latest network info from model. */
194195
void updateNetworkState();

src/qt/test/apptests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void TestRpcCommand(RPCConsole* console)
4141
QTest::keyClicks(lineEdit, "getblockchaininfo");
4242
QTest::keyClick(lineEdit, Qt::Key_Return);
4343
QVERIFY(mw_spy.wait(1000));
44-
QCOMPARE(mw_spy.count(), 2);
44+
QCOMPARE(mw_spy.count(), 4);
4545
QString output = messagesWidget->toPlainText();
4646
UniValue value;
4747
value.read(output.right(output.size() - output.indexOf("{")).toStdString());

0 commit comments

Comments
 (0)