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 : #include "llmq/quorums_utils.h" 6 : 7 : #include "bls/bls_wrapper.h" 8 : #include "chainparams.h" 9 : #include "hash.h" 10 : #include "quorums.h" 11 : #include "quorums_utils.h" 12 : #include "random.h" 13 : #include "validation.h" 14 : 15 : namespace llmq 16 : { 17 : 18 : namespace utils 19 : { 20 : 21 810 : uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256& blockHash, const std::vector<bool>& validMembers, const CBLSPublicKey& pubKey, const uint256& vvecHash) 22 : { 23 810 : CHashWriter hw(SER_NETWORK, 0); 24 810 : hw << static_cast<uint8_t>(llmqType); 25 810 : hw << blockHash; 26 810 : hw << DYNBITSET(validMembers); 27 810 : hw << pubKey; 28 810 : hw << vvecHash; 29 810 : return hw.GetHash(); 30 : } 31 : 32 15654 : uint256 BuildSignHash(Consensus::LLMQType llmqType, const uint256& quorumHash, const uint256& id, const uint256& msgHash) 33 : { 34 15654 : CHashWriter h(SER_GETHASH, 0); 35 15654 : h << static_cast<uint8_t>(llmqType); 36 15654 : h << quorumHash; 37 15654 : h << id; 38 15654 : h << msgHash; 39 15654 : return h.GetHash(); 40 : } 41 : 42 136 : std::string ToHexStr(const std::vector<bool>& vBits) 43 : { 44 136 : std::vector<uint8_t> vBytes((vBits.size() + 7) / 8); 45 544 : for (size_t i = 0; i < vBits.size(); i++) { 46 408 : vBytes[i / 8] |= vBits[i] << (i % 8); 47 : } 48 272 : return HexStr(vBytes); 49 : } 50 : 51 5694 : bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash) 52 : { 53 : 54 5694 : auto& params = Params().GetConsensus().llmqs.at(llmqType); 55 : 56 : // sig shares and recovered sigs are only accepted from recent/active quorums 57 : // we allow one more active quorum as specified in consensus, as otherwise there is a small window where things could 58 : // fail while we are on the brink of a new quorum 59 11388 : auto quorums = quorumManager->ScanQuorums(llmqType, (int)params.signingActiveQuorumCount + 1); 60 9180 : for (auto& q : quorums) { 61 9161 : if (q->pindexQuorum->GetBlockHash() == quorumHash) { 62 5675 : return true; 63 : } 64 : } 65 19 : return false; 66 : } 67 : 68 : template <typename CacheType> 69 1425 : void InitQuorumsCache(CacheType& cache) 70 : { 71 3318 : for (auto& llmq : Params().GetConsensus().llmqs) { 72 1893 : cache.emplace(std::piecewise_construct, std::forward_as_tuple(llmq.first), 73 1893 : std::forward_as_tuple(llmq.second.signingActiveQuorumCount + 1)); 74 : } 75 1425 : } 76 : 77 : template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, bool, StaticSaltedHasher>>>(std::map<Consensus::LLMQType, unordered_lru_cache<uint256, bool, StaticSaltedHasher>>& cache); 78 : template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::vector<CQuorumCPtr>, StaticSaltedHasher>>>(std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::vector<CQuorumCPtr>, StaticSaltedHasher>>& cache); 79 : template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, CQuorumCPtr, StaticSaltedHasher>>>(std::map<Consensus::LLMQType, unordered_lru_cache<uint256, CQuorumCPtr, StaticSaltedHasher>>& cache); 80 : 81 : } // namespace llmq::utils 82 : 83 : } // namespace llmq