Line data Source code
1 : // Copyright (c) 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 <core_io.h>
6 : #include <governance/common.h>
7 : #include <governance/governance.h>
8 : #include <governance/object.h>
9 : #include <rpc/util.h>
10 : #include <tinyformat.h>
11 : #include <util/check.h>
12 :
13 : #include <univalue.h>
14 :
15 6202 : RPCResult CGovernanceManager::GetJsonHelp(const std::string& key, bool optional)
16 : {
17 12404 : return {RPCResult::Type::OBJ, key, optional, key.empty() ? "" : "Count of governance objects and votes",
18 43414 : {
19 6202 : {RPCResult::Type::NUM, "objects_total", "Total number of all governance objects"},
20 6202 : {RPCResult::Type::NUM, "proposals", "Number of governance proposals"},
21 6202 : {RPCResult::Type::NUM, "triggers", "Number of triggers"},
22 6202 : {RPCResult::Type::NUM, "other", "Total number of unknown governance objects"},
23 6202 : {RPCResult::Type::NUM, "erased", "Number of removed (expired) objects"},
24 6202 : {RPCResult::Type::NUM, "votes", "Total number of votes"},
25 : }};
26 0 : }
27 :
28 64 : UniValue CGovernanceManager::ToJson() const
29 : {
30 64 : LOCK(cs_store);
31 :
32 64 : int nProposalCount = 0;
33 64 : int nTriggerCount = 0;
34 64 : int nOtherCount = 0;
35 :
36 340 : for (const auto& [_, govobj] : mapObjects) {
37 276 : switch (Assert(govobj)->GetObjectType()) {
38 : case GovernanceObject::PROPOSAL:
39 192 : nProposalCount++;
40 192 : break;
41 : case GovernanceObject::TRIGGER:
42 84 : nTriggerCount++;
43 84 : break;
44 : default:
45 0 : nOtherCount++;
46 0 : break;
47 : }
48 : }
49 :
50 64 : UniValue jsonObj(UniValue::VOBJ);
51 64 : jsonObj.pushKV("objects_total", mapObjects.size());
52 64 : jsonObj.pushKV("proposals", nProposalCount);
53 64 : jsonObj.pushKV("triggers", nTriggerCount);
54 64 : jsonObj.pushKV("other", nOtherCount);
55 64 : jsonObj.pushKV("erased", mapErasedGovernanceObjects.size());
56 64 : jsonObj.pushKV("votes", cmapVoteToObject.GetSize());
57 64 : return jsonObj;
58 64 : }
59 :
60 0 : RPCResult CGovernanceObject::GetInnerJsonHelp(const std::string& key, bool optional)
61 : {
62 0 : return Governance::Object::GetJsonHelp(key, optional);
63 : }
64 :
65 0 : UniValue CGovernanceObject::GetInnerJson() const
66 : {
67 0 : return m_obj.ToJson();
68 : }
69 :
70 : // CGovernanceObject::GetStateJson() defined in governance/object.cpp
71 23082 : RPCResult CGovernanceObject::GetStateJsonHelp(const std::string& key, bool optional, const std::string& local_valid_key)
72 : {
73 46164 : return {RPCResult::Type::OBJ, key, optional, key.empty() ? "" : "Object state info",
74 323148 : {
75 23082 : {RPCResult::Type::STR_HEX, "DataHex", "Governance object (hex)"},
76 23082 : {RPCResult::Type::STR, "DataString", "Governance object (string)"},
77 23082 : {RPCResult::Type::STR_HEX, "Hash", "Hash of governance object"},
78 23082 : GetRpcResult("collateralHash", /*optional=*/false, /*override_name=*/"CollateralHash"),
79 23082 : {RPCResult::Type::NUM, "ObjectType", "Object types"},
80 23082 : {RPCResult::Type::NUM, "CreationTime", "Object creation timestamp"},
81 23082 : {RPCResult::Type::STR_HEX, "SigningMasternode", /*optional=*/true, "Signing masternode’s vin (for triggers only)"},
82 23082 : {RPCResult::Type::BOOL, local_valid_key, "Returns true if valid"},
83 23082 : {RPCResult::Type::STR, "IsValidReason", strprintf("%s error (human-readable string, empty if %s true)", local_valid_key, local_valid_key)},
84 23082 : {RPCResult::Type::BOOL, "fCachedValid", "Returns true if minimum support has been reached flagging object as valid"},
85 23082 : {RPCResult::Type::BOOL, "fCachedFunding", "Returns true if minimum support has been reached flagging object as fundable"},
86 23082 : {RPCResult::Type::BOOL, "fCachedDelete", "Returns true if minimum support has been reached flagging object as marked for deletion"},
87 23082 : {RPCResult::Type::BOOL, "fCachedEndorsed", "Returns true if minimum support has been reached flagging object as endorsed"},
88 : }};
89 0 : }
90 :
91 42006 : RPCResult CGovernanceObject::GetVotesJsonHelp(const std::string& key, bool optional)
92 : {
93 84012 : return {RPCResult::Type::OBJ, key, optional, key.empty() ? "" : "Object vote counts",
94 210030 : {
95 42006 : {RPCResult::Type::NUM, "AbsoluteYesCount", "Number of Yes votes minus number of No votes"},
96 42006 : {RPCResult::Type::NUM, "YesCount", "Number of Yes votes"},
97 42006 : {RPCResult::Type::NUM, "NoCount", "Number of No votes"},
98 42006 : {RPCResult::Type::NUM, "AbstainCount", "Number of Abstain votes"},
99 : }};
100 0 : }
101 :
102 18558 : UniValue CGovernanceObject::GetVotesJson(const CDeterministicMNList& tip_mn_list, vote_signal_enum_t signal) const
103 : {
104 18558 : UniValue obj(UniValue::VOBJ);
105 18558 : obj.pushKV("AbsoluteYesCount", GetAbsoluteYesCount(tip_mn_list, signal));
106 18558 : obj.pushKV("YesCount", GetYesCount(tip_mn_list, signal));
107 18558 : obj.pushKV("NoCount", GetNoCount(tip_mn_list, signal));
108 18558 : obj.pushKV("AbstainCount", GetAbstainCount(tip_mn_list, signal));
109 18558 : return obj;
110 18558 : }
111 :
112 : namespace Governance {
113 2904 : RPCResult Object::GetJsonHelp(const std::string& key, bool optional)
114 : {
115 5808 : return {RPCResult::Type::OBJ, key, optional, key.empty() ? "" : "Object info",
116 20328 : {
117 2904 : {RPCResult::Type::STR_HEX, "objectHash", "Hash of proposal object"},
118 2904 : {RPCResult::Type::STR_HEX, "parentHash", "Hash of the parent object (root node has a hash of 0)"},
119 2904 : GetRpcResult("collateralHash"),
120 2904 : {RPCResult::Type::NUM, "createdAt", "Proposal creation timestamp"},
121 2904 : {RPCResult::Type::NUM, "revision", "Proposal revision number"},
122 29040 : {RPCResult::Type::OBJ, "data", "", {
123 : // Fields emitted through GetDataAsPlainString(), read by CProposalValidator
124 2904 : {RPCResult::Type::STR, "end_epoch", /*optional=*/true, "Proposal end timestamp"},
125 2904 : {RPCResult::Type::STR, "name", /*optional=*/true, "Proposal name"},
126 2904 : {RPCResult::Type::STR, "payment_address", /*optional=*/true, "Proposal payment address"},
127 2904 : {RPCResult::Type::STR, "payment_amount", /*optional=*/true, "Proposal payment amount"},
128 2904 : {RPCResult::Type::STR, "start_epoch", /*optional=*/true, "Proposal start timestamp"},
129 2904 : {RPCResult::Type::STR, "type", /*optional=*/true, "Object type"},
130 2904 : {RPCResult::Type::STR, "url", /*optional=*/true, "Proposal URL"},
131 : // Failure case for GetDataAsPlainString()
132 2904 : {RPCResult::Type::STR, "plain", /*optional=*/true, "Governance object data as string"},
133 : // Always emitted by ToJson()
134 2904 : {RPCResult::Type::STR_HEX, "hex", "Governance object data as hex"},
135 : }},
136 : }};
137 0 : }
138 :
139 104 : UniValue Object::ToJson() const
140 : {
141 104 : UniValue obj(UniValue::VOBJ);
142 104 : obj.pushKV("objectHash", GetHash().ToString());
143 104 : obj.pushKV("parentHash", hashParent.ToString());
144 104 : obj.pushKV("collateralHash", collateralHash.ToString());
145 104 : obj.pushKV("createdAt", time);
146 104 : obj.pushKV("revision", revision);
147 104 : UniValue data;
148 104 : if (!data.read(GetDataAsPlainString())) {
149 0 : data.clear();
150 0 : data.setObject();
151 0 : data.pushKV("plain", GetDataAsPlainString());
152 0 : }
153 104 : data.pushKV("hex", GetDataAsHexString());
154 104 : obj.pushKV("data", data);
155 104 : return obj;
156 104 : }
157 : } // namespace Governance
|