Skip to content

Commit 5866061

Browse files
committed
rpc: add GetJsonHelp() defs for CGovernance{Manager,Object},Object
Also gives us the help text for `gobject list-prepared`
1 parent 0efaad3 commit 5866061

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed

src/governance/common.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,32 @@
1313
#include <string>
1414
#include <vector>
1515

16+
struct RPCResult;
17+
18+
class UniValue;
19+
1620
/**
1721
* This module is a public interface of governance module that can be used
1822
* in other components such as wallet
1923
*/
2024

21-
class UniValue;
22-
2325
enum class GovernanceObject : int {
2426
UNKNOWN = 0,
2527
PROPOSAL,
2628
TRIGGER
2729
};
2830
template<> struct is_serializable_enum<GovernanceObject> : std::true_type {};
2931

30-
namespace Governance
31-
{
32+
namespace Governance {
3233
class Object
3334
{
3435
public:
3536
Object() = default;
3637

3738
Object(const uint256& nHashParent, int nRevision, int64_t nTime, const uint256& nCollateralHash, const std::string& strDataHex);
3839

39-
UniValue ToJson() const;
40+
[[nodiscard]] static RPCResult GetJsonHelp(const std::string& key, bool optional);
41+
[[nodiscard]] UniValue ToJson() const;
4042

4143
uint256 GetHash() const;
4244

src/governance/core_write.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
#include <governance/common.h>
66
#include <governance/governance.h>
77

8+
#include <rpc/util.h>
89
#include <util/check.h>
910

1011
#include <univalue.h>
1112

13+
RPCResult CGovernanceManager::GetJsonHelp(const std::string& key, bool optional)
14+
{
15+
return {RPCResult::Type::OBJ, key, optional, key.empty() ? "" : "Count of governance objects and votes",
16+
{
17+
{RPCResult::Type::NUM, "objects_total", "Total number of all governance objects"},
18+
{RPCResult::Type::NUM, "proposals", "Number of governance proposals"},
19+
{RPCResult::Type::NUM, "triggers", "Number of triggers"},
20+
{RPCResult::Type::NUM, "other", "Total number of unknown governance objects"},
21+
{RPCResult::Type::NUM, "erased", "Number of removed (expired) objects"},
22+
{RPCResult::Type::NUM, "votes", "Total number of votes"},
23+
}};
24+
}
25+
1226
UniValue CGovernanceManager::ToJson() const
1327
{
1428
LOCK(cs_store);
@@ -41,12 +55,43 @@ UniValue CGovernanceManager::ToJson() const
4155
return jsonObj;
4256
}
4357

58+
RPCResult CGovernanceObject::GetJsonHelp(const std::string& key, bool optional)
59+
{
60+
return Governance::Object::GetJsonHelp(key, optional);
61+
}
62+
4463
UniValue CGovernanceObject::ToJson() const
4564
{
4665
return m_obj.ToJson();
4766
}
4867

4968
namespace Governance {
69+
RPCResult Object::GetJsonHelp(const std::string& key, bool optional)
70+
{
71+
return {RPCResult::Type::OBJ, key, optional, key.empty() ? "" : "Object info",
72+
{
73+
{RPCResult::Type::STR_HEX, "objectHash", "Hash of proposal object"},
74+
{RPCResult::Type::STR_HEX, "parentHash", "Hash of the parent object (root node has a hash of 0)"},
75+
GetRpcResult("collateralHash"),
76+
{RPCResult::Type::NUM, "createdAt", "Proposal creation timestamp"},
77+
{RPCResult::Type::NUM, "revision", "Proposal revision number"},
78+
{RPCResult::Type::OBJ, "data", "", {
79+
// Fields emitted through GetDataAsPlainString(), read by CProposalValidator
80+
{RPCResult::Type::STR, "end_epoch", /*optional=*/true, "Proposal end timestamp"},
81+
{RPCResult::Type::STR, "name", /*optional=*/true, "Proposal name"},
82+
{RPCResult::Type::STR, "payment_address", /*optional=*/true, "Proposal payment address"},
83+
{RPCResult::Type::STR, "payment_amount", /*optional=*/true, "Proposal payment amount"},
84+
{RPCResult::Type::STR, "start_epoch", /*optional=*/true, "Proposal start timestamp"},
85+
{RPCResult::Type::STR, "type", /*optional=*/true, "Object type"},
86+
{RPCResult::Type::STR, "url", /*optional=*/true, "Proposal URL"},
87+
// Failure case for GetDataAsPlainString()
88+
{RPCResult::Type::STR, "plain", /*optional=*/true, "Governance object data as string"},
89+
// Always emitted by ToJson()
90+
{RPCResult::Type::STR_HEX, "hex", "Governance object data as hex"},
91+
}},
92+
}};
93+
}
94+
5095
UniValue Object::ToJson() const
5196
{
5297
UniValue obj(UniValue::VOBJ);

src/governance/governance.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ template<typename T>
3131
class CFlatDB;
3232
class CInv;
3333
class CNode;
34+
struct RPCResult;
3435

3536
class CDeterministicMNList;
3637
class CDeterministicMNManager;
@@ -282,9 +283,10 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
282283
bool IsValid() const override { return is_valid; }
283284
bool LoadCache(bool load_cache)
284285
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
286+
[[nodiscard]] static RPCResult GetJsonHelp(const std::string& key, bool optional);
285287
std::string ToString() const
286288
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
287-
UniValue ToJson() const
289+
[[nodiscard]] UniValue ToJson() const
288290
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
289291
void Clear()
290292
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);

src/governance/object.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CGovernanceObject;
2222
class CGovernanceVote;
2323
class ChainstateManager;
2424
class CMasternodeMetaMan;
25+
struct RPCResult;
2526

2627
extern RecursiveMutex cs_main;
2728

@@ -260,7 +261,8 @@ class CGovernanceObject
260261
// AFTER DESERIALIZATION OCCURS, CACHED VARIABLES MUST BE CALCULATED MANUALLY
261262
}
262263

263-
UniValue ToJson() const;
264+
[[nodiscard]] static RPCResult GetJsonHelp(const std::string& key, bool optional);
265+
[[nodiscard]] UniValue ToJson() const;
264266

265267
// FUNCTIONS FOR DEALING WITH DATA STRING
266268
void LoadData();

src/rpc/governance.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,14 @@ using wallet::isminetype;
3838

3939
static RPCHelpMan gobject_count()
4040
{
41+
const auto json_help{CGovernanceManager::GetJsonHelp(/*key=*/"", /*optional=*/false)};
4142
return RPCHelpMan{"gobject count",
4243
"Count governance objects and votes\n",
4344
{
4445
{"mode", RPCArg::Type::STR, RPCArg::DefaultHint{"json"}, "Output format: json (\"json\") or string in free form (\"all\")"},
4546
},
4647
{
47-
RPCResult{"for mode = json",
48-
RPCResult::Type::OBJ, "", "",
49-
{
50-
{RPCResult::Type::NUM, "objects_total", "Total number of all governance objects"},
51-
{RPCResult::Type::NUM, "proposals", "Number of governance proposals"},
52-
{RPCResult::Type::NUM, "triggers", "Number of triggers"},
53-
{RPCResult::Type::NUM, "other", "Total number of unknown governance objects"},
54-
{RPCResult::Type::NUM, "erased", "Number of removed (expired) objects"},
55-
{RPCResult::Type::NUM, "votes", "Total number of votes"},
56-
}
57-
},
48+
RPCResult{"for mode = json", json_help.m_type, json_help.m_key_name, json_help.m_description, json_help.m_inner},
5849
RPCResult{"for mode = all", RPCResult::Type::STR, "", "Human-friendly summary string for proposals and votes"},
5950
},
6051
RPCExamples{""},
@@ -275,11 +266,7 @@ static RPCHelpMan gobject_list_prepared()
275266
RPCResult{
276267
RPCResult::Type::ARR, "", "list of governance objects",
277268
{
278-
{RPCResult::Type::OBJ, "", "",
279-
{
280-
// TODO: list fields of output for RPC help instead ELISION
281-
{RPCResult::Type::ELISION, "", ""}
282-
}},
269+
Governance::Object::GetJsonHelp(/*key=*/"", /*optional=*/false)
283270
}
284271
},
285272
RPCExamples{""},

0 commit comments

Comments
 (0)