Line data Source code
1 : // Copyright (c) 2018-2025 The Dash Core developers
2 : // Distributed under the MIT/X11 software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include <llmq/debug.h>
6 :
7 : #include <evo/deterministicmns.h>
8 : #include <llmq/dkgsessionhandler.h>
9 : #include <llmq/utils.h>
10 : #include <util/helpers.h>
11 : #include <util/std23.h>
12 :
13 : #include <chainparams.h>
14 : #include <timedata.h>
15 : #include <validation.h>
16 :
17 : namespace llmq
18 : {
19 0 : UniValue CDKGDebugSessionStatus::ToJson(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
20 : const ChainstateManager& chainman, int quorumIndex, int detailLevel) const
21 : {
22 0 : UniValue ret(UniValue::VOBJ);
23 :
24 0 : if (!Params().GetLLMQ(llmqType).has_value() || quorumHash.IsNull()) {
25 0 : return ret;
26 : }
27 :
28 0 : std::vector<CDeterministicMNCPtr> dmnMembers;
29 0 : if (detailLevel == 2) {
30 0 : const CBlockIndex* pindex = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(quorumHash));
31 0 : if (pindex != nullptr) {
32 0 : dmnMembers = utils::GetAllQuorumMembers(llmqType, {dmnman, qsnapman, chainman, pindex});
33 0 : }
34 0 : }
35 :
36 0 : ret.pushKV("llmqType", std23::to_underlying(llmqType));
37 0 : ret.pushKV("quorumHash", quorumHash.ToString());
38 0 : ret.pushKV("quorumHeight", quorumHeight);
39 0 : ret.pushKV("phase", std23::to_underlying(phase));
40 :
41 0 : ret.pushKV("sentContributions", statusBits.sentContributions);
42 0 : ret.pushKV("sentComplaint", statusBits.sentComplaint);
43 0 : ret.pushKV("sentJustification", statusBits.sentJustification);
44 0 : ret.pushKV("sentPrematureCommitment", statusBits.sentPrematureCommitment);
45 0 : ret.pushKV("aborted", statusBits.aborted);
46 :
47 0 : struct ArrOrCount {
48 0 : int count{0};
49 0 : UniValue arr{UniValue::VARR};
50 : };
51 :
52 0 : ArrOrCount badMembers;
53 0 : ArrOrCount weComplain;
54 0 : ArrOrCount receivedContributions;
55 0 : ArrOrCount receivedComplaints;
56 0 : ArrOrCount receivedJustifications;
57 0 : ArrOrCount receivedPrematureCommitments;
58 0 : ArrOrCount complaintsFromMembers;
59 :
60 0 : auto add = [&](ArrOrCount& v, size_t idx, bool flag) {
61 0 : if (flag) {
62 0 : if (detailLevel == 0) {
63 0 : v.count++;
64 0 : } else if (detailLevel == 1) {
65 0 : v.arr.push_back(idx);
66 0 : } else if (detailLevel == 2) {
67 0 : UniValue a(UniValue::VOBJ);
68 0 : a.pushKV("memberIndex", idx);
69 0 : if (idx < dmnMembers.size()) {
70 0 : a.pushKV("proTxHash", dmnMembers[idx]->proTxHash.ToString());
71 0 : }
72 0 : v.arr.push_back(a);
73 0 : }
74 0 : }
75 0 : };
76 0 : auto push = [&](const ArrOrCount& v, const std::string& name) {
77 0 : if (detailLevel == 0) {
78 0 : ret.pushKV(name, v.count);
79 0 : } else {
80 0 : ret.pushKV(name, v.arr);
81 : }
82 0 : };
83 :
84 0 : for (const auto i : util::irange(members.size())) {
85 0 : const auto& m = members[i];
86 0 : add(badMembers, i, m.statusBits.bad);
87 0 : add(weComplain, i, m.statusBits.weComplain);
88 0 : add(receivedContributions, i, m.statusBits.receivedContribution);
89 0 : add(receivedComplaints, i, m.statusBits.receivedComplaint);
90 0 : add(receivedJustifications, i, m.statusBits.receivedJustification);
91 0 : add(receivedPrematureCommitments, i, m.statusBits.receivedPrematureCommitment);
92 : }
93 0 : push(badMembers, "badMembers");
94 0 : push(weComplain, "weComplain");
95 0 : push(receivedContributions, "receivedContributions");
96 0 : push(receivedComplaints, "receivedComplaints");
97 0 : push(receivedJustifications, "receivedJustifications");
98 0 : push(receivedPrematureCommitments, "receivedPrematureCommitments");
99 :
100 0 : if (detailLevel == 2) {
101 0 : UniValue arr(UniValue::VARR);
102 0 : for (const auto& dmn : dmnMembers) {
103 0 : arr.push_back(dmn->proTxHash.ToString());
104 : }
105 0 : ret.pushKV("allMembers", arr);
106 0 : }
107 :
108 0 : return ret;
109 0 : }
110 :
111 0 : CDKGDebugManager::CDKGDebugManager(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
112 : const ChainstateManager& chainman) :
113 0 : m_dmnman{dmnman},
114 0 : m_qsnapman{qsnapman},
115 0 : m_chainman{chainman}
116 0 : {
117 0 : }
118 :
119 0 : CDKGDebugManager::~CDKGDebugManager() = default;
120 :
121 0 : size_t CDKGDebugManager::GetSessionCount() const
122 : {
123 0 : return WITH_LOCK(cs_lockStatus, return localStatus.sessions.size());
124 : }
125 :
126 0 : UniValue CDKGDebugManager::ToJson(int detailLevel) const
127 : {
128 0 : LOCK(cs_lockStatus);
129 :
130 0 : UniValue ret(UniValue::VOBJ);
131 0 : ret.pushKV("time", localStatus.nTime);
132 0 : ret.pushKV("timeStr", FormatISO8601DateTime(localStatus.nTime));
133 :
134 : // TODO Support array of sessions
135 0 : UniValue sessionsArrJson(UniValue::VARR);
136 0 : for (const auto& p : localStatus.sessions) {
137 0 : const auto& llmq_params_opt = Params().GetLLMQ(p.first.first);
138 0 : if (!llmq_params_opt.has_value()) {
139 0 : continue;
140 : }
141 0 : UniValue s(UniValue::VOBJ);
142 0 : s.pushKV("llmqType", std::string(llmq_params_opt->name));
143 0 : s.pushKV("quorumIndex", p.first.second);
144 0 : s.pushKV("status", p.second.ToJson(m_dmnman, m_qsnapman, m_chainman, p.first.second, detailLevel));
145 :
146 0 : sessionsArrJson.push_back(s);
147 0 : }
148 0 : ret.pushKV("session", sessionsArrJson);
149 :
150 0 : return ret;
151 0 : }
152 :
153 0 : void CDKGDebugManager::ResetLocalSessionStatus(Consensus::LLMQType llmqType, int quorumIndex)
154 : {
155 0 : LOCK(cs_lockStatus);
156 :
157 0 : auto it = localStatus.sessions.find(std::make_pair(llmqType, quorumIndex));
158 0 : if (it == localStatus.sessions.end()) {
159 0 : return;
160 : }
161 :
162 0 : localStatus.sessions.erase(it);
163 0 : localStatus.nTime = GetAdjustedTime();
164 0 : }
165 :
166 0 : void CDKGDebugManager::InitLocalSessionStatus(const Consensus::LLMQParams& llmqParams, int quorumIndex, const uint256& quorumHash, int quorumHeight)
167 : {
168 0 : LOCK(cs_lockStatus);
169 :
170 0 : auto it = localStatus.sessions.find(std::make_pair(llmqParams.type, quorumIndex));
171 0 : if (it == localStatus.sessions.end()) {
172 0 : it = localStatus.sessions.emplace(std::make_pair(llmqParams.type, quorumIndex), CDKGDebugSessionStatus()).first;
173 0 : }
174 :
175 0 : auto& session = it->second;
176 0 : session.llmqType = llmqParams.type;
177 0 : session.quorumHash = quorumHash;
178 0 : session.quorumHeight = (uint32_t)quorumHeight;
179 0 : session.phase = QuorumPhase{0};
180 0 : session.statusBitset = 0;
181 0 : session.members.clear();
182 0 : session.members.resize((size_t)llmqParams.size);
183 0 : }
184 :
185 0 : void CDKGDebugManager::UpdateLocalSessionStatus(Consensus::LLMQType llmqType, int quorumIndex, std::function<bool(CDKGDebugSessionStatus& status)>&& func)
186 : {
187 0 : LOCK(cs_lockStatus);
188 :
189 0 : auto it = localStatus.sessions.find(std::make_pair(llmqType, quorumIndex));
190 0 : if (it == localStatus.sessions.end()) {
191 0 : return;
192 : }
193 :
194 0 : if (func(it->second)) {
195 0 : localStatus.nTime = GetAdjustedTime();
196 0 : }
197 0 : }
198 :
199 0 : void CDKGDebugManager::UpdateLocalMemberStatus(Consensus::LLMQType llmqType, int quorumIndex, size_t memberIdx, std::function<bool(CDKGDebugMemberStatus& status)>&& func)
200 : {
201 0 : LOCK(cs_lockStatus);
202 :
203 0 : auto it = localStatus.sessions.find(std::make_pair(llmqType, quorumIndex));
204 0 : if (it == localStatus.sessions.end()) {
205 0 : return;
206 : }
207 :
208 0 : if (func(it->second.members.at(memberIdx))) {
209 0 : localStatus.nTime = GetAdjustedTime();
210 0 : }
211 0 : }
212 :
213 0 : void CDKGDebugManager::MarkPhaseAdvanced(Consensus::LLMQType llmqType, int quorumIndex, QuorumPhase newPhase)
214 : {
215 0 : UpdateLocalSessionStatus(llmqType, quorumIndex, [&](CDKGDebugSessionStatus& status) {
216 0 : bool changed = status.phase != newPhase;
217 0 : status.phase = newPhase;
218 0 : return changed;
219 : });
220 0 : }
221 :
222 0 : void CDKGDebugManager::MarkAborted(Consensus::LLMQType llmqType, int quorumIndex)
223 : {
224 0 : UpdateLocalSessionStatus(llmqType, quorumIndex, [&](CDKGDebugSessionStatus& status) {
225 0 : status.statusBits.aborted = true;
226 0 : return true;
227 : });
228 0 : }
229 :
230 : } // namespace llmq
|