Skip to content

Commit 647d2a8

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 a570d85 commit 647d2a8

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
@@ -211,6 +211,11 @@ CMasternode& CMasternode::operator=(CMasternode from)
211211
}
212212

213213
CScript CMasternode::GetDefaultRewardScript() const
214+
{
215+
return GetDefaultRewardScript(pubKeyCollateralAddress);
216+
}
217+
218+
CScript CMasternode::GetDefaultRewardScript(const CPubKey& pubKeyCollateralAddress)
214219
{
215220
return GetScriptForDestination(pubKeyCollateralAddress.GetID());
216221
}
@@ -495,12 +500,15 @@ bool CMasternode::IsValidNetAddr() const
495500
(IsReachable(addr) && addr.IsRoutable());
496501
}
497502

498-
CMasternodeBroadcast::CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, const MasternodeTier nMasternodeTier, int protocolVersionIn)
503+
CMasternodeBroadcast::CMasternodeBroadcast(
504+
const CService& newAddr, const CTxIn& newVin,
505+
const CPubKey& pubKeyCollateralAddressNew, const CScript& rewardScriptIn, const CPubKey& pubKeyMasternodeNew,
506+
const MasternodeTier nMasternodeTier, const int protocolVersionIn)
499507
{
500508
vin = newVin;
501509
addr = newAddr;
502510
pubKeyCollateralAddress = pubKeyCollateralAddressNew;
503-
rewardScript = GetDefaultRewardScript();
511+
rewardScript = rewardScriptIn;
504512
pubKeyMasternode = pubKeyMasternodeNew;
505513
protocolVersion = protocolVersionIn;
506514
nTier = nMasternodeTier;
@@ -510,8 +518,10 @@ CMasternodeBroadcast::CMasternodeBroadcast(const CMasternode& mn)
510518
: CMasternode(mn)
511519
{}
512520

521+
namespace
522+
{
513523

514-
bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet, bool fOffline)
524+
bool checkBlockchainSync(std::string& strErrorRet, bool fOffline)
515525
{
516526
if (!fOffline && !masternodeSync.IsBlockchainSynced()) {
517527
strErrorRet = "Sync in progress. Must wait until sync is complete to start Masternode";
@@ -520,7 +530,8 @@ bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet,
520530
}
521531
return true;
522532
}
523-
bool CMasternodeBroadcastFactory::setMasternodeKeys(
533+
534+
bool setMasternodeKeys(
524535
const std::string& strKeyMasternode,
525536
std::pair<CKey,CPubKey>& masternodeKeyPair,
526537
std::string& strErrorRet)
@@ -532,7 +543,8 @@ bool CMasternodeBroadcastFactory::setMasternodeKeys(
532543
}
533544
return true;
534545
}
535-
bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
546+
547+
bool setMasternodeCollateralKeys(
536548
const std::string& txHash,
537549
const std::string& outputIndex,
538550
const std::string& service,
@@ -557,7 +569,7 @@ bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
557569
return true;
558570
}
559571

560-
bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
572+
bool checkMasternodeCollateral(
561573
const CTxIn& txin,
562574
const std::string& txHash,
563575
const std::string& outputIndex,
@@ -589,13 +601,14 @@ bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
589601
return true;
590602
}
591603

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

643+
} // anonymous namespace
644+
618645
bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEntry configEntry,
619646
CPubKey pubkeyCollateralAddress,
620647
std::string& strErrorRet,
@@ -625,6 +652,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
625652
const bool deferRelay = true;
626653
CTxIn txin;
627654
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
655+
CScript rewardScript;
628656
std::pair<CKey,CPubKey> masternodeKeyPair;
629657
MasternodeTier nMasternodeTier;
630658

@@ -635,6 +663,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
635663
collateralPrivateKeyIsRemote,
636664
txin,
637665
masternodeKeyPair,
666+
rewardScript,
638667
masternodeCollateralKeyPair,
639668
nMasternodeTier))
640669
{
@@ -645,6 +674,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
645674
txin,
646675
CService(configEntry.getIp()),
647676
pubkeyCollateralAddress,
677+
rewardScript,
648678
masternodeKeyPair.second,
649679
nMasternodeTier,
650680
deferRelay,
@@ -673,6 +703,7 @@ bool CMasternodeBroadcastFactory::Create(
673703

674704
CTxIn txin;
675705
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
706+
CScript rewardScript;
676707
std::pair<CKey,CPubKey> masternodeKeyPair;
677708
MasternodeTier nMasternodeTier;
678709

@@ -683,6 +714,7 @@ bool CMasternodeBroadcastFactory::Create(
683714
collateralPrivateKeyIsRemote,
684715
txin,
685716
masternodeKeyPair,
717+
rewardScript,
686718
masternodeCollateralKeyPair,
687719
nMasternodeTier))
688720
{
@@ -693,6 +725,7 @@ bool CMasternodeBroadcastFactory::Create(
693725
CService(strService),
694726
masternodeCollateralKeyPair.first,
695727
masternodeCollateralKeyPair.second,
728+
rewardScript,
696729
masternodeKeyPair.first,
697730
masternodeKeyPair.second,
698731
nMasternodeTier,
@@ -771,19 +804,22 @@ CMasternodePing createDelayedMasternodePing(const CMasternodeBroadcast& mnb)
771804
} // anonymous namespace
772805

773806
void CMasternodeBroadcastFactory::createWithoutSignatures(
774-
CTxIn txin,
775-
CService service,
776-
CPubKey pubKeyCollateralAddressNew,
777-
CPubKey pubKeyMasternodeNew,
807+
const CTxIn& txin,
808+
const CService& service,
809+
const CPubKey& pubKeyCollateralAddressNew,
810+
const CScript& rewardScript,
811+
const CPubKey& pubKeyMasternodeNew,
778812
const MasternodeTier nMasternodeTier,
779-
bool deferRelay,
813+
const bool deferRelay,
780814
CMasternodeBroadcast& mnbRet)
781815
{
782816
LogPrint("masternode", "CMasternodeBroadcastFactory::createWithoutSignatures -- pubKeyCollateralAddressNew = %s, pubKeyMasternodeNew.GetID() = %s\n",
783817
CBitcoinAddress(pubKeyCollateralAddressNew.GetID()).ToString(),
784818
pubKeyMasternodeNew.GetID().ToString());
785819

786-
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nMasternodeTier, PROTOCOL_VERSION);
820+
mnbRet = CMasternodeBroadcast(service, txin,
821+
pubKeyCollateralAddressNew, rewardScript, pubKeyMasternodeNew,
822+
nMasternodeTier, PROTOCOL_VERSION);
787823
const CMasternodePing mnp = (deferRelay
788824
? createDelayedMasternodePing(mnbRet)
789825
: CMasternodePing(txin));
@@ -792,22 +828,23 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
792828
}
793829

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

809846
createWithoutSignatures(
810-
txin,service,pubKeyCollateralAddressNew,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
847+
txin,service,pubKeyCollateralAddressNew,rewardScript,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
811848

812849
if(!provideSignatures(keyMasternodeNew,pubKeyMasternodeNew,keyCollateralAddressNew,mnbRet,strErrorRet))
813850
{
@@ -1030,7 +1067,7 @@ CMasternodePing::CMasternodePing()
10301067
vchSig = std::vector<unsigned char>();
10311068
}
10321069

1033-
CMasternodePing::CMasternodePing(CTxIn& newVin)
1070+
CMasternodePing::CMasternodePing(const CTxIn& newVin)
10341071
{
10351072
vin = newVin;
10361073
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)