Skip to content

Commit 4fd7c14

Browse files
committed
console ui implementation of generate
1 parent 8ab1923 commit 4fd7c14

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/qt/rpcconsole.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <QVariant>
4949

5050
#include <chrono>
51+
#include <regex>
5152

5253
const int CONSOLE_HISTORY = 50;
5354
const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
@@ -415,6 +416,37 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
415416
std::string result;
416417
std::string executableCommand = command.toStdString() + "\n";
417418

419+
std::regex validRegex("generate\\s*[\\(]?\\s*(\\d+)?\\s*[,]?\\s*(\\d+)?\\s*[\\)]?\\s*");
420+
std::regex invalidRegex("generate[\\s\\(]+.*");
421+
std::smatch match;
422+
423+
424+
if (std::regex_match(executableCommand, match, validRegex)) {
425+
std::string nblocks=match[1];
426+
std::string maxtries=match[2];
427+
if (nblocks=="") nblocks = "1";
428+
if (maxtries=="") maxtries = "1000000";
429+
430+
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "getnewaddress\n", nullptr, wallet_model)) {
431+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
432+
}
433+
else {
434+
std::string address = result;
435+
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "generatetoaddress " + nblocks + " " + address + " " + maxtries + "\n", nullptr, wallet_model)) {
436+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
437+
} else {
438+
std::string answer = "{\n \"address\": \"" + address + "\",\n \"blocks\": "+ result + "\n}";
439+
Q_EMIT reply(RPCConsole::CMD_REPLY, QString::fromStdString( "\n" + answer + "\n\n" ));
440+
}
441+
}
442+
return;
443+
}else {
444+
//If the previous regex failed, but the following is valid - i.e. starts with 'generate' - there is a syntax error with parameters
445+
if (std::regex_search(executableCommand, match, invalidRegex)) {
446+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error."));
447+
return;
448+
}
449+
}
418450
// Catch the console-only-help command before RPC call is executed and reply with help text as-if a RPC reply.
419451
if(executableCommand == "help-console\n") {
420452
Q_EMIT reply(RPCConsole::CMD_REPLY, QString(("\n"

0 commit comments

Comments
 (0)