Skip to content

Commit 684e687

Browse files
committed
Merge bitcoin-core#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#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
2 parents aedf71d + 38eb37c commit 684e687

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

src/qt/rpcconsole.cpp

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -930,57 +930,71 @@ void RPCConsole::setMempoolSize(long numberOfTxs, size_t dynUsage)
930930

931931
void RPCConsole::on_lineEdit_returnPressed()
932932
{
933-
QString cmd = ui->lineEdit->text();
933+
QString cmd = ui->lineEdit->text().trimmed();
934934

935-
if(!cmd.isEmpty())
936-
{
937-
std::string strFilteredCmd;
938-
try {
939-
std::string dummy;
940-
if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) {
941-
// Failed to parse command, so we cannot even filter it for the history
942-
throw std::runtime_error("Invalid command line");
943-
}
944-
} catch (const std::exception& e) {
945-
QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what()));
946-
return;
935+
if (cmd.isEmpty()) {
936+
return;
937+
}
938+
939+
std::string strFilteredCmd;
940+
try {
941+
std::string dummy;
942+
if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) {
943+
// Failed to parse command, so we cannot even filter it for the history
944+
throw std::runtime_error("Invalid command line");
947945
}
946+
} catch (const std::exception& e) {
947+
QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what()));
948+
return;
949+
}
950+
951+
// A special case allows to request shutdown even a long-running command is executed.
952+
if (cmd == QLatin1String("stop")) {
953+
std::string dummy;
954+
RPCExecuteCommandLine(m_node, dummy, cmd.toStdString());
955+
return;
956+
}
948957

949-
ui->lineEdit->clear();
958+
if (m_is_executing) {
959+
return;
960+
}
950961

951-
cmdBeforeBrowsing = QString();
962+
ui->lineEdit->clear();
952963

953964
#ifdef ENABLE_WALLET
954-
WalletModel* wallet_model = ui->WalletSelector->currentData().value<WalletModel*>();
965+
WalletModel* wallet_model = ui->WalletSelector->currentData().value<WalletModel*>();
955966

956-
if (m_last_wallet_model != wallet_model) {
957-
if (wallet_model) {
958-
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
959-
} else {
960-
message(CMD_REQUEST, tr("Executing command without any wallet"));
961-
}
962-
m_last_wallet_model = wallet_model;
967+
if (m_last_wallet_model != wallet_model) {
968+
if (wallet_model) {
969+
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
970+
} else {
971+
message(CMD_REQUEST, tr("Executing command without any wallet"));
963972
}
964-
#endif
965-
966-
message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
967-
Q_EMIT cmdRequest(cmd, m_last_wallet_model);
968-
969-
cmd = QString::fromStdString(strFilteredCmd);
970-
971-
// Remove command, if already in history
972-
history.removeOne(cmd);
973-
// Append command to history
974-
history.append(cmd);
975-
// Enforce maximum history size
976-
while(history.size() > CONSOLE_HISTORY)
977-
history.removeFirst();
978-
// Set pointer to end of history
979-
historyPtr = history.size();
973+
m_last_wallet_model = wallet_model;
974+
}
975+
#endif // ENABLE_WALLET
980976

981-
// Scroll console view to end
982-
scrollToEnd();
977+
message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
978+
//: A console message indicating an entered command is currently being executed.
979+
message(CMD_REPLY, tr("Executing…"));
980+
m_is_executing = true;
981+
Q_EMIT cmdRequest(cmd, m_last_wallet_model);
982+
983+
cmd = QString::fromStdString(strFilteredCmd);
984+
985+
// Remove command, if already in history
986+
history.removeOne(cmd);
987+
// Append command to history
988+
history.append(cmd);
989+
// Enforce maximum history size
990+
while (history.size() > CONSOLE_HISTORY) {
991+
history.removeFirst();
983992
}
993+
// Set pointer to end of history
994+
historyPtr = history.size();
995+
996+
// Scroll console view to end
997+
scrollToEnd();
984998
}
985999

9861000
void RPCConsole::browseHistory(int offset)
@@ -1010,7 +1024,13 @@ void RPCConsole::startExecutor()
10101024
executor->moveToThread(&thread);
10111025

10121026
// Replies from executor object must go to this object
1013-
connect(executor, &RPCExecutor::reply, this, qOverload<int, const QString&>(&RPCConsole::message));
1027+
connect(executor, &RPCExecutor::reply, this, [this](int category, const QString& command) {
1028+
// Remove "Executing…" message.
1029+
ui->messagesWidget->undo();
1030+
message(category, command);
1031+
scrollToEnd();
1032+
m_is_executing = false;
1033+
});
10141034

10151035
// Requests from this object must go to executor
10161036
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
@@ -166,6 +166,7 @@ public Q_SLOTS:
166166
QCompleter *autoCompleter = nullptr;
167167
QThread thread;
168168
WalletModel* m_last_wallet_model{nullptr};
169+
bool m_is_executing{false};
169170

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

src/qt/test/apptests.cpp

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

0 commit comments

Comments
 (0)