LCOV - code coverage report
Current view: top level - src/llmq - dkgsessionhandler.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 41 46 89.1 %
Date: 2026-06-25 07:23:43 Functions: 10 10 100.0 %

          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/dkgsessionhandler.h>
       6             : 
       7             : #include <logging.h>
       8             : #include <uint256.h>
       9             : 
      10             : #include <stdexcept>
      11             : 
      12             : namespace llmq {
      13        4032 : CDKGSessionHandler::CDKGSessionHandler(const Consensus::LLMQParams& _params) :
      14        3996 :     params{_params},
      15             :     // we allow size*2 messages as we need to make sure we see bad behavior (double messages)
      16        3996 :     pendingContributions{(size_t)_params.size * 2},
      17        3996 :     pendingComplaints{(size_t)_params.size * 2},
      18        3996 :     pendingJustifications{(size_t)_params.size * 2},
      19        3996 :     pendingPrematureCommitments{(size_t)_params.size * 2}
      20        4032 : {
      21        3996 :     if (params.type == Consensus::LLMQType::LLMQ_NONE) {
      22           0 :         throw std::runtime_error("Can't initialize CDKGSessionHandler with LLMQ_NONE type.");
      23             :     }
      24        4032 : }
      25             : 
      26        4068 : CDKGSessionHandler::~CDKGSessionHandler() = default;
      27             : 
      28       18259 : void CDKGPendingMessages::PushPendingMessage(NodeId from, std::shared_ptr<CDataStream> pm, const uint256& hash)
      29             : {
      30       18259 :     LOCK(cs_messages);
      31             : 
      32       18259 :     if (messagesPerNode[from] >= maxMessagesPerNode) {
      33             :         // TODO ban?
      34           0 :         LogPrint(BCLog::LLMQ_DKG, "CDKGPendingMessages::%s -- too many messages, peer=%d\n", __func__, from);
      35           0 :         return;
      36             :     }
      37       18259 :     messagesPerNode[from]++;
      38             : 
      39       18259 :     if (!seenMessages.emplace(hash).second) {
      40           0 :         LogPrint(BCLog::LLMQ_DKG, "CDKGPendingMessages::%s -- already seen %s, peer=%d\n", __func__, hash.ToString(), from);
      41           0 :         return;
      42             :     }
      43             : 
      44       18259 :     pendingMessages.emplace_back(std::make_pair(from, std::move(pm)));
      45       18259 : }
      46             : 
      47      138559 : std::list<CDKGPendingMessages::BinaryMessage> CDKGPendingMessages::PopPendingMessages(size_t maxCount)
      48             : {
      49      138559 :     LOCK(cs_messages);
      50             : 
      51      138559 :     std::list<BinaryMessage> ret;
      52      156520 :     while (!pendingMessages.empty() && ret.size() < maxCount) {
      53       17969 :         ret.emplace_back(std::move(pendingMessages.front()));
      54       17957 :         pendingMessages.pop_front();
      55             :     }
      56             : 
      57      138551 :     return ret;
      58      138567 : }
      59             : 
      60      803351 : bool CDKGPendingMessages::HasSeen(const uint256& hash) const
      61             : {
      62      803351 :     LOCK(cs_messages);
      63      803351 :     return seenMessages.count(hash) != 0;
      64      803351 : }
      65             : 
      66       44172 : void CDKGPendingMessages::Clear()
      67             : {
      68       44172 :     LOCK(cs_messages);
      69       44172 :     pendingMessages.clear();
      70       44172 :     messagesPerNode.clear();
      71       44172 :     seenMessages.clear();
      72       44172 : }
      73             : 
      74       11043 : void CDKGSessionHandler::ClearPendingMessages()
      75             : {
      76       11043 :     pendingContributions.Clear();
      77       11043 :     pendingComplaints.Clear();
      78       11043 :     pendingJustifications.Clear();
      79       11043 :     pendingPrematureCommitments.Clear();
      80       11043 : }
      81             : } // namespace llmq

Generated by: LCOV version 1.16