From 0a519bb0aafc4a32d0590165c44f543b27dccea9 Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 11 Dec 2020 12:51:37 -0300 Subject: [PATCH 1/5] [Masternode] Cache decoded Masternode private key --- src/activemasternode.cpp | 3 ++- src/activemasternode.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 20df68b6e029..3f6d61e8d268 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -56,11 +56,12 @@ OperationResult initMasternode(const std::string& _strMasterNodePrivKey, const s CKey key; CPubKey pubkey; - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, key, pubkey)) { return errorOut(_("Invalid masternodeprivkey. Please see the documentation.")); } + activeMasternode.pubKeyMasternode = pubkey; + activeMasternode.privKeyMasternode = key; fMasterNode = true; return OperationResult(true); } diff --git a/src/activemasternode.h b/src/activemasternode.h index d05d4844d745..2f596bf6f3df 100644 --- a/src/activemasternode.h +++ b/src/activemasternode.h @@ -40,6 +40,7 @@ class CActiveMasternode // Initialized by init.cpp // Keys for the main Masternode CPubKey pubKeyMasternode; + CKey privKeyMasternode; // Initialized while registering Masternode Optional vin; From c4562021b38588f02fc2a0980f8492fe1310d216 Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 11 Dec 2020 12:54:57 -0300 Subject: [PATCH 2/5] [Masternode] Stop parsing the masternode private key string on every ping. We are already caching it. --- src/activemasternode.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 3f6d61e8d268..849dcc2bf857 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -170,11 +170,8 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage) return false; } - CPubKey pubKeyMasternode; - CKey keyMasternode; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { - errorMessage = "Error upon calling GetKeysFromSecret.\n"; + if (!privKeyMasternode.IsValid() || !pubKeyMasternode.IsValid()) { + errorMessage = "Error upon masternode key.\n"; return false; } @@ -182,7 +179,7 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage) const uint256& nBlockHash = mnodeman.GetBlockHashToPing(); CMasternodePing mnp(*vin, nBlockHash); - if (!mnp.Sign(keyMasternode, pubKeyMasternode)) { + if (!mnp.Sign(privKeyMasternode, pubKeyMasternode)) { errorMessage = "Couldn't sign Masternode Ping"; return false; } From 329e6ef631195148acd90d477babcf928d95f0c9 Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 11 Dec 2020 12:58:33 -0300 Subject: [PATCH 3/5] [Masternode] Stop parsing the MN private key string on every finalized budget vote. We are already caching it. [Masternode] CMasternodePayments::ProcessBlock, stop parsing the masternode private key string. Stop parsing strMasterNodePrivKey when we already have it parsed in mnfinalbudget and mnbudgetvote --- src/activemasternode.cpp | 10 ++++++++++ src/activemasternode.h | 2 ++ src/budget/budgetmanager.cpp | 3 +-- src/masternode-payments.cpp | 7 ++----- src/rpc/budget.cpp | 9 ++++----- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 849dcc2bf857..90b40cb2aed1 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -232,3 +232,13 @@ bool CActiveMasternode::EnableHotColdMasterNode(CTxIn& newVin, CService& newServ return true; } + +bool CActiveMasternode::GetKeys(CKey& _privKeyMasternode, CPubKey& _pubKeyMasternode) +{ + if (!privKeyMasternode.IsValid() || !pubKeyMasternode.IsValid()) { + return error("Error trying to get masternode keys"); + } + _privKeyMasternode = privKeyMasternode; + _pubKeyMasternode = pubKeyMasternode; + return true; +} diff --git a/src/activemasternode.h b/src/activemasternode.h index 2f596bf6f3df..4a611ba977ca 100644 --- a/src/activemasternode.h +++ b/src/activemasternode.h @@ -56,6 +56,8 @@ class CActiveMasternode bool SendMasternodePing(std::string& errorMessage); /// Enable cold wallet mode (run a Masternode with no funds) bool EnableHotColdMasterNode(CTxIn& vin, CService& addr); + + bool GetKeys(CKey& privKeyMasternode, CPubKey& pubKeyMasternode); }; #endif diff --git a/src/budget/budgetmanager.cpp b/src/budget/budgetmanager.cpp index cae091d82d09..339ecb31e3ec 100644 --- a/src/budget/budgetmanager.cpp +++ b/src/budget/budgetmanager.cpp @@ -500,8 +500,7 @@ void CBudgetManager::VoteOnFinalizedBudgets() // Get masternode keys CPubKey pubKeyMasternode; CKey keyMasternode; - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { - LogPrintf("%s: Unable to get masternode keys\n", __func__); + if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { return; } // Sign finalized budgets diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index 8d60ef179261..894ce0e95d2c 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -680,11 +680,8 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) } } - std::string errorMessage; - CPubKey pubKeyMasternode; - CKey keyMasternode; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { + CPubKey pubKeyMasternode; CKey keyMasternode; + if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { LogPrint(BCLog::MASTERNODE,"CMasternodePayments::ProcessBlock() - Error upon calling GetKeysFromSecret.\n"); return false; } diff --git a/src/rpc/budget.cpp b/src/rpc/budget.cpp index d1ea19378ac0..d3c569c3a733 100644 --- a/src/rpc/budget.cpp +++ b/src/rpc/budget.cpp @@ -273,7 +273,7 @@ UniValue mnbudgetvote(const JSONRPCRequest& request) UniValue statusObj(UniValue::VOBJ); while (true) { - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) { + if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { failed++; statusObj.pushKV("node", "local"); statusObj.pushKV("result", "failed"); @@ -816,11 +816,10 @@ UniValue mnfinalbudget(const JSONRPCRequest& request) std::string strHash = request.params[1].get_str(); uint256 hash(uint256S(strHash)); - CPubKey pubKeyMasternode; - CKey keyMasternode; - - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) + CPubKey pubKeyMasternode; CKey keyMasternode; + if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { return "Error upon calling GetKeysFromSecret"; + } CMasternode* pmn = mnodeman.Find(activeMasternode.vin->prevout); if (pmn == NULL) { From 9e18f86ec8902c24f772d338c697b7b0991388ae Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 11 Dec 2020 13:06:16 -0300 Subject: [PATCH 4/5] [Masternode] Clean now unused global strMasterNodePrivKey. --- src/activemasternode.cpp | 3 +-- src/masternodeman.h | 1 - src/util.cpp | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 90b40cb2aed1..887268d65aca 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -33,7 +33,6 @@ OperationResult initMasternode(const std::string& _strMasterNodePrivKey, const s // Global params set strMasterNodeAddr = _strMasterNodeAddr; - strMasterNodePrivKey = _strMasterNodePrivKey; // Address parsing. const CChainParams& params = Params(); @@ -56,7 +55,7 @@ OperationResult initMasternode(const std::string& _strMasterNodePrivKey, const s CKey key; CPubKey pubkey; - if (!CMessageSigner::GetKeysFromSecret(strMasterNodePrivKey, key, pubkey)) { + if (!CMessageSigner::GetKeysFromSecret(_strMasterNodePrivKey, key, pubkey)) { return errorOut(_("Invalid masternodeprivkey. Please see the documentation.")); } diff --git a/src/masternodeman.h b/src/masternodeman.h index 9030d2d23865..62b9da2cd0ef 100644 --- a/src/masternodeman.h +++ b/src/masternodeman.h @@ -26,7 +26,6 @@ class CActiveMasternode; extern CMasternodeMan mnodeman; extern CActiveMasternode activeMasternode; -extern std::string strMasterNodePrivKey; void DumpMasternodes(); diff --git a/src/util.cpp b/src/util.cpp index 6ed16bbafeae..0ef573ca4913 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -90,7 +90,6 @@ const char * const PIVX_MASTERNODE_CONF_FILENAME = "masternode.conf"; // PIVX only features // Masternode std::atomic fMasterNode{false}; -std::string strMasterNodePrivKey = ""; std::string strMasterNodeAddr = ""; bool fLiteMode = false; From 89cc58301c9077190298fe6cc0b3c780478a6b99 Mon Sep 17 00:00:00 2001 From: furszy Date: Sun, 13 Dec 2020 20:44:56 -0300 Subject: [PATCH 5/5] [Masternde] Refactor activeMasternode.GetKeys callers to not expect a bool. --- src/activemasternode.cpp | 5 ++--- src/activemasternode.h | 2 +- src/budget/budgetmanager.cpp | 5 ++--- src/masternode-payments.cpp | 5 +---- src/rpc/budget.cpp | 14 +++----------- 5 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 887268d65aca..b21271707be1 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -232,12 +232,11 @@ bool CActiveMasternode::EnableHotColdMasterNode(CTxIn& newVin, CService& newServ return true; } -bool CActiveMasternode::GetKeys(CKey& _privKeyMasternode, CPubKey& _pubKeyMasternode) +void CActiveMasternode::GetKeys(CKey& _privKeyMasternode, CPubKey& _pubKeyMasternode) { if (!privKeyMasternode.IsValid() || !pubKeyMasternode.IsValid()) { - return error("Error trying to get masternode keys"); + throw std::runtime_error("Error trying to get masternode keys"); } _privKeyMasternode = privKeyMasternode; _pubKeyMasternode = pubKeyMasternode; - return true; } diff --git a/src/activemasternode.h b/src/activemasternode.h index 4a611ba977ca..cfa4351d3756 100644 --- a/src/activemasternode.h +++ b/src/activemasternode.h @@ -57,7 +57,7 @@ class CActiveMasternode /// Enable cold wallet mode (run a Masternode with no funds) bool EnableHotColdMasterNode(CTxIn& vin, CService& addr); - bool GetKeys(CKey& privKeyMasternode, CPubKey& pubKeyMasternode); + void GetKeys(CKey& privKeyMasternode, CPubKey& pubKeyMasternode); }; #endif diff --git a/src/budget/budgetmanager.cpp b/src/budget/budgetmanager.cpp index 339ecb31e3ec..d04bb2e29ed7 100644 --- a/src/budget/budgetmanager.cpp +++ b/src/budget/budgetmanager.cpp @@ -500,9 +500,8 @@ void CBudgetManager::VoteOnFinalizedBudgets() // Get masternode keys CPubKey pubKeyMasternode; CKey keyMasternode; - if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { - return; - } + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); + // Sign finalized budgets for (const uint256& budgetHash: vBudgetHashes) { CFinalizedBudgetVote vote(*(activeMasternode.vin), budgetHash); diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index 894ce0e95d2c..5c283ece3c80 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -681,10 +681,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) } CPubKey pubKeyMasternode; CKey keyMasternode; - if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { - LogPrint(BCLog::MASTERNODE,"CMasternodePayments::ProcessBlock() - Error upon calling GetKeysFromSecret.\n"); - return false; - } + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); LogPrint(BCLog::MASTERNODE,"CMasternodePayments::ProcessBlock() - Signing Winner\n"); if (newWinner.Sign(keyMasternode, pubKeyMasternode)) { diff --git a/src/rpc/budget.cpp b/src/rpc/budget.cpp index d3c569c3a733..9eebc048e571 100644 --- a/src/rpc/budget.cpp +++ b/src/rpc/budget.cpp @@ -273,14 +273,8 @@ UniValue mnbudgetvote(const JSONRPCRequest& request) UniValue statusObj(UniValue::VOBJ); while (true) { - if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { - failed++; - statusObj.pushKV("node", "local"); - statusObj.pushKV("result", "failed"); - statusObj.pushKV("error", "Masternode signing error, GetKeysFromSecret failed."); - resultsObj.push_back(statusObj); - break; - } + // Get MN keys + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); CMasternode* pmn = mnodeman.Find(activeMasternode.vin->prevout); if (pmn == NULL) { @@ -817,9 +811,7 @@ UniValue mnfinalbudget(const JSONRPCRequest& request) uint256 hash(uint256S(strHash)); CPubKey pubKeyMasternode; CKey keyMasternode; - if (!activeMasternode.GetKeys(keyMasternode, pubKeyMasternode)) { - return "Error upon calling GetKeysFromSecret"; - } + activeMasternode.GetKeys(keyMasternode, pubKeyMasternode); CMasternode* pmn = mnodeman.Find(activeMasternode.vin->prevout); if (pmn == NULL) {