diff options
author | MarcoFalke <falke.marco@gmail.com> | 2019-07-09 19:31:44 -0400 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2019-07-09 19:31:52 -0400 |
commit | 357488f660a570dc97d969ae92e026854d167142 (patch) | |
tree | f37c9a7b58547f84b3d1a323ec2cc56d604cb56f /src/rpc/util.h | |
parent | 8046a3e0befeea641b6309bc0c742b7481e681d9 (diff) | |
parent | b6fb617aaaad5f9cdd7f2ad2825b253ca792055d (diff) |
Merge #16240: JSONRPCRequest-aware RPCHelpMan
b6fb617aaaad5f9cdd7f2ad2825b253ca792055d rpc: switch to using RPCHelpMan.Check() (Karl-Johan Alm)
c7a9fc234f3ce400ce78b9b434d2d210b2646c50 Make the RPCHelpMan aware of JSONRPCRequest and add Check() helper (Karl-Johan Alm)
5c5e32bbe3dfa790dd8bb421fbd6301ae10b09f5 rpc: migrate JSONRPCRequest functionality into request.cpp (Karl-Johan Alm)
0ab8ba1ac65b70f044a5e323b13d098cef33695a rpc: fix RPC help requirements for getblocktemplate (Karl-Johan Alm)
Pull request description:
Every single RPC call has a helper-section at the start, which throws a help string if the user asks for help or if the user provided too few/many arguments.
```C++
const RPCHelpMan help{...};
if (request.fHelp || !help.IsValidNumArgs(request.params.size())) {
throw std::runtime_error(help.ToString());
}
```
or (older version)
```C++
if (request.fHelp || request.params.size() < min || request.params.size() > max)
throw std::runtime_error(
RPCHelpMan{...}.ToString()
);
```
It seems like an obvious improvement, and less copy-pasting, to make `RPCHelpMan` aware of `JSONRPCRequest`, and to let it handle the checks instead. Both of the above become
```C++
RPCHelpMan{...}.Check(request);
```
which means we save roughly 3 lines per RPC command, and the `RPCHelpMan` instance is never referenced afterwards, so the approach is a tiny fraction cleaner.
This is a complete update, sans a few special case locations that had special rules. 623 lines turn into 284 (which includes the addition to `RPCHelpMan`).
ACKs for top commit:
laanwj:
code rview and lightly tested ACK b6fb617aaaad5f9cdd7f2ad2825b253ca792055d
MarcoFalke:
ACK b6fb617aaa, looked at the diff, verified move-only where applicable
Tree-SHA512: eb73f47f812512905b852e313281d1c8df803db40a6188aa39d5a7586631664db6764491152a8a96769946c796dc56d38c6e3a66ddd06ba3fb9d20050e6274e1
Diffstat (limited to 'src/rpc/util.h')
-rw-r--r-- | src/rpc/util.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/rpc/util.h b/src/rpc/util.h index ae3c3d63e0..5f5b398391 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -9,6 +9,7 @@ #include <outputtype.h> #include <pubkey.h> #include <rpc/protocol.h> +#include <rpc/request.h> #include <script/script.h> #include <script/sign.h> #include <script/standard.h> @@ -242,6 +243,15 @@ public: std::string ToString() const; /** If the supplied number of args is neither too small nor too high */ bool IsValidNumArgs(size_t num_args) const; + /** + * Check if the given request is valid according to this command or if + * the user is asking for help information, and throw help when appropriate. + */ + inline void Check(const JSONRPCRequest& request) const { + if (request.fHelp || !IsValidNumArgs(request.params.size())) { + throw std::runtime_error(ToString()); + } + } private: const std::string m_name; |