LCOV - code coverage report
Current view: top level - src/rpc - server.h (source / functions) Hit Total Coverage
Test: bitcoincash_test.info Lines: 6 6 100.0 %
Date: 2018-04-13 15:12:50 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2016 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             : #ifndef BITCOIN_RPCSERVER_H
       7             : #define BITCOIN_RPCSERVER_H
       8             : 
       9             : #include "amount.h"
      10             : #include "rpc/protocol.h"
      11             : #include "uint256.h"
      12             : 
      13             : #include <cstdint>
      14             : #include <functional>
      15             : #include <list>
      16             : #include <map>
      17             : #include <string>
      18             : 
      19             : #include <univalue.h>
      20             : 
      21             : static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
      22             : 
      23             : class CRPCCommand;
      24             : 
      25             : namespace RPCServer {
      26             : void OnStarted(std::function<void()> slot);
      27             : void OnStopped(std::function<void()> slot);
      28             : void OnPreCommand(std::function<void(const CRPCCommand &)> slot);
      29             : void OnPostCommand(std::function<void(const CRPCCommand &)> slot);
      30             : } // namespace RPCServer
      31             : 
      32             : class CBlockIndex;
      33             : class Config;
      34             : class CNetAddr;
      35             : 
      36             : /** Wrapper for UniValue::VType, which includes typeAny:
      37             :  * Used to denote don't care type. Only used by RPCTypeCheckObj */
      38             : struct UniValueType {
      39             :     UniValueType(UniValue::VType _type) : typeAny(false), type(_type) {}
      40             :     UniValueType() : typeAny(true) {}
      41             :     bool typeAny;
      42             :     UniValue::VType type;
      43             : };
      44             : 
      45         344 : class JSONRPCRequest {
      46             : public:
      47             :     UniValue id;
      48             :     std::string strMethod;
      49             :     UniValue params;
      50             :     bool fHelp;
      51             :     std::string URI;
      52             :     std::string authUser;
      53             : 
      54         516 :     JSONRPCRequest() {
      55          86 :         id = NullUniValue;
      56          86 :         params = NullUniValue;
      57          86 :         fHelp = false;
      58          86 :     }
      59             : 
      60             :     void parse(const UniValue &valRequest);
      61             : };
      62             : 
      63             : /** Query whether RPC is running */
      64             : bool IsRPCRunning();
      65             : 
      66             : /**
      67             :  * Set the RPC warmup status.  When this is done, all RPC calls will error out
      68             :  * immediately with RPC_IN_WARMUP.
      69             :  */
      70             : void SetRPCWarmupStatus(const std::string &newStatus);
      71             : /* Mark warmup as done.  RPC calls will be processed from now on.  */
      72             : void SetRPCWarmupFinished();
      73             : 
      74             : /* returns the current warmup state.  */
      75             : bool RPCIsInWarmup(std::string *statusOut);
      76             : 
      77             : /**
      78             :  * Type-check arguments; throws JSONRPCError if wrong type given. Does not check
      79             :  * that the right number of arguments are passed, just that any passed are the
      80             :  * correct type.
      81             :  */
      82             : void RPCTypeCheck(const UniValue &params,
      83             :                   const std::list<UniValue::VType> &typesExpected,
      84             :                   bool fAllowNull = false);
      85             : 
      86             : /**
      87             :  * Type-check one argument; throws JSONRPCError if wrong type given.
      88             :  */
      89             : void RPCTypeCheckArgument(const UniValue &value, UniValue::VType typeExpected);
      90             : 
      91             : /*
      92             :   Check for expected keys/value types in an Object.
      93             : */
      94             : void RPCTypeCheckObj(const UniValue &o,
      95             :                      const std::map<std::string, UniValueType> &typesExpected,
      96             :                      bool fAllowNull = false, bool fStrict = false);
      97             : 
      98             : /** Opaque base class for timers returned by NewTimerFunc.
      99             :  * This provides no methods at the moment, but makes sure that delete cleans up
     100             :  * the whole state.
     101             :  */
     102             : class RPCTimerBase {
     103             : public:
     104             :     virtual ~RPCTimerBase() {}
     105             : };
     106             : 
     107             : /**
     108             :  * RPC timer "driver".
     109             :  */
     110             : class RPCTimerInterface {
     111             : public:
     112             :     virtual ~RPCTimerInterface() {}
     113             :     /** Implementation name */
     114             :     virtual const char *Name() = 0;
     115             :     /** Factory function for timers.
     116             :      * RPC will call the function to create a timer that will call func in
     117             :      * *millis* milliseconds.
     118             :      * @note As the RPC mechanism is backend-neutral, it can use different
     119             :      * implementations of timers.
     120             :      * This is needed to cope with the case in which there is no HTTP server,
     121             :      * but only GUI RPC console, and to break the dependency of pcserver on
     122             :      * httprpc.
     123             :      */
     124             :     virtual RPCTimerBase *NewTimer(std::function<void(void)> &func,
     125             :                                    int64_t millis) = 0;
     126             : };
     127             : 
     128             : /** Set the factory function for timers */
     129             : void RPCSetTimerInterface(RPCTimerInterface *iface);
     130             : /** Set the factory function for timer, but only, if unset */
     131             : void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface);
     132             : /** Unset factory function for timers */
     133             : void RPCUnsetTimerInterface(RPCTimerInterface *iface);
     134             : 
     135             : /**
     136             :  * Run func nSeconds from now.
     137             :  * Overrides previous timer <name> (if any).
     138             :  */
     139             : void RPCRunLater(const std::string &name, std::function<void(void)> func,
     140             :                  int64_t nSeconds);
     141             : 
     142             : typedef UniValue (*rpcfn_type)(Config &config,
     143             :                                const JSONRPCRequest &jsonRequest);
     144             : typedef UniValue (*const_rpcfn_type)(const Config &config,
     145             :                                      const JSONRPCRequest &jsonRequest);
     146             : 
     147             : class CRPCCommand {
     148             : public:
     149             :     std::string category;
     150             :     std::string name;
     151             :     rpcfn_type actor;
     152             :     bool okSafeMode;
     153             :     std::vector<std::string> argNames;
     154             : 
     155             :     CRPCCommand(std::string _category, std::string _name, rpcfn_type _actor,
     156             :                 bool _okSafeMode, std::vector<std::string> _argNames)
     157             :         : category{std::move(_category)}, name{std::move(_name)}, actor{_actor},
     158             :           okSafeMode{_okSafeMode}, argNames{std::move(_argNames)} {}
     159             : 
     160             :     /**
     161             :      * It is safe to cast from void(const int*) to void(int*) but C++ do not
     162             :      * understand type variance. As a result, we need to do the dirty job
     163             :      * ourselves.
     164             :      */
     165             :     CRPCCommand(std::string _category, std::string _name,
     166             :                 const_rpcfn_type _actor, bool _okSafeMode,
     167             :                 std::vector<std::string> _argNames)
     168             :         : category{std::move(_category)}, name{std::move(_name)},
     169             :           actor{reinterpret_cast<rpcfn_type>(_actor)},
     170             :           okSafeMode{_okSafeMode}, argNames{std::move(_argNames)} {}
     171             : };
     172             : 
     173             : /**
     174             :  * Bitcoin RPC command dispatcher.
     175             :  */
     176             : class CRPCTable {
     177             : private:
     178             :     std::map<std::string, const CRPCCommand *> mapCommands;
     179             : 
     180             : public:
     181             :     CRPCTable();
     182             :     const CRPCCommand *operator[](const std::string &name) const;
     183             :     std::string help(Config &config, const std::string &name,
     184             :                      const JSONRPCRequest &helpreq) const;
     185             : 
     186             :     /**
     187             :      * Execute a method.
     188             :      * @param request The JSONRPCRequest to execute
     189             :      * @returns Result of the call.
     190             :      * @throws an exception (UniValue) when an error happens.
     191             :      */
     192             :     UniValue execute(Config &config, const JSONRPCRequest &request) const;
     193             : 
     194             :     /**
     195             :      * Returns a list of registered commands
     196             :      * @returns List of registered commands.
     197             :      */
     198             :     std::vector<std::string> listCommands() const;
     199             : 
     200             :     /**
     201             :      * Appends a CRPCCommand to the dispatch table.
     202             :      * Returns false if RPC server is already running (dump concurrency
     203             :      * protection).
     204             :      * Commands cannot be overwritten (returns false).
     205             :      */
     206             :     bool appendCommand(const std::string &name, const CRPCCommand *pcmd);
     207             : };
     208             : 
     209             : extern CRPCTable tableRPC;
     210             : 
     211             : /**
     212             :  * Utilities: convert hex-encoded Values
     213             :  * (throws error if not hex).
     214             :  */
     215             : extern uint256 ParseHashV(const UniValue &v, std::string strName);
     216             : extern uint256 ParseHashO(const UniValue &o, std::string strKey);
     217             : extern std::vector<uint8_t> ParseHexV(const UniValue &v, std::string strName);
     218             : extern std::vector<uint8_t> ParseHexO(const UniValue &o, std::string strKey);
     219             : 
     220             : extern Amount AmountFromValue(const UniValue &value);
     221             : extern UniValue ValueFromAmount(const Amount &amount);
     222             : extern std::string HelpExampleCli(const std::string &methodname,
     223             :                                   const std::string &args);
     224             : extern std::string HelpExampleRpc(const std::string &methodname,
     225             :                                   const std::string &args);
     226             : 
     227             : bool StartRPC();
     228             : void InterruptRPC();
     229             : void StopRPC();
     230             : std::string JSONRPCExecBatch(Config &config, const JSONRPCRequest &req,
     231             :                              const UniValue &vReq);
     232             : void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
     233             : 
     234             : // Retrieves any serialization flags requested in command line argument
     235             : int RPCSerializationFlags();
     236             : 
     237             : #endif // BITCOIN_RPCSERVER_H

Generated by: LCOV version 1.12