Repository navigation
Expand file tree
/
Copy pathMasternodeBroadcastFactory.cpp
More file actions
394 lines (356 loc) · 12.9 KB
/
Copy pathMasternodeBroadcastFactory.cpp
File metadata and controls
394 lines (356 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <MasternodeBroadcastFactory.h>
#include <masternode.h>
#include <Logging.h>
#include <tinyformat.h>
#include <MasternodeHelpers.h>
#include <obfuscation.h>
#include <chain.h>
#include <base58address.h>
#include <TransactionDiskAccessor.h>
#include <timedata.h>
#include <WalletTx.h>
#include <script/standard.h>
#include <keystore.h>
extern CChain chainActive;
extern bool fReindex;
extern bool fImporting;
static bool GetVinAndKeysFromOutput(const CKeyStore& walletKeyStore, CScript pubScript, CPubKey& pubKeyRet, CKey& keyRet)
{
// wait for reindex and/or import to finish
if (fImporting || fReindex) return false;
CTxDestination address1;
ExtractDestination(pubScript, address1);
CBitcoinAddress address2(address1);
CKeyID keyID;
if (!address2.GetKeyID(keyID)) {
LogPrintf("CWallet::GetVinAndKeysFromOutput -- Address does not refer to a key\n");
return false;
}
if (!walletKeyStore.GetKey(keyID, keyRet)) {
LogPrintf("CWallet::GetVinAndKeysFromOutput -- Private key for address is not known\n");
return false;
}
pubKeyRet = keyRet.GetPubKey();
return true;
}
static bool ParseInputReference(std::string strTxHash, std::string strOutputIndex, CTxIn& txinRet)
{
uint256 txHash = uint256S(strTxHash);
int nOutputIndex;
try {
nOutputIndex = std::stoi(strOutputIndex.c_str());
} catch (const std::exception& e) {
LogPrintf("%s: %s on strOutputIndex\n", __func__, e.what());
return false;
}
txinRet = CTxIn(txHash,nOutputIndex);
return true;
}
namespace
{
bool checkBlockchainSync(std::string& strErrorRet, bool fOffline)
{
if (!fOffline && !IsBlockchainSynced()) {
strErrorRet = "Sync in progress. Must wait until sync is complete to start Masternode";
LogPrint("masternode","CMasternodeBroadcastFactory::Create -- %s\n", strErrorRet);
return false;
}
return true;
}
bool setMasternodeKeys(
const std::string& strKeyMasternode,
std::pair<CKey,CPubKey>& masternodeKeyPair,
std::string& strErrorRet)
{
if (!CObfuScationSigner::GetKeysFromSecret(strKeyMasternode, masternodeKeyPair.first, masternodeKeyPair.second)) {
strErrorRet = strprintf("Invalid masternode key %s", strKeyMasternode);
LogPrint("masternode","CMasternodeBroadcastFactory::Create -- %s\n", strErrorRet);
return false;
}
return true;
}
bool setMasternodeCollateralKeys(
bool collateralPrivKeyIsRemote,
const CKeyStore& keyStore,
const CScript& scriptPubKey,
const COutPoint& outpoint,
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
std::string& strError)
{
if(collateralPrivKeyIsRemote)
{
masternodeCollateralKeyPair = std::pair<CKey,CPubKey>();
return true;
}
if (fImporting || fReindex) return false;
if (!GetVinAndKeysFromOutput(keyStore,scriptPubKey, masternodeCollateralKeyPair.second, masternodeCollateralKeyPair.first))
{
strError = strprintf("Could not allocate txin %s:%s for masternode", outpoint.hash.ToString(), std::to_string(outpoint.n));
LogPrint("masternode","CMasternodeBroadcastFactory::Create -- %s\n", strError);
return false;
}
return true;
}
bool checkMasternodeCollateral(
const std::string& service,
const CAmount& collateralAmount,
MasternodeTier& nMasternodeTier,
std::string& strErrorRet)
{
nMasternodeTier = CMasternode::GetTierByCollateralAmount(collateralAmount);
if(!CMasternode::IsTierValid(nMasternodeTier))
{
strErrorRet = strprintf("Invalid tier selected for masternode %s, collateral value is: %d", service, collateralAmount);
LogPrint("masternode","CMasternodeBroadcastFactory::Create -- %s\n", strErrorRet);
return false;
}
return true;
}
bool createArgumentsFromConfig(
const CKeyStore& walletKeyStore,
const CMasternodeConfig::CMasternodeEntry configEntry,
std::string& strErrorRet,
bool fOffline,
bool collateralPrivKeyIsRemote,
CTxIn& txin,
std::pair<CKey,CPubKey>& masternodeKeyPair,
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
MasternodeTier& nMasternodeTier)
{
std::string strService = configEntry.getIp();
std::string strKeyMasternode = configEntry.getPrivKey();
std::string strTxHash = configEntry.getTxHash();
std::string strOutputIndex = configEntry.getOutputIndex();
CTransaction fundingTx;
uint256 blockHash;
if (fImporting || fReindex) return false;
if(!ParseInputReference(strTxHash,strOutputIndex,txin))
{
return false;
}
if(!GetTransaction(txin.prevout.hash,fundingTx,blockHash,true))
{
strErrorRet = strprintf("Could not find txin %s:%s for masternode", strTxHash, strOutputIndex);
LogPrint("masternode","CMasternodeBroadcastFactory::Create -- %s\n", strErrorRet);
return false;
}
const CScript& collateralScript = fundingTx.vout[txin.prevout.n].scriptPubKey;
const CAmount& collateralAmount = fundingTx.vout[txin.prevout.n].nValue;
//need correct blocks to send ping
if (!checkBlockchainSync(strErrorRet,fOffline)||
!setMasternodeKeys(strKeyMasternode,masternodeKeyPair,strErrorRet) ||
!setMasternodeCollateralKeys(
collateralPrivKeyIsRemote,
walletKeyStore,
collateralScript,
txin.prevout,
masternodeCollateralKeyPair,
strErrorRet) ||
!checkMasternodeCollateral(strService, collateralAmount, nMasternodeTier, strErrorRet))
{
return false;
}
return true;
}
} // anonymous namespace
bool CMasternodeBroadcastFactory::Create(
const CMasternodeConfig::CMasternodeEntry configEntry,
CPubKey pubkeyCollateralAddress,
std::string& strErrorRet,
CMasternodeBroadcast& mnbRet,
bool fOffline)
{
static CBasicKeyStore dummyKeyStore;
const bool collateralPrivateKeyIsRemote = true;
const bool deferRelay = true;
CTxIn txin;
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
std::pair<CKey,CPubKey> masternodeKeyPair;
MasternodeTier nMasternodeTier;
if(!createArgumentsFromConfig(
dummyKeyStore,
configEntry,
strErrorRet,
fOffline,
collateralPrivateKeyIsRemote,
txin,
masternodeKeyPair,
masternodeCollateralKeyPair,
nMasternodeTier))
{
return false;
}
createWithoutSignatures(
txin,
CService(configEntry.getIp()),
pubkeyCollateralAddress,
masternodeKeyPair.second,
nMasternodeTier,
deferRelay,
mnbRet);
if(!signPing(masternodeKeyPair.first,masternodeKeyPair.second,mnbRet.lastPing,strErrorRet))
{
mnbRet = CMasternodeBroadcast();
return false;
}
return true;
}
bool CMasternodeBroadcastFactory::Create(
const CKeyStore& walletKeyStore,
const CMasternodeConfig::CMasternodeEntry configEntry,
std::string& strErrorRet,
CMasternodeBroadcast& mnbRet,
bool fOffline,
bool deferRelay)
{
const bool collateralPrivateKeyIsRemote = false;
std::string strService = configEntry.getIp();
std::string strKeyMasternode = configEntry.getPrivKey();
std::string strTxHash = configEntry.getTxHash();
std::string strOutputIndex = configEntry.getOutputIndex();
CTxIn txin;
std::pair<CKey,CPubKey> masternodeCollateralKeyPair;
std::pair<CKey,CPubKey> masternodeKeyPair;
MasternodeTier nMasternodeTier;
if(!createArgumentsFromConfig(
walletKeyStore,
configEntry,
strErrorRet,
fOffline,
collateralPrivateKeyIsRemote,
txin,
masternodeKeyPair,
masternodeCollateralKeyPair,
nMasternodeTier))
{
return false;
}
return Create(txin,
CService(strService),
masternodeCollateralKeyPair.first,
masternodeCollateralKeyPair.second,
masternodeKeyPair.first,
masternodeKeyPair.second,
nMasternodeTier,
strErrorRet,
mnbRet,
deferRelay);
}
bool CMasternodeBroadcastFactory::signPing(
const CKey& keyMasternodeNew,
const CPubKey& pubKeyMasternodeNew,
CMasternodePing& mnp,
std::string& strErrorRet)
{
if(!CObfuScationSigner::SignAndVerify<CMasternodePing>(mnp,keyMasternodeNew,pubKeyMasternodeNew,strErrorRet))
{
strErrorRet = strprintf("Failed to sign ping, masternode=%s", mnp.vin.prevout.hash.ToString());
LogPrint("masternode","%s -- %s\n",__func__, strErrorRet);
return false;
}
return true;
}
bool CMasternodeBroadcastFactory::signBroadcast(
CKey keyCollateralAddressNew,
CMasternodeBroadcast& mnb,
std::string& strErrorRet)
{
if (! CObfuScationSigner::SignAndVerify<CMasternodeBroadcast>(mnb,keyCollateralAddressNew,mnb.pubKeyCollateralAddress,strErrorRet))
{
strErrorRet = strprintf("Failed to sign broadcast, masternode=%s", mnb.vin.prevout.hash.ToString());
LogPrint("masternode","%s -- %s\n", __func__, strErrorRet);
mnb = CMasternodeBroadcast();
return false;
}
return true;
}
bool CMasternodeBroadcastFactory::provideSignatures(
CKey keyMasternodeNew,
CPubKey pubKeyMasternodeNew,
CKey keyCollateralAddressNew,
CMasternodeBroadcast& mnb,
std::string& strErrorRet)
{
if(!signPing(keyMasternodeNew,pubKeyMasternodeNew,mnb.lastPing,strErrorRet))
{
return false;
}
if (!signBroadcast(keyCollateralAddressNew,mnb,strErrorRet))
{
return false;
}
return true;
}
namespace
{
CMasternodePing createDelayedMasternodePing(const CMasternodeBroadcast& mnb)
{
CMasternodePing ping;
const int64_t offsetTimeBy45BlocksInSeconds = 60 * 45;
ping.vin = mnb.vin;
const int depthOfTx = ComputeMasternodeInputAge(mnb);
const int offset = std::min( std::max(0, depthOfTx), 12 );
const auto* block = chainActive[chainActive.Height() - offset];
ping.blockHash = block->GetBlockHash();
ping.sigTime = std::max(block->GetBlockTime() + offsetTimeBy45BlocksInSeconds, GetAdjustedTime());
ping.signature = std::vector<unsigned char>();
LogPrint("masternode","mnp - relay block-time & sigtime: %d vs. %d\n", block->GetBlockTime(), ping.sigTime);
return ping;
}
} // anonymous namespace
void CMasternodeBroadcastFactory::createWithoutSignatures(
const CTxIn& txin,
const CService& service,
const CPubKey& pubKeyCollateralAddressNew,
const CPubKey& pubKeyMasternodeNew,
const MasternodeTier nMasternodeTier,
bool deferRelay,
CMasternodeBroadcast& mnbRet)
{
LogPrint("masternode", "CMasternodeBroadcastFactory::createWithoutSignatures -- pubKeyCollateralAddressNew = %s, pubKeyMasternodeNew.GetID() = %s\n",
CBitcoinAddress(pubKeyCollateralAddressNew.GetID()).ToString(),
pubKeyMasternodeNew.GetID().ToString());
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nMasternodeTier, PROTOCOL_VERSION);
const CMasternodePing mnp = (deferRelay
? createDelayedMasternodePing(mnbRet)
: createCurrentPing(txin));
mnbRet.lastPing = mnp;
/* We have to ensure that the sigtime of the broadcast itself is later
than when the collateral gets its 15 confirmations (and it might not
even be confirmed once yet). */
const auto* pindexConf = ComputeMasternodeConfirmationBlockIndex(mnbRet);
if (pindexConf == nullptr) {
/* The signature time is accepted on the network up to one hour
into the future; we use 45 minutes here to ensure this is still
fine even with some clock drift. That will also be more than
enough to be later than the confirmation block time assuming the
collateral is going to be mined "now". */
mnbRet.sigTime = GetAdjustedTime() + 45 * 60;
LogPrint("masternode", "Using future sigtime for masternode broadcast: %d\n", mnbRet.sigTime);
} else {
mnbRet.sigTime = pindexConf->GetBlockTime();
LogPrint("masternode", "Using collateral confirmation time for broadcast: %d\n", mnbRet.sigTime);
}
}
bool CMasternodeBroadcastFactory::Create(
const CTxIn& txin,
const CService& service,
const CKey& keyCollateralAddressNew,
const CPubKey& pubKeyCollateralAddressNew,
const CKey& keyMasternodeNew,
const CPubKey& pubKeyMasternodeNew,
const MasternodeTier nMasternodeTier,
std::string& strErrorRet,
CMasternodeBroadcast& mnbRet,
bool deferRelay)
{
// wait for reindex and/or import to finish
if (fImporting || fReindex) return false;
createWithoutSignatures(
txin,service,pubKeyCollateralAddressNew,pubKeyMasternodeNew,nMasternodeTier,deferRelay,mnbRet);
if(!provideSignatures(keyMasternodeNew,pubKeyMasternodeNew,keyCollateralAddressNew,mnbRet,strErrorRet))
{
return false;
}
return true;
}