Skip to content

Commit a959f60

Browse files
codablockUdjinM6
authored andcommitted
De-duplicate "gobject vote-alias" and "gobject "vote-many" code (#2217)
Use same code for both vote modes
1 parent 075ca09 commit a959f60

File tree

1 file changed

+11
-126
lines changed

1 file changed

+11
-126
lines changed

src/rpc/governance.cpp

Lines changed: 11 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,12 @@ UniValue gobject(const JSONRPCRequest& request)
399399
return returnObj;
400400
}
401401

402-
if(strCommand == "vote-many")
402+
if(strCommand == "vote-many" || strCommand == "vote-alias")
403403
{
404-
if(request.params.size() != 4)
404+
if(strCommand == "vote-many" && request.params.size() != 4)
405405
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-many <governance-hash> [funding|valid|delete] [yes|no|abstain]'");
406+
else if(strCommand == "vote-alias" && request.params.size() != 5)
407+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-alias <governance-hash> [funding|valid|delete] [yes|no|abstain] <alias-name>'");
406408

407409
uint256 hash;
408410
std::string strVote;
@@ -411,6 +413,10 @@ UniValue gobject(const JSONRPCRequest& request)
411413
std::string strVoteSignal = request.params[2].get_str();
412414
std::string strVoteOutcome = request.params[3].get_str();
413415

416+
std::string strAlias;
417+
if (strCommand == "vote-alias") {
418+
strAlias = request.params[4].get_str();
419+
}
414420

415421
vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
416422
if(eVoteSignal == VOTE_SIGNAL_NONE) {
@@ -430,6 +436,9 @@ UniValue gobject(const JSONRPCRequest& request)
430436
UniValue resultsObj(UniValue::VOBJ);
431437

432438
for (const auto& mne : masternodeConfig.getEntries()) {
439+
// skip masternode if using "vote-alias" and it's the wrong masternode
440+
if(strCommand == "vote-alias" && strAlias != mne.getAlias()) continue;
441+
433442
std::string strError;
434443
std::vector<unsigned char> vchMasterNodeSignature;
435444
std::string strMasterNodeSignMessage;
@@ -500,130 +509,6 @@ UniValue gobject(const JSONRPCRequest& request)
500509
return returnObj;
501510
}
502511

503-
504-
// MASTERNODES CAN VOTE ON GOVERNANCE OBJECTS ON THE NETWORK FOR VARIOUS SIGNALS AND OUTCOMES
505-
if(strCommand == "vote-alias")
506-
{
507-
if(request.params.size() != 5)
508-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-alias <governance-hash> [funding|valid|delete] [yes|no|abstain] <alias-name>'");
509-
510-
uint256 hash;
511-
std::string strVote;
512-
513-
// COLLECT NEEDED PARAMETRS FROM USER
514-
515-
hash = ParseHashV(request.params[1], "Object hash");
516-
std::string strVoteSignal = request.params[2].get_str();
517-
std::string strVoteOutcome = request.params[3].get_str();
518-
std::string strAlias = request.params[4].get_str();
519-
520-
// CONVERT NAMED SIGNAL/ACTION AND CONVERT
521-
522-
vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
523-
if(eVoteSignal == VOTE_SIGNAL_NONE) {
524-
throw JSONRPCError(RPC_INVALID_PARAMETER,
525-
"Invalid vote signal. Please using one of the following: "
526-
"(funding|valid|delete|endorsed)");
527-
}
528-
529-
vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
530-
if(eVoteOutcome == VOTE_OUTCOME_NONE) {
531-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
532-
}
533-
534-
// EXECUTE VOTE FOR EACH MASTERNODE, COUNT SUCCESSES VS FAILURES
535-
536-
int nSuccessful = 0;
537-
int nFailed = 0;
538-
539-
UniValue resultsObj(UniValue::VOBJ);
540-
541-
for (const auto& mne : masternodeConfig.getEntries())
542-
{
543-
// IF WE HAVE A SPECIFIC NODE REQUESTED TO VOTE, DO THAT
544-
if(strAlias != mne.getAlias()) continue;
545-
546-
// INIT OUR NEEDED VARIABLES TO EXECUTE THE VOTE
547-
std::string strError;
548-
std::vector<unsigned char> vchMasterNodeSignature;
549-
std::string strMasterNodeSignMessage;
550-
551-
CPubKey pubKeyCollateralAddress;
552-
CKey keyCollateralAddress;
553-
CPubKey pubKeyMasternode;
554-
CKey keyMasternode;
555-
556-
// SETUP THE SIGNING KEY FROM MASTERNODE.CONF ENTRY
557-
558-
UniValue statusObj(UniValue::VOBJ);
559-
560-
if(!CMessageSigner::GetKeysFromSecret(mne.getPrivKey(), keyMasternode, pubKeyMasternode)) {
561-
nFailed++;
562-
statusObj.push_back(Pair("result", "failed"));
563-
statusObj.push_back(Pair("errorMessage", strprintf("Invalid masternode key %s.", mne.getPrivKey())));
564-
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
565-
continue;
566-
}
567-
568-
// SEARCH FOR THIS MASTERNODE ON THE NETWORK, THE NODE MUST BE ACTIVE TO VOTE
569-
570-
uint256 nTxHash;
571-
nTxHash.SetHex(mne.getTxHash());
572-
573-
int nOutputIndex = 0;
574-
if(!ParseInt32(mne.getOutputIndex(), &nOutputIndex)) {
575-
continue;
576-
}
577-
578-
COutPoint outpoint(nTxHash, nOutputIndex);
579-
580-
CMasternode mn;
581-
bool fMnFound = mnodeman.Get(outpoint, mn);
582-
583-
if(!fMnFound) {
584-
nFailed++;
585-
statusObj.push_back(Pair("result", "failed"));
586-
statusObj.push_back(Pair("errorMessage", "Masternode must be publicly available on network to vote. Masternode not found."));
587-
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
588-
continue;
589-
}
590-
591-
// CREATE NEW GOVERNANCE OBJECT VOTE WITH OUTCOME/SIGNAL
592-
593-
CGovernanceVote vote(outpoint, hash, eVoteSignal, eVoteOutcome);
594-
if(!vote.Sign(keyMasternode, pubKeyMasternode)) {
595-
nFailed++;
596-
statusObj.push_back(Pair("result", "failed"));
597-
statusObj.push_back(Pair("errorMessage", "Failure to sign."));
598-
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
599-
continue;
600-
}
601-
602-
// UPDATE LOCAL DATABASE WITH NEW OBJECT SETTINGS
603-
604-
CGovernanceException exception;
605-
if(governance.ProcessVoteAndRelay(vote, exception, *g_connman)) {
606-
nSuccessful++;
607-
statusObj.push_back(Pair("result", "success"));
608-
}
609-
else {
610-
nFailed++;
611-
statusObj.push_back(Pair("result", "failed"));
612-
statusObj.push_back(Pair("errorMessage", exception.GetMessage()));
613-
}
614-
615-
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
616-
}
617-
618-
// REPORT STATS TO THE USER
619-
620-
UniValue returnObj(UniValue::VOBJ);
621-
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", nSuccessful, nFailed)));
622-
returnObj.push_back(Pair("detail", resultsObj));
623-
624-
return returnObj;
625-
}
626-
627512
// USERS CAN QUERY THE SYSTEM FOR A LIST OF VARIOUS GOVERNANCE ITEMS
628513
if(strCommand == "list" || strCommand == "diff")
629514
{

0 commit comments

Comments
 (0)