Skip to content

Commit

Permalink
Call CheckMnbAndUpdateMasternodeList when starting MN (dashpay#1945)
Browse files Browse the repository at this point in the history
* Call CheckMnbAndUpdateMasternodeList when when starting MN

Otherwise the invoking node won't have nCollateralMinConfBlockHash
updated correctly, resulting in the MN showing up in the wrong position
when masternode ranks are calculated.

* Remove UpdateMasternodeList and remaining uses of it
  • Loading branch information
codablock authored and andvgal committed Dec 25, 2018
1 parent cc062a1 commit 4beb5c1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
22 changes: 0 additions & 22 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1495,28 +1495,6 @@ std::string CMasternodeMan::ToString() const
return info.str();
}

void CMasternodeMan::UpdateMasternodeList(CMasternodeBroadcast mnb, CConnman& connman)
{
LOCK2(cs_main, cs);
mapSeenMasternodePing.insert(std::make_pair(mnb.lastPing.GetHash(), mnb.lastPing));
mapSeenMasternodeBroadcast.insert(std::make_pair(mnb.GetHash(), std::make_pair(GetTime(), mnb)));

LogPrintf("CMasternodeMan::UpdateMasternodeList -- masternode=%s addr=%s\n", mnb.outpoint.ToStringShort(), mnb.addr.ToString());

CMasternode* pmn = Find(mnb.outpoint);
if(pmn == NULL) {
if(Add(mnb)) {
masternodeSync.BumpAssetLastTime("CMasternodeMan::UpdateMasternodeList - new");
}
} else {
CMasternodeBroadcast mnbOld = mapSeenMasternodeBroadcast[CMasternodeBroadcast(*pmn).GetHash()].second;
if(pmn->UpdateFromNewBroadcast(mnb, connman)) {
masternodeSync.BumpAssetLastTime("CMasternodeMan::UpdateMasternodeList - seen");
mapSeenMasternodeBroadcast.erase(mnbOld.GetHash());
}
}
}

bool CMasternodeMan::CheckMnbAndUpdateMasternodeList(CNode* pfrom, CMasternodeBroadcast mnb, int& nDos, CConnman& connman)
{
// Need to lock cs_main here to ensure consistent locking order because the SimpleCheck call below locks cs_main
Expand Down
4 changes: 1 addition & 3 deletions src/masternodeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ class CMasternodeMan

std::string ToString() const;

/// Update masternode list and maps using provided CMasternodeBroadcast
void UpdateMasternodeList(CMasternodeBroadcast mnb, CConnman& connman);
/// Perform complete check and only then update list and maps
/// Perform complete check and only then update masternode list and maps using provided CMasternodeBroadcast
bool CheckMnbAndUpdateMasternodeList(CNode* pfrom, CMasternodeBroadcast mnb, int& nDos, CConnman& connman);
bool IsMnbRecoveryRequested(const uint256& hash) { return mMnbRecoveryRequests.count(hash); }

Expand Down
16 changes: 12 additions & 4 deletions src/qt/masternodelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ void MasternodeList::StartAlias(std::string strAlias)

bool fSuccess = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), strError, mnb);

int nDoS;
if (fSuccess && !mnodeman.CheckMnbAndUpdateMasternodeList(NULL, mnb, nDoS, *g_connman)) {
strError = "Failed to verify MNB";
fSuccess = false;
}

if(fSuccess) {
strStatusHtml += "<br>Successfully started masternode.";
mnodeman.UpdateMasternodeList(mnb, *g_connman);
mnb.Relay(*g_connman);
mnodeman.NotifyMasternodeUpdates(*g_connman);
} else {
strStatusHtml += "<br>Failed to start masternode.<br>Error: " + strError;
Expand Down Expand Up @@ -152,10 +156,14 @@ void MasternodeList::StartAll(std::string strCommand)

bool fSuccess = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), strError, mnb);

int nDoS;
if (fSuccess && !mnodeman.CheckMnbAndUpdateMasternodeList(NULL, mnb, nDoS, *g_connman)) {
strError = "Failed to verify MNB";
fSuccess = false;
}

if(fSuccess) {
nCountSuccessful++;
mnodeman.UpdateMasternodeList(mnb, *g_connman);
mnb.Relay(*g_connman);
mnodeman.NotifyMasternodeUpdates(*g_connman);
} else {
nCountFailed++;
Expand Down
33 changes: 16 additions & 17 deletions src/rpc/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,14 @@ UniValue masternode(const JSONRPCRequest& request)

bool fResult = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), strError, mnb);

int nDoS;
if (fResult && !mnodeman.CheckMnbAndUpdateMasternodeList(NULL, mnb, nDoS, *g_connman)) {
strError = "Failed to verify MNB";
fResult = false;
}

statusObj.push_back(Pair("result", fResult ? "successful" : "failed"));
if(fResult) {
mnodeman.UpdateMasternodeList(mnb, *g_connman);
mnb.Relay(*g_connman);
} else {
if(!fResult) {
statusObj.push_back(Pair("errorMessage", strError));
}
mnodeman.NotifyMasternodeUpdates(*g_connman);
Expand Down Expand Up @@ -343,14 +346,18 @@ UniValue masternode(const JSONRPCRequest& request)

bool fResult = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), strError, mnb);

int nDoS;
if (fResult && !mnodeman.CheckMnbAndUpdateMasternodeList(NULL, mnb, nDoS, *g_connman)) {
strError = "Failed to verify MNB";
fResult = false;
}

UniValue statusObj(UniValue::VOBJ);
statusObj.push_back(Pair("alias", mne.getAlias()));
statusObj.push_back(Pair("result", fResult ? "successful" : "failed"));

if (fResult) {
nSuccessful++;
mnodeman.UpdateMasternodeList(mnb, *g_connman);
mnb.Relay(*g_connman);
} else {
nFailed++;
statusObj.push_back(Pair("errorMessage", strError));
Expand Down Expand Up @@ -809,10 +816,9 @@ UniValue masternodebroadcast(const JSONRPCRequest& request)
if (strCommand == "relay")
{
if (request.params.size() < 2 || request.params.size() > 3)
throw JSONRPCError(RPC_INVALID_PARAMETER, "masternodebroadcast relay \"hexstring\" ( fast )\n"
throw JSONRPCError(RPC_INVALID_PARAMETER, "masternodebroadcast relay \"hexstring\"\n"
"\nArguments:\n"
"1. \"hex\" (string, required) Broadcast messages hex string\n"
"2. fast (string, optional) If none, using safe method\n");
"1. \"hex\" (string, required) Broadcast messages hex string\n");

std::vector<CMasternodeBroadcast> vecMnb;

Expand All @@ -821,7 +827,6 @@ UniValue masternodebroadcast(const JSONRPCRequest& request)

int nSuccessful = 0;
int nFailed = 0;
bool fSafe = request.params.size() == 2;
UniValue returnObj(UniValue::VOBJ);

// verify all signatures first, bailout if any of them broken
Expand All @@ -834,13 +839,7 @@ UniValue masternodebroadcast(const JSONRPCRequest& request)
int nDos = 0;
bool fResult;
if (mnb.CheckSignature(nDos)) {
if (fSafe) {
fResult = mnodeman.CheckMnbAndUpdateMasternodeList(NULL, mnb, nDos, *g_connman);
} else {
mnodeman.UpdateMasternodeList(mnb, *g_connman);
mnb.Relay(*g_connman);
fResult = true;
}
fResult = mnodeman.CheckMnbAndUpdateMasternodeList(NULL, mnb, nDos, *g_connman);
mnodeman.NotifyMasternodeUpdates(*g_connman);
} else fResult = false;

Expand Down

0 comments on commit 4beb5c1

Please sign in to comment.