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 0 : CDKGSessionHandler::CDKGSessionHandler(const Consensus::LLMQParams& _params) : 14 0 : params{_params}, 15 : // we allow size*2 messages as we need to make sure we see bad behavior (double messages) 16 0 : pendingContributions{(size_t)_params.size * 2}, 17 0 : pendingComplaints{(size_t)_params.size * 2}, 18 0 : pendingJustifications{(size_t)_params.size * 2}, 19 0 : pendingPrematureCommitments{(size_t)_params.size * 2} 20 0 : { 21 0 : if (params.type == Consensus::LLMQType::LLMQ_NONE) { 22 0 : throw std::runtime_error("Can't initialize CDKGSessionHandler with LLMQ_NONE type."); 23 : } 24 0 : } 25 : 26 0 : CDKGSessionHandler::~CDKGSessionHandler() = default; 27 : 28 0 : void CDKGPendingMessages::PushPendingMessage(NodeId from, std::shared_ptr<CDataStream> pm, const uint256& hash) 29 : { 30 0 : LOCK(cs_messages); 31 : 32 0 : 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 0 : messagesPerNode[from]++; 38 : 39 0 : 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 0 : pendingMessages.emplace_back(std::make_pair(from, std::move(pm))); 45 0 : } 46 : 47 0 : std::list<CDKGPendingMessages::BinaryMessage> CDKGPendingMessages::PopPendingMessages(size_t maxCount) 48 : { 49 0 : LOCK(cs_messages); 50 : 51 0 : std::list<BinaryMessage> ret; 52 0 : while (!pendingMessages.empty() && ret.size() < maxCount) { 53 0 : ret.emplace_back(std::move(pendingMessages.front())); 54 0 : pendingMessages.pop_front(); 55 : } 56 : 57 0 : return ret; 58 0 : } 59 : 60 0 : bool CDKGPendingMessages::HasSeen(const uint256& hash) const 61 : { 62 0 : LOCK(cs_messages); 63 0 : return seenMessages.count(hash) != 0; 64 0 : } 65 : 66 0 : void CDKGPendingMessages::Clear() 67 : { 68 0 : LOCK(cs_messages); 69 0 : pendingMessages.clear(); 70 0 : messagesPerNode.clear(); 71 0 : seenMessages.clear(); 72 0 : } 73 : 74 0 : void CDKGSessionHandler::ClearPendingMessages() 75 : { 76 0 : pendingContributions.Clear(); 77 0 : pendingComplaints.Clear(); 78 0 : pendingJustifications.Clear(); 79 0 : pendingPrematureCommitments.Clear(); 80 0 : } 81 : } // namespace llmq