diff --git a/divi/src/Makefile.am b/divi/src/Makefile.am index fd7bf78be..7e4f38c95 100644 --- a/divi/src/Makefile.am +++ b/divi/src/Makefile.am @@ -216,6 +216,7 @@ BITCOIN_CORE_H = \ IndexDatabaseUpdateCollector.h \ NodeState.h \ NodeStateRegistry.h \ + RPCContext.h \ BlocksInFlightRegistry.h \ NodeSignals.h \ BlockRejects.h\ @@ -436,6 +437,7 @@ libbitcoin_server_a_SOURCES = \ NodeState.cpp \ BlocksInFlightRegistry.cpp \ NodeStateRegistry.cpp \ + RPCContext.cpp \ BlockFileHelpers.cpp \ main.cpp \ TransactionSearchIndexes.cpp \ diff --git a/divi/src/RPCContext.cpp b/divi/src/RPCContext.cpp new file mode 100644 index 000000000..f5ea35be0 --- /dev/null +++ b/divi/src/RPCContext.cpp @@ -0,0 +1,10 @@ +#include "RPCContext.h" + +#include "spork.h" +#include "MasternodeModule.h" + +RPCContext::RPCContext () + : sporkManager(GetSporkManager ()), masternodeModule(GetMasternodeModule ()) +{} + +RPCContext::~RPCContext () = default; diff --git a/divi/src/RPCContext.h b/divi/src/RPCContext.h new file mode 100644 index 000000000..e4cacb551 --- /dev/null +++ b/divi/src/RPCContext.h @@ -0,0 +1,48 @@ +#ifndef RPC_CONTEXT_H +#define RPC_CONTEXT_H + +class CSporkManager; +class MasternodeModule; + +/** This class holds all the "context" (such as the chainstate, masternode + * and other references) needed to handle RPC method calls. */ +class RPCContext +{ + +private: + + CSporkManager& sporkManager; + const ::MasternodeModule& masternodeModule; + +public: + + /** Constructs a new instance based on globals. */ + RPCContext (); + + RPCContext (const RPCContext&) = delete; + void operator= (const RPCContext&) = delete; + + ~RPCContext (); + + /** Returns the global instance that should be used during RPC calls + * (called from the RPC method handler). + * + * This method is implemented in rpcserver.cpp. + */ + static RPCContext& Get (); + + CSporkManager& + SporkManager () + { + return sporkManager; + } + + const ::MasternodeModule& + MasternodeModule () const + { + return masternodeModule; + } + +}; + +#endif // RPC_CONTEXT_H diff --git a/divi/src/RpcMasternodeFeatures.cpp b/divi/src/RpcMasternodeFeatures.cpp index 00eaa06c3..2f53c86cf 100644 --- a/divi/src/RpcMasternodeFeatures.cpp +++ b/divi/src/RpcMasternodeFeatures.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -78,9 +79,9 @@ namespace /** Relays an already parsed masternode broadcast, optionally updating the * contained ping if we can (because it is our masternode). */ -MasternodeStartResult RelayParsedMasternodeBroadcast(CMasternodeBroadcast mnb, const bool updatePing) +MasternodeStartResult RelayParsedMasternodeBroadcast(const RPCContext& ctx, CMasternodeBroadcast mnb, const bool updatePing) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); auto& activeMasternode = mnModule.getActiveMasternode(); auto& mnodeman = mnModule.getMasternodeManager(); @@ -146,18 +147,18 @@ bool SignMasternodeBroadcast(const CKeyStore& keystore, std::string& hexData) return true; } -MasternodeStartResult RelayMasternodeBroadcast(const std::string& hexData, const std::string& signature, const bool updatePing) +MasternodeStartResult RelayMasternodeBroadcast(const RPCContext& ctx, const std::string& hexData, const std::string& signature, const bool updatePing) { CMasternodeBroadcast mnb = readFromHex(hexData); if(!signature.empty()) mnb.signature = ParseHex(signature); - return RelayParsedMasternodeBroadcast(mnb, updatePing); + return RelayParsedMasternodeBroadcast(ctx, mnb, updatePing); } -MasternodeStartResult StartMasternode(const CKeyStore& keyStore, const StoredMasternodeBroadcasts& stored, std::string alias, bool deferRelay) +MasternodeStartResult StartMasternode(const RPCContext& ctx, const CKeyStore& keyStore, const StoredMasternodeBroadcasts& stored, std::string alias, bool deferRelay) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); MasternodeStartResult result; for(const auto& configEntry : mnModule.getMasternodeConfigurations().getEntries()) @@ -206,16 +207,16 @@ MasternodeStartResult StartMasternode(const CKeyStore& keyStore, const StoredMas return result; } - return RelayParsedMasternodeBroadcast (mnb, updatePing); + return RelayParsedMasternodeBroadcast (ctx, mnb, updatePing); } result.status = false; result.errorMessage = "Invalid alias, couldn't find MN. Check your masternode.conf file"; return result; } -ActiveMasternodeStatus GetActiveMasternodeStatus() +ActiveMasternodeStatus GetActiveMasternodeStatus(const RPCContext& ctx) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); if (!mnModule.localNodeIsAMasternode()) throw std::runtime_error("This is not a masternode"); auto& activeMasternode = mnModule.getActiveMasternode(); auto& mnodeman = mnModule.getMasternodeManager(); @@ -276,9 +277,9 @@ unsigned FindLastPayeePaymentTime(const CBlockIndex* chainTip, const MasternodeP return 0u; } -std::vector GetMasternodeList(std::string strFilter, const CBlockIndex* chainTip) +std::vector GetMasternodeList(const RPCContext& ctx, std::string strFilter, const CBlockIndex* chainTip) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); auto& networkMessageManager = mnModule.getNetworkMessageManager(); const MasternodePaymentData& paymentData = mnModule.getMasternodePaymentData(); @@ -324,9 +325,9 @@ std::vector GetMasternodeList(std::string strFilter, const return masternodeList; } -static int StableMasternodeCount() +static int StableMasternodeCount(const RPCContext& ctx) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); auto& networkMessageManager = mnModule.getNetworkMessageManager(); AssertLockHeld(networkMessageManager.cs_process_message); int nStable_size = 0; @@ -353,9 +354,9 @@ static int StableMasternodeCount() } -static void CountNetworks(int& ipv4, int& ipv6, int& onion) +static void CountNetworks(const RPCContext& ctx, int& ipv4, int& ipv6, int& onion) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); auto& networkMessageManager = mnModule.getNetworkMessageManager(); mnModule.getMasternodeManager().Check(); @@ -393,9 +394,9 @@ static unsigned CountEnabled(const std::vector& masternodes) return count; } -MasternodeCountData GetMasternodeCounts(const CBlockIndex* chainTip) +MasternodeCountData GetMasternodeCounts(const RPCContext& ctx, const CBlockIndex* chainTip) { - const auto& mnModule = GetMasternodeModule(); + const auto& mnModule = ctx.MasternodeModule(); auto& networkMessageManager = mnModule.getNetworkMessageManager(); auto& masternodePayments = mnModule.getMasternodePayments(); @@ -405,8 +406,8 @@ MasternodeCountData GetMasternodeCounts(const CBlockIndex* chainTip) { LOCK(networkMessageManager.cs_process_message); - CountNetworks(data.ipv4, data.ipv6, data.onion); - data.stable = StableMasternodeCount(); + CountNetworks(ctx, data.ipv4, data.ipv6, data.onion); + data.stable = StableMasternodeCount(ctx); data.enabledAndActive = CountEnabled(networkMessageManager.masternodes); data.total = networkMessageManager.masternodes.size(); } diff --git a/divi/src/RpcMasternodeFeatures.h b/divi/src/RpcMasternodeFeatures.h index c60205b49..84a581fec 100644 --- a/divi/src/RpcMasternodeFeatures.h +++ b/divi/src/RpcMasternodeFeatures.h @@ -5,6 +5,7 @@ #include class CBlockIndex; class CKeyStore; +class RPCContext; class StoredMasternodeBroadcasts; struct MasternodeStartResult { @@ -55,10 +56,10 @@ struct MasternodeCountData /** Relays a broadcast given in serialised form as hex string. If the signature * is present, then it will replace the signature in the broadcast. If * updatePing is true, then the masternode ping is re-signed freshly. */ -MasternodeStartResult RelayMasternodeBroadcast(const std::string& hexData, const std::string& signature, bool updatePing); +MasternodeStartResult RelayMasternodeBroadcast(const RPCContext& ctx, const std::string& hexData, const std::string& signature, bool updatePing); bool SignMasternodeBroadcast(const CKeyStore& keystore, std::string& hexData); -MasternodeStartResult StartMasternode(const CKeyStore& keyStore, const StoredMasternodeBroadcasts& stored, std::string alias, bool deferRelay); -ActiveMasternodeStatus GetActiveMasternodeStatus(); -std::vector GetMasternodeList(std::string strFilter, const CBlockIndex* chainTip); -MasternodeCountData GetMasternodeCounts(const CBlockIndex* chainTip); +MasternodeStartResult StartMasternode(const RPCContext& ctx, const CKeyStore& keyStore, const StoredMasternodeBroadcasts& stored, std::string alias, bool deferRelay); +ActiveMasternodeStatus GetActiveMasternodeStatus(const RPCContext& ctx); +std::vector GetMasternodeList(const RPCContext& ctx, std::string strFilter, const CBlockIndex* chainTip); +MasternodeCountData GetMasternodeCounts(const RPCContext& ctx, const CBlockIndex* chainTip); #endif// RPC_MASTERNODE_FEATURES_H diff --git a/divi/src/rpcblockchain.cpp b/divi/src/rpcblockchain.cpp index 8590c4f9a..b3a498607 100644 --- a/divi/src/rpcblockchain.cpp +++ b/divi/src/rpcblockchain.cpp @@ -6,6 +6,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include "checkpoints.h" #include "main.h" #include "BlockDiskAccessor.h" @@ -27,7 +28,6 @@ #include #include #include -#include using namespace json_spirit; using namespace std; @@ -628,6 +628,7 @@ Value invalidateblock(const Array& params, bool fHelp, CWallet* pwallet) uint256 hash(strHash); CValidationState state; + auto& ctx = RPCContext::Get(); ChainstateManager::Reference chainstate; auto& blockMap = chainstate->GetBlockMap(); const auto mit = blockMap.find(hash); @@ -638,7 +639,7 @@ Value invalidateblock(const Array& params, bool fHelp, CWallet* pwallet) InvalidateBlock(*chainstate, state, pblockindex); if (state.IsValid()) { - ActivateBestChain(*chainstate, GetSporkManager(), state); + ActivateBestChain(*chainstate, ctx.SporkManager(), state); } if (!state.IsValid()) { @@ -665,6 +666,7 @@ Value reconsiderblock(const Array& params, bool fHelp, CWallet* pwallet) uint256 hash(strHash); CValidationState state; + auto& ctx = RPCContext::Get(); ChainstateManager::Reference chainstate; auto& blockMap = chainstate->GetBlockMap(); const auto mit = blockMap.find(hash); @@ -675,7 +677,7 @@ Value reconsiderblock(const Array& params, bool fHelp, CWallet* pwallet) ReconsiderBlock(*chainstate, state, pblockindex); if (state.IsValid()) { - ActivateBestChain(*chainstate, GetSporkManager(), state); + ActivateBestChain(*chainstate, ctx.SporkManager(), state); } if (!state.IsValid()) { diff --git a/divi/src/rpclottery.cpp b/divi/src/rpclottery.cpp index 7d5e414f2..d56e85be5 100644 --- a/divi/src/rpclottery.cpp +++ b/divi/src/rpclottery.cpp @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include #include