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 29, 2022
1 parent 8ab1923 commit b7782a4
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 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,33 @@ 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)) {
const std::string nblocks{match.str(1) == "" ? "1" : match.str(1)};
const std::string maxtries{match.str(2) == "" ? "1000000" : match.str(2)};

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 b7782a4

Please sign in to comment.