LCOV - code coverage report
Current view: top level - src/llmq - debug.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 132 154 85.7 %
Date: 2026-06-25 07:23:43 Functions: 22 23 95.7 %

          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       25306 : UniValue CDKGDebugSessionStatus::ToJson(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
      20             :                                         const ChainstateManager& chainman, int quorumIndex, int detailLevel) const
      21             : {
      22       25306 :     UniValue ret(UniValue::VOBJ);
      23             : 
      24       25306 :     if (!Params().GetLLMQ(llmqType).has_value() || quorumHash.IsNull()) {
      25           0 :         return ret;
      26             :     }
      27             : 
      28       25306 :     std::vector<CDeterministicMNCPtr> dmnMembers;
      29       25306 :     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       25306 :     ret.pushKV("llmqType", std23::to_underlying(llmqType));
      37       25306 :     ret.pushKV("quorumHash", quorumHash.ToString());
      38       25306 :     ret.pushKV("quorumHeight", quorumHeight);
      39       25306 :     ret.pushKV("phase", std23::to_underlying(phase));
      40             : 
      41       25306 :     ret.pushKV("sentContributions", statusBits.sentContributions);
      42       25306 :     ret.pushKV("sentComplaint", statusBits.sentComplaint);
      43       25306 :     ret.pushKV("sentJustification", statusBits.sentJustification);
      44       25306 :     ret.pushKV("sentPrematureCommitment", statusBits.sentPrematureCommitment);
      45       25306 :     ret.pushKV("aborted", statusBits.aborted);
      46             : 
      47      177142 :     struct ArrOrCount {
      48      177142 :         int count{0};
      49      177142 :         UniValue arr{UniValue::VARR};
      50             :     };
      51             : 
      52       25306 :     ArrOrCount badMembers;
      53       25306 :     ArrOrCount weComplain;
      54       25306 :     ArrOrCount receivedContributions;
      55       25306 :     ArrOrCount receivedComplaints;
      56       25306 :     ArrOrCount receivedJustifications;
      57       25306 :     ArrOrCount receivedPrematureCommitments;
      58       25306 :     ArrOrCount complaintsFromMembers;
      59             : 
      60      620608 :     auto add = [&](ArrOrCount& v, size_t idx, bool flag) {
      61      595302 :         if (flag) {
      62      117407 :             if (detailLevel == 0) {
      63      117407 :                 v.count++;
      64      117407 :             } 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      117407 :         }
      75      595302 :     };
      76      177142 :     auto push = [&](const ArrOrCount& v, const std::string& name) {
      77      151836 :         if (detailLevel == 0) {
      78      151836 :             ret.pushKV(name, v.count);
      79      151836 :         } else {
      80           0 :             ret.pushKV(name, v.arr);
      81             :         }
      82      151836 :     };
      83             : 
      84      124523 :     for (const auto i : util::irange(members.size())) {
      85       99217 :         const auto& m = members[i];
      86       99217 :         add(badMembers, i, m.statusBits.bad);
      87       99217 :         add(weComplain, i, m.statusBits.weComplain);
      88       99217 :         add(receivedContributions, i, m.statusBits.receivedContribution);
      89       99217 :         add(receivedComplaints, i, m.statusBits.receivedComplaint);
      90       99217 :         add(receivedJustifications, i, m.statusBits.receivedJustification);
      91       99217 :         add(receivedPrematureCommitments, i, m.statusBits.receivedPrematureCommitment);
      92             :     }
      93       25306 :     push(badMembers, "badMembers");
      94       25306 :     push(weComplain, "weComplain");
      95       25306 :     push(receivedContributions, "receivedContributions");
      96       25306 :     push(receivedComplaints, "receivedComplaints");
      97       25306 :     push(receivedJustifications, "receivedJustifications");
      98       25306 :     push(receivedPrematureCommitments, "receivedPrematureCommitments");
      99             : 
     100       25306 :     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       25306 :     return ret;
     109       25306 : }
     110             : 
     111        1998 : CDKGDebugManager::CDKGDebugManager(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
     112             :                                    const ChainstateManager& chainman) :
     113         666 :     m_dmnman{dmnman},
     114         666 :     m_qsnapman{qsnapman},
     115         666 :     m_chainman{chainman}
     116         666 : {
     117        1332 : }
     118             : 
     119        1332 : CDKGDebugManager::~CDKGDebugManager() = default;
     120             : 
     121          32 : size_t CDKGDebugManager::GetSessionCount() const
     122             : {
     123          64 :     return WITH_LOCK(cs_lockStatus, return localStatus.sessions.size());
     124             : }
     125             : 
     126       15357 : UniValue CDKGDebugManager::ToJson(int detailLevel) const
     127             : {
     128       15357 :     LOCK(cs_lockStatus);
     129             : 
     130       15357 :     UniValue ret(UniValue::VOBJ);
     131       15357 :     ret.pushKV("time", localStatus.nTime);
     132       15357 :     ret.pushKV("timeStr", FormatISO8601DateTime(localStatus.nTime));
     133             : 
     134             :     // TODO Support array of sessions
     135       15357 :     UniValue sessionsArrJson(UniValue::VARR);
     136       40663 :     for (const auto& p : localStatus.sessions) {
     137       25306 :         const auto& llmq_params_opt = Params().GetLLMQ(p.first.first);
     138       25306 :         if (!llmq_params_opt.has_value()) {
     139           0 :             continue;
     140             :         }
     141       25306 :         UniValue s(UniValue::VOBJ);
     142       25306 :         s.pushKV("llmqType", std::string(llmq_params_opt->name));
     143       25306 :         s.pushKV("quorumIndex", p.first.second);
     144       25306 :         s.pushKV("status", p.second.ToJson(m_dmnman, m_qsnapman, m_chainman, p.first.second, detailLevel));
     145             : 
     146       25306 :         sessionsArrJson.push_back(s);
     147       25306 :     }
     148       15357 :     ret.pushKV("session", sessionsArrJson);
     149             : 
     150       15357 :     return ret;
     151       15357 : }
     152             : 
     153       11043 : void CDKGDebugManager::ResetLocalSessionStatus(Consensus::LLMQType llmqType, int quorumIndex)
     154             : {
     155       11043 :     LOCK(cs_lockStatus);
     156             : 
     157       11043 :     auto it = localStatus.sessions.find(std::make_pair(llmqType, quorumIndex));
     158       11043 :     if (it == localStatus.sessions.end()) {
     159        8795 :         return;
     160             :     }
     161             : 
     162        2248 :     localStatus.sessions.erase(it);
     163        2248 :     localStatus.nTime = GetAdjustedTime();
     164       11043 : }
     165             : 
     166        3037 : void CDKGDebugManager::InitLocalSessionStatus(const Consensus::LLMQParams& llmqParams, int quorumIndex, const uint256& quorumHash, int quorumHeight)
     167             : {
     168        3037 :     LOCK(cs_lockStatus);
     169             : 
     170        3037 :     auto it = localStatus.sessions.find(std::make_pair(llmqParams.type, quorumIndex));
     171        3037 :     if (it == localStatus.sessions.end()) {
     172        3037 :         it = localStatus.sessions.emplace(std::make_pair(llmqParams.type, quorumIndex), CDKGDebugSessionStatus()).first;
     173        3037 :     }
     174             : 
     175        3037 :     auto& session = it->second;
     176        3037 :     session.llmqType = llmqParams.type;
     177        3037 :     session.quorumHash = quorumHash;
     178        3037 :     session.quorumHeight = (uint32_t)quorumHeight;
     179        3037 :     session.phase = QuorumPhase{0};
     180        3037 :     session.statusBitset = 0;
     181        3037 :     session.members.clear();
     182        3037 :     session.members.resize((size_t)llmqParams.size);
     183        3037 : }
     184             : 
     185       39740 : void CDKGDebugManager::UpdateLocalSessionStatus(Consensus::LLMQType llmqType, int quorumIndex, std::function<bool(CDKGDebugSessionStatus& status)>&& func)
     186             : {
     187       39740 :     LOCK(cs_lockStatus);
     188             : 
     189       39740 :     auto it = localStatus.sessions.find(std::make_pair(llmqType, quorumIndex));
     190       39740 :     if (it == localStatus.sessions.end()) {
     191       18077 :         return;
     192             :     }
     193             : 
     194       21663 :     if (func(it->second)) {
     195       21663 :         localStatus.nTime = GetAdjustedTime();
     196       21663 :     }
     197       39740 : }
     198             : 
     199       18820 : void CDKGDebugManager::UpdateLocalMemberStatus(Consensus::LLMQType llmqType, int quorumIndex, size_t memberIdx, std::function<bool(CDKGDebugMemberStatus& status)>&& func)
     200             : {
     201       18820 :     LOCK(cs_lockStatus);
     202             : 
     203       18820 :     auto it = localStatus.sessions.find(std::make_pair(llmqType, quorumIndex));
     204       18820 :     if (it == localStatus.sessions.end()) {
     205           8 :         return;
     206             :     }
     207             : 
     208       18812 :     if (func(it->second.members.at(memberIdx))) {
     209       18812 :         localStatus.nTime = GetAdjustedTime();
     210       18812 :     }
     211       18820 : }
     212             : 
     213        4873 : void CDKGDebugManager::MarkPhaseAdvanced(Consensus::LLMQType llmqType, int quorumIndex, QuorumPhase newPhase)
     214             : {
     215        7910 :     UpdateLocalSessionStatus(llmqType, quorumIndex, [&](CDKGDebugSessionStatus& status) {
     216        3037 :         bool changed = status.phase != newPhase;
     217        3037 :         status.phase = newPhase;
     218        3037 :         return changed;
     219             :     });
     220        4873 : }
     221             : 
     222        9885 : void CDKGDebugManager::MarkAborted(Consensus::LLMQType llmqType, int quorumIndex)
     223             : {
     224       11306 :     UpdateLocalSessionStatus(llmqType, quorumIndex, [&](CDKGDebugSessionStatus& status) {
     225        1421 :         status.statusBits.aborted = true;
     226        1421 :         return true;
     227             :     });
     228        9885 : }
     229             : 
     230             : } // namespace llmq

Generated by: LCOV version 1.16