Skip to content

Commit

Permalink
console ui implementation of generate
Browse files Browse the repository at this point in the history
  • Loading branch information
hernanmarino committed Dec 28, 2022
1 parent 8ab1923 commit 454fc48
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <QVariant>

#include <chrono>
#include <regex>

const int CONSOLE_HISTORY = 50;
const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
Expand Down Expand Up @@ -414,7 +415,34 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
{
std::string result;
std::string executableCommand = command.toStdString() + "\n";

std::regex validRegex("generate\\s*[\\(]?\\s*(\\d+)?\\s*[,]?\\s*(\\d+)?\\s*[\\)]?\\s*");
std::regex invalidRegex("generate[\\s\\(]+.*");
std::smatch match;
if (std::regex_match(executableCommand, match, validRegex)) {
std::string nblocks=match[1];
std::string maxtries=match[2];
if (nblocks=="") nblocks = "1";
if (maxtries=="") maxtries = "1000000";
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "getnewaddress\n", nullptr, wallet_model)) {
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
}
else {
std::string address = result;
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "generatetoaddress " + nblocks + " " + address + " " + maxtries + "\n", nullptr, wallet_model)) {
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
} else {
std::string answer = "{\n \"address\": \"" + address + "\",\n \"blocks\": "+ result + "\n}";
Q_EMIT reply(RPCConsole::CMD_REPLY, QString::fromStdString( "\n" + answer + "\n\n" ));
}
}
return;
}else {
//If the previous regex failed, but the following is valid - i.e. starts with 'generate' - there is a syntax error with parameters
if (std::regex_search(executableCommand, match, invalidRegex)) {
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error."));
return;
}
}
// Catch the console-only-help command before RPC call is executed and reply with help text as-if a RPC reply.
if(executableCommand == "help-console\n") {
Q_EMIT reply(RPCConsole::CMD_REPLY, QString(("\n"
Expand Down

0 comments on commit 454fc48

Please sign in to comment.