Line data Source code
1 : // Copyright (c) 2018-2025 The Dash Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include <evo/dmnstate.h>
6 :
7 : #include <evo/netinfo.h>
8 : #include <script/standard.h>
9 : #include <univalue.h>
10 :
11 0 : std::string CDeterministicMNState::ToString() const
12 : {
13 0 : CTxDestination dest;
14 0 : std::string payoutAddress = "unknown";
15 0 : std::string operatorPayoutAddress = "none";
16 0 : if (ExtractDestination(scriptPayout, dest)) {
17 0 : payoutAddress = EncodeDestination(dest);
18 0 : }
19 0 : if (ExtractDestination(scriptOperatorPayout, dest)) {
20 0 : operatorPayoutAddress = EncodeDestination(dest);
21 0 : }
22 :
23 0 : return strprintf("CDeterministicMNState(nVersion=%d, nRegisteredHeight=%d, nLastPaidHeight=%d, nPoSePenalty=%d, "
24 : "nPoSeRevivedHeight=%d, nPoSeBanHeight=%d, nRevocationReason=%d, "
25 : "ownerAddress=%s, pubKeyOperator=%s, votingAddress=%s, netInfo=%s, payoutAddress=%s, "
26 : "operatorPayoutAddress=%s)\n",
27 0 : nVersion, nRegisteredHeight, nLastPaidHeight, nPoSePenalty, nPoSeRevivedHeight, nPoSeBanHeight,
28 0 : nRevocationReason, EncodeDestination(PKHash(keyIDOwner)), pubKeyOperator.ToString(),
29 0 : EncodeDestination(PKHash(keyIDVoting)), netInfo->ToString(), payoutAddress, operatorPayoutAddress);
30 0 : }
31 :
32 0 : UniValue CDeterministicMNStateDiff::ToJson(MnType nType) const
33 : {
34 0 : UniValue obj(UniValue::VOBJ);
35 0 : if (fields & Field_nVersion) {
36 0 : obj.pushKV("version", state.nVersion);
37 0 : }
38 0 : if (fields & Field_netInfo) {
39 0 : if (IsServiceDeprecatedRPCEnabled()) {
40 0 : obj.pushKV("service", state.netInfo->GetPrimary().ToStringAddrPort());
41 0 : }
42 0 : }
43 0 : if (fields & Field_nRegisteredHeight) {
44 0 : obj.pushKV("registeredHeight", state.nRegisteredHeight);
45 0 : }
46 0 : if (fields & Field_nLastPaidHeight) {
47 0 : obj.pushKV("lastPaidHeight", state.nLastPaidHeight);
48 0 : }
49 0 : if (fields & Field_nConsecutivePayments) {
50 0 : obj.pushKV("consecutivePayments", state.nConsecutivePayments);
51 0 : }
52 0 : if (fields & Field_nPoSePenalty) {
53 0 : obj.pushKV("PoSePenalty", state.nPoSePenalty);
54 0 : }
55 0 : if (fields & Field_nPoSeRevivedHeight) {
56 0 : obj.pushKV("PoSeRevivedHeight", state.nPoSeRevivedHeight);
57 0 : }
58 0 : if (fields & Field_nPoSeBanHeight) {
59 0 : obj.pushKV("PoSeBanHeight", state.nPoSeBanHeight);
60 0 : }
61 0 : if (fields & Field_nRevocationReason) {
62 0 : obj.pushKV("revocationReason", state.nRevocationReason);
63 0 : }
64 0 : if (fields & Field_keyIDOwner) {
65 0 : obj.pushKV("ownerAddress", EncodeDestination(PKHash(state.keyIDOwner)));
66 0 : }
67 0 : if (fields & Field_keyIDVoting) {
68 0 : obj.pushKV("votingAddress", EncodeDestination(PKHash(state.keyIDVoting)));
69 0 : }
70 0 : if (fields & Field_scriptPayout) {
71 0 : CTxDestination dest;
72 0 : if (ExtractDestination(state.scriptPayout, dest)) {
73 0 : obj.pushKV("payoutAddress", EncodeDestination(dest));
74 0 : }
75 0 : }
76 0 : if (fields & Field_scriptOperatorPayout) {
77 0 : CTxDestination dest;
78 0 : if (ExtractDestination(state.scriptOperatorPayout, dest)) {
79 0 : obj.pushKV("operatorPayoutAddress", EncodeDestination(dest));
80 0 : }
81 0 : }
82 0 : if (fields & Field_pubKeyOperator) {
83 0 : obj.pushKV("pubKeyOperator", state.pubKeyOperator.ToString());
84 0 : }
85 0 : if (nType == MnType::Evo) {
86 0 : if (fields & Field_platformNodeID) {
87 0 : obj.pushKV("platformNodeID", state.platformNodeID.ToString());
88 0 : }
89 0 : if (IsServiceDeprecatedRPCEnabled()) {
90 : // platformP2PPort/platformHTTPPort are deprecated scalar duplicates of netInfo's
91 : // Platform entries. From ExtAddr onwards the scalar fields are unused (always 0), so
92 : // when the diff carries an ExtAddr netInfo report the live port from it to stay
93 : // consistent with the "addresses" output below.
94 0 : const bool has_ext_netinfo = (fields & Field_netInfo) && state.netInfo->CanStorePlatform();
95 0 : if (fields & Field_platformP2PPort) {
96 0 : obj.pushKV("platformP2PPort",
97 0 : has_ext_netinfo && state.netInfo->HasEntries(NetInfoPurpose::PLATFORM_P2P)
98 0 : ? state.netInfo->GetEntries(NetInfoPurpose::PLATFORM_P2P)[0].GetPort()
99 0 : : state.platformP2PPort);
100 0 : }
101 0 : if (fields & Field_platformHTTPPort) {
102 0 : obj.pushKV("platformHTTPPort",
103 0 : has_ext_netinfo && state.netInfo->HasEntries(NetInfoPurpose::PLATFORM_HTTPS)
104 0 : ? state.netInfo->GetEntries(NetInfoPurpose::PLATFORM_HTTPS)[0].GetPort()
105 0 : : state.platformHTTPPort);
106 0 : }
107 0 : }
108 0 : }
109 : {
110 0 : const bool has_netinfo = (fields & Field_netInfo);
111 :
112 0 : UniValue netInfoObj(UniValue::VOBJ);
113 0 : if (has_netinfo) {
114 0 : netInfoObj = state.netInfo->ToJson();
115 0 : }
116 0 : if (nType == MnType::Evo && (!has_netinfo || !state.netInfo->CanStorePlatform())) {
117 0 : auto unknownAddr = [](uint16_t port) -> UniValue {
118 0 : UniValue obj(UniValue::VARR);
119 : // We don't know what the address is because it wasn't changed in the
120 : // diff but we still need to report the port number in addr:port format
121 0 : obj.push_back(strprintf("255.255.255.255:%d", port));
122 0 : return obj;
123 0 : };
124 0 : if (fields & Field_platformP2PPort) {
125 0 : netInfoObj.pushKV(PurposeToString(NetInfoPurpose::PLATFORM_P2P).data(),
126 0 : (has_netinfo)
127 0 : ? ArrFromService(CService(state.netInfo->GetPrimary(), state.platformP2PPort))
128 0 : : unknownAddr(state.platformP2PPort));
129 0 : }
130 0 : if (fields & Field_platformHTTPPort) {
131 0 : netInfoObj.pushKV(PurposeToString(NetInfoPurpose::PLATFORM_HTTPS).data(),
132 0 : (has_netinfo)
133 0 : ? ArrFromService(CService(state.netInfo->GetPrimary(), state.platformHTTPPort))
134 0 : : unknownAddr(state.platformHTTPPort));
135 0 : }
136 0 : }
137 0 : if (!netInfoObj.empty()) {
138 0 : obj.pushKV("addresses", netInfoObj);
139 0 : }
140 0 : }
141 0 : return obj;
142 0 : }
|