Skip to content

Commit

Permalink
[Init] Add -mnoperatorprivatekey to flag the active mn as deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Mar 26, 2021
1 parent 4c48e47 commit 95b79fb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
4 changes: 1 addition & 3 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ OperationResult CActiveDeterministicMasternodeManager::SetOperatorKey(const std:
if (strMNOperatorPrivKey.empty()) {
return errorOut("ERROR: Masternode operator priv key cannot be empty.");
}
CPubKey pubkey;
if (!CMessageSigner::GetKeysFromSecret(strMNOperatorPrivKey, info.keyOperator, pubkey)) {
if (!CMessageSigner::GetKeysFromSecret(strMNOperatorPrivKey, info.keyOperator, info.keyIDOperator)) {
return errorOut(_("Invalid mnoperatorprivatekey. Please see the documentation."));
}
info.keyIDOperator = pubkey.GetID();
return OperationResult(true);
}

Expand Down
45 changes: 31 additions & 14 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void PrepareShutdown()
pEvoNotificationInterface = nullptr;
}

if (fMasterNode) {
if (activeMasternodeManager) {
UnregisterValidationInterface(activeMasternodeManager);
}

Expand Down Expand Up @@ -573,6 +573,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-masternodeprivkey=<n>", _("Set the masternode private key"));
strUsage += HelpMessageOpt("-masternodeaddr=<n>", strprintf(_("Set external address:port to get to this masternode (example: %s)"), "128.127.106.235:51472"));
strUsage += HelpMessageOpt("-budgetvotemode=<mode>", _("Change automatic finalized budget voting behavior. mode=auto: Vote for only exact finalized budget match to my generated budget. (string, default: auto)"));
strUsage += HelpMessageOpt("-mnoperatorprivatekey=<WIF>", _("Set the masternode operator private key. Only valid with -masternode=1. When set, the masternode acts as a deterministic masternode."));

strUsage += HelpMessageGroup(_("Node relay options:"));
strUsage += HelpMessageOpt("-datacarrier", strprintf(_("Relay and mine data carrier transactions (default: %u)"), DEFAULT_ACCEPT_DATACARRIER));
Expand Down Expand Up @@ -1908,14 +1909,35 @@ bool AppInitMain()
}

if (fMasterNode) {
LogPrintf("IS MASTER NODE\n");

// init and register activeMasternodeManager
activeMasternodeManager = new CActiveDeterministicMasternodeManager();
RegisterValidationInterface(activeMasternodeManager);

auto res = initMasternode(gArgs.GetArg("-masternodeprivkey", ""), gArgs.GetArg("-masternodeaddr", ""), true);
if (!res) { return UIError(res.getError()); }
const bool isDIP3Enforced = deterministicMNManager->IsDIP3Enforced();
const std::string& mnoperatorkeyStr = gArgs.GetArg("-mnoperatorprivatekey", "");
const bool fDeterministic = !mnoperatorkeyStr.empty();
LogPrintf("IS %sMASTERNODE\n", (fDeterministic ? "DETERMINISTIC " : ""));

if (fDeterministic) {
// Check enforcement
if (!isDIP3Enforced) {
const std::string strError = "Cannot start deterministic masternode before enforcement. Remove -mnoperatorprivatekey to start as legacy masternode";
LogPrintf("-- ERROR: %s\n", strError);
return UIError(_(strError.c_str()));
}
// Create and register activeMasternodeManager
activeMasternodeManager = new CActiveDeterministicMasternodeManager();
auto res = activeMasternodeManager->SetOperatorKey(mnoperatorkeyStr);
if (!res) { return UIError(res.getError()); }
RegisterValidationInterface(activeMasternodeManager);
// Init active masternode
activeMasternodeManager->Init();
} else {
// Check enforcement
if (isDIP3Enforced) {
const std::string strError = "Legacy masternode system disabled. Use -mnoperatorprivatekey to start as deterministic masternode";
LogPrintf("-- ERROR: %s\n", strError);
return UIError(_(strError.c_str()));
}
auto res = initMasternode(gArgs.GetArg("-masternodeprivkey", ""), gArgs.GetArg("-masternodeaddr", ""), true);
if (!res) { return UIError(res.getError()); }
}
}

//get the mode of budget voting for this masternode
Expand Down Expand Up @@ -1952,11 +1974,6 @@ bool AppInitMain()
return false;
}

if (fMasterNode) {
assert(activeMasternodeManager);
activeMasternodeManager->Init();
}

// ********************************************************* Step 11: start node

if (!strErrors.str().empty())
Expand Down
10 changes: 10 additions & 0 deletions src/messagesigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRe
return true;
}

bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CKeyID& keyIDRet)
{
CPubKey pubkey;
if (!GetKeysFromSecret(strSecret, keyRet, pubkey)) {
return false;
}
keyIDRet = pubkey.GetID();
return true;
}

uint256 CMessageSigner::GetMessageHash(const std::string& strMessage)
{
CHashWriter ss(SER_GETHASH, 0);
Expand Down
1 change: 1 addition & 0 deletions src/messagesigner.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CMessageSigner
public:
/// Set the private/public key values, returns true if successful
static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet);
static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CKeyID& keyIDRet);
/// Get the hash based on the input message
static uint256 GetMessageHash(const std::string& strMessage);
/// Sign the message, returns true if successful
Expand Down

0 comments on commit 95b79fb

Please sign in to comment.