Skip to content

Commit c505560

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 7b3e7a2 commit c505560

File tree

8 files changed

+65
-12
lines changed

8 files changed

+65
-12
lines changed

divi/src/MasternodeBroadcastFactory.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <obfuscation.h>
88
#include <chain.h>
99
#include <base58address.h>
10+
#include <script/standard.h>
1011
#include <TransactionDiskAccessor.h>
1112
#include <timedata.h>
1213
#include <WalletTx.h>
@@ -112,6 +113,7 @@ bool createArgumentsFromConfig(
112113
bool collateralPrivKeyIsRemote,
113114
CTxIn& txin,
114115
std::pair<CKey,CPubKey>& masternodeKeyPair,
116+
CScript& rewardScript,
115117
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
116118
MasternodeTier& nMasternodeTier)
117119
{
@@ -147,6 +149,18 @@ bool createArgumentsFromConfig(
147149
{
148150
return false;
149151
}
152+
153+
if (configEntry.getRewardAddress().empty())
154+
rewardScript = CMasternode::GetDefaultRewardScript(masternodeCollateralKeyPair.second);
155+
else {
156+
const CBitcoinAddress addr(configEntry.getRewardAddress());
157+
if (!addr.IsValid()) {
158+
strErrorRet = strprintf("Invalid reward address for masternode: %s", configEntry.getRewardAddress());
159+
return false;
160+
}
161+
rewardScript = GetScriptForDestination(addr.Get());
162+
}
163+
150164
return true;
151165
}
152166

@@ -164,6 +178,7 @@ bool CMasternodeBroadcastFactory::Create(
164178
const bool deferRelay = true;
165179
CTxIn txin;
166180
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
181+
CScript rewardScript;
167182
std::pair<CKey,CPubKey> masternodeKeyPair;
168183
MasternodeTier nMasternodeTier;
169184

@@ -175,6 +190,7 @@ bool CMasternodeBroadcastFactory::Create(
175190
collateralPrivateKeyIsRemote,
176191
txin,
177192
masternodeKeyPair,
193+
rewardScript,
178194
masternodeCollateralKeyPair,
179195
nMasternodeTier))
180196
{
@@ -185,6 +201,7 @@ bool CMasternodeBroadcastFactory::Create(
185201
txin,
186202
CService(configEntry.getIp()),
187203
pubkeyCollateralAddress,
204+
rewardScript,
188205
masternodeKeyPair.second,
189206
nMasternodeTier,
190207
deferRelay,
@@ -214,6 +231,7 @@ bool CMasternodeBroadcastFactory::Create(
214231

215232
CTxIn txin;
216233
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
234+
CScript rewardScript;
217235
std::pair<CKey,CPubKey> masternodeKeyPair;
218236
MasternodeTier nMasternodeTier;
219237

@@ -225,6 +243,7 @@ bool CMasternodeBroadcastFactory::Create(
225243
collateralPrivateKeyIsRemote,
226244
txin,
227245
masternodeKeyPair,
246+
rewardScript,
228247
masternodeCollateralKeyPair,
229248
nMasternodeTier))
230249
{
@@ -235,6 +254,7 @@ bool CMasternodeBroadcastFactory::Create(
235254
CService(strService),
236255
masternodeCollateralKeyPair.first,
237256
masternodeCollateralKeyPair.second,
257+
rewardScript,
238258
masternodeKeyPair.first,
239259
masternodeKeyPair.second,
240260
nMasternodeTier,
@@ -316,6 +336,7 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
316336
const CTxIn& txin,
317337
const CService& service,
318338
const CPubKey& pubKeyCollateralAddressNew,
339+
const CScript& rewardScript,
319340
const CPubKey& pubKeyMasternodeNew,
320341
const MasternodeTier nMasternodeTier,
321342
bool deferRelay,
@@ -325,7 +346,9 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
325346
CBitcoinAddress(pubKeyCollateralAddressNew.GetID()).ToString(),
326347
pubKeyMasternodeNew.GetID().ToString());
327348

328-
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nMasternodeTier, PROTOCOL_VERSION);
349+
mnbRet = CMasternodeBroadcast(service, txin,
350+
pubKeyCollateralAddressNew, rewardScript, pubKeyMasternodeNew,
351+
nMasternodeTier, PROTOCOL_VERSION);
329352
const CMasternodePing mnp = (deferRelay
330353
? createDelayedMasternodePing(mnbRet)
331354
: createCurrentPing(txin));
@@ -354,6 +377,7 @@ bool CMasternodeBroadcastFactory::Create(
354377
const CService& service,
355378
const CKey& keyCollateralAddressNew,
356379
const CPubKey& pubKeyCollateralAddressNew,
380+
const CScript& rewardScript,
357381
const CKey& keyMasternodeNew,
358382
const CPubKey& pubKeyMasternodeNew,
359383
const MasternodeTier nMasternodeTier,
@@ -365,7 +389,7 @@ bool CMasternodeBroadcastFactory::Create(
365389
if (fImporting || fReindex) return false;
366390

367391
createWithoutSignatures(
368-
txin,service,pubKeyCollateralAddressNew,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
392+
txin,service,pubKeyCollateralAddressNew,rewardScript,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
369393

370394
if(!provideSignatures(keyMasternodeNew,pubKeyMasternodeNew,keyCollateralAddressNew,mnbRet,strErrorRet))
371395
{

divi/src/MasternodeBroadcastFactory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CMasternodeBroadcastFactory
4242
const CTxIn& txin,
4343
const CService& service,
4444
const CPubKey& pubKeyCollateralAddressNew,
45+
const CScript& rewardScript,
4546
const CPubKey& pubKeyMasternodeNew,
4647
MasternodeTier nMasternodeTier,
4748
bool deferRelay,
@@ -63,6 +64,7 @@ class CMasternodeBroadcastFactory
6364
const CService& service,
6465
const CKey& keyCollateralAddressNew,
6566
const CPubKey& pubKeyCollateralAddressNew,
67+
const CScript& rewardScript,
6668
const CKey& keyMasternodeNew,
6769
const CPubKey& pubKeyMasternodeNew,
6870
MasternodeTier nMasternodeTier,

divi/src/masternode.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ CMasternode& CMasternode::operator=(CMasternode from)
132132
}
133133

134134
CScript CMasternode::GetDefaultRewardScript() const
135+
{
136+
return GetDefaultRewardScript(pubKeyCollateralAddress);
137+
}
138+
139+
CScript CMasternode::GetDefaultRewardScript(const CPubKey& pubKeyCollateralAddress)
135140
{
136141
return GetScriptForDestination(pubKeyCollateralAddress.GetID());
137142
}
@@ -244,13 +249,13 @@ bool CMasternode::IsValidNetAddr() const
244249

245250
CMasternodeBroadcast::CMasternodeBroadcast(
246251
const CService& newAddr, const CTxIn& newVin,
247-
const CPubKey& pubKeyCollateralAddressNew, const CPubKey& pubKeyMasternodeNew,
252+
const CPubKey& pubKeyCollateralAddressNew, const CScript& rewardScriptIn, const CPubKey& pubKeyMasternodeNew,
248253
const MasternodeTier nMasternodeTier, const int protocolVersionIn)
249254
{
250255
vin = newVin;
251256
addr = newAddr;
252257
pubKeyCollateralAddress = pubKeyCollateralAddressNew;
253-
rewardScript = GetDefaultRewardScript();
258+
rewardScript = rewardScriptIn;
254259
pubKeyMasternode = pubKeyMasternodeNew;
255260
protocolVersion = protocolVersionIn;
256261
nTier = nMasternodeTier;

divi/src/masternode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class CMasternode
9090
/** Returns the "default" reward script, which is the one
9191
* matching the collateral address. */
9292
CScript GetDefaultRewardScript() const;
93+
static CScript GetDefaultRewardScript(const CPubKey& pubKeyCollateralAddress);
9394

9495
/** Calculates the score of the current masternode, based on the given
9596
* seed hash. It should be the result of GetBlockHashForScoring of
@@ -149,6 +150,7 @@ class CMasternodeBroadcast : public CMasternode
149150
const CService& newAddr,
150151
const CTxIn& newVin,
151152
const CPubKey& pubKeyCollateralAddress,
153+
const CScript& rewardScriptIn,
152154
const CPubKey& pubKeyMasternode,
153155
MasternodeTier nMasternodeTier,
154156
int protocolVersionIn);

divi/src/masternodeconfig.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ boost::filesystem::path GetMasternodeConfigFile(const Settings& settings)
4141
} // anonymous namespace
4242

4343
void CMasternodeConfig::add(const std::string& alias, const std::string& ip, const std::string& privKey,
44-
const std::string& txHash, const std::string& outputIndex)
44+
const std::string& txHash, const std::string& outputIndex,
45+
const std::string& rewardAddr)
4546
{
46-
entries.emplace_back(alias, ip, privKey, txHash, outputIndex);
47+
entries.emplace_back(alias, ip, privKey, txHash, outputIndex, rewardAddr);
4748
}
4849

4950
bool CMasternodeConfig::read(const Settings& settings, std::string& strErr)
@@ -87,7 +88,12 @@ bool CMasternodeConfig::read(const Settings& settings, std::string& strErr)
8788
}
8889
}
8990

90-
add(alias, ip, privKey, txHash, outputIndex);
91+
/* This might fail if there is no address, but that is fine and we will
92+
just leave the string empty in that case. */
93+
std::string rewardAddr;
94+
iss >> rewardAddr;
95+
96+
add(alias, ip, privKey, txHash, outputIndex, rewardAddr);
9197
}
9298

9399
streamConfig.close();

divi/src/masternodeconfig.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ class CMasternodeConfig
2626
std::string privKey;
2727
std::string txHash;
2828
std::string outputIndex;
29+
std::string rewardAddress;
2930

3031
public:
3132
CMasternodeEntry(const std::string& alias, const std::string& ip,
3233
const std::string& privKey,
33-
const std::string& txHash, const std::string& outputIndex)
34+
const std::string& txHash, const std::string& outputIndex,
35+
const std::string& rewardAddress)
3436
{
3537
this->alias = alias;
3638
this->ip = ip;
3739
this->privKey = privKey;
3840
this->txHash = txHash;
3941
this->outputIndex = outputIndex;
42+
this->rewardAddress = rewardAddress;
4043
}
4144

4245
const std::string& getAlias() const
@@ -89,6 +92,16 @@ class CMasternodeConfig
8992
this->ip = ip;
9093
}
9194

95+
const std::string& getRewardAddress() const
96+
{
97+
return rewardAddress;
98+
}
99+
100+
void setRewardAddress(const std::string& addr)
101+
{
102+
this->rewardAddress = addr;
103+
}
104+
92105
/** Tries to parse the entry's input reference into an outpoint.
93106
* Returns true on success. */
94107
bool parseInputReference(COutPoint& outp) const;
@@ -99,7 +112,8 @@ class CMasternodeConfig
99112
void clear();
100113
bool read(const Settings& settings, std::string& strErr);
101114
void add(const std::string& alias, const std::string& ip, const std::string& privKey,
102-
const std::string& txHash, const std::string& outputIndex);
115+
const std::string& txHash, const std::string& outputIndex,
116+
const std::string& rewardAddr);
103117
const std::vector<CMasternodeEntry>& getEntries() const;
104118
int getCount() const;
105119

divi/src/rpcmasternode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Value setupmasternode(const Array& params, bool fHelp)
270270
if (ipAndPort.find(':') == std::string::npos)
271271
ipAndPort += ":" + std::to_string(Params().GetDefaultPort());
272272

273-
CMasternodeConfig::CMasternodeEntry config(alias,ipAndPort,CBitcoinSecret(masternodeKey).ToString(),txHash,outputIndex);
273+
CMasternodeConfig::CMasternodeEntry config(alias,ipAndPort,CBitcoinSecret(masternodeKey).ToString(),txHash,outputIndex, "");
274274

275275
CMasternodeBroadcast mnb;
276276
std::string errorMsg;

divi/src/test/ActiveMasternode_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ActiveMasternodeTestFixture
3030

3131
void AddDummyConfiguration(CTxIn txIn, CService service)
3232
{
33-
configurations_->add("dummy configuration", service.ToString(), "", txIn.prevout.hash.ToString(), std::to_string(txIn.prevout.n));
33+
configurations_->add("dummy configuration", service.ToString(), "", txIn.prevout.hash.ToString(), std::to_string(txIn.prevout.n), "");
3434
}
3535
void disableMasternode()
3636
{
@@ -131,4 +131,4 @@ BOOST_AUTO_TEST_CASE(willCannotResetStatusOfInactiveMasternodeWhenChainSyncIsReq
131131
BOOST_CHECK(activeMasternode_->status == ACTIVE_MASTERNODE_STARTED);
132132
}
133133

134-
BOOST_AUTO_TEST_SUITE_END()
134+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)