PIVX Core  5.6.99
P2P Digital Currency
quorums_utils.h
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 #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>
26 {
27  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 static void IterateNodesRandom(NodesContainer& nodeStates, Continue&& cont, Callback&& callback, FastRandomContext& rnd)
36 {
37  std::vector<typename NodesContainer::iterator> rndNodes;
38  rndNodes.reserve(nodeStates.size());
39  for (auto it = nodeStates.begin(); it != nodeStates.end(); ++it) {
40  rndNodes.emplace_back(it);
41  }
42  if (rndNodes.empty()) {
43  return;
44  }
45  Shuffle(rndNodes.begin(), rndNodes.end(), rnd);
46 
47  size_t idx = 0;
48  while (!rndNodes.empty() && cont()) {
49  auto nodeId = rndNodes[idx]->first;
50  auto& ns = rndNodes[idx]->second;
51 
52  if (callback(nodeId, ns)) {
53  idx = (idx + 1) % rndNodes.size();
54  } else {
55  rndNodes.erase(rndNodes.begin() + idx);
56  if (rndNodes.empty()) {
57  break;
58  }
59  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
Fast randomness source.
Definition: random.h:107
256-bit opaque blob.
Definition: uint256.h:138
#define T(expected, seed, data)
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
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:217