Skip to content

Commit

Permalink
refactor: moving options helpers 'Is Quorum <...>' to llmq/options
Browse files Browse the repository at this point in the history
  • Loading branch information
knst committed Jan 10, 2024
1 parent f52d5aa commit 60bc3a8
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 100 deletions.
4 changes: 2 additions & 2 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

static void PreComputeQuorumMembers(const CBlockIndex* pindex, bool reset_cache = false)
{
for (const Consensus::LLMQParams& params : llmq::utils::GetEnabledQuorumParams(pindex->pprev)) {
for (const Consensus::LLMQParams& params : llmq::GetEnabledQuorumParams(pindex->pprev)) {
if (llmq::IsQuorumRotationEnabled(params, pindex) && (pindex->nHeight % params.dkgInterval == 0)) {
llmq::utils::GetAllQuorumMembers(params.type, pindex, reset_cache);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<cons
// until the first non-null commitment has been mined. After the non-null commitment, no other commitments are
// allowed, including null commitments.
// Note: must only check quorums that were enabled at the _previous_ block height to match mining logic
for (const Consensus::LLMQParams& params : utils::GetEnabledQuorumParams(pindex->pprev)) {
for (const Consensus::LLMQParams& params : GetEnabledQuorumParams(pindex->pprev)) {
// skip these checks when replaying blocks after the crash
if (m_chainstate.m_chain.Tip() == nullptr) {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void CDKGSessionManager::ProcessMessage(CNode& pfrom, const std::string& msg_typ
return;
}

if (!utils::IsQuorumTypeEnabled(llmqType, pQuorumBaseBlockIndex->pprev)) {
if (!IsQuorumTypeEnabled(llmqType, pQuorumBaseBlockIndex->pprev)) {
LogPrintf("CDKGSessionManager -- llmqType [%d] quorums aren't active\n", ToUnderlying(llmqType));
m_peerman->Misbehaving(pfrom.GetId(), 100);
return;
Expand Down
77 changes: 76 additions & 1 deletion src/llmq/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
#include <spork.h>
#include <util/ranges.h>
#include <util/system.h>
#include <util/underlying.h>

#include <map>
#include <string>
#include <stdexcept>

static constexpr int TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT = 847000;

namespace llmq
{
Expand Down Expand Up @@ -111,4 +114,76 @@ std::map<Consensus::LLMQType, QvvecSyncMode> GetEnabledQuorumVvecSyncEntries()
return mapQuorumVvecSyncEntries;
}

bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev)
{
return IsQuorumTypeEnabledInternal(llmqType, pindexPrev, std::nullopt);
}

bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev,
std::optional<bool> optDIP0024IsActive)
{
const Consensus::Params& consensusParams = Params().GetConsensus();

const bool fDIP0024IsActive{optDIP0024IsActive.value_or(DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0024))};
switch (llmqType)
{
case Consensus::LLMQType::LLMQ_DEVNET:
return true;
case Consensus::LLMQType::LLMQ_50_60:
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) return true;
// fall through
case Consensus::LLMQType::LLMQ_TEST_INSTANTSEND:
return !fDIP0024IsActive;

case Consensus::LLMQType::LLMQ_TEST:
case Consensus::LLMQType::LLMQ_TEST_PLATFORM:
case Consensus::LLMQType::LLMQ_400_60:
case Consensus::LLMQType::LLMQ_400_85:
case Consensus::LLMQType::LLMQ_DEVNET_PLATFORM:
return true;

case Consensus::LLMQType::LLMQ_TEST_V17: {
return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_TESTDUMMY);
}
case Consensus::LLMQType::LLMQ_100_67:
return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0020);

case Consensus::LLMQType::LLMQ_60_75:
case Consensus::LLMQType::LLMQ_DEVNET_DIP0024:
case Consensus::LLMQType::LLMQ_TEST_DIP0024: {
return fDIP0024IsActive;
}
case Consensus::LLMQType::LLMQ_25_67:
return pindexPrev->nHeight >= TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT;

default:
throw std::runtime_error(strprintf("%s: Unknown LLMQ type %d", __func__, ToUnderlying(llmqType)));
}

// Something wrong with conditions above, they are not consistent
assert(false);
}

std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(gsl::not_null<const CBlockIndex*> pindex)
{
std::vector<Consensus::LLMQType> ret;
ret.reserve(Params().GetConsensus().llmqs.size());
for (const auto& params : Params().GetConsensus().llmqs) {
if (IsQuorumTypeEnabled(params.type, pindex)) {
ret.push_back(params.type);
}
}
return ret;
}

std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(gsl::not_null<const CBlockIndex*> pindex)
{
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> ret;
ret.reserve(Params().GetConsensus().llmqs.size());

std::copy_if(Params().GetConsensus().llmqs.begin(), Params().GetConsensus().llmqs.end(), std::back_inserter(ret),
[&pindex](const auto& params){return IsQuorumTypeEnabled(params.type, pindex);});

return ret;
}
} // namespace llmq
8 changes: 8 additions & 0 deletions src/llmq/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <gsl/pointers.h>

#include <map>
#include <optional>
#include <vector>

class CBlockIndex;

Expand Down Expand Up @@ -40,6 +42,12 @@ bool IsWatchQuorumsEnabled();
/// Returns the parsed entries given by `-llmq-qvvec-sync`
std::map<Consensus::LLMQType, QvvecSyncMode> GetEnabledQuorumVvecSyncEntries();

bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev);
bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev, std::optional<bool> optDIP0024IsActive);

std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(gsl::not_null<const CBlockIndex*> pindex);
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(gsl::not_null<const CBlockIndex*> pindex);

} // namespace llmq

#endif // BITCOIN_LLMQ_OPTIONS_H
2 changes: 1 addition & 1 deletion src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp

std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t nCountRequested) const
{
if (pindexStart == nullptr || nCountRequested == 0 || !utils::IsQuorumTypeEnabled(llmqType, pindexStart)) {
if (pindexStart == nullptr || nCountRequested == 0 || !IsQuorumTypeEnabled(llmqType, pindexStart)) {
return {};
}

Expand Down
76 changes: 0 additions & 76 deletions src/llmq/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
#include <util/time.h>
#include <util/underlying.h>
#include <validation.h>
#include <versionbits.h>

#include <atomic>
#include <optional>

class CBLSSignature;

static constexpr int TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT = 847000;

/**
* Forward declarations
*/
Expand Down Expand Up @@ -850,79 +847,6 @@ void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, gsl::not
}
}

bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev)
{
return IsQuorumTypeEnabledInternal(llmqType, pindexPrev, std::nullopt);
}

bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev,
std::optional<bool> optDIP0024IsActive)
{
const Consensus::Params& consensusParams = Params().GetConsensus();

const bool fDIP0024IsActive{optDIP0024IsActive.value_or(DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0024))};
switch (llmqType)
{
case Consensus::LLMQType::LLMQ_DEVNET:
return true;
case Consensus::LLMQType::LLMQ_50_60:
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) return true;
// fall through
case Consensus::LLMQType::LLMQ_TEST_INSTANTSEND:
return !fDIP0024IsActive;

case Consensus::LLMQType::LLMQ_TEST:
case Consensus::LLMQType::LLMQ_TEST_PLATFORM:
case Consensus::LLMQType::LLMQ_400_60:
case Consensus::LLMQType::LLMQ_400_85:
case Consensus::LLMQType::LLMQ_DEVNET_PLATFORM:
return true;

case Consensus::LLMQType::LLMQ_TEST_V17: {
return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_TESTDUMMY);
}
case Consensus::LLMQType::LLMQ_100_67:
return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0020);

case Consensus::LLMQType::LLMQ_60_75:
case Consensus::LLMQType::LLMQ_DEVNET_DIP0024:
case Consensus::LLMQType::LLMQ_TEST_DIP0024: {
return fDIP0024IsActive;
}
case Consensus::LLMQType::LLMQ_25_67:
return pindexPrev->nHeight >= TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT;

default:
throw std::runtime_error(strprintf("%s: Unknown LLMQ type %d", __func__, ToUnderlying(llmqType)));
}

// Something wrong with conditions above, they are not consistent
assert(false);
}

std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(gsl::not_null<const CBlockIndex*> pindex)
{
std::vector<Consensus::LLMQType> ret;
ret.reserve(Params().GetConsensus().llmqs.size());
for (const auto& params : Params().GetConsensus().llmqs) {
if (IsQuorumTypeEnabled(params.type, pindex)) {
ret.push_back(params.type);
}
}
return ret;
}

std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(gsl::not_null<const CBlockIndex*> pindex)
{
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> ret;
ret.reserve(Params().GetConsensus().llmqs.size());

std::copy_if(Params().GetConsensus().llmqs.begin(), Params().GetConsensus().llmqs.end(), std::back_inserter(ret),
[&pindex](const auto& params){return IsQuorumTypeEnabled(params.type, pindex);});

return ret;
}

template <typename CacheType>
void InitQuorumsCache(CacheType& cache, bool limit_by_connections)
{
Expand Down
11 changes: 1 addition & 10 deletions src/llmq/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
#define BITCOIN_LLMQ_UTILS_H

#include <llmq/params.h>
#include <set>
#include <sync.h>
#include <gsl/pointers.h>
#include <uint256.h>

#include <map>
#include <optional>
#include <set>
#include <vector>

class CConnman;
Expand All @@ -24,8 +23,6 @@ using CDeterministicMNCPtr = std::shared_ptr<const CDeterministicMN>;
namespace llmq
{

class CQuorumSnapshot;

namespace utils
{

Expand All @@ -40,12 +37,6 @@ std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType,
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);

bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev);
bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindexPrev, std::optional<bool> optDIP0024IsActive);

std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(gsl::not_null<const CBlockIndex*> pindex);
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(gsl::not_null<const CBlockIndex*> pindex);

template <typename CacheType>
void InitQuorumsCache(CacheType& cache, bool limit_by_connections = true);

Expand Down
4 changes: 2 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <llmq/chainlocks.h>
#include <llmq/context.h>
#include <llmq/instantsend.h>
#include <llmq/utils.h>
#include <llmq/options.h>
#include <masternode/payments.h>
#include <spork.h>
#include <validation.h>
Expand Down Expand Up @@ -156,7 +156,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
: pblock->GetBlockTime();

if (fDIP0003Active_context) {
for (const Consensus::LLMQParams& params : llmq::utils::GetEnabledQuorumParams(pindexPrev)) {
for (const Consensus::LLMQParams& params : llmq::GetEnabledQuorumParams(pindexPrev)) {
std::vector<CTransactionRef> vqcTx;
if (quorum_block_processor.GetMineableCommitmentsTx(params,
nHeight,
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static UniValue quorum_list(const JSONRPCRequest& request, const ChainstateManag

CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip());

for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pindexTip)) {
for (const auto& type : llmq::GetEnabledQuorumTypes(pindexTip)) {
const auto& llmq_params_opt = Params().GetLLMQ(type);
CHECK_NONFATAL(llmq_params_opt.has_value());
UniValue v(UniValue::VARR);
Expand Down Expand Up @@ -141,7 +141,7 @@ static UniValue quorum_list_extended(const JSONRPCRequest& request, const Chains

CBlockIndex* pblockindex = nHeight != -1 ? WITH_LOCK(cs_main, return chainman.ActiveChain()[nHeight]) : WITH_LOCK(cs_main, return chainman.ActiveChain().Tip());

for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pblockindex)) {
for (const auto& type : llmq::GetEnabledQuorumTypes(pblockindex)) {
const auto& llmq_params_opt = Params().GetLLMQ(type);
CHECK_NONFATAL(llmq_params_opt.has_value());
const auto& llmq_params = llmq_params_opt.value();
Expand Down Expand Up @@ -299,7 +299,7 @@ static UniValue quorum_dkgstatus(const JSONRPCRequest& request, const Chainstate

UniValue minableCommitments(UniValue::VARR);
UniValue quorumArrConnections(UniValue::VARR);
for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pindexTip)) {
for (const auto& type : llmq::GetEnabledQuorumTypes(pindexTip)) {
const auto& llmq_params_opt = Params().GetLLMQ(type);
CHECK_NONFATAL(llmq_params_opt.has_value());
const auto& llmq_params = llmq_params_opt.value();
Expand Down Expand Up @@ -403,7 +403,7 @@ static UniValue quorum_memberof(const JSONRPCRequest& request, const ChainstateM
}

UniValue result(UniValue::VARR);
for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pindexTip)) {
for (const auto& type : llmq::GetEnabledQuorumTypes(pindexTip)) {
const auto& llmq_params_opt = Params().GetLLMQ(type);
CHECK_NONFATAL(llmq_params_opt.has_value());
size_t count = llmq_params_opt->signingActiveQuorumCount;
Expand Down
6 changes: 3 additions & 3 deletions src/test/evo_utils_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
#include <test/util/setup_common.h>

#include <llmq/params.h>
#include <llmq/utils.h>
#include <llmq/options.h>

#include <chainparams.h>

#include <validation.h>

#include <boost/test/unit_test.hpp>

/* TODO: rename this file and test to llmq_utils_test */
/* TODO: rename this file and test to llmq_options_test */
BOOST_AUTO_TEST_SUITE(evo_utils_tests)

void Test(NodeContext& node)
{
using namespace llmq::utils;
using namespace llmq;
auto tip = node.chainman->ActiveTip();
const auto& consensus_params = Params().GetConsensus();
BOOST_CHECK_EQUAL(IsQuorumTypeEnabledInternal(consensus_params.llmqTypeDIP0024InstantSend, tip, false), false);
Expand Down

0 comments on commit 60bc3a8

Please sign in to comment.