Line data Source code
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_DKGSESSIONMGR_H 7 : #define PIVX_LLMQ_QUORUMS_DKGSESSIONMGR_H 8 : 9 : #include "ctpl_stl.h" 10 : #include "llmq/quorums_dkgsessionhandler.h" 11 : #include "validation.h" 12 : 13 : class UniValue; 14 : 15 : namespace llmq 16 : { 17 : 18 : class CDKGSessionManager 19 : { 20 : static const int64_t MAX_CONTRIBUTION_CACHE_TIME = 60 * 1000; 21 : 22 : private: 23 : CDBWrapper& llmqDb; 24 : CBLSWorker& blsWorker; 25 : 26 : std::map<Consensus::LLMQType, CDKGSessionHandler> dkgSessionHandlers; 27 : 28 : RecursiveMutex contributionsCacheCs; 29 : struct ContributionsCacheKey { 30 : Consensus::LLMQType llmqType; 31 : uint256 quorumHash; 32 : uint256 proTxHash; 33 5113 : bool operator<(const ContributionsCacheKey& r) const 34 : { 35 5113 : if (llmqType != r.llmqType) return llmqType < r.llmqType; 36 5113 : if (quorumHash != r.quorumHash) return quorumHash < r.quorumHash; 37 2876 : return proTxHash < r.proTxHash; 38 : } 39 : }; 40 : struct ContributionsCacheEntry { 41 : int64_t entryTime; 42 : BLSVerificationVectorPtr vvec; 43 : CBLSSecretKey skContribution; 44 : }; 45 : std::map<ContributionsCacheKey, ContributionsCacheEntry> contributionsCache; 46 : 47 : public: 48 : CDKGSessionManager(CDBWrapper& _evoDb, CBLSWorker& _blsWorker); 49 475 : ~CDKGSessionManager() {}; 50 : 51 : void StartThreads(); 52 : void StopThreads(); 53 : 54 : void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload); 55 : 56 : bool ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv); 57 : bool AlreadyHave(const CInv& inv) const; 58 : bool GetContribution(const uint256& hash, CDKGContribution& ret) const; 59 : bool GetComplaint(const uint256& hash, CDKGComplaint& ret) const; 60 : bool GetJustification(const uint256& hash, CDKGJustification& ret) const; 61 : bool GetPrematureCommitment(const uint256& hash, CDKGPrematureCommitment& ret) const; 62 : 63 : // Verified contributions are written while in the DKG 64 : void WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec); 65 : void WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const CBLSSecretKey& skContribution); 66 : bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet); 67 : bool GetVerifiedContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, BLSVerificationVectorPtr& vvecRet, CBLSSecretKey& skContributionRet); 68 : 69 : private: 70 : void CleanupCache(); 71 : }; 72 : 73 : extern std::unique_ptr<CDKGSessionManager> quorumDKGSessionManager; 74 : 75 : } 76 : 77 : #endif // PIVX_LLMQ_QUORUMS_DKGSESSIONMGR_H