Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

quorums_dkgsession.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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 #ifndef DASH_QUORUMS_DKGSESSION_H
6 #define DASH_QUORUMS_DKGSESSION_H
7 
8 #include <consensus/params.h>
9 #include <net.h>
10 #include <batchedlogger.h>
11 
12 #include <bls/bls_ies.h>
13 #include <bls/bls_worker.h>
14 
15 #include <evo/deterministicmns.h>
16 
17 #include <llmq/quorums_utils.h>
18 
19 class UniValue;
20 
21 namespace llmq
22 {
23 
24 class CFinalCommitment;
25 class CDKGSession;
26 class CDKGSessionManager;
27 class CDKGPendingMessages;
28 
29 class CDKGLogger : public CBatchedLogger
30 {
31 public:
32  CDKGLogger(const CDKGSession& _quorumDkg, const std::string& _func);
33  CDKGLogger(const std::string& _llmqTypeName, const uint256& _quorumHash, int _height, bool _areWeMember, const std::string& _func);
34 };
35 
37 {
38 public:
43  std::shared_ptr<CBLSIESMultiRecipientObjects<CBLSSecretKey>> contributions;
45 
46 public:
47  template<typename Stream>
48  inline void SerializeWithoutSig(Stream& s) const
49  {
50  s << llmqType;
51  s << quorumHash;
52  s << proTxHash;
53  s << *vvec;
54  s << *contributions;
55  }
56  template<typename Stream>
57  inline void Serialize(Stream& s) const
58  {
60  s << sig;
61  }
62  template<typename Stream>
63  inline void Unserialize(Stream& s)
64  {
67 
68  s >> llmqType;
69  s >> quorumHash;
70  s >> proTxHash;
71  s >> tmp1;
72  s >> tmp2;
73  s >> sig;
74 
75  vvec = std::make_shared<BLSVerificationVector>(std::move(tmp1));
76  contributions = std::make_shared<CBLSIESMultiRecipientObjects<CBLSSecretKey>>(std::move(tmp2));
77  }
78 
80  {
81  CHashWriter hw(SER_GETHASH, 0);
83  hw << CBLSSignature();
84  return hw.GetHash();
85  }
86 };
87 
89 {
90 public:
94  std::vector<bool> badMembers;
95  std::vector<bool> complainForMembers;
97 
98 public:
100  explicit CDKGComplaint(const Consensus::LLMQParams& params);
101 
103 
104  template<typename Stream, typename Operation>
105  inline void SerializationOp(Stream& s, Operation ser_action)
106  {
112  READWRITE(sig);
113  }
114 
116  {
117  CDKGComplaint tmp(*this);
118  tmp.sig = CBLSSignature();
120  }
121 };
122 
124 {
125 public:
129  std::vector<std::pair<uint32_t, CBLSSecretKey>> contributions;
131 
132 public:
134 
135  template<typename Stream, typename Operation>
136  inline void SerializationOp(Stream& s, Operation ser_action)
137  {
142  READWRITE(sig);
143  }
144 
146  {
147  CDKGJustification tmp(*this);
148  tmp.sig = CBLSSignature();
150  }
151 };
152 
153 // each member commits to a single set of valid members with this message
154 // then each node aggregate all received premature commitments
155 // into a single CFinalCommitment, which is only valid if
156 // enough (>=minSize) premature commitments were aggregated
158 {
159 public:
163  std::vector<bool> validMembers;
164 
167 
168  CBLSSignature quorumSig; // threshold sig share of quorumHash+validMembers+pubKeyHash+vvecHash
169  CBLSSignature sig; // single member sig of quorumHash+validMembers+pubKeyHash+vvecHash
170 
171 public:
173  explicit CDKGPrematureCommitment(const Consensus::LLMQParams& params);
174 
175  int CountValidMembers() const
176  {
177  return (int)std::count(validMembers.begin(), validMembers.end(), true);
178  }
179 
180 public:
182 
183  template<typename Stream, typename Operation>
184  inline void SerializationOp(Stream& s, Operation ser_action)
185  {
193  READWRITE(sig);
194  }
195 
197  {
199  }
200 };
201 
203 {
204 public:
205  CDKGMember(CDeterministicMNCPtr _dmn, size_t _idx);
206 
208  size_t idx;
210 
211  std::set<uint256> contributions;
212  std::set<uint256> complaints;
213  std::set<uint256> justifications;
214  std::set<uint256> prematureCommitments;
215 
216  std::set<uint256> badMemberVotes;
217  std::set<uint256> complaintsFromOthers;
218 
219  bool bad{false};
220  bool badConnection{false};
221  bool weComplain{false};
222  bool someoneComplain{false};
223 };
224 
240 {
241  friend class CDKGSessionHandler;
242  friend class CDKGSessionManager;
243  friend class CDKGLogger;
244 
245 private:
247 
251 
253 
254 private:
255  std::vector<std::unique_ptr<CDKGMember>> members;
256  std::map<uint256, size_t> membersMap;
257  std::set<uint256> relayMembers;
260 
262  std::vector<BLSVerificationVectorPtr> receivedVvecs;
263  // these are not necessarily verified yet. Only trust in what was written to the DB
265 
268  size_t myIdx{(size_t)-1};
269 
270  // all indexed by msg hash
271  // we expect to only receive a single vvec and contribution per member, but we must also be able to relay
272  // conflicting messages as otherwise an attacker might be able to broadcast conflicting (valid+invalid) messages
273  // and thus split the quorum. Such members are later removed from the quorum.
275  std::map<uint256, CDKGContribution> contributions;
276  std::map<uint256, CDKGComplaint> complaints;
277  std::map<uint256, CDKGJustification> justifications;
278  std::map<uint256, CDKGPrematureCommitment> prematureCommitments;
279 
280  std::vector<size_t> pendingContributionVerifications;
281 
282  // filled by ReceivePrematureCommitment and used by FinalizeCommitments
283  std::set<uint256> validCommitments;
284 
285 public:
286  CDKGSession(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
287  params(_params), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager) {}
288 
289  bool Init(const CBlockIndex* pindexQuorum, const std::vector<CDeterministicMNCPtr>& mns, const uint256& _myProTxHash);
290 
291  size_t GetMyMemberIndex() const { return myIdx; }
292 
306  // Phase 1: contribution
307  void Contribute(CDKGPendingMessages& pendingMessages);
308  void SendContributions(CDKGPendingMessages& pendingMessages);
309  bool PreVerifyMessage(const uint256& hash, const CDKGContribution& qc, bool& retBan) const;
310  void ReceiveMessage(const uint256& hash, const CDKGContribution& qc, bool& retBan);
312 
313  // Phase 2: complaint
314  void VerifyAndComplain(CDKGPendingMessages& pendingMessages);
316  void SendComplaint(CDKGPendingMessages& pendingMessages);
317  bool PreVerifyMessage(const uint256& hash, const CDKGComplaint& qc, bool& retBan) const;
318  void ReceiveMessage(const uint256& hash, const CDKGComplaint& qc, bool& retBan);
319 
320  // Phase 3: justification
321  void VerifyAndJustify(CDKGPendingMessages& pendingMessages);
322  void SendJustification(CDKGPendingMessages& pendingMessages, const std::set<uint256>& forMembers);
323  bool PreVerifyMessage(const uint256& hash, const CDKGJustification& qj, bool& retBan) const;
324  void ReceiveMessage(const uint256& hash, const CDKGJustification& qj, bool& retBan);
325 
326  // Phase 4: commit
327  void VerifyAndCommit(CDKGPendingMessages& pendingMessages);
328  void SendCommitment(CDKGPendingMessages& pendingMessages);
329  bool PreVerifyMessage(const uint256& hash, const CDKGPrematureCommitment& qc, bool& retBan) const;
330  void ReceiveMessage(const uint256& hash, const CDKGPrematureCommitment& qc, bool& retBan);
331 
332  // Phase 5: aggregate/finalize
333  std::vector<CFinalCommitment> FinalizeCommitments();
334 
335  bool AreWeMember() const { return !myProTxHash.IsNull(); }
336  void MarkBadMember(size_t idx);
337 
338  void RelayInvToParticipants(const CInv& inv) const;
339 
340 public:
341  CDKGMember* GetMember(const uint256& proTxHash) const;
342 };
343 
344 void SetSimulatedDKGErrorRate(const std::string& type, double rate);
345 
346 } // namespace llmq
347 
348 #endif //DASH_QUORUMS_DKGSESSION_H
Acts as a FIFO queue for incoming DKG messages.
void SetSimulatedDKGErrorRate(const std::string &type, double rate)
BLSSecretKeyVector skContributions
std::set< uint256 > relayMembers
CDKGSessionManager & dkgManager
#define READWRITE(obj)
Definition: serialize.h:165
void ReceiveMessage(const uint256 &hash, const CDKGContribution &qc, bool &retBan)
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
inv message data
Definition: protocol.h:429
std::vector< bool > badMembers
std::map< uint256, CDKGComplaint > complaints
Consensus::LLMQType llmqType
const CBlockIndex * pindexQuorum
std::map< uint256, CDKGPrematureCommitment > prematureCommitments
std::set< uint256 > justifications
CDKGSession(const Consensus::LLMQParams &_params, CBLSWorker &_blsWorker, CDKGSessionManager &_dkgManager)
void SendCommitment(CDKGPendingMessages &pendingMessages)
std::vector< CBLSPublicKey > BLSVerificationVector
Definition: bls.h:465
std::vector< std::unique_ptr< CDKGMember > > members
void SerializeWithoutSig(Stream &s) const
std::shared_ptr< const CDeterministicMN > CDeterministicMNCPtr
void Unserialize(Stream &s)
CCriticalSection invCs
std::set< uint256 > contributions
CDeterministicMNCPtr dmn
std::vector< CBLSSecretKey > BLSSecretKeyVector
Definition: bls.h:467
std::set< uint256 > prematureCommitments
CBLSWorkerCache cache
bool IsNull() const
Definition: uint256.h:33
std::map< uint256, CDKGJustification > justifications
std::map< uint256, CDKGContribution > contributions
const Consensus::LLMQParams & params
CDKGMember * GetMember(const uint256 &proTxHash) const
std::set< uint256 > complaints
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:254
Definition: bls.h:218
void Serialize(Stream &s) const
bool PreVerifyMessage(const uint256 &hash, const CDKGContribution &qc, bool &retBan) const
LLMQType
Definition: params.h:48
std::shared_ptr< BLSVerificationVector > BLSVerificationVectorPtr
Definition: bls.h:471
bool AreWeMember() const
std::vector< CBLSId > BLSIdVector
Definition: bls.h:464
std::size_t size_t
Definition: bits.hpp:21
static uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256 &blockHash, const std::vector< bool > &validMembers, const CBLSPublicKey &pubKey, const uint256 &vvecHash)
size_t GetMyMemberIndex() const
void MarkBadMember(size_t idx)
#define ADD_SERIALIZE_METHODS
Implement three methods for serializable objects.
Definition: serialize.h:174
uint256 GetSignHash() const
void VerifyAndComplain(CDKGPendingMessages &pendingMessages)
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
void VerifyAndJustify(CDKGPendingMessages &pendingMessages)
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
void VerifyConnectionAndMinProtoVersions()
std::vector< BLSVerificationVectorPtr > receivedVvecs
#define DYNBITSET(obj)
Definition: serialize.h:372
void RelayInvToParticipants(const CInv &inv) const
uint256 GetSignHash() const
BLSVerificationVectorPtr vvec
std::vector< size_t > pendingContributionVerifications
The DKG session is a single instance of the DKG process.
void Contribute(CDKGPendingMessages &pendingMessages)
The following sets of methods are for the first 4 phases handled in the session.
uint256 GetHash()
Definition: hash.h:203
256-bit opaque blob.
Definition: uint256.h:123
CDKGMember(CDeterministicMNCPtr _dmn, size_t _idx)
BLSSecretKeyVector receivedSkContributions
std::vector< CFinalCommitment > FinalizeCommitments()
std::set< uint256 > badMemberVotes
void SendComplaint(CDKGPendingMessages &pendingMessages)
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
void VerifyAndCommit(CDKGPendingMessages &pendingMessages)
bool Init(const CBlockIndex *pindexQuorum, const std::vector< CDeterministicMNCPtr > &mns, const uint256 &_myProTxHash)
std::vector< std::pair< uint32_t, CBLSSecretKey > > contributions
Consensus::LLMQType llmqType
static int count
Definition: tests.c:45
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:184
std::map< uint256, size_t > membersMap
BLSVerificationVectorPtr vvecContribution
std::set< uint256 > complaintsFromOthers
std::vector< bool > complainForMembers
Handles multiple sequential sessions of one specific LLMQ type.
std::set< uint256 > validCommitments
void SendJustification(CDKGPendingMessages &pendingMessages, const std::set< uint256 > &forMembers)
void SendContributions(CDKGPendingMessages &pendingMessages)
CDKGLogger(const CDKGSession &_quorumDkg, const std::string &_func)
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
Consensus::LLMQType llmqType
std::shared_ptr< CBLSIESMultiRecipientObjects< CBLSSecretKey > > contributions
Released under the MIT license