Skip to content

Commit 9412539

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 ccd725b commit 9412539

File tree

6 files changed

+127
-87
lines changed

6 files changed

+127
-87
lines changed

divi/src/masternode.cpp

Lines changed: 60 additions & 23 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
}
@@ -492,12 +497,15 @@ bool CMasternode::IsValidNetAddr() const
492497
(IsReachable(addr) && addr.IsRoutable());
493498
}
494499

495-
CMasternodeBroadcast::CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, const MasternodeTier nMasternodeTier, int protocolVersionIn)
500+
CMasternodeBroadcast::CMasternodeBroadcast(
501+
const CService& newAddr, const CTxIn& newVin,
502+
const CPubKey& pubKeyCollateralAddressNew, const CScript& rewardScriptIn, const CPubKey& pubKeyMasternodeNew,
503+
const MasternodeTier nMasternodeTier, const int protocolVersionIn)
496504
{
497505
vin = newVin;
498506
addr = newAddr;
499507
pubKeyCollateralAddress = pubKeyCollateralAddressNew;
500-
rewardScript = GetDefaultRewardScript();
508+
rewardScript = rewardScriptIn;
501509
pubKeyMasternode = pubKeyMasternodeNew;
502510
protocolVersion = protocolVersionIn;
503511
nTier = nMasternodeTier;
@@ -507,8 +515,10 @@ CMasternodeBroadcast::CMasternodeBroadcast(const CMasternode& mn)
507515
: CMasternode(mn)
508516
{}
509517

518+
namespace
519+
{
510520

511-
bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet, bool fOffline)
521+
bool checkBlockchainSync(std::string& strErrorRet, bool fOffline)
512522
{
513523
if (!fOffline && !masternodeSync.IsBlockchainSynced()) {
514524
strErrorRet = "Sync in progress. Must wait until sync is complete to start Masternode";
@@ -517,7 +527,8 @@ bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet,
517527
}
518528
return true;
519529
}
520-
bool CMasternodeBroadcastFactory::setMasternodeKeys(
530+
531+
bool setMasternodeKeys(
521532
const std::string& strKeyMasternode,
522533
std::pair<CKey,CPubKey>& masternodeKeyPair,
523534
std::string& strErrorRet)
@@ -529,7 +540,8 @@ bool CMasternodeBroadcastFactory::setMasternodeKeys(
529540
}
530541
return true;
531542
}
532-
bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
543+
544+
bool setMasternodeCollateralKeys(
533545
const std::string& txHash,
534546
const std::string& outputIndex,
535547
const std::string& service,
@@ -554,7 +566,7 @@ bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
554566
return true;
555567
}
556568

557-
bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
569+
bool checkMasternodeCollateral(
558570
const CTxIn& txin,
559571
const std::string& txHash,
560572
const std::string& outputIndex,
@@ -586,13 +598,14 @@ bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
586598
return true;
587599
}
588600

589-
bool CMasternodeBroadcastFactory::createArgumentsFromConfig(
601+
bool createArgumentsFromConfig(
590602
const CMasternodeConfig::CMasternodeEntry configEntry,
591603
std::string& strErrorRet,
592604
bool fOffline,
593605
bool collateralPrivKeyIsRemote,
594606
CTxIn& txin,
595607
std::pair<CKey,CPubKey>& masternodeKeyPair,
608+
CScript& rewardScript,
596609
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
597610
MasternodeTier& nMasternodeTier
598611
)
@@ -609,9 +622,23 @@ bool CMasternodeBroadcastFactory::createArgumentsFromConfig(
609622
{
610623
return false;
611624
}
625+
626+
if (configEntry.getRewardAddress().empty())
627+
rewardScript = CMasternode::GetDefaultRewardScript(masternodeCollateralKeyPair.second);
628+
else {
629+
const CBitcoinAddress addr(configEntry.getRewardAddress());
630+
if (!addr.IsValid()) {
631+
strErrorRet = strprintf("Invalid reward address for masternode: %s", configEntry.getRewardAddress());
632+
return false;
633+
}
634+
rewardScript = GetScriptForDestination(addr.Get());
635+
}
636+
612637
return true;
613638
}
614639

640+
} // anonymous namespace
641+
615642
bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEntry configEntry,
616643
CPubKey pubkeyCollateralAddress,
617644
std::string& strErrorRet,
@@ -622,6 +649,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
622649
const bool deferRelay = true;
623650
CTxIn txin;
624651
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
652+
CScript rewardScript;
625653
std::pair<CKey,CPubKey> masternodeKeyPair;
626654
MasternodeTier nMasternodeTier;
627655

@@ -632,6 +660,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
632660
collateralPrivateKeyIsRemote,
633661
txin,
634662
masternodeKeyPair,
663+
rewardScript,
635664
masternodeCollateralKeyPair,
636665
nMasternodeTier))
637666
{
@@ -642,6 +671,7 @@ bool CMasternodeBroadcastFactory::Create(const CMasternodeConfig::CMasternodeEnt
642671
txin,
643672
CService(configEntry.getIp()),
644673
pubkeyCollateralAddress,
674+
rewardScript,
645675
masternodeKeyPair.second,
646676
nMasternodeTier,
647677
deferRelay,
@@ -670,6 +700,7 @@ bool CMasternodeBroadcastFactory::Create(
670700

671701
CTxIn txin;
672702
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
703+
CScript rewardScript;
673704
std::pair<CKey,CPubKey> masternodeKeyPair;
674705
MasternodeTier nMasternodeTier;
675706

@@ -680,6 +711,7 @@ bool CMasternodeBroadcastFactory::Create(
680711
collateralPrivateKeyIsRemote,
681712
txin,
682713
masternodeKeyPair,
714+
rewardScript,
683715
masternodeCollateralKeyPair,
684716
nMasternodeTier))
685717
{
@@ -690,6 +722,7 @@ bool CMasternodeBroadcastFactory::Create(
690722
CService(strService),
691723
masternodeCollateralKeyPair.first,
692724
masternodeCollateralKeyPair.second,
725+
rewardScript,
693726
masternodeKeyPair.first,
694727
masternodeKeyPair.second,
695728
nMasternodeTier,
@@ -747,41 +780,45 @@ bool CMasternodeBroadcastFactory::provideSignatures(
747780
}
748781

749782
void CMasternodeBroadcastFactory::createWithoutSignatures(
750-
CTxIn txin,
751-
CService service,
752-
CPubKey pubKeyCollateralAddressNew,
753-
CPubKey pubKeyMasternodeNew,
783+
const CTxIn& txin,
784+
const CService& service,
785+
const CPubKey& pubKeyCollateralAddressNew,
786+
const CScript& rewardScript,
787+
const CPubKey& pubKeyMasternodeNew,
754788
const MasternodeTier nMasternodeTier,
755-
bool deferRelay,
789+
const bool deferRelay,
756790
CMasternodeBroadcast& mnbRet)
757791
{
758792
LogPrint("masternode", "CMasternodeBroadcastFactory::createWithoutSignatures -- pubKeyCollateralAddressNew = %s, pubKeyMasternodeNew.GetID() = %s\n",
759793
CBitcoinAddress(pubKeyCollateralAddressNew.GetID()).ToString(),
760794
pubKeyMasternodeNew.GetID().ToString());
761795

762796
CMasternodePing mnp = (deferRelay)? CMasternodePing::createDelayedMasternodePing(txin): CMasternodePing(txin);
763-
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nMasternodeTier, PROTOCOL_VERSION);
797+
mnbRet = CMasternodeBroadcast(service, txin,
798+
pubKeyCollateralAddressNew, rewardScript, pubKeyMasternodeNew,
799+
nMasternodeTier, PROTOCOL_VERSION);
764800
mnbRet.lastPing = mnp;
765801
mnbRet.sigTime = mnp.sigTime;
766802
}
767803

768804
bool CMasternodeBroadcastFactory::Create(
769-
CTxIn txin,
770-
CService service,
771-
CKey keyCollateralAddressNew,
772-
CPubKey pubKeyCollateralAddressNew,
773-
CKey keyMasternodeNew,
774-
CPubKey pubKeyMasternodeNew,
805+
const CTxIn& txin,
806+
const CService& service,
807+
const CKey& keyCollateralAddressNew,
808+
const CPubKey& pubKeyCollateralAddressNew,
809+
const CScript& rewardScript,
810+
const CKey& keyMasternodeNew,
811+
const CPubKey& pubKeyMasternodeNew,
775812
const MasternodeTier nMasternodeTier,
776813
std::string& strErrorRet,
777814
CMasternodeBroadcast& mnbRet,
778-
bool deferRelay)
815+
const bool deferRelay)
779816
{
780817
// wait for reindex and/or import to finish
781818
if (fImporting || fReindex) return false;
782819

783820
createWithoutSignatures(
784-
txin,service,pubKeyCollateralAddressNew,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
821+
txin,service,pubKeyCollateralAddressNew,rewardScript,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
785822

786823
if(!provideSignatures(keyMasternodeNew,pubKeyMasternodeNew,keyCollateralAddressNew,mnbRet,strErrorRet))
787824
{
@@ -967,15 +1004,15 @@ CMasternodePing::CMasternodePing()
9671004
vchSig = std::vector<unsigned char>();
9681005
}
9691006

970-
CMasternodePing::CMasternodePing(CTxIn& newVin)
1007+
CMasternodePing::CMasternodePing(const CTxIn& newVin)
9711008
{
9721009
vin = newVin;
9731010
blockHash = chainActive[chainActive.Height() - 12]->GetBlockHash();
9741011
sigTime = GetAdjustedTime();
9751012
vchSig = std::vector<unsigned char>();
9761013
}
9771014

978-
CMasternodePing CMasternodePing::createDelayedMasternodePing(CTxIn& newVin)
1015+
CMasternodePing CMasternodePing::createDelayedMasternodePing(const CTxIn& newVin)
9791016
{
9801017
CMasternodePing ping;
9811018
const int64_t offsetTimeBy45BlocksInSeconds = 60 * 45;

divi/src/masternode.h

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class CMasternodePing
6161
//removed stop
6262

6363
CMasternodePing();
64-
CMasternodePing(CTxIn& newVin);
65-
static CMasternodePing createDelayedMasternodePing(CTxIn& newVin);
64+
CMasternodePing(const CTxIn& newVin);
65+
static CMasternodePing createDelayedMasternodePing(const CTxIn& newVin);
6666

6767
ADD_SERIALIZE_METHODS;
6868

@@ -172,6 +172,7 @@ class CMasternode
172172
/** Returns the "default" reward script, which is the one
173173
* matching the collateral address. */
174174
CScript GetDefaultRewardScript() const;
175+
static CScript GetDefaultRewardScript(const CPubKey& pubKeyCollateralAddress);
175176

176177
void swap(CMasternode& first, CMasternode& second); // nothrow
177178

@@ -260,15 +261,20 @@ class CMasternode
260261

261262
class CMasternodeBroadcast : public CMasternode
262263
{
263-
public:
264-
CMasternodeBroadcast() = default;
264+
private:
265265
CMasternodeBroadcast(
266-
CService newAddr,
267-
CTxIn newVin,
268-
CPubKey pubKeyCollateralAddress,
269-
CPubKey pubKeyMasternode,
266+
const CService& newAddr,
267+
const CTxIn& newVin,
268+
const CPubKey& pubKeyCollateralAddress,
269+
const CScript& rewardScriptIn,
270+
const CPubKey& pubKeyMasternode,
270271
MasternodeTier nMasternodeTier,
271272
int protocolVersionIn);
273+
274+
friend class CMasternodeBroadcastFactory;
275+
276+
public:
277+
CMasternodeBroadcast() = default;
272278
CMasternodeBroadcast(const CMasternode& mn);
273279

274280
bool CheckAndUpdate(int& nDoS);
@@ -335,10 +341,11 @@ class CMasternodeBroadcastFactory
335341
bool fOffline = false);
336342
private:
337343
static void createWithoutSignatures(
338-
CTxIn txin,
339-
CService service,
340-
CPubKey pubKeyCollateralAddressNew,
341-
CPubKey pubKeyMasternodeNew,
344+
const CTxIn& txin,
345+
const CService& service,
346+
const CPubKey& pubKeyCollateralAddressNew,
347+
const CScript& rewardScript,
348+
const CPubKey& pubKeyMasternodeNew,
342349
MasternodeTier nMasternodeTier,
343350
bool deferRelay,
344351
CMasternodeBroadcast& mnbRet);
@@ -361,45 +368,17 @@ class CMasternodeBroadcastFactory
361368
CMasternodeBroadcast& mnb,
362369
std::string& strErrorRet);
363370

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

405384
#endif

0 commit comments

Comments
 (0)