Fabcoin Core  0.16.2
P2P Digital Currency
execrpccommand.cpp
Go to the documentation of this file.
1 #include <execrpccommand.h>
2 #include <rpcconsole.h>
3 #include <QJsonDocument>
4 #include <univalue.h>
5 
6 ExecRPCCommand::ExecRPCCommand(const QString &command, const QStringList &mandatory, const QStringList &optional, const QMap<QString, QString>& translations, QObject *parent)
7  : QObject(parent)
8 {
9  m_command = command;
10  m_mandatoryParams = mandatory;
11  m_optionalParams = optional;
12  m_translations = translations;
13 }
14 
15 bool ExecRPCCommand::exec(const QMap<QString, QString> &params, QVariant &result, QString &resultJson, QString &errorMessage)
16 {
17  QStringList commandLine;
18  commandLine.append(m_command);
19 
20  // Check the list of mandatory parameters
21  QStringList mandatoryNotPresent;
22  for(int i = 0; i < m_mandatoryParams.count(); i++)
23  {
24  QString key = m_mandatoryParams[i];
25  if(!params.contains(key))
26  {
27  mandatoryNotPresent.append(m_translations[key]);
28  }
29  else
30  {
31  commandLine.append(params[key]);
32  }
33  }
34  if(mandatoryNotPresent.count() > 0)
35  {
36  errorMessage = tr("Mandatory fields are not present:\n%1").arg(mandatoryNotPresent.join(", "));
37  return false;
38  }
39 
40  // Check the list of optional parameters
41  bool haveOptional = false;
42  int optionalParamsAt = commandLine.size();
43  QStringList optionalNotPresent;
44  for(int i = m_optionalParams.count() - 1; i > -1; i--)
45  {
46  QString key = m_optionalParams[i];
47  if(params.contains(key))
48  {
49  if(!haveOptional) haveOptional = true;
50  commandLine.insert(optionalParamsAt, params[key]);
51  }
52  else
53  {
54  if(haveOptional)
55  {
56  optionalNotPresent.prepend(m_translations[key]);
57  }
58  }
59  }
60  if(optionalNotPresent.count() > 0)
61  {
62  errorMessage = tr("Optional fields are not present:\n%1").arg(optionalNotPresent.join(", "));
63  return false;
64  }
65 
66  // Execute the RPC command
67  try
68  {
69  std::string strResult;
70  std::string strCommand = commandLine.join(' ').toStdString();
71  if(RPCConsole::RPCExecuteCommandLine(strResult, strCommand))
72  {
73  resultJson = strResult.c_str();
74  QJsonDocument doc = QJsonDocument::fromJson(strResult.c_str());
75  result = doc.toVariant();
76  return true;
77  }
78  else
79  {
80  errorMessage = tr("Parse error: unbalanced ' or \"");
81  }
82  }
83  catch (UniValue& objError)
84  {
85  try // Nice formatting for standard-format error
86  {
87  int code = find_value(objError, "code").get_int();
88  std::string message = find_value(objError, "message").get_str();
89  errorMessage = QString::fromStdString(message) + " (code " + QString::number(code) + ")";
90  }
91  catch (const std::runtime_error&) // raised when converting to invalid type, i.e. missing code or message
92  { // Show raw JSON object
93  errorMessage = QString::fromStdString(objError.write());
94  }
95  }
96  catch (const std::exception& e)
97  {
98  errorMessage = QString("Error: ") + QString::fromStdString(e.what());
99  }
100 
101  return false;
102 }
103 
104 void ExecRPCCommand::appendParam(QMap<QString, QString> &params, const QString &paramName, const QString &paramValue)
105 {
106  QString _paramValue = paramValue.trimmed();
107  if(!(_paramValue.isNull() || _paramValue.isEmpty())) params[paramName] = _paramValue;
108 }
109 
110 
ExecRPCCommand(const QString &command, const QStringList &mandatory, const QStringList &optional, const QMap< QString, QString > &translations, QObject *parent=0)
ExecRPCCommand Constructor.
static bool RPCExecuteCommandLine(std::string &strResult, const std::string &strCommand, std::string *const pstrFilteredOut=nullptr)
Definition: rpcconsole.h:40
bytes code
Definition: SmartVM.cpp:45
bool exec(const QMap< QString, QString > &params, QVariant &result, QString &resultJson, QString &errorMessage)
exec Execute the RPC command
QMap< QString, QString > m_translations
const Object_type::value_type::Value_type & find_value(const Object_type &obj, const String_type &name)
static void appendParam(QMap< QString, QString > &params, const QString &paramName, const QString &paramValue)
appendParam Append paramether to the list
#define e(i)
Definition: sha.cpp:733
QStringList m_mandatoryParams
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
QStringList m_optionalParams