Skip to content

Commit 503e7da

Browse files
committed
[Consensus] Prevent usage of same collateral for MN and DMN instance
1 parent f218357 commit 503e7da

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/activemasternode.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ void CActiveMasternode::ManageStatus()
262262

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

265+
// If a DMN has been registered with same collateral, disable me.
266+
CMasternode* pmn;
267+
pmn = mnodeman.Find(pubKeyMasternode);
268+
if (pmn && deterministicMNManager->GetListAtChainTip().HasMNByCollateral(pmn->vin.prevout)) {
269+
LogPrintf("%s: Disabling active legacy Masternode %s as the collateral is now registered with a DMN\n",
270+
__func__, pmn->vin.prevout.ToString());
271+
status = ACTIVE_MASTERNODE_NOT_CAPABLE;
272+
notCapableReason = "Collateral registered with DMN";
273+
return;
274+
}
275+
265276
//need correct blocks to send ping
266277
if (!Params().IsRegTestNet() && !masternodeSync.IsBlockchainSynced()) {
267278
status = ACTIVE_MASTERNODE_SYNC_IN_PROCESS;
@@ -272,8 +283,6 @@ void CActiveMasternode::ManageStatus()
272283
if (status == ACTIVE_MASTERNODE_SYNC_IN_PROCESS) status = ACTIVE_MASTERNODE_INITIAL;
273284

274285
if (status == ACTIVE_MASTERNODE_INITIAL) {
275-
CMasternode* pmn;
276-
pmn = mnodeman.Find(pubKeyMasternode);
277286
if (pmn != nullptr) {
278287
if (pmn->IsEnabled() && pmn->protocolVersion == PROTOCOL_VERSION)
279288
EnableHotColdMasterNode(pmn->vin, pmn->addr);

src/evo/deterministicmns.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "evo/specialtx.h"
1212
#include "guiinterface.h"
1313
#include "masternode.h" // for MN_COLL_AMT, MasternodeCollateralMinConf
14+
#include "masternodeman.h" // for mnodeman (!TODO: remove)
1415
#include "script/standard.h"
1516
#include "spork.h"
1617
#include "sync.h"
@@ -652,6 +653,14 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C
652653
dmn->collateralOutpoint = pl.collateralOutpoint.hash.IsNull() ? COutPoint(tx.GetHash(), pl.collateralOutpoint.n)
653654
: pl.collateralOutpoint;
654655

656+
// if the collateral outpoint appears in the legacy masternode list, remove the old node
657+
// !TODO: remove this when the transition to DMN is complete
658+
CMasternode* old_mn = mnodeman.Find(dmn->collateralOutpoint);
659+
if (old_mn) {
660+
old_mn->SetSpent();
661+
mnodeman.CheckAndRemove();
662+
}
663+
655664
Coin coin;
656665
if (!pl.collateralOutpoint.hash.IsNull() && (!GetUTXOCoin(pl.collateralOutpoint, coin) || coin.out.nValue != MN_COLL_AMT)) {
657666
// should actually never get to this point as CheckProRegTx should have handled this case.

src/masternodeman.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ bool CMasternodeMan::Add(CMasternode& mn)
196196
return false;
197197
}
198198

199+
if (deterministicMNManager->GetListAtChainTip().HasMNByCollateral(mn.vin.prevout)) {
200+
LogPrint(BCLog::MASTERNODE, "ERROR: Not Adding Masternode %s as the collateral is already registered with a DMN\n",
201+
mn.vin.prevout.ToString());
202+
return false;
203+
}
204+
199205
LOCK(cs);
200206

201207
if (!mn.IsAvailableState())
@@ -244,7 +250,7 @@ int CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
244250
return 0;
245251
}
246252

247-
//remove inactive and outdated
253+
//remove inactive and outdated (or replaced by DMN)
248254
auto it = mapMasternodes.begin();
249255
while (it != mapMasternodes.end()) {
250256
MasternodeRef& mn = it->second;
@@ -253,8 +259,7 @@ int CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
253259
activeState == CMasternode::MASTERNODE_VIN_SPENT ||
254260
(forceExpiredRemoval && activeState == CMasternode::MASTERNODE_EXPIRED) ||
255261
mn->protocolVersion < ActiveProtocol()) {
256-
LogPrint(BCLog::MASTERNODE, "Removing inactive Masternode %s\n", it->first.ToString());
257-
262+
LogPrint(BCLog::MASTERNODE, "Removing inactive (legacy) Masternode %s\n", it->first.ToString());
258263
//erase all of the broadcasts we've seen from this vin
259264
// -- if we missed a few pings and the node was removed, this will allow is to get it back without them
260265
// sending a brand new mnb

0 commit comments

Comments
 (0)