Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/command.h
- This file was added.
// Copyright (c) 2018 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#ifndef BITCOIN_RPC_COMMAND_H | |||||
#define BITCOIN_RPC_COMMAND_H | |||||
#include "rpc/commandname.h" | |||||
#include "rpc/jsonrpcrequest.h" | |||||
#include "rpc/parameter.h" | |||||
#include <boost/noncopyable.hpp> | |||||
#include <memory> | |||||
#include <string> | |||||
#include <utility> | |||||
#include <univalue.h> | |||||
/** | |||||
* Abstract class to define RPC commands. | |||||
* Subclasses of RPCCommand will be instantiated on each RPC call. | |||||
*/ | |||||
class RPCCommand : public boost::noncopyable { | |||||
private: | |||||
RPCCommandName name; | |||||
std::vector<std::unique_ptr<RPCParameterBase>> params; | |||||
/** | |||||
* Subclasses of RPCCommand store parameter values as private member | |||||
* variables. | |||||
*/ | |||||
public: | |||||
RPCCommand(const RPCCommandName &nameIn, | |||||
std::vector<std::unique_ptr<RPCParameterBase>> ¶msIn) | |||||
: name(nameIn) { | |||||
for (std::unique_ptr<RPCParameterBase> ¶mIn : paramsIn) { | |||||
params.push_back(std::move(paramIn)); | |||||
} | |||||
} | |||||
RPCCommandName GetName() const { return name; } | |||||
const std::vector<std::unique_ptr<RPCParameterBase>> & | |||||
GetParameters() const { | |||||
return params; | |||||
} | |||||
/** | |||||
* Validates incoming parameter values and saves them for future execution. | |||||
* Returns false if any of the parameters did not validate. | |||||
*/ | |||||
bool ValidateAndSaveParameters(const JSONRPCRequest &jsonRequest) { | |||||
for (std::unique_ptr<RPCParameterBase> ¶m : params) { | |||||
// TODO maybe passing in the whole jsonRequest to Validate() and | |||||
// letting it decide which param names to examine is better? | |||||
UniValue input = jsonRequest.params[param->GetName()]; | |||||
if (!param->Validate(input)) { | |||||
return false; | |||||
} else { | |||||
SaveParameterValue(*(param.get())); | |||||
} | |||||
} | |||||
return true; | |||||
} | |||||
/** | |||||
* Saves the value of a given parameter into it's respective private member | |||||
* variable. Typically, implementations will build a switch-statement | |||||
* using the parameter names as cases. | |||||
*/ | |||||
virtual void SaveParameterValue(RPCParameterBase ¶m) = 0; | |||||
/** | |||||
* Execute() defines the behavior of the command and must be defined for | |||||
* all commands. | |||||
*/ | |||||
virtual UniValue Execute(const JSONRPCRequest &jsonRequest) const = 0; | |||||
}; | |||||
#endif // BITCOIN_RPC_COMMAND_H |