2022-06-08 01:36:46 +02:00
|
|
|
// Copyright (c) 2018-2022 The Dash Core developers
|
2018-11-23 15:42:09 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2021-10-02 19:32:24 +02:00
|
|
|
#ifndef BITCOIN_LLMQ_UTILS_H
|
|
|
|
#define BITCOIN_LLMQ_UTILS_H
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <consensus/params.h>
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2021-04-02 03:19:34 +02:00
|
|
|
#include <random.h>
|
2022-04-16 16:46:04 +02:00
|
|
|
#include <set>
|
2021-04-16 05:41:16 +02:00
|
|
|
#include <sync.h>
|
2022-10-15 22:11:49 +02:00
|
|
|
#include <versionbits.h>
|
2023-11-10 15:33:21 +01:00
|
|
|
#include <gsl/pointers.h>
|
2022-10-15 22:11:49 +02:00
|
|
|
|
2022-04-19 08:25:20 +02:00
|
|
|
#include <optional>
|
2022-04-16 16:46:04 +02:00
|
|
|
#include <vector>
|
2021-04-16 05:41:16 +02:00
|
|
|
|
2022-04-07 17:32:40 +02:00
|
|
|
class CConnman;
|
2021-04-16 05:41:16 +02:00
|
|
|
class CBlockIndex;
|
|
|
|
class CDeterministicMN;
|
2022-04-16 16:46:04 +02:00
|
|
|
class CDeterministicMNList;
|
2021-10-05 23:26:29 +02:00
|
|
|
using CDeterministicMNCPtr = std::shared_ptr<const CDeterministicMN>;
|
2021-04-16 05:41:16 +02:00
|
|
|
class CBLSPublicKey;
|
2020-12-29 11:30:50 +01:00
|
|
|
|
2018-11-23 15:42:09 +01:00
|
|
|
namespace llmq
|
|
|
|
{
|
|
|
|
|
2022-09-22 13:14:48 +02:00
|
|
|
class CQuorumManager;
|
2022-04-16 16:46:04 +02:00
|
|
|
class CQuorumSnapshot;
|
|
|
|
|
2023-12-04 11:38:47 +01:00
|
|
|
// A separate cache instance instead of versionbitscache has been introduced to avoid locking cs_main
|
2020-12-29 11:30:50 +01:00
|
|
|
// and dealing with all kinds of deadlocks.
|
2023-12-04 11:38:47 +01:00
|
|
|
// TODO: drop llmq_versionbitscache completely so far as VersionBitsCache do not uses anymore cs_main
|
|
|
|
extern VersionBitsCache llmq_versionbitscache;
|
2020-12-29 11:30:50 +01:00
|
|
|
|
2021-02-01 17:10:19 +01:00
|
|
|
static const bool DEFAULT_ENABLE_QUORUM_DATA_RECOVERY = true;
|
|
|
|
|
2021-03-16 23:50:41 +01:00
|
|
|
enum class QvvecSyncMode {
|
|
|
|
Invalid = -1,
|
|
|
|
Always = 0,
|
|
|
|
OnlyIfTypeMember = 1,
|
|
|
|
};
|
|
|
|
|
2022-08-02 19:14:25 +02:00
|
|
|
namespace utils
|
2018-11-23 15:42:09 +01:00
|
|
|
{
|
2022-04-16 16:46:04 +02:00
|
|
|
|
2022-08-02 19:14:25 +02:00
|
|
|
// includes members which failed DKG
|
2023-11-10 15:33:21 +01:00
|
|
|
std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool reset_cache = false);
|
2022-04-16 16:46:04 +02:00
|
|
|
|
2023-11-10 15:33:21 +01:00
|
|
|
uint256 GetHashModifier(const Consensus::LLMQParams& llmqParams, gsl::not_null<const CBlockIndex*> pCycleQuorumBaseBlockIndex);
|
2022-08-02 19:14:25 +02:00
|
|
|
uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256& blockHash, const std::vector<bool>& validMembers, const CBLSPublicKey& pubKey, const uint256& vvecHash);
|
|
|
|
uint256 BuildSignHash(Consensus::LLMQType llmqType, const uint256& quorumHash, const uint256& id, const uint256& msgHash);
|
2019-01-15 15:35:26 +01:00
|
|
|
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options
|
2022-08-02 19:14:25 +02:00
|
|
|
bool IsAllMembersConnectedEnabled(Consensus::LLMQType llmqType);
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options
|
2022-08-02 19:14:25 +02:00
|
|
|
bool IsQuorumPoseEnabled(Consensus::LLMQType llmqType);
|
|
|
|
uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256& proTxHash2);
|
2023-11-10 15:33:21 +01:00
|
|
|
std::set<uint256> GetQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound);
|
|
|
|
std::set<uint256> GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound);
|
|
|
|
std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, size_t memberCount, size_t connectionCount);
|
2020-03-16 10:54:59 +01:00
|
|
|
|
2023-11-10 15:33:21 +01:00
|
|
|
bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, CConnman& connman, const uint256& myProTxHash);
|
|
|
|
void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, CConnman& connman, const uint256& myProTxHash);
|
2022-04-16 16:46:04 +02:00
|
|
|
|
2022-09-22 13:14:48 +02:00
|
|
|
bool IsQuorumActive(Consensus::LLMQType llmqType, const CQuorumManager& qman, const uint256& quorumHash);
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options maybe not
|
2023-12-04 09:40:01 +01:00
|
|
|
bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, const CQuorumManager& qman, gsl::not_null<const CBlockIndex*> pindexPrev);
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options maybe not
|
2023-12-04 09:40:01 +01:00
|
|
|
bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, const CQuorumManager& qman, gsl::not_null<const CBlockIndex*> pindexPrev, std::optional<bool> optDIP0024IsActive, std::optional<bool> optHaveDIP0024Quorums);
|
2019-01-15 15:35:26 +01:00
|
|
|
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options maybe not
|
2023-11-10 15:33:21 +01:00
|
|
|
std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(gsl::not_null<const CBlockIndex*> pindex);
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options maybe not
|
2023-11-10 15:33:21 +01:00
|
|
|
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(gsl::not_null<const CBlockIndex*> pindex);
|
2022-04-16 16:46:04 +02:00
|
|
|
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options
|
2023-11-10 15:33:21 +01:00
|
|
|
bool IsQuorumRotationEnabled(const Consensus::LLMQParams& llmqParams, gsl::not_null<const CBlockIndex*> pindex);
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO deployments
|
2023-11-10 15:33:21 +01:00
|
|
|
bool IsV20Active(gsl::not_null<const CBlockIndex*> pindex);
|
2021-02-01 17:10:19 +01:00
|
|
|
|
2022-08-02 19:14:25 +02:00
|
|
|
/// Returns the state of `-llmq-data-recovery`
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options
|
2022-08-02 19:14:25 +02:00
|
|
|
bool QuorumDataRecoveryEnabled();
|
2021-03-29 20:09:09 +02:00
|
|
|
|
2023-12-04 09:03:51 +01:00
|
|
|
// TODO options
|
2022-08-02 19:14:25 +02:00
|
|
|
/// Returns the state of `-watchquorums`
|
|
|
|
bool IsWatchQuorumsEnabled();
|
2021-02-01 17:10:19 +01:00
|
|
|
|
2022-08-02 19:14:25 +02:00
|
|
|
/// Returns the parsed entries given by `-llmq-qvvec-sync`
|
|
|
|
std::map<Consensus::LLMQType, QvvecSyncMode> GetEnabledQuorumVvecSyncEntries();
|
|
|
|
|
|
|
|
template<typename NodesContainer, typename Continue, typename Callback>
|
2023-02-11 03:25:11 +01:00
|
|
|
void IterateNodesRandom(NodesContainer& nodeStates, Continue&& cont, Callback&& callback, FastRandomContext& rnd)
|
2022-08-02 19:14:25 +02:00
|
|
|
{
|
|
|
|
std::vector<typename NodesContainer::iterator> rndNodes;
|
|
|
|
rndNodes.reserve(nodeStates.size());
|
|
|
|
for (auto it = nodeStates.begin(); it != nodeStates.end(); ++it) {
|
|
|
|
rndNodes.emplace_back(it);
|
2019-01-15 15:35:26 +01:00
|
|
|
}
|
2022-08-02 19:14:25 +02:00
|
|
|
if (rndNodes.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Shuffle(rndNodes.begin(), rndNodes.end(), rnd);
|
|
|
|
|
|
|
|
size_t idx = 0;
|
|
|
|
while (!rndNodes.empty() && cont()) {
|
|
|
|
auto nodeId = rndNodes[idx]->first;
|
|
|
|
auto& ns = rndNodes[idx]->second;
|
|
|
|
|
|
|
|
if (callback(nodeId, ns)) {
|
|
|
|
idx = (idx + 1) % rndNodes.size();
|
|
|
|
} else {
|
|
|
|
rndNodes.erase(rndNodes.begin() + idx);
|
|
|
|
if (rndNodes.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx %= rndNodes.size();
|
Feature request #3425 - Add information to "protx diff" (#3468)
* llmq: Add more information to the result of CFinalCommitment::ToJson
Example output:
```
{
"version": 1,
"llmqType": 1,
"quorumHash":
"000002a20a35757fc467f0ebe8663f76e4c5df74411cc5f1735cfd92278071fe",
"signersCount": 49,
"signers": "03fbffffffffff",
"validMembersCount": 49,
"validMembers": "03fbffffffffff",
"quorumPublicKey":
"8db1108b50dd741076b577f6798f0bf382ca4d11dafd55ed14aaaf613f604aa55bf1da1f0469f75105376e440381ee23",
"quorumVvecHash":
"feedb15d5a7ca25a68d9c51036973d37925d8439d4769d7dd7bace7440089071",
"quorumSig":
"1879f6260ef427dc9542088b39e02a8e73e34c0484269cbc12108ab991d438f81e5be80a5b4b5595b6634113fdf060060c8fe190677627fb2d25f6e052c7f93598d36dd415f8f84c693c6a5943098616a1e135522852d8f572bb6e1470df4a94",
"membersSig":
"8ef7d881fcc1e8111dd82c556ab62dad56ea5ecb47a9ac067eb2057d92d27e39530630a7f77d19adc857d33eb485a1b601b2d78ac4c73742f2036089bfb1f25441f1dfcd3cfd1eed6e7d664c0e97bf75b7d7097ebd647791fbcef070fe3e3e91"
}
```
* llmq: Changed the byte order in ToHexStr(const std::vector<bool>& vBits)
2020-05-11 14:35:28 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-02 19:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CacheType>
|
2023-11-29 15:17:58 +01:00
|
|
|
void InitQuorumsCache(CacheType& cache, bool limit_by_connections = true);
|
2022-08-02 19:14:25 +02:00
|
|
|
|
2023-12-20 16:54:00 +01:00
|
|
|
[[ nodiscard ]] static constexpr int max_cycles(const Consensus::LLMQParams& llmqParams, int quorums_count)
|
|
|
|
{
|
|
|
|
return llmqParams.useRotation ? quorums_count / llmqParams.signingActiveQuorumCount : quorums_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[ nodiscard ]] static constexpr int max_store_depth(const Consensus::LLMQParams& llmqParams)
|
|
|
|
{
|
|
|
|
// For how many blocks recent DKG info should be kept
|
|
|
|
return max_cycles(llmqParams, llmqParams.keepOldKeys) * llmqParams.dkgInterval;
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:14:25 +02:00
|
|
|
} // namespace utils
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2023-03-13 17:11:17 +01:00
|
|
|
[[ nodiscard ]] const std::optional<Consensus::LLMQParams> GetLLMQParams(Consensus::LLMQType llmqType);
|
2021-01-18 01:27:29 +01:00
|
|
|
|
2019-07-15 20:55:01 +02:00
|
|
|
} // namespace llmq
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2021-10-02 19:32:24 +02:00
|
|
|
#endif // BITCOIN_LLMQ_UTILS_H
|