Skip to content

Commit 8a07010

Browse files
committed
Abstract out masternode initialization process.
1 parent d98378e commit 8a07010

File tree

6 files changed

+72
-42
lines changed

6 files changed

+72
-42
lines changed

src/activemasternode.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,56 @@
1414
#include "netbase.h"
1515
#include "protocol.h"
1616

17+
void initMasternode(const std::string& _strMasterNodePrivKey, const std::string& _strMasterNodeAddr, bool isFromInit)
18+
{
19+
if (!isFromInit && fMasterNode) {
20+
throw std::runtime_error("ERROR: Masternode already initialized.\n");
21+
}
22+
23+
LOCK(cs_main); // Lock cs_main so the node doesn't perform any action while we setup the Masternode
24+
LogPrintf("Initializing masternode, addr %s..\n", _strMasterNodeAddr.c_str());
25+
26+
if (_strMasterNodePrivKey.empty()) {
27+
throw std::runtime_error("ERROR: Masternode priv key cannot be empty.\n");
28+
}
29+
30+
if (_strMasterNodeAddr.empty()) {
31+
throw std::runtime_error("ERROR: Empty masternodeaddr\n");
32+
}
33+
34+
// Global params set
35+
strMasterNodeAddr = _strMasterNodeAddr;
36+
strMasterNodePrivKey = _strMasterNodePrivKey;
37+
38+
// Address parsing.
39+
const CChainParams& params = Params();
40+
int nPort;
41+
int nDefaultPort = params.GetDefaultPort();
42+
std::string strHost;
43+
SplitHostPort(strMasterNodeAddr, nPort, strHost);
44+
45+
// Allow for the port number to be omitted here and just double check
46+
// that if a port is supplied, it matches the required default port.
47+
if (nPort == 0) nPort = nDefaultPort;
48+
if (nPort != nDefaultPort && !params.IsRegTestNet()) {
49+
throw std::runtime_error(strprintf(_("Invalid -masternodeaddr port %d, only %d is supported on %s-net."),
50+
nPort, nDefaultPort, Params().NetworkIDString()));
51+
}
52+
CService addrTest(LookupNumeric(strHost.c_str(), nPort));
53+
if (!addrTest.IsValid()) {
54+
throw std::runtime_error(strprintf(_("Invalid -masternodeaddr address: %s"), strMasterNodeAddr));
55+
}
56+
57+
CKey key;
58+
CPubKey pubkey;
59+
60+
if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, key, pubkey)) {
61+
throw std::runtime_error(_("Invalid masternodeprivkey. Please see documenation."));
62+
}
63+
activeMasternode.pubKeyMasternode = pubkey;
64+
fMasterNode = true;
65+
}
66+
1767
//
1868
// Bootup the Masternode, look for a 10000 PIVX input and register on the network
1969
//

src/activemasternode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#define ACTIVE_MASTERNODE_NOT_CAPABLE 3
1919
#define ACTIVE_MASTERNODE_STARTED 4
2020

21+
// Responsible for initializing the masternode
22+
void initMasternode(const std::string& strMasterNodePrivKey, const std::string& strMasterNodeAddr, bool isFromInit);
23+
2124
// Responsible for activating the Masternode and pinging the network
2225
class CActiveMasternode
2326
{

src/init.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,46 +1809,7 @@ bool AppInit2()
18091809

18101810
if (fMasterNode) {
18111811
LogPrintf("IS MASTER NODE\n");
1812-
strMasterNodeAddr = gArgs.GetArg("-masternodeaddr", "");
1813-
1814-
LogPrintf(" addr %s\n", strMasterNodeAddr.c_str());
1815-
1816-
if (!strMasterNodeAddr.empty()) {
1817-
const CChainParams& params = Params();
1818-
int nPort;
1819-
int nDefaultPort = params.GetDefaultPort();
1820-
std::string strHost;
1821-
SplitHostPort(strMasterNodeAddr, nPort, strHost);
1822-
1823-
// Allow for the port number to be omitted here and just double check
1824-
// that if a port is supplied, it matches the required default port.
1825-
if (nPort == 0) nPort = nDefaultPort;
1826-
if (nPort != nDefaultPort && !params.IsRegTestNet()) {
1827-
return UIError(strprintf(_("Invalid -masternodeaddr port %d, only %d is supported on %s-net."),
1828-
nPort, nDefaultPort, Params().NetworkIDString()));
1829-
}
1830-
CService addrTest(LookupNumeric(strHost.c_str(), nPort));
1831-
if (!addrTest.IsValid()) {
1832-
return UIError(strprintf(_("Invalid -masternodeaddr address: %s"), strMasterNodeAddr));
1833-
}
1834-
}
1835-
1836-
strMasterNodePrivKey = gArgs.GetArg("-masternodeprivkey", "");
1837-
if (!strMasterNodePrivKey.empty()) {
1838-
std::string errorMessage;
1839-
1840-
CKey key;
1841-
CPubKey pubkey;
1842-
1843-
if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, key, pubkey)) {
1844-
return UIError(_("Invalid masternodeprivkey. Please see documenation."));
1845-
}
1846-
1847-
activeMasternode.pubKeyMasternode = pubkey;
1848-
1849-
} else {
1850-
return UIError(_("You must specify a masternodeprivkey in the configuration. Please see documentation for help."));
1851-
}
1812+
initMasternode(gArgs.GetArg("-masternodeprivkey", ""), gArgs.GetArg("-masternodeaddr", ""), true);
18521813
}
18531814

18541815
//get the mode of budget voting for this masternode

src/rpc/masternode.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818

1919
#include <boost/tokenizer.hpp>
2020

21+
UniValue initmasternode(const JSONRPCRequest& request)
22+
{
23+
if (request.fHelp || (request.params.empty() || request.params.size() > 2)) {
24+
throw std::runtime_error(
25+
"initmasternode ( \"masternodePrivKey\" \"masternodeAddr\" )\n"
26+
"\nInitialize masternode on demand if it's not already initialized.\n"
27+
);
28+
}
29+
30+
std::string _strMasterNodePrivKey = request.params[0].get_str();
31+
std::string _strMasterNodeAddr = request.params[1].get_str();
32+
initMasternode(_strMasterNodePrivKey, _strMasterNodeAddr, false);
33+
return NullUniValue;
34+
}
35+
2136
UniValue getcachedblockhashes(const JSONRPCRequest& request)
2237
{
2338
if (request.fHelp || request.params.size() > 0)
@@ -923,6 +938,7 @@ static const CRPCCommand commands[] =
923938
{ "masternode", "createmasternodebroadcast", &createmasternodebroadcast, true },
924939
{ "masternode", "decodemasternodebroadcast", &decodemasternodebroadcast, true },
925940
{ "masternode", "relaymasternodebroadcast", &relaymasternodebroadcast, true },
941+
{ "masternode", "initmasternode", &initmasternode, true },
926942

927943
/* Not shown in help */
928944
{ "hidden", "getcachedblockhashes", &getcachedblockhashes, true },

src/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const char * const PIVX_MASTERNODE_CONF_FILENAME = "masternode.conf";
8989

9090
// PIVX only features
9191
// Masternode
92-
bool fMasterNode = false;
92+
std::atomic<bool> fMasterNode{false};
9393
std::string strMasterNodePrivKey = "";
9494
std::string strMasterNodeAddr = "";
9595
bool fLiteMode = false;

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern const char * const DEFAULT_DEBUGLOGFILE;
4242

4343
//PIVX only features
4444

45-
extern bool fMasterNode;
45+
extern std::atomic<bool> fMasterNode;
4646
extern bool fLiteMode;
4747
extern int64_t enforceMasternodePaymentsTime;
4848
extern std::string strMasterNodeAddr;

0 commit comments

Comments
 (0)