diff options
author | Russell Yanofsky <russ@yanofsky.org> | 2017-07-31 11:46:13 -0400 |
---|---|---|
committer | Russell Yanofsky <russ@yanofsky.org> | 2019-03-05 10:20:00 -0400 |
commit | 4e4d9e9f85eaf9c3bec48559bd4cad3e8a9333ca (patch) | |
tree | 32ed7d798e1feaa8e1490d403f4d83760cddcd04 /src/interfaces | |
parent | 91868e6288abf9d133620b585bc6de793a11e0e3 (diff) |
Remove use of CRPCTable::appendCommand in wallet code
This commit does not change behavior.
Diffstat (limited to 'src/interfaces')
-rw-r--r-- | src/interfaces/chain.cpp | 47 | ||||
-rw-r--r-- | src/interfaces/chain.h | 5 | ||||
-rw-r--r-- | src/interfaces/wallet.cpp | 3 |
3 files changed, 53 insertions, 2 deletions
diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 8ce718cc50..f04d07d49d 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -15,12 +15,15 @@ #include <primitives/block.h> #include <primitives/transaction.h> #include <protocol.h> +#include <rpc/protocol.h> +#include <rpc/server.h> #include <sync.h> #include <threadsafety.h> #include <timedata.h> #include <txmempool.h> #include <ui_interface.h> #include <uint256.h> +#include <univalue.h> #include <util/system.h> #include <validation.h> #include <validationinterface.h> @@ -212,6 +215,45 @@ public: Chain::Notifications* m_notifications; }; +class RpcHandlerImpl : public Handler +{ +public: + RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command) + { + m_command.actor = [this](const JSONRPCRequest& request, UniValue& result, bool last_handler) { + if (!m_wrapped_command) return false; + try { + return m_wrapped_command->actor(request, result, last_handler); + } catch (const UniValue& e) { + // If this is not the last handler and a wallet not found + // exception was thrown, return false so the next handler can + // try to handle the request. Otherwise, reraise the exception. + if (!last_handler) { + const UniValue& code = e["code"]; + if (code.isNum() && code.get_int() == RPC_WALLET_NOT_FOUND) { + return false; + } + } + throw; + } + }; + ::tableRPC.appendCommand(m_command.name, &m_command); + } + + void disconnect() override final + { + if (m_wrapped_command) { + m_wrapped_command = nullptr; + ::tableRPC.removeCommand(m_command.name, &m_command); + } + } + + ~RpcHandlerImpl() override { disconnect(); } + + CRPCCommand m_command; + const CRPCCommand* m_wrapped_command; +}; + class ChainImpl : public Chain { public: @@ -310,8 +352,11 @@ public: return MakeUnique<NotificationsHandlerImpl>(*this, notifications); } void waitForNotifications() override { SyncWithValidationInterfaceQueue(); } + std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override + { + return MakeUnique<RpcHandlerImpl>(command); + } }; - } // namespace std::unique_ptr<Chain> MakeChain() { return MakeUnique<ChainImpl>(); } diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 72862617b3..0936449e22 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -16,6 +16,7 @@ class CBlock; class CFeeRate; +class CRPCCommand; class CScheduler; class CValidationState; class uint256; @@ -243,6 +244,10 @@ public: //! Wait for pending notifications to be handled. virtual void waitForNotifications() = 0; + + //! Register handler for RPC. Command is not copied, so reference + //! needs to remain valid until Handler is disconnected. + virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0; }; //! Interface to let node manage chain clients (wallets, or maybe tools for diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 7abbee0912..97eadac6a9 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -514,7 +514,7 @@ public: : m_chain(chain), m_wallet_filenames(std::move(wallet_filenames)) { } - void registerRpcs() override { return RegisterWalletRPCCommands(::tableRPC); } + void registerRpcs() override { return RegisterWalletRPCCommands(m_chain, m_rpc_handlers); } bool verify() override { return VerifyWallets(m_chain, m_wallet_filenames); } bool load() override { return LoadWallets(m_chain, m_wallet_filenames); } void start(CScheduler& scheduler) override { return StartWallets(scheduler); } @@ -524,6 +524,7 @@ public: Chain& m_chain; std::vector<std::string> m_wallet_filenames; + std::vector<std::unique_ptr<Handler>> m_rpc_handlers; }; } // namespace |