PIVX Core  5.6.99
P2P Digital Currency
quorums_utils.cpp
Go to the documentation of this file.
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 uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256& blockHash, const std::vector<bool>& validMembers, const CBLSPublicKey& pubKey, const uint256& vvecHash)
22 {
23  CHashWriter hw(SER_NETWORK, 0);
24  hw << static_cast<uint8_t>(llmqType);
25  hw << blockHash;
26  hw << DYNBITSET(validMembers);
27  hw << pubKey;
28  hw << vvecHash;
29  return hw.GetHash();
30 }
31 
32 uint256 BuildSignHash(Consensus::LLMQType llmqType, const uint256& quorumHash, const uint256& id, const uint256& msgHash)
33 {
35  h << static_cast<uint8_t>(llmqType);
36  h << quorumHash;
37  h << id;
38  h << msgHash;
39  return h.GetHash();
40 }
41 
42 std::string ToHexStr(const std::vector<bool>& vBits)
43 {
44  std::vector<uint8_t> vBytes((vBits.size() + 7) / 8);
45  for (size_t i = 0; i < vBits.size(); i++) {
46  vBytes[i / 8] |= vBits[i] << (i % 8);
47  }
48  return HexStr(vBytes);
49 }
50 
51 bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash)
52 {
53 
54  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  auto quorums = quorumManager->ScanQuorums(llmqType, (int)params.signingActiveQuorumCount + 1);
60  for (auto& q : quorums) {
61  if (q->pindexQuorum->GetBlockHash() == quorumHash) {
62  return true;
63  }
64  }
65  return false;
66 }
67 
68 template <typename CacheType>
69 void InitQuorumsCache(CacheType& cache)
70 {
71  for (auto& llmq : Params().GetConsensus().llmqs) {
72  cache.emplace(std::piecewise_construct, std::forward_as_tuple(llmq.first),
73  std::forward_as_tuple(llmq.second.signingActiveQuorumCount + 1));
74  }
75 }
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
const CChainParams & Params()
Return the currently selected parameters.
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:72
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:216
uint256 GetHash()
Definition: hash.h:236
256-bit opaque blob.
Definition: uint256.h:138
LLMQType
Definition: params.h:90
std::string ToHexStr(const std::vector< bool > &vBits)
uint256 BuildSignHash(Consensus::LLMQType llmqType, const uint256 &quorumHash, const uint256 &id, const uint256 &msgHash)
bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256 &quorumHash)
void InitQuorumsCache(CacheType &cache)
uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256 &blockHash, const std::vector< bool > &validMembers, const CBLSPublicKey &pubKey, const uint256 &vvecHash)
Definition: quorums.cpp:26
std::unique_ptr< CQuorumManager > quorumManager
Definition: quorums.cpp:31
@ SER_NETWORK
Definition: serialize.h:174
@ SER_GETHASH
Definition: serialize.h:176
#define DYNBITSET(obj)
Definition: serialize.h:533
std::map< LLMQType, LLMQParams > llmqs
Definition: params.h:279
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.