LCOV - code coverage report
Current view: top level - src/rpc - server.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.1 % 306 297
Test Date: 2025-08-25 05:11:47 Functions: 96.9 % 32 31
Branches: 57.0 % 588 335

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <bitcoin-build-config.h> // IWYU pragma: keep
       7                 :             : 
       8                 :             : #include <rpc/server.h>
       9                 :             : 
      10                 :             : #include <common/args.h>
      11                 :             : #include <common/system.h>
      12                 :             : #include <logging.h>
      13                 :             : #include <node/context.h>
      14                 :             : #include <node/kernel_notifications.h>
      15                 :             : #include <rpc/server_util.h>
      16                 :             : #include <rpc/util.h>
      17                 :             : #include <sync.h>
      18                 :             : #include <util/signalinterrupt.h>
      19                 :             : #include <util/strencodings.h>
      20                 :             : #include <util/string.h>
      21                 :             : #include <util/time.h>
      22                 :             : #include <validation.h>
      23                 :             : 
      24                 :             : #include <algorithm>
      25                 :             : #include <cassert>
      26                 :             : #include <chrono>
      27                 :             : #include <memory>
      28                 :             : #include <mutex>
      29                 :             : #include <unordered_map>
      30                 :             : 
      31                 :             : using util::SplitString;
      32                 :             : 
      33                 :             : static GlobalMutex g_rpc_warmup_mutex;
      34                 :             : static std::atomic<bool> g_rpc_running{false};
      35                 :             : static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
      36                 :             : static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
      37                 :             : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler);
      38                 :             : 
      39                 :      406856 : struct RPCCommandExecutionInfo
      40                 :             : {
      41                 :             :     std::string method;
      42                 :             :     SteadyClock::time_point start;
      43                 :             : };
      44                 :             : 
      45                 :             : struct RPCServerInfo
      46                 :             : {
      47                 :             :     Mutex mutex;
      48                 :             :     std::list<RPCCommandExecutionInfo> active_commands GUARDED_BY(mutex);
      49                 :             : };
      50                 :             : 
      51                 :             : static RPCServerInfo g_rpc_server_info;
      52                 :             : 
      53                 :             : struct RPCCommandExecution
      54                 :             : {
      55                 :             :     std::list<RPCCommandExecutionInfo>::iterator it;
      56                 :      203428 :     explicit RPCCommandExecution(const std::string& method)
      57                 :      203428 :     {
      58                 :      203428 :         LOCK(g_rpc_server_info.mutex);
      59   [ -  +  +  -  :      406856 :         it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, SteadyClock::now()});
                   +  - ]
      60         [ +  - ]:      406856 :     }
      61                 :      203428 :     ~RPCCommandExecution()
      62                 :             :     {
      63                 :      203428 :         LOCK(g_rpc_server_info.mutex);
      64         [ +  - ]:      203428 :         g_rpc_server_info.active_commands.erase(it);
      65                 :      203428 :     }
      66                 :             : };
      67                 :             : 
      68                 :         172 : std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest& helpreq) const
      69                 :             : {
      70         [ +  - ]:         172 :     std::string strRet;
      71                 :         172 :     std::string category;
      72         [ +  - ]:         172 :     std::set<intptr_t> setDone;
      73                 :         172 :     std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
      74         [ +  - ]:         172 :     vCommands.reserve(mapCommands.size());
      75                 :             : 
      76         [ +  + ]:       27870 :     for (const auto& entry : mapCommands)
      77   [ +  -  +  - ]:       55396 :         vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
      78                 :         172 :     std::ranges::sort(vCommands);
      79                 :             : 
      80         [ +  - ]:         172 :     JSONRPCRequest jreq = helpreq;
      81                 :         172 :     jreq.mode = JSONRPCRequest::GET_HELP;
      82                 :         172 :     jreq.params = UniValue();
      83                 :             : 
      84   [ -  +  +  + ]:       27870 :     for (const auto& [_, pcmd] : vCommands) {
      85         [ -  + ]:       27698 :         std::string strMethod = pcmd->name;
      86   [ +  +  +  +  :       27698 :         if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
                   +  + ]
      87                 :       26685 :             continue;
      88         [ +  - ]:        1013 :         jreq.strMethod = strMethod;
      89                 :        1013 :         try
      90                 :             :         {
      91         [ +  - ]:        1013 :             UniValue unused_result;
      92   [ +  -  +  - ]:        1013 :             if (setDone.insert(pcmd->unique_id).second)
      93         [ -  + ]:        1013 :                 pcmd->actor(jreq, unused_result, /*last_handler=*/true);
      94         [ -  + ]:        2026 :         } catch (const HelpResult& e) {
      95         [ +  - ]:        1013 :             std::string strHelp{e.what()};
      96         [ +  + ]:        1013 :             if (strCommand == "")
      97                 :             :             {
      98         [ +  - ]:         850 :                 if (strHelp.find('\n') != std::string::npos)
      99         [ +  - ]:         850 :                     strHelp = strHelp.substr(0, strHelp.find('\n'));
     100                 :             : 
     101         [ +  + ]:         850 :                 if (category != pcmd->category)
     102                 :             :                 {
     103         [ +  + ]:          66 :                     if (!category.empty())
     104         [ +  - ]:          58 :                         strRet += "\n";
     105         [ +  - ]:          66 :                     category = pcmd->category;
     106   [ -  +  +  -  :         264 :                     strRet += "== " + Capitalize(category) + " ==\n";
             +  -  -  + ]
     107                 :             :                 }
     108                 :             :             }
     109         [ +  - ]:        2026 :             strRet += strHelp + "\n";
     110         [ +  - ]:        1013 :         }
     111                 :       27698 :     }
     112         [ +  + ]:         172 :     if (strRet == "")
     113         [ +  - ]:           1 :         strRet = strprintf("help: unknown command: %s\n", strCommand);
     114   [ -  +  +  - ]:         172 :     strRet = strRet.substr(0,strRet.size()-1);
     115                 :         344 :     return strRet;
     116                 :         172 : }
     117                 :             : 
     118                 :        2779 : static RPCHelpMan help()
     119                 :             : {
     120                 :        2779 :     return RPCHelpMan{
     121                 :        2779 :         "help",
     122         [ +  - ]:        5558 :         "List all commands, or get help for a specified command.\n",
     123                 :             :                 {
     124   [ +  -  +  -  :        8337 :                     {"command", RPCArg::Type::STR, RPCArg::DefaultHint{"all commands"}, "The command to get help on"},
                   +  - ]
     125                 :             :                 },
     126                 :             :                 {
     127   [ +  -  +  - ]:        5558 :                     RPCResult{RPCResult::Type::STR, "", "The help text"},
     128   [ +  -  +  - ]:        5558 :                     RPCResult{RPCResult::Type::ANY, "", ""},
     129                 :             :                 },
     130         [ +  - ]:        5558 :                 RPCExamples{""},
     131                 :        2779 :         [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
     132                 :             : {
     133         [ -  + ]:         173 :     std::string strCommand;
     134   [ -  +  +  + ]:         173 :     if (jsonRequest.params.size() > 0) {
     135   [ +  -  +  -  :         165 :         strCommand = jsonRequest.params[0].get_str();
                   +  - ]
     136                 :             :     }
     137         [ +  + ]:         173 :     if (strCommand == "dump_all_command_conversions") {
     138                 :             :         // Used for testing only, undocumented
     139         [ +  - ]:           1 :         return tableRPC.dumpArgMap(jsonRequest);
     140                 :             :     }
     141                 :             : 
     142   [ +  -  +  - ]:         344 :     return tableRPC.help(strCommand, jsonRequest);
     143                 :         173 : },
     144   [ +  -  +  -  :       27790 :     };
          +  -  +  -  +  
          +  +  +  -  -  
                   -  - ]
     145   [ +  -  +  -  :       11116 : }
             +  -  -  - ]
     146                 :             : 
     147                 :        3566 : static RPCHelpMan stop()
     148                 :             : {
     149   [ +  +  +  -  :        3566 :     static const std::string RESULT{CLIENT_NAME " stopping"};
                   +  - ]
     150                 :        3566 :     return RPCHelpMan{
     151                 :        3566 :         "stop",
     152                 :             :     // Also accept the hidden 'wait' integer argument (milliseconds)
     153                 :             :     // For instance, 'stop 1000' makes the call wait 1 second before returning
     154                 :             :     // to the client (intended for testing)
     155         [ +  - ]:        7132 :         "Request a graceful shutdown of " CLIENT_NAME ".",
     156                 :             :                 {
     157   [ +  -  +  -  :        7132 :                     {"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "how long to wait in ms", RPCArgOptions{.hidden=true}},
                   +  - ]
     158                 :             :                 },
     159   [ +  -  +  -  :       10698 :                 RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
             +  -  +  - ]
     160   [ +  -  +  - ]:       10698 :                 RPCExamples{""},
     161                 :        3566 :         [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
     162                 :             : {
     163                 :             :     // Event loop will exit after current HTTP requests have been handled, so
     164                 :             :     // this reply will get back to the client.
     165                 :         962 :     CHECK_NONFATAL((CHECK_NONFATAL(EnsureAnyNodeContext(jsonRequest.context).shutdown_request))());
     166         [ +  + ]:         962 :     if (jsonRequest.params[0].isNum()) {
     167                 :         961 :         UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].getInt<int>()});
     168                 :             :     }
     169                 :         962 :     return RESULT;
     170                 :             : },
     171   [ +  -  +  -  :       17830 :     };
             +  +  -  - ]
     172         [ +  - ]:        3566 : }
     173                 :             : 
     174                 :        2605 : static RPCHelpMan uptime()
     175                 :             : {
     176                 :        2605 :     return RPCHelpMan{
     177                 :        2605 :         "uptime",
     178         [ +  - ]:        5210 :         "Returns the total uptime of the server.\n",
     179                 :             :                             {},
     180         [ +  - ]:        5210 :                             RPCResult{
     181         [ +  - ]:        5210 :                                 RPCResult::Type::NUM, "", "The number of seconds that the server has been running"
     182         [ +  - ]:        5210 :                             },
     183                 :        2605 :                 RPCExamples{
     184   [ +  -  +  -  :        5210 :                     HelpExampleCli("uptime", "")
                   +  - ]
     185   [ +  -  +  -  :       10420 :                 + HelpExampleRpc("uptime", "")
             +  -  +  - ]
     186         [ +  - ]:        2605 :                 },
     187                 :        2605 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     188                 :             : {
     189                 :           1 :     return GetTime() - GetStartupTime();
     190                 :             : }
     191   [ +  -  +  - ]:       10420 :     };
     192                 :             : }
     193                 :             : 
     194                 :        2607 : static RPCHelpMan getrpcinfo()
     195                 :             : {
     196                 :        2607 :     return RPCHelpMan{
     197                 :        2607 :         "getrpcinfo",
     198         [ +  - ]:        5214 :         "Returns details of the RPC server.\n",
     199                 :             :                 {},
     200         [ +  - ]:        5214 :                 RPCResult{
     201         [ +  - ]:        5214 :                     RPCResult::Type::OBJ, "", "",
     202                 :             :                     {
     203   [ +  -  +  - ]:        5214 :                         {RPCResult::Type::ARR, "active_commands", "All active commands",
     204                 :             :                         {
     205   [ +  -  +  - ]:        5214 :                             {RPCResult::Type::OBJ, "", "Information about an active command",
     206                 :             :                             {
     207   [ +  -  +  - ]:        5214 :                                  {RPCResult::Type::STR, "method", "The name of the RPC command"},
     208   [ +  -  +  - ]:        5214 :                                  {RPCResult::Type::NUM, "duration", "The running time in microseconds"},
     209                 :             :                             }},
     210                 :             :                         }},
     211   [ +  -  +  - ]:        5214 :                         {RPCResult::Type::STR, "logpath", "The complete file path to the debug log"},
     212                 :             :                     }
     213   [ +  -  +  -  :       28677 :                 },
          +  -  +  -  +  
          +  +  +  +  +  
          -  -  -  -  -  
                      - ]
     214                 :        2607 :                 RPCExamples{
     215   [ +  -  +  -  :        5214 :                     HelpExampleCli("getrpcinfo", "")
                   +  - ]
     216   [ +  -  +  -  :       13035 :                 + HelpExampleRpc("getrpcinfo", "")},
          +  -  +  -  +  
                      - ]
     217                 :        2607 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     218                 :             : {
     219                 :           3 :     LOCK(g_rpc_server_info.mutex);
     220                 :           3 :     UniValue active_commands(UniValue::VARR);
     221         [ +  + ]:           7 :     for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
     222                 :           4 :         UniValue entry(UniValue::VOBJ);
     223   [ +  -  +  -  :           8 :         entry.pushKV("method", info.method);
                   +  - ]
     224   [ +  -  +  -  :           8 :         entry.pushKV("duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.start)});
                   +  - ]
     225         [ +  - ]:           4 :         active_commands.push_back(std::move(entry));
     226                 :           4 :     }
     227                 :             : 
     228                 :           3 :     UniValue result(UniValue::VOBJ);
     229   [ +  -  +  - ]:           6 :     result.pushKV("active_commands", std::move(active_commands));
     230                 :             : 
     231   [ +  -  +  - ]:           3 :     const std::string path = LogInstance().m_file_path.utf8string();
     232         [ -  + ]:           6 :     UniValue log_path(UniValue::VSTR, path);
     233   [ +  -  +  - ]:           6 :     result.pushKV("logpath", std::move(log_path));
     234                 :             : 
     235                 :           6 :     return result;
     236         [ +  - ]:           6 : }
     237   [ +  -  +  - ]:       10428 :     };
     238   [ +  -  +  -  :       13035 : }
          +  -  +  -  +  
             -  -  -  -  
                      - ]
     239                 :             : 
     240                 :             : static const CRPCCommand vRPCCommands[]{
     241                 :             :     /* Overall control/query calls */
     242                 :             :     {"control", &getrpcinfo},
     243                 :             :     {"control", &help},
     244                 :             :     {"control", &stop},
     245                 :             :     {"control", &uptime},
     246                 :             : };
     247                 :             : 
     248                 :        1309 : CRPCTable::CRPCTable()
     249                 :             : {
     250         [ +  + ]:        6545 :     for (const auto& c : vRPCCommands) {
     251         [ +  - ]:        5236 :         appendCommand(c.name, &c);
     252                 :             :     }
     253                 :        1309 : }
     254                 :             : 
     255                 :      162975 : void CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
     256                 :             : {
     257                 :      162975 :     CHECK_NONFATAL(!IsRPCRunning()); // Only add commands before rpc is running
     258                 :             : 
     259                 :      162975 :     mapCommands[name].push_back(pcmd);
     260                 :      162975 : }
     261                 :             : 
     262                 :       23598 : bool CRPCTable::removeCommand(const std::string& name, const CRPCCommand* pcmd)
     263                 :             : {
     264                 :       23598 :     auto it = mapCommands.find(name);
     265         [ +  - ]:       23598 :     if (it != mapCommands.end()) {
     266                 :       23598 :         auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
     267         [ +  - ]:       23598 :         if (it->second.end() != new_end) {
     268                 :       23598 :             it->second.erase(new_end, it->second.end());
     269                 :       23598 :             return true;
     270                 :             :         }
     271                 :             :     }
     272                 :             :     return false;
     273                 :             : }
     274                 :             : 
     275                 :        1081 : void StartRPC()
     276                 :             : {
     277         [ +  - ]:        1081 :     LogDebug(BCLog::RPC, "Starting RPC\n");
     278                 :        1081 :     g_rpc_running = true;
     279                 :        1081 : }
     280                 :             : 
     281                 :        1122 : void InterruptRPC()
     282                 :             : {
     283                 :        1122 :     static std::once_flag g_rpc_interrupt_flag;
     284                 :             :     // This function could be called twice if the GUI has been started with -server=1.
     285                 :        1122 :     std::call_once(g_rpc_interrupt_flag, []() {
     286         [ +  + ]:        1122 :         LogDebug(BCLog::RPC, "Interrupting RPC\n");
     287                 :             :         // Interrupt e.g. running longpolls
     288                 :        1122 :         g_rpc_running = false;
     289                 :        1122 :     });
     290                 :        1122 : }
     291                 :             : 
     292                 :        1122 : void StopRPC()
     293                 :             : {
     294                 :        1122 :     static std::once_flag g_rpc_stop_flag;
     295                 :             :     // This function could be called twice if the GUI has been started with -server=1.
     296         [ -  + ]:        1122 :     assert(!g_rpc_running);
     297                 :        1122 :     std::call_once(g_rpc_stop_flag, [&]() {
     298         [ +  + ]:        1122 :         LogDebug(BCLog::RPC, "Stopping RPC\n");
     299                 :        1122 :         DeleteAuthCookie();
     300         [ +  + ]:        1122 :         LogDebug(BCLog::RPC, "RPC stopped.\n");
     301                 :        1122 :     });
     302                 :        1122 : }
     303                 :             : 
     304                 :      168909 : bool IsRPCRunning()
     305                 :             : {
     306                 :      168909 :     return g_rpc_running;
     307                 :             : }
     308                 :             : 
     309                 :        5928 : void RpcInterruptionPoint()
     310                 :             : {
     311   [ -  +  -  -  :        5928 :     if (!IsRPCRunning()) throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
                   -  - ]
     312                 :        5928 : }
     313                 :             : 
     314                 :        7353 : void SetRPCWarmupStatus(const std::string& newStatus)
     315                 :             : {
     316                 :        7353 :     LOCK(g_rpc_warmup_mutex);
     317   [ +  -  +  - ]:       14706 :     rpcWarmupStatus = newStatus;
     318                 :        7353 : }
     319                 :             : 
     320                 :         620 : void SetRPCWarmupStarting()
     321                 :             : {
     322                 :         620 :     LOCK(g_rpc_warmup_mutex);
     323         [ +  - ]:         620 :     fRPCInWarmup = true;
     324                 :         620 : }
     325                 :             : 
     326                 :         988 : void SetRPCWarmupFinished()
     327                 :             : {
     328                 :         988 :     LOCK(g_rpc_warmup_mutex);
     329         [ -  + ]:         988 :     assert(fRPCInWarmup);
     330         [ +  - ]:         988 :     fRPCInWarmup = false;
     331                 :         988 : }
     332                 :             : 
     333                 :         772 : bool RPCIsInWarmup(std::string *outStatus)
     334                 :             : {
     335                 :         772 :     LOCK(g_rpc_warmup_mutex);
     336         [ +  + ]:         772 :     if (outStatus)
     337         [ +  - ]:         705 :         *outStatus = rpcWarmupStatus;
     338         [ +  - ]:         772 :     return fRPCInWarmup;
     339                 :         772 : }
     340                 :             : 
     341                 :       51408 : bool IsDeprecatedRPCEnabled(const std::string& method)
     342                 :             : {
     343         [ +  - ]:       51408 :     const std::vector<std::string> enabled_methods = gArgs.GetArgs("-deprecatedrpc");
     344                 :             : 
     345                 :       51408 :     return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
     346                 :       51408 : }
     347                 :             : 
     348                 :      204213 : UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors)
     349                 :             : {
     350         [ +  + ]:      204213 :     UniValue result;
     351         [ +  + ]:      204213 :     if (catch_errors) {
     352                 :      204179 :         try {
     353         [ +  + ]:      204179 :             result = tableRPC.execute(jreq);
     354      [ -  +  - ]:        6710 :         } catch (UniValue& e) {
     355   [ +  -  +  -  :        6710 :             return JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
                   +  - ]
     356                 :        6710 :         } catch (const std::exception& e) {
     357   [ -  -  -  -  :           0 :             return JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_MISC_ERROR, e.what()), jreq.id, jreq.m_json_version);
          -  -  -  -  -  
                      - ]
     358                 :           0 :         }
     359                 :             :     } else {
     360         [ +  + ]:          34 :         result = tableRPC.execute(jreq);
     361                 :             :     }
     362                 :             : 
     363   [ +  -  +  -  :      197501 :     return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id, jreq.m_json_version);
                   +  - ]
     364                 :      204213 : }
     365                 :             : 
     366                 :             : /**
     367                 :             :  * Process named arguments into a vector of positional arguments, based on the
     368                 :             :  * passed-in specification for the RPC call's arguments.
     369                 :             :  */
     370                 :      100690 : static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, const std::vector<std::pair<std::string, bool>>& argNames)
     371                 :             : {
     372                 :      100690 :     JSONRPCRequest out = in;
     373                 :      100690 :     out.params = UniValue(UniValue::VARR);
     374                 :             :     // Build a map of parameters, and remove ones that have been processed, so that we can throw a focused error if
     375                 :             :     // there is an unknown one.
     376         [ +  - ]:      100690 :     const std::vector<std::string>& keys = in.params.getKeys();
     377         [ +  - ]:      100690 :     const std::vector<UniValue>& values = in.params.getValues();
     378                 :      100690 :     std::unordered_map<std::string, const UniValue*> argsIn;
     379   [ -  +  +  + ]:      170137 :     for (size_t i=0; i<keys.size(); ++i) {
     380   [ +  -  +  + ]:       69449 :         auto [_, inserted] = argsIn.emplace(keys[i], &values[i]);
     381         [ +  + ]:       69449 :         if (!inserted) {
     382   [ +  -  +  - ]:           6 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + keys[i] + " specified multiple times");
     383                 :             :         }
     384                 :             :     }
     385                 :             :     // Process expected parameters. If any parameters were left unspecified in
     386                 :             :     // the request before a parameter that was specified, null values need to be
     387                 :             :     // inserted at the unspecified parameter positions, and the "hole" variable
     388                 :             :     // below tracks the number of null values that need to be inserted.
     389                 :             :     // The "initial_hole_size" variable stores the size of the initial hole,
     390                 :             :     // i.e. how many initial positional arguments were left unspecified. This is
     391                 :             :     // used after the for-loop to add initial positional arguments from the
     392                 :             :     // "args" parameter, if present.
     393                 :      100688 :     int hole = 0;
     394                 :      100688 :     int initial_hole_size = 0;
     395                 :      100688 :     const std::string* initial_param = nullptr;
     396                 :      100688 :     UniValue options{UniValue::VOBJ};
     397   [ -  +  +  + ]:      239816 :     for (const auto& [argNamePattern, named_only]: argNames) {
     398   [ -  +  +  - ]:      139129 :         std::vector<std::string> vargNames = SplitString(argNamePattern, '|');
     399                 :      139129 :         auto fr = argsIn.end();
     400         [ +  + ]:      212796 :         for (const std::string & argName : vargNames) {
     401                 :      142439 :             fr = argsIn.find(argName);
     402         [ +  + ]:      142439 :             if (fr != argsIn.end()) {
     403                 :             :                 break;
     404                 :             :             }
     405                 :             :         }
     406                 :             : 
     407                 :             :         // Handle named-only parameters by pushing them into a temporary options
     408                 :             :         // object, and then pushing the accumulated options as the next
     409                 :             :         // positional argument.
     410         [ +  + ]:      139129 :         if (named_only) {
     411         [ +  + ]:        9458 :             if (fr != argsIn.end()) {
     412   [ +  -  -  + ]:         377 :                 if (options.exists(fr->first)) {
     413   [ #  #  #  # ]:           0 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + fr->first + " specified multiple times");
     414                 :             :                 }
     415   [ +  -  -  +  :        1131 :                 options.pushKVEnd(fr->first, *fr->second);
                   +  - ]
     416                 :         377 :                 argsIn.erase(fr);
     417                 :             :             }
     418                 :        9458 :             continue;
     419                 :             :         }
     420                 :             : 
     421   [ -  +  +  +  :      129671 :         if (!options.empty() || fr != argsIn.end()) {
                   +  + ]
     422         [ +  + ]:       79506 :             for (int i = 0; i < hole; ++i) {
     423                 :             :                 // Fill hole between specified parameters with JSON nulls,
     424                 :             :                 // but not at the end (for backwards compatibility with calls
     425                 :             :                 // that act based on number of specified parameters).
     426         [ +  - ]:       10854 :                 out.params.push_back(UniValue());
     427                 :             :             }
     428                 :       68652 :             hole = 0;
     429         [ +  + ]:       68652 :             if (!initial_param) initial_param = &argNamePattern;
     430                 :             :         } else {
     431                 :       61019 :             hole += 1;
     432   [ -  +  +  + ]:       61019 :             if (out.params.empty()) initial_hole_size = hole;
     433                 :             :         }
     434                 :             : 
     435                 :             :         // If named input parameter "fr" is present, push it onto out.params. If
     436                 :             :         // options are present, push them onto out.params. If both are present,
     437                 :             :         // throw an error.
     438         [ +  + ]:      129671 :         if (fr != argsIn.end()) {
     439   [ -  +  +  + ]:       68395 :             if (!options.empty()) {
     440   [ +  -  +  -  :           3 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + fr->first + " conflicts with parameter " + options.getKeys().front());
             +  -  +  - ]
     441                 :             :             }
     442   [ +  -  +  - ]:       68394 :             out.params.push_back(*fr->second);
     443                 :       68394 :             argsIn.erase(fr);
     444                 :             :         }
     445   [ -  +  +  + ]:      129670 :         if (!options.empty()) {
     446         [ +  - ]:         257 :             out.params.push_back(std::move(options));
     447                 :         257 :             options = UniValue{UniValue::VOBJ};
     448                 :             :         }
     449                 :      139129 :     }
     450                 :             :     // If leftover "args" param was found, use it as a source of positional
     451                 :             :     // arguments and add named arguments after. This is a convenience for
     452                 :             :     // clients that want to pass a combination of named and positional
     453                 :             :     // arguments as described in doc/JSON-RPC-interface.md#parameter-passing
     454         [ +  - ]:      201374 :     auto positional_args{argsIn.extract("args")};
     455   [ +  +  +  - ]:      100687 :     if (positional_args && positional_args.mapped()->isArray()) {
     456   [ -  +  +  +  :         668 :         if (initial_hole_size < (int)positional_args.mapped()->size() && initial_param) {
                   +  + ]
     457   [ +  -  +  - ]:          18 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + *initial_param + " specified twice both as positional and named argument");
     458                 :             :         }
     459                 :             :         // Assign positional_args to out.params and append named_args after.
     460                 :         662 :         UniValue named_args{std::move(out.params)};
     461         [ +  - ]:         662 :         out.params = *positional_args.mapped();
     462   [ -  +  -  +  :        1390 :         for (size_t i{out.params.size()}; i < named_args.size(); ++i) {
                   +  + ]
     463   [ +  -  +  -  :         728 :             out.params.push_back(named_args[i]);
                   +  - ]
     464                 :             :         }
     465                 :         662 :     }
     466                 :             :     // If there are still arguments in the argsIn map, this is an error.
     467         [ +  + ]:      100681 :     if (!argsIn.empty()) {
     468   [ +  -  +  - ]:          10 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown named parameter " + argsIn.begin()->first);
     469                 :             :     }
     470                 :             :     // Return request with named arguments transformed to positional arguments
     471         [ +  + ]:      100676 :     return out;
     472                 :      100702 : }
     473                 :             : 
     474                 :      203428 : static bool ExecuteCommands(const std::vector<const CRPCCommand*>& commands, const JSONRPCRequest& request, UniValue& result)
     475                 :             : {
     476         [ +  - ]:      203428 :     for (const auto& command : commands) {
     477         [ -  + ]:      203428 :         if (ExecuteCommand(*command, request, result, &command == &commands.back())) {
     478                 :             :             return true;
     479                 :             :         }
     480                 :             :     }
     481                 :             :     return false;
     482                 :             : }
     483                 :             : 
     484                 :      204280 : UniValue CRPCTable::execute(const JSONRPCRequest &request) const
     485                 :             : {
     486                 :             :     // Return immediately if in warmup
     487                 :      204280 :     {
     488                 :      204280 :         LOCK(g_rpc_warmup_mutex);
     489         [ +  + ]:      204280 :         if (fRPCInWarmup)
     490         [ +  - ]:        1008 :             throw JSONRPCError(RPC_IN_WARMUP, rpcWarmupStatus);
     491                 :        1008 :     }
     492                 :             : 
     493                 :             :     // Find method
     494                 :      203272 :     auto it = mapCommands.find(request.strMethod);
     495         [ +  + ]:      203272 :     if (it != mapCommands.end()) {
     496         [ +  + ]:      203261 :         UniValue result;
     497   [ +  +  -  + ]:      203261 :         if (ExecuteCommands(it->second, request, result)) {
     498                 :      197544 :             return result;
     499                 :             :         }
     500                 :        5717 :     }
     501   [ +  -  +  - ]:          22 :     throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
     502                 :             : }
     503                 :             : 
     504                 :      203428 : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler)
     505                 :             : {
     506                 :      203428 :     try {
     507         [ +  - ]:      203428 :         RPCCommandExecution execution(request.strMethod);
     508                 :             :         // Execute, convert arguments to array if necessary
     509         [ +  + ]:      203428 :         if (request.params.isObject()) {
     510   [ +  +  +  + ]:      100690 :             return command.actor(transformNamedArguments(request, command.argNames), result, last_handler);
     511                 :             :         } else {
     512         [ +  + ]:      102738 :             return command.actor(request, result, last_handler);
     513                 :             :         }
     514      [ +  +  + ]:      209145 :     } catch (const UniValue::type_error& e) {
     515   [ +  -  +  - ]:          36 :         throw JSONRPCError(RPC_TYPE_ERROR, e.what());
     516                 :          73 :     } catch (const std::exception& e) {
     517   [ +  -  +  - ]:         110 :         throw JSONRPCError(RPC_MISC_ERROR, e.what());
     518                 :          55 :     }
     519                 :             : }
     520                 :             : 
     521                 :           0 : std::vector<std::string> CRPCTable::listCommands() const
     522                 :             : {
     523                 :           0 :     std::vector<std::string> commandList;
     524         [ #  # ]:           0 :     commandList.reserve(mapCommands.size());
     525   [ #  #  #  # ]:           0 :     for (const auto& i : mapCommands) commandList.emplace_back(i.first);
     526                 :           0 :     return commandList;
     527                 :           0 : }
     528                 :             : 
     529                 :           1 : UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const
     530                 :             : {
     531                 :           1 :     JSONRPCRequest request = args_request;
     532                 :           1 :     request.mode = JSONRPCRequest::GET_ARGS;
     533                 :             : 
     534                 :           1 :     UniValue ret{UniValue::VARR};
     535         [ +  + ]:         168 :     for (const auto& cmd : mapCommands) {
     536         [ +  - ]:         167 :         UniValue result;
     537   [ +  -  +  - ]:         167 :         if (ExecuteCommands(cmd.second, request, result)) {
     538   [ +  -  +  + ]:         601 :             for (const auto& values : result.getValues()) {
     539   [ +  -  +  - ]:         434 :                 ret.push_back(values);
     540                 :             :             }
     541                 :             :         }
     542                 :         167 :     }
     543                 :           1 :     return ret;
     544                 :           1 : }
     545                 :             : 
     546                 :             : CRPCTable tableRPC;
        

Generated by: LCOV version 2.0-1