Skip to content

Commit

Permalink
[Consensus] Prevent usage of same collateral for MN and DMN instance
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 4, 2021
1 parent f218357 commit 503e7da
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ void CActiveMasternode::ManageStatus()

LogPrint(BCLog::MASTERNODE, "CActiveMasternode::ManageStatus() - Begin\n");

// If a DMN has been registered with same collateral, disable me.
CMasternode* pmn;
pmn = mnodeman.Find(pubKeyMasternode);
if (pmn && deterministicMNManager->GetListAtChainTip().HasMNByCollateral(pmn->vin.prevout)) {
LogPrintf("%s: Disabling active legacy Masternode %s as the collateral is now registered with a DMN\n",
__func__, pmn->vin.prevout.ToString());
status = ACTIVE_MASTERNODE_NOT_CAPABLE;
notCapableReason = "Collateral registered with DMN";
return;
}

//need correct blocks to send ping
if (!Params().IsRegTestNet() && !masternodeSync.IsBlockchainSynced()) {
status = ACTIVE_MASTERNODE_SYNC_IN_PROCESS;
Expand All @@ -272,8 +283,6 @@ void CActiveMasternode::ManageStatus()
if (status == ACTIVE_MASTERNODE_SYNC_IN_PROCESS) status = ACTIVE_MASTERNODE_INITIAL;

if (status == ACTIVE_MASTERNODE_INITIAL) {
CMasternode* pmn;
pmn = mnodeman.Find(pubKeyMasternode);
if (pmn != nullptr) {
if (pmn->IsEnabled() && pmn->protocolVersion == PROTOCOL_VERSION)
EnableHotColdMasterNode(pmn->vin, pmn->addr);
Expand Down
9 changes: 9 additions & 0 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "evo/specialtx.h"
#include "guiinterface.h"
#include "masternode.h" // for MN_COLL_AMT, MasternodeCollateralMinConf
#include "masternodeman.h" // for mnodeman (!TODO: remove)
#include "script/standard.h"
#include "spork.h"
#include "sync.h"
Expand Down Expand Up @@ -652,6 +653,14 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C
dmn->collateralOutpoint = pl.collateralOutpoint.hash.IsNull() ? COutPoint(tx.GetHash(), pl.collateralOutpoint.n)
: pl.collateralOutpoint;

// if the collateral outpoint appears in the legacy masternode list, remove the old node
// !TODO: remove this when the transition to DMN is complete
CMasternode* old_mn = mnodeman.Find(dmn->collateralOutpoint);
if (old_mn) {
old_mn->SetSpent();
mnodeman.CheckAndRemove();
}

Coin coin;
if (!pl.collateralOutpoint.hash.IsNull() && (!GetUTXOCoin(pl.collateralOutpoint, coin) || coin.out.nValue != MN_COLL_AMT)) {
// should actually never get to this point as CheckProRegTx should have handled this case.
Expand Down
11 changes: 8 additions & 3 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ bool CMasternodeMan::Add(CMasternode& mn)
return false;
}

if (deterministicMNManager->GetListAtChainTip().HasMNByCollateral(mn.vin.prevout)) {
LogPrint(BCLog::MASTERNODE, "ERROR: Not Adding Masternode %s as the collateral is already registered with a DMN\n",
mn.vin.prevout.ToString());
return false;
}

LOCK(cs);

if (!mn.IsAvailableState())
Expand Down Expand Up @@ -244,7 +250,7 @@ int CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
return 0;
}

//remove inactive and outdated
//remove inactive and outdated (or replaced by DMN)
auto it = mapMasternodes.begin();
while (it != mapMasternodes.end()) {
MasternodeRef& mn = it->second;
Expand All @@ -253,8 +259,7 @@ int CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
activeState == CMasternode::MASTERNODE_VIN_SPENT ||
(forceExpiredRemoval && activeState == CMasternode::MASTERNODE_EXPIRED) ||
mn->protocolVersion < ActiveProtocol()) {
LogPrint(BCLog::MASTERNODE, "Removing inactive Masternode %s\n", it->first.ToString());

LogPrint(BCLog::MASTERNODE, "Removing inactive (legacy) Masternode %s\n", it->first.ToString());
//erase all of the broadcasts we've seen from this vin
// -- if we missed a few pings and the node was removed, this will allow is to get it back without them
// sending a brand new mnb
Expand Down

0 comments on commit 503e7da

Please sign in to comment.