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