diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -495,8 +495,10 @@ : m_chain(chain), m_wallet_filenames(std::move(wallet_filenames)) {} void registerRpcs() override { + g_rpc_chain = &m_chain; RegisterWalletRPCCommands(m_chain, m_rpc_handlers); RegisterDumpRPCCommands(m_chain, m_rpc_handlers); + return; } bool verify(const CChainParams &chainParams) override { return VerifyWallets(chainParams, m_chain, m_wallet_filenames); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2816,3 +2816,5 @@ t.appendCommand(commands[vcidx].name, &commands[vcidx]); } } + +NodeContext *g_rpc_node = nullptr; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc/util.h b/src/rpc/util.h --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -20,14 +20,8 @@ class CKeyStore; class CPubKey; class CScript; -struct NodeContext; class UniValue; -//! Pointers to interfaces that need to be accessible from RPC methods. Due to -//! limitations of the RPC framework, there's currently no direct way to pass in -//! state to RPC method implementations. -extern NodeContext *g_rpc_node; - /** * Wrapper for UniValue::VType, which includes typeAny: used to denote don't * care type. diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -14,8 +14,6 @@ #include -NodeContext *g_rpc_node = nullptr; - void RPCTypeCheck(const UniValue ¶ms, const std::list &typesExpected, bool fAllowNull) { diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -95,6 +95,7 @@ // Ideally we'd move all the RPC tests to the functional testing framework // instead of unit tests, but for now we need these here. RPCServer rpcServer; + g_rpc_node = &m_node; RegisterAllRPCCommands(config, rpcServer, tableRPC); /** diff --git a/src/wallet/rpcwallet.h b/src/wallet/rpcwallet.h --- a/src/wallet/rpcwallet.h +++ b/src/wallet/rpcwallet.h @@ -24,6 +24,12 @@ class Handler; } // namespace interfaces +//! Pointer to chain interface that needs to be declared as a global to be +//! accessible loadwallet and createwallet methods. Due to limitations of the +//! RPC framework, there's currently no direct way to pass in state to RPC +//! methods without globals. +extern interfaces::Chain *g_rpc_chain; + void RegisterWalletRPCCommands( interfaces::Chain &chain, std::vector> &handlers); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3096,7 +3096,7 @@ std::string error, warning; std::shared_ptr const wallet = - LoadWallet(chainParams, *g_rpc_node->chain, location, error, warning); + LoadWallet(chainParams, *g_rpc_chain, location, error, warning); if (!wallet) { throw JSONRPCError(RPC_WALLET_ERROR, error); } @@ -3163,14 +3163,14 @@ // Wallet::Verify will check if we're trying to create a wallet with a // duplicate name. - if (!CWallet::Verify(chainParams, *g_rpc_node->chain, location, false, - error, warning)) { + if (!CWallet::Verify(chainParams, *g_rpc_chain, location, false, error, + warning)) { throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error); } std::shared_ptr const wallet = CWallet::CreateWalletFromFile( - chainParams, *g_rpc_node->chain, location, flags); + chainParams, *g_rpc_chain, location, flags); if (!wallet) { throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed."); } @@ -4804,3 +4804,5 @@ handlers.emplace_back(chain.handleRpc(commands[vcidx])); } } + +interfaces::Chain *g_rpc_chain = nullptr;