diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -495,6 +495,7 @@ : 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); } 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); } @@ -3164,8 +3164,8 @@ std::string warning; WalletCreationStatus status; std::shared_ptr wallet = CreateWallet( - config.GetChainParams(), *g_rpc_node->chain, - request.params[0].get_str(), error, warning, status, passphrase, flags); + config.GetChainParams(), *g_rpc_chain, request.params[0].get_str(), + error, warning, status, passphrase, flags); if (status == WalletCreationStatus::CREATION_FAILED) { throw JSONRPCError(RPC_WALLET_ERROR, error); } else if (status == WalletCreationStatus::ENCRYPTION_FAILED) { @@ -4800,3 +4800,5 @@ handlers.emplace_back(chain.handleRpc(commands[vcidx])); } } + +interfaces::Chain *g_rpc_chain = nullptr;