PIVX Core  5.6.99
P2P Digital Currency
quorums_dkgsessionhandler.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 The Dash Core developers
2 // Copyright (c) 2022 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_LLMQ_QUORUMS_DKGSESSIONHANDLER_H
7 #define PIVX_LLMQ_QUORUMS_DKGSESSIONHANDLER_H
8 
9 #include "ctpl_stl.h"
11 #include "validation.h"
12 
13 namespace llmq
14 {
15 
25 };
26 
36 {
37 public:
38  typedef std::pair<NodeId, std::shared_ptr<CDataStream>> BinaryMessage;
39 
40 private:
41  mutable RecursiveMutex cs;
43  std::list<BinaryMessage> pendingMessages;
44  std::map<NodeId, size_t> messagesPerNode;
45  std::set<uint256> seenMessages;
46 
47 public:
48  explicit CDKGPendingMessages(size_t _maxMessagesPerNode);
49 
50  void PushPendingMessage(NodeId from, CDataStream& vRecv, int invType);
51  std::list<BinaryMessage> PopPendingMessages(size_t maxCount);
52  bool HasSeen(const uint256& hash) const;
53  void Clear();
54 
55  template<typename Message>
56  void PushPendingMessage(NodeId from, Message& msg, int invType)
57  {
58  CDataStream ds(SER_NETWORK, PROTOCOL_VERSION);
59  ds << msg;
60  PushPendingMessage(from, ds, invType);
61  }
62 
63  // Might return nullptr messages, which indicates that deserialization failed for some reason
64  template<typename Message>
65  std::vector<std::pair<NodeId, std::shared_ptr<Message>>> PopAndDeserializeMessages(size_t maxCount)
66  {
67  auto binaryMessages = PopPendingMessages(maxCount);
68  if (binaryMessages.empty()) {
69  return {};
70  }
71 
72  std::vector<std::pair<NodeId, std::shared_ptr<Message>>> ret;
73  ret.reserve(binaryMessages.size());
74  for (const auto& bm : binaryMessages) {
75  auto msg = std::make_shared<Message>();
76  try {
77  *bm.second >> *msg;
78  } catch (...) {
79  msg = nullptr;
80  }
81  ret.emplace_back(std::make_pair(bm.first, std::move(msg)));
82  }
83 
84  return ret;
85  }
86 };
87 
95 {
96 private:
97  friend class CDKGSessionManager;
98 
99  mutable RecursiveMutex cs;
100  std::atomic<bool> stopRequested{false};
101 
105 
107  int currentHeight{-1};
108  int quorumHeight{-1};
110  std::shared_ptr<CDKGSession> curSession;
111  std::thread phaseHandlerThread;
112 
117 
118 public:
121 
122  void UpdatedBlockTip(const CBlockIndex *pindexNew);
123  void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv);
124 
125  void StartThread();
126  void StopThread();
127 
128 private:
129  bool InitNewQuorum(const CBlockIndex* pindexQuorum);
130 
134  };
136 
137  typedef std::function<void()> StartPhaseFunc;
138  typedef std::function<bool()> WhileWaitFunc;
139  void WaitForNextPhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, const WhileWaitFunc& runWhileWaiting);
140  void WaitForNewQuorum(const uint256& oldQuorumHash);
141  void SleepBeforePhase(QuorumPhase curPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc& runWhileWaiting);
142  void HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc& startPhaseFunc, const WhileWaitFunc& runWhileWaiting);
143  void HandleDKGRound();
144  void PhaseHandlerThread();
145 };
146 
147 }
148 
149 #endif // PIVX_LLMQ_QUORUMS_DKGSESSIONHANDLER_H
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
Information about a peer.
Definition: net.h:669
Acts as a FIFO queue for incoming DKG messages.
bool HasSeen(const uint256 &hash) const
void PushPendingMessage(NodeId from, CDataStream &vRecv, int invType)
std::pair< NodeId, std::shared_ptr< CDataStream > > BinaryMessage
std::list< BinaryMessage > PopPendingMessages(size_t maxCount)
std::list< BinaryMessage > pendingMessages
std::vector< std::pair< NodeId, std::shared_ptr< Message > > > PopAndDeserializeMessages(size_t maxCount)
void PushPendingMessage(NodeId from, Message &msg, int invType)
std::map< NodeId, size_t > messagesPerNode
CDKGPendingMessages(size_t _maxMessagesPerNode)
Handles multiple sequential sessions of one specific LLMQ type.
QuorumPhaseAndHash GetPhaseAndQuorumHash() const
std::function< bool()> WhileWaitFunc
bool InitNewQuorum(const CBlockIndex *pindexQuorum)
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv)
void HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256 &expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc &startPhaseFunc, const WhileWaitFunc &runWhileWaiting)
void SleepBeforePhase(QuorumPhase curPhase, const uint256 &expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc &runWhileWaiting)
std::shared_ptr< CDKGSession > curSession
CDKGPendingMessages pendingPrematureCommitments
CDKGPendingMessages pendingContributions
void UpdatedBlockTip(const CBlockIndex *pindexNew)
const Consensus::LLMQParams & params
void WaitForNewQuorum(const uint256 &oldQuorumHash)
CDKGSessionHandler(const Consensus::LLMQParams &_params, CBLSWorker &blsWorker, CDKGSessionManager &_dkgManager)
void WaitForNextPhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256 &expectedQuorumHash, const WhileWaitFunc &runWhileWaiting)
CDKGPendingMessages pendingJustifications
std::function< void()> StartPhaseFunc
256-bit opaque blob.
Definition: uint256.h:138
Definition: quorums.cpp:26
int NodeId
Definition: net.h:109
@ SER_NETWORK
Definition: serialize.h:174
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:175