PIVX Core  5.6.99
P2P Digital Currency
quorums.h
Go to the documentation of this file.
1 // Copyright (c) 2018 The Dash Core developers
2 // Copyright (c) 2023 The PIVX 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_H
7 #define PIVX_LLMQ_QUORUMS_H
8 
9 #include "bls/bls_worker.h"
10 #include "bls/bls_wrapper.h"
11 #include "consensus/params.h"
12 #include "evo/deterministicmns.h"
13 #include "evo/evodb.h"
14 #include "saltedhasher.h"
15 #include "unordered_lru_cache.h"
16 #include "validationinterface.h"
17 
18 namespace llmq
19 {
20 
21 class CDKGSessionManager;
22 
32 class CQuorum
33 {
34  friend class CQuorumManager;
35 
36 public:
41  std::vector<CDeterministicMNCPtr> members;
42 
43  // These are only valid when we either participated in the DKG or fully watched it
46 
47 private:
48  // Recovery of public key shares is very slow, so we start a background thread that pre-populates a cache so that
49  // the public key shares are ready when needed later
51  std::atomic<bool> stopCachePopulatorThread;
52  std::thread cachePopulatorThread;
53 
54 public:
55  CQuorum(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker) : params(_params), blsCache(_blsWorker), stopCachePopulatorThread(false) {}
56  ~CQuorum();
57  void Init(const CFinalCommitment& _qc, const CBlockIndex* _pindexQuorum, const uint256& _minedBlockHash, const std::vector<CDeterministicMNCPtr>& _members);
58 
59  bool IsMember(const uint256& proTxHash) const;
60  bool IsValidMember(const uint256& proTxHash) const;
61  int GetMemberIndex(const uint256& proTxHash) const;
62 
63  CBLSPublicKey GetPubKeyShare(size_t memberIdx) const;
64  CBLSSecretKey GetSkShare() const;
65 
66 private:
69  static void StartCachePopulatorThread(std::shared_ptr<CQuorum> _this);
70 };
71 typedef std::shared_ptr<CQuorum> CQuorumPtr;
72 typedef std::shared_ptr<const CQuorum> CQuorumCPtr;
73 
81 {
82 private:
86 
88  mutable std::map<Consensus::LLMQType, unordered_lru_cache<uint256, CQuorumCPtr, StaticSaltedHasher>> mapQuorumsCache;
89  mutable std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::vector<CQuorumCPtr>, StaticSaltedHasher>> scanQuorumsCache;
90 
91 public:
92  CQuorumManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager);
93 
94  void UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitialDownload);
95 
96 public:
97  bool HasQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash);
98 
99  // all these methods will lock cs_main
100  CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash);
101 
102  std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount);
103 
104  // this method will not lock cs_main
105  std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount);
106 
107 private:
108  // all these methods will not lock cs_main
109  void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexNew);
110 
111  bool BuildQuorumFromCommitment(const CFinalCommitment& qc, const CBlockIndex* pindexQuorum, const uint256& minedBlockHash, std::shared_ptr<CQuorum>& quorum) const;
112  bool BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr<CQuorum>& quorum) const;
113 
114  CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum);
115 };
116 
117 extern std::unique_ptr<CQuorumManager> quorumManager;
118 } // namespace llmq
119 
120 #endif // PIVX_LLMQ_QUORUMS_H
false
Definition: bls_dkg.cpp:151
std::shared_ptr< BLSVerificationVector > BLSVerificationVectorPtr
Definition: bls_wrapper.h:415
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
Definition: evodb.h:32
An object of this class represents a quorum which was mined on-chain (through a quorum commitment) It...
Definition: quorums.h:33
std::thread cachePopulatorThread
Definition: quorums.h:52
void WriteContributions(CEvoDB &evoDb)
Definition: quorums.cpp:107
CBLSPublicKey GetPubKeyShare(size_t memberIdx) const
Definition: quorums.cpp:83
const CBlockIndex * pindexQuorum
Definition: quorums.h:39
CQuorum(const Consensus::LLMQParams &_params, CBLSWorker &_blsWorker)
Definition: quorums.h:55
bool ReadContributions(CEvoDB &evoDb)
Definition: quorums.cpp:119
CBLSWorkerCache blsCache
Definition: quorums.h:50
uint256 minedBlockHash
Definition: quorums.h:40
const Consensus::LLMQParams & params
Definition: quorums.h:37
BLSVerificationVectorPtr quorumVvec
Definition: quorums.h:44
std::vector< CDeterministicMNCPtr > members
Definition: quorums.h:41
int GetMemberIndex(const uint256 &proTxHash) const
Definition: quorums.cpp:97
CFinalCommitment qc
Definition: quorums.h:38
CBLSSecretKey skShare
Definition: quorums.h:45
bool IsValidMember(const uint256 &proTxHash) const
Definition: quorums.cpp:73
std::atomic< bool > stopCachePopulatorThread
Definition: quorums.h:51
void Init(const CFinalCommitment &_qc, const CBlockIndex *_pindexQuorum, const uint256 &_minedBlockHash, const std::vector< CDeterministicMNCPtr > &_members)
Definition: quorums.cpp:55
CBLSSecretKey GetSkShare() const
Definition: quorums.cpp:92
bool IsMember(const uint256 &proTxHash) const
Definition: quorums.cpp:63
static void StartCachePopulatorThread(std::shared_ptr< CQuorum > _this)
Definition: quorums.cpp:137
The quorum manager maintains quorums which were mined on chain.
Definition: quorums.h:81
std::vector< CQuorumCPtr > ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount)
Definition: quorums.cpp:253
std::map< Consensus::LLMQType, unordered_lru_cache< uint256, CQuorumCPtr, StaticSaltedHasher > > mapQuorumsCache
Definition: quorums.h:88
CDKGSessionManager & dkgManager
Definition: quorums.h:85
void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexNew)
CQuorumManager(CEvoDB &_evoDb, CBLSWorker &_blsWorker, CDKGSessionManager &_dkgManager)
Definition: quorums.cpp:158
RecursiveMutex quorumsCacheCs
Definition: quorums.h:87
bool BuildQuorumContributions(const CFinalCommitment &fqc, std::shared_ptr< CQuorum > &quorum) const
Definition: quorums.cpp:212
std::map< Consensus::LLMQType, unordered_lru_cache< uint256, std::vector< CQuorumCPtr >, StaticSaltedHasher > > scanQuorumsCache
Definition: quorums.h:89
CEvoDB & evoDb
Definition: quorums.h:83
CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256 &quorumHash)
Definition: quorums.cpp:318
bool BuildQuorumFromCommitment(const CFinalCommitment &qc, const CBlockIndex *pindexQuorum, const uint256 &minedBlockHash, std::shared_ptr< CQuorum > &quorum) const
Definition: quorums.cpp:182
bool HasQuorum(Consensus::LLMQType llmqType, const uint256 &quorumHash)
Definition: quorums.cpp:248
CBLSWorker & blsWorker
Definition: quorums.h:84
void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload)
Definition: quorums.cpp:166
256-bit opaque blob.
Definition: uint256.h:138
std::unique_ptr< CEvoDB > evoDb
Definition: evodb.cpp:10
LLMQType
Definition: params.h:90
Definition: quorums.cpp:26
std::shared_ptr< const CQuorum > CQuorumCPtr
Definition: quorums.h:72
std::unique_ptr< CQuorumManager > quorumManager
Definition: quorums.cpp:31
std::shared_ptr< CQuorum > CQuorumPtr
Definition: quorums.h:71
@ proTxHash
Definition: rpcevo.cpp:50