forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasternode.cpp
More file actions
696 lines (624 loc) · 29 KB
/
masternode.cpp
File metadata and controls
696 lines (624 loc) · 29 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright (c) 2014-2024 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
#include <evo/assetlocktx.h>
#include <evo/chainhelper.h>
#include <evo/deterministicmns.h>
#include <governance/classes.h>
#include <index/txindex.h>
#include <node/blockstorage.h>
#include <node/context.h>
#include <governance/governance.h>
#include <masternode/node.h>
#include <masternode/payments.h>
#include <net.h>
#include <netbase.h>
#include <rpc/blockchain.h>
#include <rpc/server.h>
#include <rpc/server_util.h>
#include <rpc/util.h>
#include <univalue.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/rpc/util.h>
#ifdef ENABLE_WALLET
#include <wallet/wallet.h>
#endif // ENABLE_WALLET
#include <fstream>
#include <iomanip>
static RPCHelpMan masternode_connect()
{
return RPCHelpMan{"masternode connect",
"Connect to given masternode\n",
{
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the masternode to connect"},
{"v2transport", RPCArg::Type::BOOL, RPCArg::DefaultHint{"set by -v2transport"}, "Attempt to connect using BIP324 v2 transport protocol"},
},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::string strAddress = request.params[0].get_str();
std::optional<CService> addr{Lookup(strAddress, 0, false)};
if (!addr.has_value()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Incorrect masternode address %s", strAddress));
}
const NodeContext& node = EnsureAnyNodeContext(request.context);
CConnman& connman = EnsureConnman(node);
bool node_v2transport = connman.GetLocalServices() & NODE_P2P_V2;
bool use_v2transport = request.params[1].isNull() ? node_v2transport : ParseBoolV(request.params[1], "v2transport");
if (use_v2transport && !node_v2transport) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Adding v2transport connections requires -v2transport init flag to be set.");
}
connman.OpenMasternodeConnection(CAddress(addr.value(), NODE_NETWORK), use_v2transport);
if (!connman.IsConnected(CAddress(addr.value(), NODE_NETWORK), CConnman::AllNodes)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Couldn't connect to masternode %s", strAddress));
}
return "successfully connected";
},
};
}
static RPCHelpMan masternode_count()
{
return RPCHelpMan{"masternode count",
"Get information about number of masternodes.\n",
{},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const NodeContext& node = EnsureAnyNodeContext(request.context);
auto mnList = node.dmnman->GetListAtChainTip();
int total = mnList.GetAllMNsCount();
int enabled = mnList.GetValidMNsCount();
UniValue obj(UniValue::VOBJ);
obj.pushKV("total", total);
obj.pushKV("enabled", enabled);
int evo_total = mnList.GetAllEvoCount();
int evo_enabled = mnList.GetValidEvoCount();
UniValue evoObj(UniValue::VOBJ);
evoObj.pushKV("total", evo_total);
evoObj.pushKV("enabled", evo_enabled);
UniValue regularObj(UniValue::VOBJ);
regularObj.pushKV("total", total - evo_total);
regularObj.pushKV("enabled", enabled - evo_enabled);
UniValue detailedObj(UniValue::VOBJ);
detailedObj.pushKV("regular", regularObj);
detailedObj.pushKV("evo", evoObj);
obj.pushKV("detailed", detailedObj);
return obj;
},
};
}
#ifdef ENABLE_WALLET
static RPCHelpMan masternode_outputs()
{
return RPCHelpMan{"masternode outputs",
"Print masternode compatible outputs\n",
{},
RPCResult {
RPCResult::Type::ARR, "", "A list of outpoints that can be/are used as masternode collaterals",
{
{RPCResult::Type::STR, "", "A (potential) masternode collateral"},
}},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
// Find possible candidates
std::vector<COutput> vPossibleCoins;
CCoinControl coin_control;
coin_control.nCoinType = CoinType::ONLY_MASTERNODE_COLLATERAL;
{
LOCK(wallet->cs_wallet);
wallet->AvailableCoins(vPossibleCoins, &coin_control);
}
UniValue outputsArr(UniValue::VARR);
for (const auto& out : vPossibleCoins) {
outputsArr.push_back(out.GetInputCoin().outpoint.ToStringShort());
}
return outputsArr;
},
};
}
#endif // ENABLE_WALLET
static RPCHelpMan masternode_status()
{
return RPCHelpMan{"masternode status",
"Print masternode status information\n",
{},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (!node.mn_activeman) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "This node does not run an active masternode.");
}
UniValue mnObj(UniValue::VOBJ);
// keep compatibility with legacy status for now (TODO: get deprecated/removed later)
mnObj.pushKV("outpoint", node.mn_activeman->GetOutPoint().ToStringShort());
mnObj.pushKV("service", node.mn_activeman->GetService().ToStringAddrPort());
auto dmn = CHECK_NONFATAL(node.dmnman)->GetListAtChainTip().GetMN(node.mn_activeman->GetProTxHash());
if (dmn) {
mnObj.pushKV("proTxHash", dmn->proTxHash.ToString());
mnObj.pushKV("type", std::string(GetMnType(dmn->nType).description));
mnObj.pushKV("collateralHash", dmn->collateralOutpoint.hash.ToString());
mnObj.pushKV("collateralIndex", (int)dmn->collateralOutpoint.n);
mnObj.pushKV("dmnState", dmn->pdmnState->ToJson(dmn->nType));
}
mnObj.pushKV("state", node.mn_activeman->GetStateString());
mnObj.pushKV("status", node.mn_activeman->GetStatus());
return mnObj;
},
};
}
static std::string GetRequiredPaymentsString(CGovernanceManager& govman, const CDeterministicMNList& tip_mn_list, int nBlockHeight, const CDeterministicMNCPtr &payee)
{
std::string strPayments = "Unknown";
if (payee) {
CTxDestination dest;
if (!ExtractDestination(payee->pdmnState->scriptPayout, dest)) {
NONFATAL_UNREACHABLE();
}
strPayments = EncodeDestination(dest);
if (payee->nOperatorReward != 0 && payee->pdmnState->scriptOperatorPayout != CScript()) {
if (!ExtractDestination(payee->pdmnState->scriptOperatorPayout, dest)) {
NONFATAL_UNREACHABLE();
}
strPayments += ", " + EncodeDestination(dest);
}
}
if (govman.IsSuperblockTriggered(tip_mn_list, nBlockHeight)) {
std::vector<CTxOut> voutSuperblock;
if (!govman.GetSuperblockPayments(tip_mn_list, nBlockHeight, voutSuperblock)) {
return strPayments + ", error";
}
std::string strSBPayees = "Unknown";
for (const auto& txout : voutSuperblock) {
CTxDestination dest;
ExtractDestination(txout.scriptPubKey, dest);
if (strSBPayees != "Unknown") {
strSBPayees += ", " + EncodeDestination(dest);
} else {
strSBPayees = EncodeDestination(dest);
}
}
strPayments += ", " + strSBPayees;
}
return strPayments;
}
static RPCHelpMan masternode_winners()
{
return RPCHelpMan{"masternode winners",
"Print list of masternode winners\n",
{
{"count", RPCArg::Type::NUM, RPCArg::Default{10}, "number of last winners to return"},
{"filter", RPCArg::Type::STR, RPCArg::Default{""}, "filter for returned winners"},
},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const NodeContext& node = EnsureAnyNodeContext(request.context);
const ChainstateManager& chainman = EnsureChainman(node);
const CBlockIndex* pindexTip{nullptr};
{
LOCK(cs_main);
pindexTip = chainman.ActiveChain().Tip();
if (!pindexTip) return NullUniValue;
}
int nCount = 10;
std::string strFilter = "";
if (!request.params[0].isNull()) {
nCount = LocaleIndependentAtoi<int>(request.params[0].get_str());
}
if (!request.params[1].isNull()) {
strFilter = request.params[1].get_str();
}
UniValue obj(UniValue::VOBJ);
int nChainTipHeight = pindexTip->nHeight;
int nStartHeight = std::max(nChainTipHeight - nCount, 1);
const auto tip_mn_list = CHECK_NONFATAL(node.dmnman)->GetListAtChainTip();
for (int h = nStartHeight; h <= nChainTipHeight; h++) {
const CBlockIndex* pIndex = pindexTip->GetAncestor(h - 1);
auto payee = node.dmnman->GetListForBlock(pIndex).GetMNPayee(pIndex);
if (payee) {
std::string strPayments = GetRequiredPaymentsString(*CHECK_NONFATAL(node.govman), tip_mn_list, h, payee);
if (strFilter != "" && strPayments.find(strFilter) == std::string::npos) continue;
obj.pushKV(strprintf("%d", h), strPayments);
}
}
auto projection = node.dmnman->GetListForBlock(pindexTip).GetProjectedMNPayees(pindexTip, 20);
for (size_t i = 0; i < projection.size(); i++) {
int h = nChainTipHeight + 1 + i;
std::string strPayments = GetRequiredPaymentsString(*node.govman, tip_mn_list, h, projection[i]);
if (strFilter != "" && strPayments.find(strFilter) == std::string::npos) continue;
obj.pushKV(strprintf("%d", h), strPayments);
}
return obj;
},
};
}
static RPCHelpMan masternode_payments()
{
return RPCHelpMan{"masternode payments",
"\nReturns an array of deterministic masternodes and their payments for the specified block\n",
{
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"tip"}, "The hash of the starting block"},
{"count", RPCArg::Type::NUM, RPCArg::Default{1}, "The number of blocks to return. Will return <count> previous blocks if <count> is negative. Both 1 and -1 correspond to the chain tip."},
},
RPCResult {
RPCResult::Type::ARR, "", "Blocks",
{
{RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::NUM, "height", "The height of the block"},
{RPCResult::Type::STR_HEX, "blockhash", "The hash of the block"},
{RPCResult::Type::NUM, "amount", "Amount received in this block by all masternodes"},
{RPCResult::Type::ARR, "masternodes", "Masternodes that received payments in this block",
{
{RPCResult::Type::STR_HEX, "proTxHash", "The hash of the corresponding ProRegTx"},
{RPCResult::Type::NUM, "amount", "Amount received by this masternode"},
{RPCResult::Type::ARR, "payees", "Payees who received a share of this payment",
{
{RPCResult::Type::STR, "address", "Payee address"},
{RPCResult::Type::STR_HEX, "script", "Payee scriptPubKey"},
{RPCResult::Type::NUM, "amount", "Amount received by this payee"},
}},
}},
}},
},
},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const NodeContext& node = EnsureAnyNodeContext(request.context);
const ChainstateManager& chainman = EnsureChainman(node);
const CBlockIndex* pindex{nullptr};
if (g_txindex) {
g_txindex->BlockUntilSyncedToCurrentChain();
}
if (request.params[0].isNull()) {
LOCK(cs_main);
pindex = chainman.ActiveChain().Tip();
} else {
LOCK(cs_main);
uint256 blockHash(ParseHashV(request.params[0], "blockhash"));
pindex = chainman.m_blockman.LookupBlockIndex(blockHash);
if (pindex == nullptr) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
}
int64_t nCount = request.params.size() > 1 ? ParseInt64V(request.params[1], "count") : 1;
// A temporary vector which is used to sort results properly (there is no "reverse" in/for UniValue)
std::vector<UniValue> vecPayments;
CHECK_NONFATAL(node.chain_helper);
CHECK_NONFATAL(node.dmnman);
while (vecPayments.size() < uint64_t(std::abs(nCount)) && pindex != nullptr) {
CBlock block;
if (!ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
}
// Note: we have to actually calculate block reward from scratch instead of simply querying coinbase vout
// because miners might collect less coins than they potentially could and this would break our calculations.
CAmount nBlockFees{0};
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
for (const auto& tx : block.vtx) {
if (tx->IsCoinBase()) {
continue;
}
if (tx->IsPlatformTransfer()) {
auto payload = GetTxPayload<CAssetUnlockPayload>(*tx);
CHECK_NONFATAL(payload);
nBlockFees += payload->getFee();
continue;
}
CAmount nValueIn{0};
for (const auto& txin : tx->vin) {
uint256 blockHashTmp;
CTransactionRef txPrev = GetTransaction(/* block_index */ nullptr, &mempool, txin.prevout.hash, Params().GetConsensus(), blockHashTmp);
nValueIn += txPrev->vout[txin.prevout.n].nValue;
}
nBlockFees += nValueIn - tx->GetValueOut();
}
std::vector<CTxOut> voutMasternodePayments, voutDummy;
CMutableTransaction dummyTx;
CAmount blockSubsidy = GetBlockSubsidy(pindex, Params().GetConsensus());
node.chain_helper->mn_payments->FillBlockPayments(dummyTx, pindex->pprev, blockSubsidy, nBlockFees, voutMasternodePayments, voutDummy);
UniValue blockObj(UniValue::VOBJ);
CAmount payedPerBlock{0};
UniValue masternodeArr(UniValue::VARR);
UniValue protxObj(UniValue::VOBJ);
UniValue payeesArr(UniValue::VARR);
CAmount payedPerMasternode{0};
for (const auto& txout : voutMasternodePayments) {
UniValue obj(UniValue::VOBJ);
CTxDestination dest;
ExtractDestination(txout.scriptPubKey, dest);
obj.pushKV("address", EncodeDestination(dest));
obj.pushKV("script", HexStr(txout.scriptPubKey));
obj.pushKV("amount", txout.nValue);
payedPerMasternode += txout.nValue;
payeesArr.push_back(obj);
}
// NOTE: we use _previous_ block to find a payee for the current one
const auto dmnPayee = node.dmnman->GetListForBlock(pindex->pprev).GetMNPayee(pindex->pprev);
protxObj.pushKV("proTxHash", dmnPayee == nullptr ? "" : dmnPayee->proTxHash.ToString());
protxObj.pushKV("amount", payedPerMasternode);
protxObj.pushKV("payees", payeesArr);
payedPerBlock += payedPerMasternode;
masternodeArr.push_back(protxObj);
blockObj.pushKV("height", pindex->nHeight);
blockObj.pushKV("blockhash", pindex->GetBlockHash().ToString());
blockObj.pushKV("amount", payedPerBlock);
blockObj.pushKV("masternodes", masternodeArr);
vecPayments.push_back(blockObj);
if (nCount > 0) {
LOCK(cs_main);
pindex = chainman.ActiveChain().Next(pindex);
} else {
pindex = pindex->pprev;
}
}
if (nCount < 0) {
std::reverse(vecPayments.begin(), vecPayments.end());
}
UniValue paymentsArr(UniValue::VARR);
for (const auto& payment : vecPayments) {
paymentsArr.push_back(payment);
}
return paymentsArr;
},
};
}
static RPCHelpMan masternode_help()
{
return RPCHelpMan{"masternode",
"Set of commands to execute masternode related actions\n"
"\nAvailable commands:\n"
" count - Get information about number of masternodes\n"
" current - DEPRECATED Print info on current masternode winner to be paid the next block (calculated locally)\n"
#ifdef ENABLE_WALLET
" outputs - Print masternode compatible outputs\n"
#endif // ENABLE_WALLET
" status - Print masternode status information\n"
" list - Print list of all known masternodes (see masternodelist for more info)\n"
" payments - Return information about masternode payments in a mined block\n"
" winner - DEPRECATED Print info on next masternode winner to vote for\n"
" winners - Print list of masternode winners\n",
{
{"command", RPCArg::Type::STR, RPCArg::Optional::NO, "The command to execute"},
},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Must be a valid command");
},
};
}
static RPCHelpMan masternodelist_helper(bool is_composite)
{
// We need both composite and non-composite options because we support
// both options 'masternodelist' and 'masternode list'
return RPCHelpMan{is_composite ? "masternode list" : "masternodelist",
"Get a list of masternodes in different modes. This call is identical to 'masternode list' call.\n"
"Available modes:\n"
" addr - Print ip address associated with a masternode (can be additionally filtered, partial match)\n"
" recent - Print info in JSON format for active and recently banned masternodes (can be additionally filtered, partial match)\n"
" evo - Print info in JSON format for EvoNodes only\n"
" full - Print info in format 'status payee lastpaidtime lastpaidblock IP'\n"
" (can be additionally filtered, partial match)\n"
" info - Print info in format 'status payee IP'\n"
" (can be additionally filtered, partial match)\n"
" json - Print info in JSON format (can be additionally filtered, partial match)\n"
" lastpaidblock - Print the last block height a node was paid on the network\n"
" lastpaidtime - Print the last time a node was paid on the network\n"
" owneraddress - Print the masternode owner Dash address\n"
" payee - Print the masternode payout Dash address (can be additionally filtered,\n"
" partial match)\n"
" pubKeyOperator - Print the masternode operator public key\n"
" status - Print masternode status: ENABLED / POSE_BANNED\n"
" (can be additionally filtered, partial match)\n"
" votingaddress - Print the masternode voting Dash address\n",
{
{"mode", RPCArg::Type::STR, RPCArg::DefaultHint{"json"}, "The mode to run list in"},
{"filter", RPCArg::Type::STR, RPCArg::Default{""}, "Filter results. Partial match by outpoint by default in all modes, additional matches in some modes are also available"},
},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::string strMode = "json";
std::string strFilter = "";
if (!request.params[0].isNull()) strMode = request.params[0].get_str();
if (!request.params[1].isNull()) strFilter = request.params[1].get_str();
strMode = ToLower(strMode);
if (
strMode != "addr" && strMode != "full" && strMode != "info" && strMode != "json" &&
strMode != "owneraddress" && strMode != "votingaddress" &&
strMode != "lastpaidtime" && strMode != "lastpaidblock" &&
strMode != "payee" && strMode != "pubkeyoperator" &&
strMode != "status" && strMode != "recent" && strMode != "evo")
{
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("strMode %s not found", strMode));
}
const NodeContext& node = EnsureAnyNodeContext(request.context);
const ChainstateManager& chainman = EnsureChainman(node);
UniValue obj(UniValue::VOBJ);
const auto mnList = CHECK_NONFATAL(node.dmnman)->GetListAtChainTip();
const auto dmnToStatus = [&](const auto& dmn) {
if (mnList.IsMNValid(dmn)) {
return "ENABLED";
}
if (mnList.IsMNPoSeBanned(dmn)) {
return "POSE_BANNED";
}
return "UNKNOWN";
};
const auto dmnToLastPaidTime = [&](const auto& dmn) {
if (dmn.pdmnState->nLastPaidHeight == 0) {
return (int)0;
}
LOCK(cs_main);
const CBlockIndex* pindex = chainman.ActiveChain()[dmn.pdmnState->nLastPaidHeight];
return (int)pindex->nTime;
};
const bool showRecentMnsOnly = strMode == "recent";
const bool showEvoOnly = strMode == "evo";
const int tipHeight = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()->nHeight);
mnList.ForEachMN(false, [&](auto& dmn) {
if (showRecentMnsOnly && mnList.IsMNPoSeBanned(dmn)) {
if (tipHeight - dmn.pdmnState->GetBannedHeight() > Params().GetConsensus().nSuperblockCycle) {
return;
}
}
if (showEvoOnly && dmn.nType != MnType::Evo) {
return;
}
std::string strOutpoint = dmn.collateralOutpoint.ToStringShort();
Coin coin;
std::string collateralAddressStr = "UNKNOWN";
if (GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin)) {
CTxDestination collateralDest;
if (ExtractDestination(coin.out.scriptPubKey, collateralDest)) {
collateralAddressStr = EncodeDestination(collateralDest);
}
}
CScript payeeScript = dmn.pdmnState->scriptPayout;
CTxDestination payeeDest;
std::string payeeStr = "UNKNOWN";
if (ExtractDestination(payeeScript, payeeDest)) {
payeeStr = EncodeDestination(payeeDest);
}
if (strMode == "addr") {
std::string strAddress = dmn.pdmnState->addr.ToStringAddrPort();
if (strFilter !="" && strAddress.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, strAddress);
} else if (strMode == "full") {
std::ostringstream streamFull;
streamFull << std::setw(18) <<
dmnToStatus(dmn) << " " <<
dmn.pdmnState->nPoSePenalty << " " <<
payeeStr << " " << std::setw(10) <<
dmnToLastPaidTime(dmn) << " " << std::setw(6) <<
dmn.pdmnState->nLastPaidHeight << " " <<
dmn.pdmnState->addr.ToStringAddrPort();
std::string strFull = streamFull.str();
if (strFilter !="" && strFull.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, strFull);
} else if (strMode == "info") {
std::ostringstream streamInfo;
streamInfo << std::setw(18) <<
dmnToStatus(dmn) << " " <<
dmn.pdmnState->nPoSePenalty << " " <<
payeeStr << " " <<
dmn.pdmnState->addr.ToStringAddrPort();
std::string strInfo = streamInfo.str();
if (strFilter !="" && strInfo.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, strInfo);
} else if (strMode == "json" || strMode == "recent" || strMode == "evo") {
std::ostringstream streamInfo;
streamInfo << dmn.proTxHash.ToString() << " " <<
dmn.pdmnState->addr.ToStringAddrPort() << " " <<
payeeStr << " " <<
dmnToStatus(dmn) << " " <<
dmn.pdmnState->nPoSePenalty << " " <<
dmnToLastPaidTime(dmn) << " " <<
dmn.pdmnState->nLastPaidHeight << " " <<
EncodeDestination(PKHash(dmn.pdmnState->keyIDOwner)) << " " <<
EncodeDestination(PKHash(dmn.pdmnState->keyIDVoting)) << " " <<
collateralAddressStr << " " <<
dmn.pdmnState->pubKeyOperator.ToString();
std::string strInfo = streamInfo.str();
if (strFilter !="" && strInfo.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) return;
UniValue objMN(UniValue::VOBJ);
objMN.pushKV("proTxHash", dmn.proTxHash.ToString());
objMN.pushKV("address", dmn.pdmnState->addr.ToStringAddrPort());
objMN.pushKV("payee", payeeStr);
objMN.pushKV("status", dmnToStatus(dmn));
objMN.pushKV("type", std::string(GetMnType(dmn.nType).description));
if (dmn.nType == MnType::Evo) {
objMN.pushKV("platformNodeID", dmn.pdmnState->platformNodeID.ToString());
objMN.pushKV("platformP2PPort", dmn.pdmnState->platformP2PPort);
objMN.pushKV("platformHTTPPort", dmn.pdmnState->platformHTTPPort);
}
objMN.pushKV("pospenaltyscore", dmn.pdmnState->nPoSePenalty);
objMN.pushKV("consecutivePayments", dmn.pdmnState->nConsecutivePayments);
objMN.pushKV("lastpaidtime", dmnToLastPaidTime(dmn));
objMN.pushKV("lastpaidblock", dmn.pdmnState->nLastPaidHeight);
objMN.pushKV("owneraddress", EncodeDestination(PKHash(dmn.pdmnState->keyIDOwner)));
objMN.pushKV("votingaddress", EncodeDestination(PKHash(dmn.pdmnState->keyIDVoting)));
objMN.pushKV("collateraladdress", collateralAddressStr);
objMN.pushKV("pubkeyoperator", dmn.pdmnState->pubKeyOperator.ToString());
obj.pushKV(strOutpoint, objMN);
} else if (strMode == "lastpaidblock") {
if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, dmn.pdmnState->nLastPaidHeight);
} else if (strMode == "lastpaidtime") {
if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, dmnToLastPaidTime(dmn));
} else if (strMode == "payee") {
if (strFilter !="" && payeeStr.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, payeeStr);
} else if (strMode == "owneraddress") {
if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, EncodeDestination(PKHash(dmn.pdmnState->keyIDOwner)));
} else if (strMode == "pubkeyoperator") {
if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, dmn.pdmnState->pubKeyOperator.ToString());
} else if (strMode == "status") {
std::string strStatus = dmnToStatus(dmn);
if (strFilter !="" && strStatus.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, strStatus);
} else if (strMode == "votingaddress") {
if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return;
obj.pushKV(strOutpoint, EncodeDestination(PKHash(dmn.pdmnState->keyIDVoting)));
}
});
return obj;
},
};
}
static RPCHelpMan masternodelist()
{
return masternodelist_helper(false);
}
static RPCHelpMan masternodelist_composite()
{
return masternodelist_helper(true);
}
void RegisterMasternodeRPCCommands(CRPCTable &t)
{
// clang-format off
static const CRPCCommand commands[] =
{ // category actor (function)
// --------------------- -----------------------
{ "dash", &masternode_help, },
{ "dash", &masternodelist_composite, },
{ "dash", &masternodelist, },
{ "dash", &masternode_connect, },
{ "dash", &masternode_count, },
#ifdef ENABLE_WALLET
{ "dash", &masternode_outputs, },
#endif // ENABLE_WALLET
{ "dash", &masternode_status, },
{ "dash", &masternode_payments, },
{ "dash", &masternode_winners, },
};
// clang-format on
for (const auto& command : commands) {
t.appendCommand(command.name, &command);
}
}