Skip to content

Commit 6cc84b8

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 840c171 commit 6cc84b8

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>
@@ -108,6 +109,7 @@ bool createArgumentsFromConfig(
108109
bool collateralPrivKeyIsRemote,
109110
CTxIn& txin,
110111
std::pair<CKey,CPubKey>& masternodeKeyPair,
112+
CScript& rewardScript,
111113
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
112114
MasternodeTier& nMasternodeTier)
113115
{
@@ -143,6 +145,18 @@ bool createArgumentsFromConfig(
143145
{
144146
return false;
145147
}
148+
149+
if (configEntry.getRewardAddress().empty())
150+
rewardScript = CMasternode::GetDefaultRewardScript(masternodeCollateralKeyPair.second);
151+
else {
152+
const CBitcoinAddress addr(configEntry.getRewardAddress());
153+
if (!addr.IsValid()) {
154+
strErrorRet = strprintf("Invalid reward address for masternode: %s", configEntry.getRewardAddress());
155+
return false;
156+
}
157+
rewardScript = GetScriptForDestination(addr.Get());
158+
}
159+
146160
return true;
147161
}
148162

@@ -160,6 +174,7 @@ bool CMasternodeBroadcastFactory::Create(
160174
const bool deferRelay = true;
161175
CTxIn txin;
162176
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
177+
CScript rewardScript;
163178
std::pair<CKey,CPubKey> masternodeKeyPair;
164179
MasternodeTier nMasternodeTier;
165180

@@ -171,6 +186,7 @@ bool CMasternodeBroadcastFactory::Create(
171186
collateralPrivateKeyIsRemote,
172187
txin,
173188
masternodeKeyPair,
189+
rewardScript,
174190
masternodeCollateralKeyPair,
175191
nMasternodeTier))
176192
{
@@ -181,6 +197,7 @@ bool CMasternodeBroadcastFactory::Create(
181197
txin,
182198
CService(configEntry.getIp()),
183199
pubkeyCollateralAddress,
200+
rewardScript,
184201
masternodeKeyPair.second,
185202
nMasternodeTier,
186203
deferRelay,
@@ -210,6 +227,7 @@ bool CMasternodeBroadcastFactory::Create(
210227

211228
CTxIn txin;
212229
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
230+
CScript rewardScript;
213231
std::pair<CKey,CPubKey> masternodeKeyPair;
214232
MasternodeTier nMasternodeTier;
215233

@@ -221,6 +239,7 @@ bool CMasternodeBroadcastFactory::Create(
221239
collateralPrivateKeyIsRemote,
222240
txin,
223241
masternodeKeyPair,
242+
rewardScript,
224243
masternodeCollateralKeyPair,
225244
nMasternodeTier))
226245
{
@@ -231,6 +250,7 @@ bool CMasternodeBroadcastFactory::Create(
231250
CService(strService),
232251
masternodeCollateralKeyPair.first,
233252
masternodeCollateralKeyPair.second,
253+
rewardScript,
234254
masternodeKeyPair.first,
235255
masternodeKeyPair.second,
236256
nMasternodeTier,
@@ -292,6 +312,7 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
292312
const CTxIn& txin,
293313
const CService& service,
294314
const CPubKey& pubKeyCollateralAddressNew,
315+
const CScript& rewardScript,
295316
const CPubKey& pubKeyMasternodeNew,
296317
const MasternodeTier nMasternodeTier,
297318
bool deferRelay,
@@ -301,7 +322,9 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
301322
CBitcoinAddress(pubKeyCollateralAddressNew.GetID()).ToString(),
302323
pubKeyMasternodeNew.GetID().ToString());
303324

304-
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nMasternodeTier, PROTOCOL_VERSION);
325+
mnbRet = CMasternodeBroadcast(service, txin,
326+
pubKeyCollateralAddressNew, rewardScript, pubKeyMasternodeNew,
327+
nMasternodeTier, PROTOCOL_VERSION);
305328
const CMasternodePing mnp = (deferRelay
306329
? createDelayedMasternodePing(mnbRet)
307330
: createCurrentPing(txin));
@@ -330,6 +353,7 @@ bool CMasternodeBroadcastFactory::Create(
330353
const CService& service,
331354
const CKey& keyCollateralAddressNew,
332355
const CPubKey& pubKeyCollateralAddressNew,
356+
const CScript& rewardScript,
333357
const CKey& keyMasternodeNew,
334358
const CPubKey& pubKeyMasternodeNew,
335359
const MasternodeTier nMasternodeTier,
@@ -341,7 +365,7 @@ bool CMasternodeBroadcastFactory::Create(
341365
if (ReindexingOrImportingIsActive()) return false;
342366

343367
createWithoutSignatures(
344-
txin,service,pubKeyCollateralAddressNew,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
368+
txin,service,pubKeyCollateralAddressNew,rewardScript,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
345369

346370
if(!provideSignatures(keyMasternodeNew,pubKeyMasternodeNew,keyCollateralAddressNew,mnbRet,strErrorRet))
347371
{

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
* scoring 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
@@ -271,7 +271,7 @@ Value setupmasternode(const Array& params, bool fHelp)
271271
if (ipAndPort.find(':') == std::string::npos)
272272
ipAndPort += ":" + std::to_string(Params().GetDefaultPort());
273273

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

276276
CMasternodeBroadcast mnb;
277277
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)