Skip to content

Commit 57edf31

Browse files
committed
rpc: deduplicate routines used to generate governance RPC results
1 parent 5866061 commit 57edf31

File tree

5 files changed

+60
-107
lines changed

5 files changed

+60
-107
lines changed

src/governance/core_write.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,26 @@ UniValue CGovernanceManager::ToJson() const
5555
return jsonObj;
5656
}
5757

58-
RPCResult CGovernanceObject::GetJsonHelp(const std::string& key, bool optional)
58+
RPCResult CGovernanceObject::GetInnerJsonHelp(const std::string& key, bool optional)
5959
{
6060
return Governance::Object::GetJsonHelp(key, optional);
6161
}
6262

63-
UniValue CGovernanceObject::ToJson() const
63+
UniValue CGovernanceObject::GetInnerJson() const
6464
{
6565
return m_obj.ToJson();
6666
}
6767

68+
UniValue CGovernanceObject::GetVotesJson(const CDeterministicMNList& tip_mn_list, vote_signal_enum_t signal) const
69+
{
70+
UniValue obj(UniValue::VOBJ);
71+
obj.pushKV("AbsoluteYesCount", GetAbsoluteYesCount(tip_mn_list, signal));
72+
obj.pushKV("YesCount", GetYesCount(tip_mn_list, signal));
73+
obj.pushKV("NoCount", GetNoCount(tip_mn_list, signal));
74+
obj.pushKV("AbstainCount", GetAbstainCount(tip_mn_list, signal));
75+
return obj;
76+
}
77+
6878
namespace Governance {
6979
RPCResult Object::GetJsonHelp(const std::string& key, bool optional)
7080
{

src/governance/object.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,28 @@ UniValue CGovernanceObject::GetJSONObject() const
278278
return obj;
279279
}
280280

281+
UniValue CGovernanceObject::GetStateJson(const ChainstateManager& chainman, const CDeterministicMNList& tip_mn_list, const std::string& local_valid_key) const
282+
{
283+
UniValue ret(UniValue::VOBJ);
284+
ret.pushKV("DataHex", GetDataAsHexString());
285+
ret.pushKV("DataString", GetDataAsPlainString());
286+
ret.pushKV("Hash", GetHash().ToString());
287+
ret.pushKV("CollateralHash", GetCollateralHash().ToString());
288+
ret.pushKV("ObjectType", ToUnderlying(GetObjectType()));
289+
ret.pushKV("CreationTime", GetCreationTime());
290+
if (const COutPoint& outpoint = GetMasternodeOutpoint(); outpoint != COutPoint{}) {
291+
ret.pushKV("SigningMasternode", outpoint.ToStringShort());
292+
}
293+
std::string strError;
294+
ret.pushKV(local_valid_key, WITH_LOCK(::cs_main, return IsValidLocally(tip_mn_list, chainman, strError, /*fCheckCollateral=*/false)));
295+
ret.pushKV("IsValidReason", strError.c_str());
296+
ret.pushKV("fCachedValid", IsSetCachedValid());
297+
ret.pushKV("fCachedFunding", IsSetCachedFunding());
298+
ret.pushKV("fCachedDelete", IsSetCachedDelete());
299+
ret.pushKV("fCachedEndorsed", IsSetCachedEndorsed());
300+
return ret;
301+
}
302+
281303
/**
282304
* LoadData
283305
* --------------------------------------------------------

src/governance/object.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,15 @@ class CGovernanceObject
261261
// AFTER DESERIALIZATION OCCURS, CACHED VARIABLES MUST BE CALCULATED MANUALLY
262262
}
263263

264-
[[nodiscard]] static RPCResult GetJsonHelp(const std::string& key, bool optional);
265-
[[nodiscard]] UniValue ToJson() const;
264+
// JSON emitters/help
265+
[[nodiscard]] static RPCResult GetInnerJsonHelp(const std::string& key, bool optional);
266+
[[nodiscard]] UniValue GetInnerJson() const;
267+
268+
[[nodiscard]] UniValue GetStateJson(const ChainstateManager& chainman, const CDeterministicMNList& tip_mn_list, const std::string& local_valid_key) const
269+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
270+
271+
[[nodiscard]] UniValue GetVotesJson(const CDeterministicMNList& tip_mn_list, vote_signal_enum_t signal) const
272+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
266273

267274
// FUNCTIONS FOR DEALING WITH DATA STRING
268275
void LoadData();

src/qt/governancelist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void Proposal::openUrl() const
110110

111111
QString Proposal::toJson() const
112112
{
113-
const auto json = govObj.ToJson();
113+
const auto json = govObj.GetInnerJson();
114114
return QString::fromStdString(json.write(2));
115115
}
116116

src/rpc/governance.cpp

Lines changed: 16 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -623,22 +623,15 @@ static RPCHelpMan gobject_vote_alias()
623623
static UniValue ListObjects(CGovernanceManager& govman, const CDeterministicMNList& tip_mn_list, const ChainstateManager& chainman,
624624
const std::string& strCachedSignal, const std::string& strType, int nStartTime)
625625
{
626-
UniValue objResult(UniValue::VOBJ);
627-
628-
// GET MATCHING GOVERNANCE OBJECTS
629-
630626
if (g_txindex) {
631627
g_txindex->BlockUntilSyncedToCurrentChain();
632628
}
633629

634-
LOCK(cs_main);
635-
636630
std::vector<CGovernanceObject> objs;
637631
govman.GetAllNewerThan(objs, nStartTime);
638-
639632
govman.UpdateLastDiffTime(GetTime());
640-
// CREATE RESULTS FOR USER
641633

634+
UniValue objResult(UniValue::VOBJ);
642635
for (const auto& govObj : objs) {
643636
if (strCachedSignal == "valid" && !govObj.IsSetCachedValid()) continue;
644637
if (strCachedSignal == "funding" && !govObj.IsSetCachedFunding()) continue;
@@ -648,34 +641,13 @@ static UniValue ListObjects(CGovernanceManager& govman, const CDeterministicMNLi
648641
if (strType == "proposals" && govObj.GetObjectType() != GovernanceObject::PROPOSAL) continue;
649642
if (strType == "triggers" && govObj.GetObjectType() != GovernanceObject::TRIGGER) continue;
650643

651-
UniValue bObj(UniValue::VOBJ);
652-
bObj.pushKV("DataHex", govObj.GetDataAsHexString());
653-
bObj.pushKV("DataString", govObj.GetDataAsPlainString());
654-
bObj.pushKV("Hash", govObj.GetHash().ToString());
655-
bObj.pushKV("CollateralHash", govObj.GetCollateralHash().ToString());
656-
bObj.pushKV("ObjectType", ToUnderlying(govObj.GetObjectType()));
657-
bObj.pushKV("CreationTime", govObj.GetCreationTime());
658-
const COutPoint& masternodeOutpoint = govObj.GetMasternodeOutpoint();
659-
if (masternodeOutpoint != COutPoint()) {
660-
bObj.pushKV("SigningMasternode", masternodeOutpoint.ToStringShort());
661-
}
662-
663-
// REPORT STATUS FOR FUNDING VOTES SPECIFICALLY
664-
bObj.pushKV("AbsoluteYesCount", govObj.GetAbsoluteYesCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
665-
bObj.pushKV("YesCount", govObj.GetYesCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
666-
bObj.pushKV("NoCount", govObj.GetNoCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
667-
bObj.pushKV("AbstainCount", govObj.GetAbstainCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
668-
669-
// REPORT VALIDITY AND CACHING FLAGS FOR VARIOUS SETTINGS
670-
std::string strError;
671-
bObj.pushKV("fBlockchainValidity", govObj.IsValidLocally(tip_mn_list, chainman, strError, false));
672-
bObj.pushKV("IsValidReason", strError.c_str());
673-
bObj.pushKV("fCachedValid", govObj.IsSetCachedValid());
674-
bObj.pushKV("fCachedFunding", govObj.IsSetCachedFunding());
675-
bObj.pushKV("fCachedDelete", govObj.IsSetCachedDelete());
676-
bObj.pushKV("fCachedEndorsed", govObj.IsSetCachedEndorsed());
677-
678-
objResult.pushKV(govObj.GetHash().ToString(), bObj);
644+
UniValue entry{govObj.GetStateJson(chainman, tip_mn_list, /*local_valid_key=*/"fBlockchainValidity")};
645+
UniValue votes_funding{govObj.GetVotesJson(tip_mn_list, VOTE_SIGNAL_FUNDING)};
646+
entry.pushKV("AbsoluteYesCount", votes_funding["AbsoluteYesCount"]);
647+
entry.pushKV("YesCount", votes_funding["YesCount"]);
648+
entry.pushKV("NoCount", votes_funding["NoCount"]);
649+
entry.pushKV("AbstainCount", votes_funding["AbstainCount"]);
650+
objResult.pushKV(govObj.GetHash().ToString(), entry);
679651
}
680652

681653
return objResult;
@@ -763,84 +735,26 @@ static RPCHelpMan gobject_get()
763735
RPCExamples{""},
764736
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
765737
{
766-
// COLLECT VARIABLES FROM OUR USER
767-
uint256 hash(ParseHashV(request.params[0], "GovObj hash"));
768-
769738
if (g_txindex) {
770739
g_txindex->BlockUntilSyncedToCurrentChain();
771740
}
772741

773-
// FIND THE GOVERNANCE OBJECT THE USER IS LOOKING FOR
774742
const NodeContext& node = EnsureAnyNodeContext(request.context);
775743
const ChainstateManager& chainman = EnsureChainman(node);
776744

777-
CHECK_NONFATAL(node.govman);
778-
LOCK(cs_main);
779-
auto pGovObj = node.govman->FindConstGovernanceObject(hash);
780-
745+
uint256 hash(ParseHashV(request.params[0], "GovObj hash"));
746+
auto pGovObj = CHECK_NONFATAL(node.govman)->FindConstGovernanceObject(hash);
781747
if (pGovObj == nullptr) {
782748
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance object");
783749
}
784750

785-
// REPORT BASIC OBJECT STATS
786-
787-
UniValue objResult(UniValue::VOBJ);
788-
objResult.pushKV("DataHex", pGovObj->GetDataAsHexString());
789-
objResult.pushKV("DataString", pGovObj->GetDataAsPlainString());
790-
objResult.pushKV("Hash", pGovObj->GetHash().ToString());
791-
objResult.pushKV("CollateralHash", pGovObj->GetCollateralHash().ToString());
792-
objResult.pushKV("ObjectType", ToUnderlying(pGovObj->GetObjectType()));
793-
objResult.pushKV("CreationTime", pGovObj->GetCreationTime());
794-
const COutPoint& masternodeOutpoint = pGovObj->GetMasternodeOutpoint();
795-
if (masternodeOutpoint != COutPoint()) {
796-
objResult.pushKV("SigningMasternode", masternodeOutpoint.ToStringShort());
797-
}
798-
799-
// SHOW (MUCH MORE) INFORMATION ABOUT VOTES FOR GOVERNANCE OBJECT (THAN LIST/DIFF ABOVE)
800-
// -- FUNDING VOTING RESULTS
801-
802751
auto tip_mn_list = CHECK_NONFATAL(node.dmnman)->GetListAtChainTip();
803-
804-
UniValue objFundingResult(UniValue::VOBJ);
805-
objFundingResult.pushKV("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
806-
objFundingResult.pushKV("YesCount", pGovObj->GetYesCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
807-
objFundingResult.pushKV("NoCount", pGovObj->GetNoCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
808-
objFundingResult.pushKV("AbstainCount", pGovObj->GetAbstainCount(tip_mn_list, VOTE_SIGNAL_FUNDING));
809-
objResult.pushKV("FundingResult", objFundingResult);
810-
811-
// -- VALIDITY VOTING RESULTS
812-
UniValue objValid(UniValue::VOBJ);
813-
objValid.pushKV("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(tip_mn_list, VOTE_SIGNAL_VALID));
814-
objValid.pushKV("YesCount", pGovObj->GetYesCount(tip_mn_list, VOTE_SIGNAL_VALID));
815-
objValid.pushKV("NoCount", pGovObj->GetNoCount(tip_mn_list, VOTE_SIGNAL_VALID));
816-
objValid.pushKV("AbstainCount", pGovObj->GetAbstainCount(tip_mn_list, VOTE_SIGNAL_VALID));
817-
objResult.pushKV("ValidResult", objValid);
818-
819-
// -- DELETION CRITERION VOTING RESULTS
820-
UniValue objDelete(UniValue::VOBJ);
821-
objDelete.pushKV("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(tip_mn_list, VOTE_SIGNAL_DELETE));
822-
objDelete.pushKV("YesCount", pGovObj->GetYesCount(tip_mn_list, VOTE_SIGNAL_DELETE));
823-
objDelete.pushKV("NoCount", pGovObj->GetNoCount(tip_mn_list, VOTE_SIGNAL_DELETE));
824-
objDelete.pushKV("AbstainCount", pGovObj->GetAbstainCount(tip_mn_list, VOTE_SIGNAL_DELETE));
825-
objResult.pushKV("DeleteResult", objDelete);
826-
827-
// -- ENDORSED VIA MASTERNODE-ELECTED BOARD
828-
UniValue objEndorsed(UniValue::VOBJ);
829-
objEndorsed.pushKV("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(tip_mn_list, VOTE_SIGNAL_ENDORSED));
830-
objEndorsed.pushKV("YesCount", pGovObj->GetYesCount(tip_mn_list, VOTE_SIGNAL_ENDORSED));
831-
objEndorsed.pushKV("NoCount", pGovObj->GetNoCount(tip_mn_list, VOTE_SIGNAL_ENDORSED));
832-
objEndorsed.pushKV("AbstainCount", pGovObj->GetAbstainCount(tip_mn_list, VOTE_SIGNAL_ENDORSED));
833-
objResult.pushKV("EndorsedResult", objEndorsed);
834-
835-
// --
836-
std::string strError;
837-
objResult.pushKV("fLocalValidity", pGovObj->IsValidLocally(tip_mn_list, chainman, strError, false));
838-
objResult.pushKV("IsValidReason", strError.c_str());
839-
objResult.pushKV("fCachedValid", pGovObj->IsSetCachedValid());
840-
objResult.pushKV("fCachedFunding", pGovObj->IsSetCachedFunding());
841-
objResult.pushKV("fCachedDelete", pGovObj->IsSetCachedDelete());
842-
objResult.pushKV("fCachedEndorsed", pGovObj->IsSetCachedEndorsed());
843-
return objResult;
752+
UniValue ret{pGovObj->GetStateJson(chainman, tip_mn_list, /*local_valid_key=*/"fLocalValidity")};
753+
ret.pushKV("FundingResult", pGovObj->GetVotesJson(tip_mn_list, VOTE_SIGNAL_FUNDING));
754+
ret.pushKV("ValidResult", pGovObj->GetVotesJson(tip_mn_list, VOTE_SIGNAL_VALID));
755+
ret.pushKV("DeleteResult", pGovObj->GetVotesJson(tip_mn_list, VOTE_SIGNAL_DELETE));
756+
ret.pushKV("EndorsedResult", pGovObj->GetVotesJson(tip_mn_list, VOTE_SIGNAL_ENDORSED));
757+
return ret;
844758
},
845759
};
846760
}

0 commit comments

Comments
 (0)