Skip to content

Commit 2c99747

Browse files
committed
qt: add generate command to gui console
1 parent 1bf0c07 commit 2c99747

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/qt/rpcconsole.cpp

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <util/strencodings.h>
2525
#include <util/string.h>
2626
#include <util/threadnames.h>
27-
27+
#include <util/chaintype.h>
2828
#include <univalue.h>
2929

3030
#include <QAbstractButton>
@@ -96,12 +96,15 @@ public Q_SLOTS:
9696

9797
private:
9898
interfaces::Node& m_node;
99+
bool executeConsoleGenerate(const std::vector<std::string>& parsed_command, const WalletModel* wallet_model, const bool exec_help = false);
100+
void executeConsoleHelpGenerate();
99101
bool executeConsoleHelpConsole(const std::vector<std::string>& parsed_command, const WalletModel* wallet_model, const bool exec_help = false);
100102
bool executeConsoleOnlyCommand(const std::string& command, const WalletModel* wallet_model);
101103
// std::map mapping strings to methods member of RPCExecutor class
102104
// Keys must be strings with commands and (optionally) parameters in "canonical" form (separated by single space)
103105
// Keys should match the beggining of user input commands (user commands can have more parameters than the key)
104106
std::map<std::string, bool (RPCExecutor::*)(const std::vector<std::string>&, const WalletModel*, const bool)> m_method_map{
107+
{"generate", &RPCExecutor::executeConsoleGenerate},
105108
{"help-console", &RPCExecutor::executeConsoleHelpConsole}};
106109
};
107110

@@ -469,6 +472,73 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
469472
}
470473
}
471474

475+
/**
476+
* @brief Executes the console-only command "generate".
477+
* @param parsed_command A vector of strings with command and parameters, usually generated by RPCExecutor::parseHelper
478+
* @param wallet_model WalletModel to use for the command
479+
* @return True if the command was executed, false otherwise.
480+
*/
481+
bool RPCExecutor::executeConsoleGenerate(const std::vector<std::string>& parsed_command, const WalletModel* wallet_model, const bool exec_help)
482+
{
483+
// Initialize default parameters if missing
484+
const std::string nblocks{parsed_command.size() > 1 ? parsed_command[1] : "1"};
485+
const std::string maxtries{parsed_command.size() > 2 ? parsed_command[2] : "1000000"};
486+
487+
// Handle some special cases...
488+
// Default to console help generate if more than 3 parameters or if "help generate" was called
489+
if (parsed_command.size() > 3 || exec_help) {
490+
executeConsoleHelpGenerate();
491+
return true;
492+
}
493+
// Fail if we are on mainnet, to avoid generating addresses for blocks that will not be generated
494+
if (Params().GetChainType() == ChainType::MAIN) {
495+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Error: generate is not available on mainnet"));
496+
return true;
497+
}
498+
// Fail if parameters are not positive integers
499+
const auto nblocks_value{ToIntegral<int>(nblocks)};
500+
const auto maxtries_value{ToIntegral<int>(maxtries)};
501+
if (!nblocks_value || !maxtries_value || nblocks_value.value() <= 0 || maxtries_value.value() <= 0) {
502+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Error: parameters must be positive integers"));
503+
return true;
504+
}
505+
506+
// Catch the console-only generate command with 2 or less parameters before RPC call is executed .
507+
std::string blocks;
508+
std::string address;
509+
if (!RPCConsole::RPCExecuteCommandLine(m_node, address, "getnewaddress\n", /*pstrFilteredOut=*/nullptr, wallet_model)) {
510+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Error: could not generate new address"));
511+
} else {
512+
if (!RPCConsole::RPCExecuteCommandLine(m_node, blocks, "generatetoaddress " + nblocks + " " + address + " " + maxtries + "\n", /*pstrFilteredOut=*/nullptr, wallet_model)) {
513+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Error: could not generate blocks"));
514+
} else {
515+
UniValue result{UniValue::VOBJ};
516+
UniValue blocks_object{UniValue::VOBJ};
517+
blocks_object.read(blocks);
518+
result.pushKV("address", address);
519+
result.pushKV("blocks", blocks_object);
520+
Q_EMIT reply(RPCConsole::CMD_REPLY, QString::fromStdString("\n" + result.write(2) + "\n\n"));
521+
}
522+
}
523+
return true;
524+
}
525+
526+
/**
527+
* @brief Executes the console-only command "help generate".
528+
*/
529+
void RPCExecutor::executeConsoleHelpGenerate()
530+
{
531+
// Execute the console-only "help generate" command.
532+
Q_EMIT reply(RPCConsole::CMD_REPLY,
533+
QString("\n"
534+
"Generate blocks, equivalent to RPC getnewaddress followed by RPC generatetoaddress.\n"
535+
"Optional positive integer arguments are number of blocks to generate and maximum iterations to try.\n"
536+
"Equivalent to RPC generatetoaddress nblocks and maxtries arguments.\n"
537+
" example: generate\n"
538+
" example: generate 4\n"
539+
" example: generate 3 6000\n\n"));
540+
}
541+
472542
/**
473543
* @brief Executes the console-only command "help-console".
474544
* @param parsed_command A vector of strings with command and parameters, usually generated by RPCExecutor::parseHelper

0 commit comments

Comments
 (0)