Skip to content

Introduce RPCContext #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: Development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions divi/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ BITCOIN_CORE_H = \
IndexDatabaseUpdateCollector.h \
NodeState.h \
NodeStateRegistry.h \
RPCContext.h \
BlocksInFlightRegistry.h \
NodeSignals.h \
BlockRejects.h\
Expand Down Expand Up @@ -436,6 +437,7 @@ libbitcoin_server_a_SOURCES = \
NodeState.cpp \
BlocksInFlightRegistry.cpp \
NodeStateRegistry.cpp \
RPCContext.cpp \
BlockFileHelpers.cpp \
main.cpp \
TransactionSearchIndexes.cpp \
Expand Down
10 changes: 10 additions & 0 deletions divi/src/RPCContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "RPCContext.h"

#include "spork.h"
#include "MasternodeModule.h"

RPCContext::RPCContext ()
: sporkManager(GetSporkManager ()), masternodeModule(GetMasternodeModule ())
{}

RPCContext::~RPCContext () = default;
48 changes: 48 additions & 0 deletions divi/src/RPCContext.h
Original file line number Diff line number Diff line change
@@ -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
39 changes: 20 additions & 19 deletions divi/src/RpcMasternodeFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <base58address.h>
#include <MasternodeModule.h>
#include <RPCContext.h>
#include <masternode.h>
#include <keystore.h>
#include <streams.h>
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<CMasternodeBroadcast>(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())
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -276,9 +277,9 @@ unsigned FindLastPayeePaymentTime(const CBlockIndex* chainTip, const MasternodeP
return 0u;
}

std::vector<MasternodeListEntry> GetMasternodeList(std::string strFilter, const CBlockIndex* chainTip)
std::vector<MasternodeListEntry> 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();

Expand Down Expand Up @@ -324,9 +325,9 @@ std::vector<MasternodeListEntry> 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;
Expand All @@ -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();
Expand Down Expand Up @@ -393,9 +394,9 @@ static unsigned CountEnabled(const std::vector<CMasternode>& 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();

Expand All @@ -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();
}
Expand Down
11 changes: 6 additions & 5 deletions divi/src/RpcMasternodeFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
class CBlockIndex;
class CKeyStore;
class RPCContext;
class StoredMasternodeBroadcasts;
struct MasternodeStartResult
{
Expand Down Expand Up @@ -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<MasternodeListEntry> 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<MasternodeListEntry> GetMasternodeList(const RPCContext& ctx, std::string strFilter, const CBlockIndex* chainTip);
MasternodeCountData GetMasternodeCounts(const RPCContext& ctx, const CBlockIndex* chainTip);
#endif// RPC_MASTERNODE_FEATURES_H
8 changes: 5 additions & 3 deletions divi/src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <ChainstateManager.h>
#include <RPCContext.h>
#include "checkpoints.h"
#include "main.h"
#include "BlockDiskAccessor.h"
Expand All @@ -27,7 +28,6 @@
#include <JsonTxHelpers.h>
#include <init.h>
#include <JsonBlockHelpers.h>
#include <spork.h>

using namespace json_spirit;
using namespace std;
Expand Down Expand Up @@ -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);
Expand All @@ -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()) {
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down
7 changes: 4 additions & 3 deletions divi/src/rpclottery.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <chain.h>
#include <ChainstateManager.h>
#include <spork.h>
#include <LotteryWinnersCalculator.h>
#include <RPCContext.h>
#include <SuperblockSubsidyContainer.h>
#include <script/standard.h>
#include <json/json_spirit_value.h>
Expand Down Expand Up @@ -40,11 +40,12 @@ Value getlotteryblockwinners(const Array& params, bool fHelp, CWallet* pwallet)



auto& ctx = RPCContext::Get();
const auto& chainParameters = Params();
const SuperblockSubsidyContainer subsidyCointainer(chainParameters, GetSporkManager());
const SuperblockSubsidyContainer subsidyCointainer(chainParameters, ctx.SporkManager());
const ChainstateManager::Reference chainstate;
const LotteryWinnersCalculator calculator(
chainParameters.GetLotteryBlockStartBlock(), chainstate->ActiveChain(), GetSporkManager(),subsidyCointainer.superblockHeightValidator());
chainParameters.GetLotteryBlockStartBlock(), chainstate->ActiveChain(), ctx.SporkManager(),subsidyCointainer.superblockHeightValidator());
const CBlockIndex* chainTip = chainstate->ActiveChain().Tip();
if(!chainTip) throw JSONRPCError(RPC_MISC_ERROR,"Could not acquire lock on chain tip.");
int blockHeight = (params.size()>0)? std::min(params[0].get_int(),chainTip->nHeight): chainTip->nHeight;
Expand Down
Loading