mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 21:42:47 +01:00
7ac1ee0fb4
fa3d9ce3254882c545d700990fe8e9a678f31eed rpc: Assert that RPCArg names are equal to CRPCCommand ones (rpcdump) (MarcoFalke) fa32c1d5ec25bc53bf989a8ae68e688593d2859d rpc: Assert that RPCArg names are equal to CRPCCommand ones (zmq) (MarcoFalke) faaa46dc204d6d714f71dbc6f0bf02215dba0f0f rpc: Assert that RPCArg names are equal to CRPCCommand ones (mining) (MarcoFalke) fa93bc14c7411a108dd024d391344fabf0f76369 rpc: Remove unused return type from appendCommand (MarcoFalke) Pull request description: This is split out from #18531 to just touch the RPC methods in misc. Description from the main pr: ### Motivation RPCArg names in the rpc help are currently only used for documentation. However, in the future they could be used to teach the server the named arguments. Named arguments are currently registered by the `CRPCCommand`s and duplicate the RPCArg names from the documentation. This redundancy is fragile, and has lead to errors in the past (despite having linters to catch those kind of errors). See section "bugs found" for a list of bugs that have been found as a result of the changes here. ### Changes The changes here add an assert in the `CRPCCommand` constructor that the RPCArg names are identical to the ones in the `CRPCCommand`. ### Future work > Here or follow up, makes sense to also assert type of returned UniValue? Sure, but let's not get ahead of ourselves. I am going to submit any further works as follow-ups, including: * Removing the CRPCCommand arguments, now that they are asserted to be equal and thus redundant * Removing all python regex linters on the args, now that RPCMan can be used to generate any output, including the cli.cpp table * Auto-formatting and sanity checking the RPCExamples with RPCMan * Checking passed-in json in self-check. Removing redundant checks * Checking returned json against documentation to avoid regressions or false documentation * Compile the RPC documentation at compile-time to ensure it doesn't change at runtime and is completely static ### Bugs found * The assert identified issue #18607 * The changes itself fixed bug #19250 ACKs for top commit: fjahr: tested ACK fa3d9ce3254882c545d700990fe8e9a678f31eed promag: Code review ACK fa3d9ce3254882c545d700990fe8e9a678f31eed. Tree-SHA512: 068ade4b55cc195868d53b7f9a27151d45b440857bb069e261a49d102a49a38fdba5d68868516a1d66a54a73ba34681362f934ded7349e894042bde873b75719
186 lines
6.0 KiB
C++
186 lines
6.0 KiB
C++
// Copyright (c) 2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_RPC_SERVER_H
|
|
#define BITCOIN_RPC_SERVER_H
|
|
|
|
#include <amount.h>
|
|
#include <rpc/request.h>
|
|
#include <rpc/util.h>
|
|
|
|
#include <functional>
|
|
#include <map>
|
|
#include <stdint.h>
|
|
#include <string>
|
|
|
|
#include <univalue.h>
|
|
|
|
class CRPCCommand;
|
|
|
|
namespace RPCServer
|
|
{
|
|
void OnStarted(std::function<void ()> slot);
|
|
void OnStopped(std::function<void ()> slot);
|
|
}
|
|
|
|
/** Query whether RPC is running */
|
|
bool IsRPCRunning();
|
|
|
|
/** Throw JSONRPCError if RPC is not running */
|
|
void RpcInterruptionPoint();
|
|
|
|
/**
|
|
* Set the RPC warmup status. When this is done, all RPC calls will error out
|
|
* immediately with RPC_IN_WARMUP.
|
|
*/
|
|
void SetRPCWarmupStatus(const std::string& newStatus);
|
|
/* Mark warmup as done. RPC calls will be processed from now on. */
|
|
void SetRPCWarmupFinished();
|
|
|
|
/* returns the current warmup state. */
|
|
bool RPCIsInWarmup(std::string *outStatus);
|
|
|
|
/** Opaque base class for timers returned by NewTimerFunc.
|
|
* This provides no methods at the moment, but makes sure that delete
|
|
* cleans up the whole state.
|
|
*/
|
|
class RPCTimerBase
|
|
{
|
|
public:
|
|
virtual ~RPCTimerBase() {}
|
|
};
|
|
|
|
/**
|
|
* RPC timer "driver".
|
|
*/
|
|
class RPCTimerInterface
|
|
{
|
|
public:
|
|
virtual ~RPCTimerInterface() {}
|
|
/** Implementation name */
|
|
virtual const char *Name() = 0;
|
|
/** Factory function for timers.
|
|
* RPC will call the function to create a timer that will call func in *millis* milliseconds.
|
|
* @note As the RPC mechanism is backend-neutral, it can use different implementations of timers.
|
|
* This is needed to cope with the case in which there is no HTTP server, but
|
|
* only GUI RPC console, and to break the dependency of pcserver on httprpc.
|
|
*/
|
|
virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
|
|
};
|
|
|
|
/** Set the factory function for timers */
|
|
void RPCSetTimerInterface(RPCTimerInterface *iface);
|
|
/** Set the factory function for timer, but only, if unset */
|
|
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface);
|
|
/** Unset factory function for timers */
|
|
void RPCUnsetTimerInterface(RPCTimerInterface *iface);
|
|
|
|
/**
|
|
* Run func nSeconds from now.
|
|
* Overrides previous timer <name> (if any).
|
|
*/
|
|
void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
|
|
|
|
typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
|
|
typedef RPCHelpMan (*RpcMethodFnType)();
|
|
|
|
class CRPCCommand
|
|
{
|
|
public:
|
|
//! RPC method handler reading request and assigning result. Should return
|
|
//! true if request is fully handled, false if it should be passed on to
|
|
//! subsequent handlers.
|
|
using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
|
|
|
|
//! Constructor taking Actor callback supporting multiple handlers.
|
|
CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
|
|
: category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
|
|
unique_id(unique_id)
|
|
{
|
|
}
|
|
|
|
//! Simplified constructor taking plain RpcMethodFnType function pointer.
|
|
CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector<std::string> args_in)
|
|
: CRPCCommand(
|
|
category,
|
|
fn().m_name,
|
|
[fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
|
|
fn().GetArgNames(),
|
|
intptr_t(fn))
|
|
{
|
|
CHECK_NONFATAL(fn().m_name == name_in);
|
|
CHECK_NONFATAL(fn().GetArgNames() == args_in);
|
|
}
|
|
|
|
//! Simplified constructor taking plain rpcfn_type function pointer.
|
|
CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
|
|
: CRPCCommand(category, name,
|
|
[fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
|
|
{args.begin(), args.end()}, intptr_t(fn))
|
|
{
|
|
}
|
|
|
|
std::string category;
|
|
std::string name;
|
|
Actor actor;
|
|
std::vector<std::string> argNames;
|
|
intptr_t unique_id;
|
|
};
|
|
|
|
/**
|
|
* RPC command dispatcher.
|
|
*/
|
|
class CRPCTable
|
|
{
|
|
private:
|
|
std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
|
|
std::multimap<std::string, std::vector<UniValue>> mapPlatformRestrictions;
|
|
public:
|
|
CRPCTable();
|
|
std::string help(const std::string& name, const std::string& strSubCommand, const JSONRPCRequest& helpreq) const;
|
|
|
|
void InitPlatformRestrictions();
|
|
|
|
/**
|
|
* Execute a method.
|
|
* @param request The JSONRPCRequest to execute
|
|
* @returns Result of the call.
|
|
* @throws an exception (UniValue) when an error happens.
|
|
*/
|
|
UniValue execute(const JSONRPCRequest &request) const;
|
|
|
|
/**
|
|
* Returns a list of registered commands
|
|
* @returns List of registered commands.
|
|
*/
|
|
std::vector<std::string> listCommands() const;
|
|
|
|
/**
|
|
* Appends a CRPCCommand to the dispatch table.
|
|
*
|
|
* Precondition: RPC server is not running
|
|
*
|
|
* Commands with different method names but the same unique_id will
|
|
* be considered aliases, and only the first registered method name will
|
|
* show up in the help text command listing. Aliased commands do not have
|
|
* to have the same behavior. Server and client code can distinguish
|
|
* between calls based on method name, and aliased commands can also
|
|
* register different names, types, and numbers of parameters.
|
|
*/
|
|
void appendCommand(const std::string& name, const CRPCCommand* pcmd);
|
|
bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
|
|
};
|
|
|
|
bool IsDeprecatedRPCEnabled(const std::string& method);
|
|
|
|
extern CRPCTable tableRPC;
|
|
|
|
void StartRPC();
|
|
void InterruptRPC();
|
|
void StopRPC();
|
|
std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
|
|
|
|
#endif // BITCOIN_RPC_SERVER_H
|