Line data Source code
1 : // Copyright (c) 2018-2019 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef PIVX_LLMQ_QUORUMS_UTILS_H 6 : #define PIVX_LLMQ_QUORUMS_UTILS_H 7 : 8 : #include "consensus/params.h" 9 : #include "unordered_lru_cache.h" 10 : 11 : #include <vector> 12 : 13 : class CBLSPublicKey; 14 : 15 : namespace llmq 16 : { 17 : 18 : namespace utils 19 : { 20 : uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256& blockHash, const std::vector<bool>& validMembers, const CBLSPublicKey& pubKey, const uint256& vvecHash); 21 : uint256 BuildSignHash(Consensus::LLMQType llmqType, const uint256& quorumHash, const uint256& id, const uint256& msgHash); 22 : 23 : // works for sig shares and recovered sigs 24 : template<typename T> 25 14430 : uint256 BuildSignHash(const T& s) 26 : { 27 14430 : return BuildSignHash((Consensus::LLMQType)s.llmqType, s.quorumHash, s.id, s.msgHash); 28 : } 29 : 30 : std::string ToHexStr(const std::vector<bool>& vBits); 31 : 32 : bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash); 33 : 34 : template <typename NodesContainer, typename Continue, typename Callback> 35 25342 : static void IterateNodesRandom(NodesContainer& nodeStates, Continue&& cont, Callback&& callback, FastRandomContext& rnd) 36 : { 37 50684 : std::vector<typename NodesContainer::iterator> rndNodes; 38 25342 : rndNodes.reserve(nodeStates.size()); 39 91550 : for (auto it = nodeStates.begin(); it != nodeStates.end(); ++it) { 40 66208 : rndNodes.emplace_back(it); 41 : } 42 25342 : if (rndNodes.empty()) { 43 0 : return; 44 : } 45 25342 : Shuffle(rndNodes.begin(), rndNodes.end(), rnd); 46 : 47 25342 : size_t idx = 0; 48 67007 : while (!rndNodes.empty() && cont()) { 49 67007 : auto nodeId = rndNodes[idx]->first; 50 67007 : auto& ns = rndNodes[idx]->second; 51 : 52 67007 : if (callback(nodeId, ns)) { 53 799 : idx = (idx + 1) % rndNodes.size(); 54 : } else { 55 66208 : rndNodes.erase(rndNodes.begin() + idx); 56 66208 : if (rndNodes.empty()) { 57 : break; 58 : } 59 40866 : idx %= rndNodes.size(); 60 : } 61 : } 62 : } 63 : 64 : template <typename CacheType> 65 : void InitQuorumsCache(CacheType& cache); 66 : 67 : } // namespace llmq::utils 68 : 69 : } // namespace llmq 70 : 71 : #endif // PIVX_LLMQ_QUORUMS_UTILS_H