Skip to content

Commit eb39e04

Browse files
committed
Allow configuration of reward address.
Allow setting a non-default reward script for masternodes, by editing the masternode.conf file explicitly. The RPC setupmasternode will not generate such configuration lines for now, and also "old" masternode.conf files will remain valid (with the default script).
1 parent 34e92f9 commit eb39e04

File tree

6 files changed

+124
-84
lines changed

6 files changed

+124
-84
lines changed

divi/src/masternode.cpp

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ CMasternode& CMasternode::operator=(CMasternode from)
212212
}
213213

214214
CScript CMasternode::GetDefaultRewardScript() const
215+
{
216+
return GetDefaultRewardScript(pubKeyCollateralAddress);
217+
}
218+
219+
CScript CMasternode::GetDefaultRewardScript(const CPubKey& pubKeyCollateralAddress)
215220
{
216221
return GetScriptForDestination(pubKeyCollateralAddress.GetID());
217222
}
@@ -496,12 +501,15 @@ bool CMasternode::IsValidNetAddr() const
496501
(IsReachable(addr) && addr.IsRoutable());
497502
}
498503

499-
CMasternodeBroadcast::CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, const MasternodeTier nMasternodeTier, int protocolVersionIn)
504+
CMasternodeBroadcast::CMasternodeBroadcast(
505+
const CService& newAddr, const CTxIn& newVin,
506+
const CPubKey& pubKeyCollateralAddressNew, const CScript& rewardScriptIn, const CPubKey& pubKeyMasternodeNew,
507+
const MasternodeTier nMasternodeTier, const int protocolVersionIn)
500508
{
501509
vin = newVin;
502510
addr = newAddr;
503511
pubKeyCollateralAddress = pubKeyCollateralAddressNew;
504-
rewardScript = GetDefaultRewardScript();
512+
rewardScript = rewardScriptIn;
505513
pubKeyMasternode = pubKeyMasternodeNew;
506514
protocolVersion = protocolVersionIn;
507515
nTier = nMasternodeTier;
@@ -511,8 +519,10 @@ CMasternodeBroadcast::CMasternodeBroadcast(const CMasternode& mn)
511519
: CMasternode(mn)
512520
{}
513521

522+
namespace
523+
{
514524

515-
bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet, bool fOffline)
525+
bool checkBlockchainSync(std::string& strErrorRet, bool fOffline)
516526
{
517527
if (!fOffline && !masternodeSync.IsBlockchainSynced()) {
518528
strErrorRet = "Sync in progress. Must wait until sync is complete to start Masternode";
@@ -521,7 +531,8 @@ bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet,
521531
}
522532
return true;
523533
}
524-
bool CMasternodeBroadcastFactory::setMasternodeKeys(
534+
535+
bool setMasternodeKeys(
525536
const std::string& strKeyMasternode,
526537
std::pair<CKey,CPubKey>& masternodeKeyPair,
527538
std::string& strErrorRet)
@@ -533,7 +544,8 @@ bool CMasternodeBroadcastFactory::setMasternodeKeys(
533544
}
534545
return true;
535546
}
536-
bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
547+
548+
bool setMasternodeCollateralKeys(
537549
const std::string& txHash,
538550
const std::string& outputIndex,
539551
const std::string& service,
@@ -558,7 +570,7 @@ bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
558570
return true;
559571
}
560572

561-
bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
573+
bool checkMasternodeCollateral(
562574
const CTxIn& txin,
563575
const std::string& txHash,
564576
const std::string& outputIndex,
@@ -590,13 +602,14 @@ bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
590602
return true;
591603
}
592604

593-
bool CMasternodeBroadcastFactory::createArgumentsFromConfig(
605+
bool createArgumentsFromConfig(
594606
const CMasternodeConfig::CMasternodeEntry configEntry,
595607
std::string& strErrorRet,
596608
bool fOffline,
597609
bool collateralPrivKeyIsRemote,
598610
CTxIn& txin,
599611
std::pair<CKey,CPubKey>& masternodeKeyPair,
612+
CScript& rewardScript,
600613
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
601614
MasternodeTier& nMasternodeTier
602615
)
@@ -613,9 +626,23 @@ bool CMasternodeBroadcastFactory::createArgumentsFromConfig(
613626
{
614627
return false;
615628
}
629+
630+
if (configEntry.getRewardAddress().empty())
631+
rewardScript = CMasternode::GetDefaultRewardScript(masternodeCollateralKeyPair.second);
632+
else {
633+
const CBitcoinAddress addr(configEntry.getRewardAddress());
634+
if (!addr.IsValid()) {
635+
strErrorRet = strprintf("Invalid reward address for masternode: %s", configEntry.getRewardAddress());
636+
return false;
637+
}
638+
rewardScript = GetScriptForDestination(addr.Get());
639+
}
640+
616641
return true;
617642
}
618643

644+
} // anonymous namespace
645+
619646
bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEntry configEntry,
620647
CPubKey pubkeyCollateralAddress,
621648
std::string& strErrorRet,
@@ -626,6 +653,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
626653
const bool deferRelay = true;
627654
CTxIn txin;
628655
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
656+
CScript rewardScript;
629657
std::pair<CKey,CPubKey> masternodeKeyPair;
630658
MasternodeTier nMasternodeTier;
631659

@@ -636,6 +664,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
636664
collateralPrivateKeyIsRemote,
637665
txin,
638666
masternodeKeyPair,
667+
rewardScript,
639668
masternodeCollateralKeyPair,
640669
nMasternodeTier))
641670
{
@@ -646,6 +675,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
646675
txin,
647676
CService(configEntry.getIp()),
648677
pubkeyCollateralAddress,
678+
rewardScript,
649679
masternodeKeyPair.second,
650680
nMasternodeTier,
651681
deferRelay,
@@ -674,6 +704,7 @@ bool CMasternodeBroadcastFactory::Create(
674704

675705
CTxIn txin;
676706
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
707+
CScript rewardScript;
677708
std::pair<CKey,CPubKey> masternodeKeyPair;
678709
MasternodeTier nMasternodeTier;
679710

@@ -684,6 +715,7 @@ bool CMasternodeBroadcastFactory::Create(
684715
collateralPrivateKeyIsRemote,
685716
txin,
686717
masternodeKeyPair,
718+
rewardScript,
687719
masternodeCollateralKeyPair,
688720
nMasternodeTier))
689721
{
@@ -694,6 +726,7 @@ bool CMasternodeBroadcastFactory::Create(
694726
CService(strService),
695727
masternodeCollateralKeyPair.first,
696728
masternodeCollateralKeyPair.second,
729+
rewardScript,
697730
masternodeKeyPair.first,
698731
masternodeKeyPair.second,
699732
nMasternodeTier,
@@ -772,19 +805,22 @@ CMasternodePing createDelayedMasternodePing(const CMasternodeBroadcast& mnb)
772805
} // anonymous namespace
773806

774807
void CMasternodeBroadcastFactory::createWithoutSignatures(
775-
CTxIn txin,
776-
CService service,
777-
CPubKey pubKeyCollateralAddressNew,
778-
CPubKey pubKeyMasternodeNew,
808+
const CTxIn& txin,
809+
const CService& service,
810+
const CPubKey& pubKeyCollateralAddressNew,
811+
const CScript& rewardScript,
812+
const CPubKey& pubKeyMasternodeNew,
779813
const MasternodeTier nMasternodeTier,
780-
bool deferRelay,
814+
const bool deferRelay,
781815
CMasternodeBroadcast& mnbRet)
782816
{
783817
LogPrint("masternode", "CMasternodeBroadcastFactory::createWithoutSignatures -- pubKeyCollateralAddressNew = %s, pubKeyMasternodeNew.GetID() = %s\n",
784818
CBitcoinAddress(pubKeyCollateralAddressNew.GetID()).ToString(),
785819
pubKeyMasternodeNew.GetID().ToString());
786820

787-
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nMasternodeTier, PROTOCOL_VERSION);
821+
mnbRet = CMasternodeBroadcast(service, txin,
822+
pubKeyCollateralAddressNew, rewardScript, pubKeyMasternodeNew,
823+
nMasternodeTier, PROTOCOL_VERSION);
788824
const CMasternodePing mnp = (deferRelay
789825
? createDelayedMasternodePing(mnbRet)
790826
: CMasternodePing(txin));
@@ -793,22 +829,23 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
793829
}
794830

795831
bool CMasternodeBroadcastFactory::Create(
796-
CTxIn txin,
797-
CService service,
798-
CKey keyCollateralAddressNew,
799-
CPubKey pubKeyCollateralAddressNew,
800-
CKey keyMasternodeNew,
801-
CPubKey pubKeyMasternodeNew,
832+
const CTxIn& txin,
833+
const CService& service,
834+
const CKey& keyCollateralAddressNew,
835+
const CPubKey& pubKeyCollateralAddressNew,
836+
const CScript& rewardScript,
837+
const CKey& keyMasternodeNew,
838+
const CPubKey& pubKeyMasternodeNew,
802839
const MasternodeTier nMasternodeTier,
803840
std::string& strErrorRet,
804841
CMasternodeBroadcast& mnbRet,
805-
bool deferRelay)
842+
const bool deferRelay)
806843
{
807844
// wait for reindex and/or import to finish
808845
if (fImporting || fReindex) return false;
809846

810847
createWithoutSignatures(
811-
txin,service,pubKeyCollateralAddressNew,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
848+
txin,service,pubKeyCollateralAddressNew,rewardScript,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
812849

813850
if(!provideSignatures(keyMasternodeNew,pubKeyMasternodeNew,keyCollateralAddressNew,mnbRet,strErrorRet))
814851
{
@@ -1031,7 +1068,7 @@ CMasternodePing::CMasternodePing()
10311068
vchSig = std::vector<unsigned char>();
10321069
}
10331070

1034-
CMasternodePing::CMasternodePing(CTxIn& newVin)
1071+
CMasternodePing::CMasternodePing(const CTxIn& newVin)
10351072
{
10361073
vin = newVin;
10371074
blockHash = chainActive[chainActive.Height() - 12]->GetBlockHash();

divi/src/masternode.h

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CMasternodePing
5959
//removed stop
6060

6161
CMasternodePing();
62-
CMasternodePing(CTxIn& newVin);
62+
CMasternodePing(const CTxIn& newVin);
6363

6464
ADD_SERIALIZE_METHODS;
6565

@@ -190,6 +190,7 @@ class CMasternode
190190
/** Returns the "default" reward script, which is the one
191191
* matching the collateral address. */
192192
CScript GetDefaultRewardScript() const;
193+
static CScript GetDefaultRewardScript(const CPubKey& pubKeyCollateralAddress);
193194

194195
/** Calculates the score of the current masternode, based on the given
195196
* seed hash. It should be the result of GetBlockHashForScoring of
@@ -264,15 +265,20 @@ class CMasternode
264265

265266
class CMasternodeBroadcast : public CMasternode
266267
{
267-
public:
268-
CMasternodeBroadcast() = default;
268+
private:
269269
CMasternodeBroadcast(
270-
CService newAddr,
271-
CTxIn newVin,
272-
CPubKey pubKeyCollateralAddress,
273-
CPubKey pubKeyMasternode,
270+
const CService& newAddr,
271+
const CTxIn& newVin,
272+
const CPubKey& pubKeyCollateralAddress,
273+
const CScript& rewardScriptIn,
274+
const CPubKey& pubKeyMasternode,
274275
MasternodeTier nMasternodeTier,
275276
int protocolVersionIn);
277+
278+
friend class CMasternodeBroadcastFactory;
279+
280+
public:
281+
CMasternodeBroadcast() = default;
276282
CMasternodeBroadcast(const CMasternode& mn);
277283

278284
bool CheckAndUpdate(int& nDoS);
@@ -341,10 +347,11 @@ class CMasternodeBroadcastFactory
341347
bool fOffline = false);
342348
private:
343349
static void createWithoutSignatures(
344-
CTxIn txin,
345-
CService service,
346-
CPubKey pubKeyCollateralAddressNew,
347-
CPubKey pubKeyMasternodeNew,
350+
const CTxIn& txin,
351+
const CService& service,
352+
const CPubKey& pubKeyCollateralAddressNew,
353+
const CScript& rewardScript,
354+
const CPubKey& pubKeyMasternodeNew,
348355
MasternodeTier nMasternodeTier,
349356
bool deferRelay,
350357
CMasternodeBroadcast& mnbRet);
@@ -367,45 +374,17 @@ class CMasternodeBroadcastFactory
367374
CMasternodeBroadcast& mnb,
368375
std::string& strErrorRet);
369376

370-
static bool Create(CTxIn vin,
371-
CService service,
372-
CKey keyCollateralAddressNew,
373-
CPubKey pubKeyCollateralAddressNew,
374-
CKey keyMasternodeNew,
375-
CPubKey pubKeyMasternodeNew,
376-
MasternodeTier nMasternodeTier,
377-
std::string& strErrorRet,
378-
CMasternodeBroadcast& mnbRet,
379-
bool deferRelay);
380-
static bool checkBlockchainSync(std::string& strErrorRet, bool fOffline);
381-
static bool setMasternodeKeys(
382-
const std::string& strKeyMasternode,
383-
std::pair<CKey,CPubKey>& masternodeKeyPair,
384-
std::string& strErrorRet);
385-
static bool setMasternodeCollateralKeys(
386-
const std::string& txHash,
387-
const std::string& outputIndex,
388-
const std::string& service,
389-
bool collateralPrivKeyIsRemote,
390-
CTxIn& txin,
391-
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
392-
std::string& error);
393-
static bool checkMasternodeCollateral(
394-
const CTxIn& txin,
395-
const std::string& txHash,
396-
const std::string& outputIndex,
397-
const std::string& service,
398-
MasternodeTier& nMasternodeTier,
399-
std::string& strErrorRet);
400-
static bool createArgumentsFromConfig(
401-
const CMasternodeConfig::CMasternodeEntry configEntry,
402-
std::string& strErrorRet,
403-
bool fOffline,
404-
bool collateralPrivKeyIsRemote,
405-
CTxIn& txin,
406-
std::pair<CKey,CPubKey>& masternodeKeyPair,
407-
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
408-
MasternodeTier& nMasternodeTier);
377+
static bool Create(const CTxIn& vin,
378+
const CService& service,
379+
const CKey& keyCollateralAddressNew,
380+
const CPubKey& pubKeyCollateralAddressNew,
381+
const CScript& rewardScript,
382+
const CKey& keyMasternodeNew,
383+
const CPubKey& pubKeyMasternodeNew,
384+
MasternodeTier nMasternodeTier,
385+
std::string& strErrorRet,
386+
CMasternodeBroadcast& mnbRet,
387+
bool deferRelay);
409388
};
410389

411390
#endif

divi/src/masternodeconfig.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
CMasternodeConfig masternodeConfig;
1717

18-
void CMasternodeConfig::add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)
18+
void CMasternodeConfig::add(const std::string& alias, const std::string& ip, const std::string& privKey,
19+
const std::string& txHash, const std::string& outputIndex,
20+
const std::string& rewardAddr)
1921
{
20-
CMasternodeEntry cme(alias, ip, privKey, txHash, outputIndex);
21-
entries.push_back(cme);
22+
entries.emplace_back(alias, ip, privKey, txHash, outputIndex, rewardAddr);
2223
}
2324

2425
bool CMasternodeConfig::read(std::string& strErr)
@@ -62,7 +63,12 @@ bool CMasternodeConfig::read(std::string& strErr)
6263
}
6364
}
6465

65-
add(alias, ip, privKey, txHash, outputIndex);
66+
/* This might fail if there is no address, but that is fine and we will
67+
just leave the string empty in that case. */
68+
std::string rewardAddr;
69+
iss >> rewardAddr;
70+
71+
add(alias, ip, privKey, txHash, outputIndex, rewardAddr);
6672
}
6773

6874
streamConfig.close();
@@ -86,15 +92,16 @@ CMasternodeConfig::CMasternodeConfig()
8692
{
8793
entries = std::vector<CMasternodeEntry>();
8894
}
95+
8996
const std::vector<CMasternodeConfig::CMasternodeEntry>& CMasternodeConfig::getEntries() const
9097
{
9198
return entries;
9299
}
93100

94-
int CMasternodeConfig::getCount()
101+
int CMasternodeConfig::getCount() const
95102
{
96103
int c = -1;
97-
BOOST_FOREACH (CMasternodeEntry e, entries) {
104+
for (const auto& e : entries) {
98105
if (e.getAlias() != "") c++;
99106
}
100107
return c;

0 commit comments

Comments
 (0)