LCOV - code coverage report
Current view: top level - src/rpc - server.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 354 370 95.7 %
Date: 2021-06-29 14:35:33 Functions: 38 40 95.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 76 94 80.9 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2020 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #include <rpc/server.h>
#       7                 :            : 
#       8                 :            : #include <rpc/util.h>
#       9                 :            : #include <shutdown.h>
#      10                 :            : #include <sync.h>
#      11                 :            : #include <util/strencodings.h>
#      12                 :            : #include <util/system.h>
#      13                 :            : 
#      14                 :            : #include <boost/algorithm/string/classification.hpp>
#      15                 :            : #include <boost/algorithm/string/split.hpp>
#      16                 :            : #include <boost/signals2/signal.hpp>
#      17                 :            : 
#      18                 :            : #include <cassert>
#      19                 :            : #include <memory> // for unique_ptr
#      20                 :            : #include <mutex>
#      21                 :            : #include <unordered_map>
#      22                 :            : 
#      23                 :            : static Mutex g_rpc_warmup_mutex;
#      24                 :            : static std::atomic<bool> g_rpc_running{false};
#      25                 :            : static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
#      26                 :            : static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
#      27                 :            : /* Timer-creating functions */
#      28                 :            : static RPCTimerInterface* timerInterface = nullptr;
#      29                 :            : /* Map of name to timer. */
#      30                 :            : static Mutex g_deadline_timers_mutex;
#      31                 :            : static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers GUARDED_BY(g_deadline_timers_mutex);
#      32                 :            : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler);
#      33                 :            : 
#      34                 :            : struct RPCCommandExecutionInfo
#      35                 :            : {
#      36                 :            :     std::string method;
#      37                 :            :     int64_t start;
#      38                 :            : };
#      39                 :            : 
#      40                 :            : struct RPCServerInfo
#      41                 :            : {
#      42                 :            :     Mutex mutex;
#      43                 :            :     std::list<RPCCommandExecutionInfo> active_commands GUARDED_BY(mutex);
#      44                 :            : };
#      45                 :            : 
#      46                 :            : static RPCServerInfo g_rpc_server_info;
#      47                 :            : 
#      48                 :            : struct RPCCommandExecution
#      49                 :            : {
#      50                 :            :     std::list<RPCCommandExecutionInfo>::iterator it;
#      51                 :            :     explicit RPCCommandExecution(const std::string& method)
#      52                 :     111276 :     {
#      53                 :     111276 :         LOCK(g_rpc_server_info.mutex);
#      54                 :     111276 :         it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, GetTimeMicros()});
#      55                 :     111276 :     }
#      56                 :            :     ~RPCCommandExecution()
#      57                 :     111276 :     {
#      58                 :     111276 :         LOCK(g_rpc_server_info.mutex);
#      59                 :     111276 :         g_rpc_server_info.active_commands.erase(it);
#      60                 :     111276 :     }
#      61                 :            : };
#      62                 :            : 
#      63                 :            : static struct CRPCSignals
#      64                 :            : {
#      65                 :            :     boost::signals2::signal<void ()> Started;
#      66                 :            :     boost::signals2::signal<void ()> Stopped;
#      67                 :            : } g_rpcSignals;
#      68                 :            : 
#      69                 :            : void RPCServer::OnStarted(std::function<void ()> slot)
#      70                 :        659 : {
#      71                 :        659 :     g_rpcSignals.Started.connect(slot);
#      72                 :        659 : }
#      73                 :            : 
#      74                 :            : void RPCServer::OnStopped(std::function<void ()> slot)
#      75                 :        659 : {
#      76                 :        659 :     g_rpcSignals.Stopped.connect(slot);
#      77                 :        659 : }
#      78                 :            : 
#      79                 :            : std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest& helpreq) const
#      80                 :        150 : {
#      81                 :        150 :     std::string strRet;
#      82                 :        150 :     std::string category;
#      83                 :        150 :     std::set<intptr_t> setDone;
#      84                 :        150 :     std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
#      85                 :            : 
#      86         [ +  + ]:        150 :     for (const auto& entry : mapCommands)
#      87                 :      23100 :         vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
#      88                 :        150 :     sort(vCommands.begin(), vCommands.end());
#      89                 :            : 
#      90                 :        150 :     JSONRPCRequest jreq = helpreq;
#      91                 :        150 :     jreq.mode = JSONRPCRequest::GET_HELP;
#      92                 :        150 :     jreq.params = UniValue();
#      93                 :            : 
#      94         [ +  + ]:        150 :     for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)
#      95                 :      23100 :     {
#      96                 :      23100 :         const CRPCCommand *pcmd = command.second;
#      97                 :      23100 :         std::string strMethod = pcmd->name;
#      98 [ +  + ][ +  + ]:      23100 :         if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
#                 [ +  + ]
#      99                 :      22404 :             continue;
#     100                 :        696 :         jreq.strMethod = strMethod;
#     101                 :        696 :         try
#     102                 :        696 :         {
#     103                 :        696 :             UniValue unused_result;
#     104         [ +  - ]:        696 :             if (setDone.insert(pcmd->unique_id).second)
#     105                 :        696 :                 pcmd->actor(jreq, unused_result, true /* last_handler */);
#     106                 :        696 :         }
#     107                 :        696 :         catch (const std::exception& e)
#     108                 :        696 :         {
#     109                 :            :             // Help text is returned in an exception
#     110                 :        696 :             std::string strHelp = std::string(e.what());
#     111         [ +  + ]:        696 :             if (strCommand == "")
#     112                 :        552 :             {
#     113         [ +  - ]:        552 :                 if (strHelp.find('\n') != std::string::npos)
#     114                 :        552 :                     strHelp = strHelp.substr(0, strHelp.find('\n'));
#     115                 :            : 
#     116         [ +  + ]:        552 :                 if (category != pcmd->category)
#     117                 :         32 :                 {
#     118         [ +  + ]:         32 :                     if (!category.empty())
#     119                 :         28 :                         strRet += "\n";
#     120                 :         32 :                     category = pcmd->category;
#     121                 :         32 :                     strRet += "== " + Capitalize(category) + " ==\n";
#     122                 :         32 :                 }
#     123                 :        552 :             }
#     124                 :        696 :             strRet += strHelp + "\n";
#     125                 :        696 :         }
#     126                 :        696 :     }
#     127         [ +  + ]:        150 :     if (strRet == "")
#     128                 :          2 :         strRet = strprintf("help: unknown command: %s\n", strCommand);
#     129                 :        150 :     strRet = strRet.substr(0,strRet.size()-1);
#     130                 :        150 :     return strRet;
#     131                 :        150 : }
#     132                 :            : 
#     133                 :            : static RPCHelpMan help()
#     134                 :       1539 : {
#     135                 :       1539 :     return RPCHelpMan{"help",
#     136                 :       1539 :                 "\nList all commands, or get help for a specified command.\n",
#     137                 :       1539 :                 {
#     138                 :       1539 :                     {"command", RPCArg::Type::STR, RPCArg::DefaultHint{"all commands"}, "The command to get help on"},
#     139                 :       1539 :                 },
#     140                 :       1539 :                 {
#     141                 :       1539 :                     RPCResult{RPCResult::Type::STR, "", "The help text"},
#     142                 :       1539 :                     RPCResult{RPCResult::Type::ANY, "", ""},
#     143                 :       1539 :                 },
#     144                 :       1539 :                 RPCExamples{""},
#     145                 :       1539 :         [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
#     146                 :       1539 : {
#     147                 :        152 :     std::string strCommand;
#     148         [ +  + ]:        152 :     if (jsonRequest.params.size() > 0) {
#     149                 :        148 :         strCommand = jsonRequest.params[0].get_str();
#     150                 :        148 :     }
#     151         [ +  + ]:        152 :     if (strCommand == "dump_all_command_conversions") {
#     152                 :            :         // Used for testing only, undocumented
#     153                 :          1 :         return tableRPC.dumpArgMap(jsonRequest);
#     154                 :          1 :     }
#     155                 :            : 
#     156                 :        151 :     return tableRPC.help(strCommand, jsonRequest);
#     157                 :        151 : },
#     158                 :       1539 :     };
#     159                 :       1539 : }
#     160                 :            : 
#     161                 :            : static RPCHelpMan stop()
#     162                 :       2002 : {
#     163                 :       2002 :     static const std::string RESULT{PACKAGE_NAME " stopping"};
#     164                 :       2002 :     return RPCHelpMan{"stop",
#     165                 :            :     // Also accept the hidden 'wait' integer argument (milliseconds)
#     166                 :            :     // For instance, 'stop 1000' makes the call wait 1 second before returning
#     167                 :            :     // to the client (intended for testing)
#     168                 :       2002 :                 "\nRequest a graceful shutdown of " PACKAGE_NAME ".",
#     169                 :       2002 :                 {
#     170                 :       2002 :                     {"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", "", {}, /* hidden */ true},
#     171                 :       2002 :                 },
#     172                 :       2002 :                 RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
#     173                 :       2002 :                 RPCExamples{""},
#     174                 :       2002 :         [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
#     175                 :       2002 : {
#     176                 :            :     // Event loop will exit after current HTTP requests have been handled, so
#     177                 :            :     // this reply will get back to the client.
#     178                 :        616 :     StartShutdown();
#     179         [ +  - ]:        616 :     if (jsonRequest.params[0].isNum()) {
#     180                 :        616 :         UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].get_int()});
#     181                 :        616 :     }
#     182                 :        616 :     return RESULT;
#     183                 :        616 : },
#     184                 :       2002 :     };
#     185                 :       2002 : }
#     186                 :            : 
#     187                 :            : static RPCHelpMan uptime()
#     188                 :       1387 : {
#     189                 :       1387 :     return RPCHelpMan{"uptime",
#     190                 :       1387 :                 "\nReturns the total uptime of the server.\n",
#     191                 :       1387 :                             {},
#     192                 :       1387 :                             RPCResult{
#     193                 :       1387 :                                 RPCResult::Type::NUM, "", "The number of seconds that the server has been running"
#     194                 :       1387 :                             },
#     195                 :       1387 :                 RPCExamples{
#     196                 :       1387 :                     HelpExampleCli("uptime", "")
#     197                 :       1387 :                 + HelpExampleRpc("uptime", "")
#     198                 :       1387 :                 },
#     199                 :       1387 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     200                 :       1387 : {
#     201                 :          1 :     return GetTime() - GetStartupTime();
#     202                 :          1 : }
#     203                 :       1387 :     };
#     204                 :       1387 : }
#     205                 :            : 
#     206                 :            : static RPCHelpMan getrpcinfo()
#     207                 :       1617 : {
#     208                 :       1617 :     return RPCHelpMan{"getrpcinfo",
#     209                 :       1617 :                 "\nReturns details of the RPC server.\n",
#     210                 :       1617 :                 {},
#     211                 :       1617 :                 RPCResult{
#     212                 :       1617 :                     RPCResult::Type::OBJ, "", "",
#     213                 :       1617 :                     {
#     214                 :       1617 :                         {RPCResult::Type::ARR, "active_commands", "All active commands",
#     215                 :       1617 :                         {
#     216                 :       1617 :                             {RPCResult::Type::OBJ, "", "Information about an active command",
#     217                 :       1617 :                             {
#     218                 :       1617 :                                  {RPCResult::Type::STR, "method", "The name of the RPC command"},
#     219                 :       1617 :                                  {RPCResult::Type::NUM, "duration", "The running time in microseconds"},
#     220                 :       1617 :                             }},
#     221                 :       1617 :                         }},
#     222                 :       1617 :                         {RPCResult::Type::STR, "logpath", "The complete file path to the debug log"},
#     223                 :       1617 :                     }
#     224                 :       1617 :                 },
#     225                 :       1617 :                 RPCExamples{
#     226                 :       1617 :                     HelpExampleCli("getrpcinfo", "")
#     227                 :       1617 :                 + HelpExampleRpc("getrpcinfo", "")},
#     228                 :       1617 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     229                 :       1617 : {
#     230                 :        231 :     LOCK(g_rpc_server_info.mutex);
#     231                 :        231 :     UniValue active_commands(UniValue::VARR);
#     232         [ +  + ]:        232 :     for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
#     233                 :        232 :         UniValue entry(UniValue::VOBJ);
#     234                 :        232 :         entry.pushKV("method", info.method);
#     235                 :        232 :         entry.pushKV("duration", GetTimeMicros() - info.start);
#     236                 :        232 :         active_commands.push_back(entry);
#     237                 :        232 :     }
#     238                 :            : 
#     239                 :        231 :     UniValue result(UniValue::VOBJ);
#     240                 :        231 :     result.pushKV("active_commands", active_commands);
#     241                 :            : 
#     242                 :        231 :     const std::string path = LogInstance().m_file_path.string();
#     243                 :        231 :     UniValue log_path(UniValue::VSTR, path);
#     244                 :        231 :     result.pushKV("logpath", log_path);
#     245                 :            : 
#     246                 :        231 :     return result;
#     247                 :        231 : }
#     248                 :       1617 :     };
#     249                 :       1617 : }
#     250                 :            : 
#     251                 :            : // clang-format off
#     252                 :            : static const CRPCCommand vRPCCommands[] =
#     253                 :            : { //  category               actor (function)
#     254                 :            :   //  ---------------------  -----------------------
#     255                 :            :     /* Overall control/query calls */
#     256                 :            :     { "control",             &getrpcinfo,             },
#     257                 :            :     { "control",             &help,                   },
#     258                 :            :     { "control",             &stop,                   },
#     259                 :            :     { "control",             &uptime,                 },
#     260                 :            : };
#     261                 :            : // clang-format on
#     262                 :            : 
#     263                 :            : CRPCTable::CRPCTable()
#     264                 :        690 : {
#     265         [ +  + ]:       2760 :     for (const auto& c : vRPCCommands) {
#     266                 :       2760 :         appendCommand(c.name, &c);
#     267                 :       2760 :     }
#     268                 :        690 : }
#     269                 :            : 
#     270                 :            : void CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
#     271                 :     115888 : {
#     272         [ -  + ]:     115888 :     CHECK_NONFATAL(!IsRPCRunning()); // Only add commands before rpc is running
#     273                 :            : 
#     274                 :     115888 :     mapCommands[name].push_back(pcmd);
#     275                 :     115888 : }
#     276                 :            : 
#     277                 :            : bool CRPCTable::removeCommand(const std::string& name, const CRPCCommand* pcmd)
#     278                 :      40504 : {
#     279                 :      40504 :     auto it = mapCommands.find(name);
#     280         [ +  - ]:      40504 :     if (it != mapCommands.end()) {
#     281                 :      40504 :         auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
#     282         [ +  - ]:      40504 :         if (it->second.end() != new_end) {
#     283                 :      40504 :             it->second.erase(new_end, it->second.end());
#     284                 :      40504 :             return true;
#     285                 :      40504 :         }
#     286                 :          0 :     }
#     287                 :          0 :     return false;
#     288                 :          0 : }
#     289                 :            : 
#     290                 :            : void StartRPC()
#     291                 :        659 : {
#     292         [ +  - ]:        659 :     LogPrint(BCLog::RPC, "Starting RPC\n");
#     293                 :        659 :     g_rpc_running = true;
#     294                 :        659 :     g_rpcSignals.Started();
#     295                 :        659 : }
#     296                 :            : 
#     297                 :            : void InterruptRPC()
#     298                 :        663 : {
#     299                 :        663 :     static std::once_flag g_rpc_interrupt_flag;
#     300                 :            :     // This function could be called twice if the GUI has been started with -server=1.
#     301                 :        663 :     std::call_once(g_rpc_interrupt_flag, []() {
#     302         [ +  - ]:        663 :         LogPrint(BCLog::RPC, "Interrupting RPC\n");
#     303                 :            :         // Interrupt e.g. running longpolls
#     304                 :        663 :         g_rpc_running = false;
#     305                 :        663 :     });
#     306                 :        663 : }
#     307                 :            : 
#     308                 :            : void StopRPC()
#     309                 :        663 : {
#     310                 :        663 :     static std::once_flag g_rpc_stop_flag;
#     311                 :            :     // This function could be called twice if the GUI has been started with -server=1.
#     312                 :        663 :     assert(!g_rpc_running);
#     313                 :        663 :     std::call_once(g_rpc_stop_flag, []() {
#     314         [ +  - ]:        663 :         LogPrint(BCLog::RPC, "Stopping RPC\n");
#     315                 :        663 :         WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
#     316                 :        663 :         DeleteAuthCookie();
#     317                 :        663 :         g_rpcSignals.Stopped();
#     318                 :        663 :     });
#     319                 :        663 : }
#     320                 :            : 
#     321                 :            : bool IsRPCRunning()
#     322                 :     118092 : {
#     323                 :     118092 :     return g_rpc_running;
#     324                 :     118092 : }
#     325                 :            : 
#     326                 :            : void RpcInterruptionPoint()
#     327                 :       2184 : {
#     328         [ -  + ]:       2184 :     if (!IsRPCRunning()) throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
#     329                 :       2184 : }
#     330                 :            : 
#     331                 :            : void SetRPCWarmupStatus(const std::string& newStatus)
#     332                 :       4974 : {
#     333                 :       4974 :     LOCK(g_rpc_warmup_mutex);
#     334                 :       4974 :     rpcWarmupStatus = newStatus;
#     335                 :       4974 : }
#     336                 :            : 
#     337                 :            : void SetRPCWarmupFinished()
#     338                 :        620 : {
#     339                 :        620 :     LOCK(g_rpc_warmup_mutex);
#     340                 :        620 :     assert(fRPCInWarmup);
#     341                 :        620 :     fRPCInWarmup = false;
#     342                 :        620 : }
#     343                 :            : 
#     344                 :            : bool RPCIsInWarmup(std::string *outStatus)
#     345                 :        151 : {
#     346                 :        151 :     LOCK(g_rpc_warmup_mutex);
#     347         [ +  + ]:        151 :     if (outStatus)
#     348                 :         41 :         *outStatus = rpcWarmupStatus;
#     349                 :        151 :     return fRPCInWarmup;
#     350                 :        151 : }
#     351                 :            : 
#     352                 :            : bool IsDeprecatedRPCEnabled(const std::string& method)
#     353                 :       7642 : {
#     354                 :       7642 :     const std::vector<std::string> enabled_methods = gArgs.GetArgs("-deprecatedrpc");
#     355                 :            : 
#     356                 :       7642 :     return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
#     357                 :       7642 : }
#     358                 :            : 
#     359                 :            : static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue& req)
#     360                 :        148 : {
#     361                 :        148 :     UniValue rpc_result(UniValue::VOBJ);
#     362                 :            : 
#     363                 :        148 :     try {
#     364                 :        148 :         jreq.parse(req);
#     365                 :            : 
#     366                 :        148 :         UniValue result = tableRPC.execute(jreq);
#     367                 :        148 :         rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id);
#     368                 :        148 :     }
#     369                 :        148 :     catch (const UniValue& objError)
#     370                 :        148 :     {
#     371                 :          9 :         rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id);
#     372                 :          9 :     }
#     373                 :        148 :     catch (const std::exception& e)
#     374                 :        148 :     {
#     375                 :          0 :         rpc_result = JSONRPCReplyObj(NullUniValue,
#     376                 :          0 :                                      JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
#     377                 :          0 :     }
#     378                 :            : 
#     379                 :        148 :     return rpc_result;
#     380                 :        148 : }
#     381                 :            : 
#     382                 :            : std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq)
#     383                 :         14 : {
#     384                 :         14 :     UniValue ret(UniValue::VARR);
#     385         [ +  + ]:        162 :     for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++)
#     386                 :        148 :         ret.push_back(JSONRPCExecOne(jreq, vReq[reqIdx]));
#     387                 :            : 
#     388                 :         14 :     return ret.write() + "\n";
#     389                 :         14 : }
#     390                 :            : 
#     391                 :            : /**
#     392                 :            :  * Process named arguments into a vector of positional arguments, based on the
#     393                 :            :  * passed-in specification for the RPC call's arguments.
#     394                 :            :  */
#     395                 :            : static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, const std::vector<std::string>& argNames)
#     396                 :      52662 : {
#     397                 :      52662 :     JSONRPCRequest out = in;
#     398                 :      52662 :     out.params = UniValue(UniValue::VARR);
#     399                 :            :     // Build a map of parameters, and remove ones that have been processed, so that we can throw a focused error if
#     400                 :            :     // there is an unknown one.
#     401                 :      52662 :     const std::vector<std::string>& keys = in.params.getKeys();
#     402                 :      52662 :     const std::vector<UniValue>& values = in.params.getValues();
#     403                 :      52662 :     std::unordered_map<std::string, const UniValue*> argsIn;
#     404         [ +  + ]:      80243 :     for (size_t i=0; i<keys.size(); ++i) {
#     405                 :      27581 :         argsIn[keys[i]] = &values[i];
#     406                 :      27581 :     }
#     407                 :            :     // Process expected parameters.
#     408                 :      52662 :     int hole = 0;
#     409         [ +  + ]:      68467 :     for (const std::string &argNamePattern: argNames) {
#     410                 :      68467 :         std::vector<std::string> vargNames;
#     411                 :      68467 :         boost::algorithm::split(vargNames, argNamePattern, boost::algorithm::is_any_of("|"));
#     412                 :      68467 :         auto fr = argsIn.end();
#     413         [ +  + ]:      68556 :         for (const std::string & argName : vargNames) {
#     414                 :      68556 :             fr = argsIn.find(argName);
#     415         [ +  + ]:      68556 :             if (fr != argsIn.end()) {
#     416                 :      27574 :                 break;
#     417                 :      27574 :             }
#     418                 :      68556 :         }
#     419         [ +  + ]:      68467 :         if (fr != argsIn.end()) {
#     420         [ +  + ]:      31626 :             for (int i = 0; i < hole; ++i) {
#     421                 :            :                 // Fill hole between specified parameters with JSON nulls,
#     422                 :            :                 // but not at the end (for backwards compatibility with calls
#     423                 :            :                 // that act based on number of specified parameters).
#     424                 :       4052 :                 out.params.push_back(UniValue());
#     425                 :       4052 :             }
#     426                 :      27574 :             hole = 0;
#     427                 :      27574 :             out.params.push_back(*fr->second);
#     428                 :      27574 :             argsIn.erase(fr);
#     429                 :      40893 :         } else {
#     430                 :      40893 :             hole += 1;
#     431                 :      40893 :         }
#     432                 :      68467 :     }
#     433                 :            :     // If there are still arguments in the argsIn map, this is an error.
#     434         [ +  + ]:      52662 :     if (!argsIn.empty()) {
#     435                 :          7 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown named parameter " + argsIn.begin()->first);
#     436                 :          7 :     }
#     437                 :            :     // Return request with named arguments transformed to positional arguments
#     438                 :      52655 :     return out;
#     439                 :      52655 : }
#     440                 :            : 
#     441                 :            : static bool ExecuteCommands(const std::vector<const CRPCCommand*>& commands, const JSONRPCRequest& request, UniValue& result)
#     442                 :     111276 : {
#     443         [ +  + ]:     111276 :     for (const auto& command : commands) {
#     444         [ +  + ]:     111276 :         if (ExecuteCommand(*command, request, result, &command == &commands.back())) {
#     445                 :     105414 :             return true;
#     446                 :     105414 :         }
#     447                 :     111276 :     }
#     448                 :     111276 :     return false;
#     449                 :     111276 : }
#     450                 :            : 
#     451                 :            : UniValue CRPCTable::execute(const JSONRPCRequest &request) const
#     452                 :     111461 : {
#     453                 :            :     // Return immediately if in warmup
#     454                 :     111461 :     {
#     455                 :     111461 :         LOCK(g_rpc_warmup_mutex);
#     456         [ +  + ]:     111461 :         if (fRPCInWarmup)
#     457                 :        335 :             throw JSONRPCError(RPC_IN_WARMUP, rpcWarmupStatus);
#     458                 :     111126 :     }
#     459                 :            : 
#     460                 :            :     // Find method
#     461                 :     111126 :     auto it = mapCommands.find(request.strMethod);
#     462         [ +  + ]:     111126 :     if (it != mapCommands.end()) {
#     463                 :     111122 :         UniValue result;
#     464         [ +  + ]:     111122 :         if (ExecuteCommands(it->second, request, result)) {
#     465                 :     105260 :             return result;
#     466                 :     105260 :         }
#     467                 :       5866 :     }
#     468                 :       5866 :     throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
#     469                 :       5866 : }
#     470                 :            : 
#     471                 :            : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler)
#     472                 :     111276 : {
#     473                 :     111276 :     try
#     474                 :     111276 :     {
#     475                 :     111276 :         RPCCommandExecution execution(request.strMethod);
#     476                 :            :         // Execute, convert arguments to array if necessary
#     477         [ +  + ]:     111276 :         if (request.params.isObject()) {
#     478                 :      52662 :             return command.actor(transformNamedArguments(request, command.argNames), result, last_handler);
#     479                 :      58614 :         } else {
#     480                 :      58614 :             return command.actor(request, result, last_handler);
#     481                 :      58614 :         }
#     482                 :         79 :     }
#     483                 :         79 :     catch (const std::exception& e)
#     484                 :         79 :     {
#     485                 :         79 :         throw JSONRPCError(RPC_MISC_ERROR, e.what());
#     486                 :         79 :     }
#     487                 :     111276 : }
#     488                 :            : 
#     489                 :            : std::vector<std::string> CRPCTable::listCommands() const
#     490                 :          0 : {
#     491                 :          0 :     std::vector<std::string> commandList;
#     492         [ #  # ]:          0 :     for (const auto& i : mapCommands) commandList.emplace_back(i.first);
#     493                 :          0 :     return commandList;
#     494                 :          0 : }
#     495                 :            : 
#     496                 :            : UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const
#     497                 :          1 : {
#     498                 :          1 :     JSONRPCRequest request = args_request;
#     499                 :          1 :     request.mode = JSONRPCRequest::GET_ARGS;
#     500                 :            : 
#     501                 :          1 :     UniValue ret{UniValue::VARR};
#     502         [ +  + ]:        154 :     for (const auto& cmd : mapCommands) {
#     503                 :        154 :         UniValue result;
#     504         [ +  - ]:        154 :         if (ExecuteCommands(cmd.second, request, result)) {
#     505         [ +  + ]:        294 :             for (const auto& values : result.getValues()) {
#     506                 :        294 :                 ret.push_back(values);
#     507                 :        294 :             }
#     508                 :        154 :         }
#     509                 :        154 :     }
#     510                 :          1 :     return ret;
#     511                 :          1 : }
#     512                 :            : 
#     513                 :            : void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
#     514                 :          0 : {
#     515         [ #  # ]:          0 :     if (!timerInterface)
#     516                 :          0 :         timerInterface = iface;
#     517                 :          0 : }
#     518                 :            : 
#     519                 :            : void RPCSetTimerInterface(RPCTimerInterface *iface)
#     520                 :        656 : {
#     521                 :        656 :     timerInterface = iface;
#     522                 :        656 : }
#     523                 :            : 
#     524                 :            : void RPCUnsetTimerInterface(RPCTimerInterface *iface)
#     525                 :        656 : {
#     526         [ +  - ]:        656 :     if (timerInterface == iface)
#     527                 :        656 :         timerInterface = nullptr;
#     528                 :        656 : }
#     529                 :            : 
#     530                 :            : void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds)
#     531                 :         57 : {
#     532         [ -  + ]:         57 :     if (!timerInterface)
#     533                 :          0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC");
#     534                 :         57 :     LOCK(g_deadline_timers_mutex);
#     535                 :         57 :     deadlineTimers.erase(name);
#     536         [ +  - ]:         57 :     LogPrint(BCLog::RPC, "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name());
#     537                 :         57 :     deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));
#     538                 :         57 : }
#     539                 :            : 
#     540                 :            : int RPCSerializationFlags()
#     541                 :       2906 : {
#     542                 :       2906 :     int flag = 0;
#     543         [ +  + ]:       2906 :     if (gArgs.GetArg("-rpcserialversion", DEFAULT_RPC_SERIALIZE_VERSION) == 0)
#     544                 :         24 :         flag |= SERIALIZE_TRANSACTION_NO_WITNESS;
#     545                 :       2906 :     return flag;
#     546                 :       2906 : }
#     547                 :            : 
#     548                 :            : CRPCTable tableRPC;

Generated by: LCOV version 1.14