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