Skip to content

Commit

Permalink
refactor: minimize passing around llmqType just to search for LLMQPar…
Browse files Browse the repository at this point in the history
…ams (dashpay#4551)

* minimize passing around llmqType just to search for LLMQParams

* more

Co-authored-by: UdjinM6 <[email protected]>
  • Loading branch information
PastaPastaPasta and UdjinM6 authored Oct 28, 2021
1 parent c20ecca commit a0b68ca
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ void CDeterministicMNManager::HandleQuorumCommitment(const llmq::CFinalCommitmen
{
// The commitment has already been validated at this point, so it's safe to use members of it

auto members = llmq::CLLMQUtils::GetAllQuorumMembers(qc.llmqType, pQuorumBaseBlockIndex);
auto members = llmq::CLLMQUtils::GetAllQuorumMembers(llmq::GetLLMQParams(qc.llmqType), pQuorumBaseBlockIndex);

for (size_t i = 0; i < members.size(); i++) {
if (!mnList.HasMN(members[i]->proTxHash)) {
Expand Down
43 changes: 21 additions & 22 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
// 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 auto& type : CLLMQUtils::GetEnabledQuorumTypes(pindex->pprev)) {
for (const Consensus::LLMQParams& params : CLLMQUtils::GetEnabledQuorumParams(pindex->pprev)) {
// skip these checks when replaying blocks after the crash
if (!::ChainActive().Tip()) {
break;
}

// does the currently processed block contain a (possibly null) commitment for the current session?
bool hasCommitmentInNewBlock = qcs.count(type) != 0;
bool isCommitmentRequired = IsCommitmentRequired(type, pindex->nHeight);
bool hasCommitmentInNewBlock = qcs.count(params.type) != 0;
bool isCommitmentRequired = IsCommitmentRequired(params, pindex->nHeight);

if (hasCommitmentInNewBlock && !isCommitmentRequired) {
// If we're either not in the mining phase or a non-null commitment was mined already, reject the block
Expand Down Expand Up @@ -191,7 +191,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH

const auto& llmq_params = GetLLMQParams(qc.llmqType);

uint256 quorumHash = GetQuorumBlockHash(qc.llmqType, nHeight);
uint256 quorumHash = GetQuorumBlockHash(llmq_params, nHeight);

// skip `bad-qc-block` checks below when replaying blocks after the crash
if (!::ChainActive().Tip()) {
Expand All @@ -217,7 +217,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
return state.DoS(100, false, REJECT_INVALID, "bad-qc-dup");
}

if (!IsMiningPhase(llmq_params.type, nHeight)) {
if (!IsMiningPhase(llmq_params, nHeight)) {
// should not happen as it's already handled in ProcessBlock
return state.DoS(100, false, REJECT_INVALID, "bad-qc-height");
}
Expand Down Expand Up @@ -367,38 +367,37 @@ bool CQuorumBlockProcessor::GetCommitmentsFromBlock(const CBlock& block, const C
return true;
}

bool CQuorumBlockProcessor::IsMiningPhase(Consensus::LLMQType llmqType, int nHeight)
bool CQuorumBlockProcessor::IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight)
{
const auto& llmq_params = GetLLMQParams(llmqType);
int phaseIndex = nHeight % llmq_params.dkgInterval;
if (phaseIndex >= llmq_params.dkgMiningWindowStart && phaseIndex <= llmq_params.dkgMiningWindowEnd) {
int phaseIndex = nHeight % llmqParams.dkgInterval;
if (phaseIndex >= llmqParams.dkgMiningWindowStart && phaseIndex <= llmqParams.dkgMiningWindowEnd) {
return true;
}
return false;
}

bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight) const
bool CQuorumBlockProcessor::IsCommitmentRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const
{
AssertLockHeld(cs_main);

uint256 quorumHash = GetQuorumBlockHash(llmqType, nHeight);
uint256 quorumHash = GetQuorumBlockHash(llmqParams, nHeight);

// perform extra check for quorumHash.IsNull as the quorum hash is unknown for the first block of a session
// this is because the currently processed block's hash will be the quorumHash of this session
bool isMiningPhase = !quorumHash.IsNull() && IsMiningPhase(llmqType, nHeight);
bool isMiningPhase = !quorumHash.IsNull() && IsMiningPhase(llmqParams, nHeight);

// did we already mine a non-null commitment for this session?
bool hasMinedCommitment = !quorumHash.IsNull() && HasMinedCommitment(llmqType, quorumHash);
bool hasMinedCommitment = !quorumHash.IsNull() && HasMinedCommitment(llmqParams.type, quorumHash);

return isMiningPhase && !hasMinedCommitment;
}

// WARNING: This method returns uint256() on the first block of the DKG interval (because the block hash is not known yet)
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight)
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, int nHeight)
{
AssertLockHeld(cs_main);

int quorumStartHeight = nHeight - (nHeight % GetLLMQParams(llmqType).dkgInterval);
int quorumStartHeight = nHeight - (nHeight % llmqParams.dkgInterval);
uint256 quorumBlockHash;
if (!GetBlockHash(quorumBlockHash, quorumStartHeight)) {
return {};
Expand Down Expand Up @@ -545,27 +544,27 @@ bool CQuorumBlockProcessor::GetMineableCommitmentByHash(const uint256& commitmen

// Will return false if no commitment should be mined
// Will return true and a null commitment if no mineable commitment is known and none was mined yet
bool CQuorumBlockProcessor::GetMineableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret) const
bool CQuorumBlockProcessor::GetMineableCommitment(const Consensus::LLMQParams& llmqParams, int nHeight, CFinalCommitment& ret) const
{
AssertLockHeld(cs_main);

if (!IsCommitmentRequired(llmqType, nHeight)) {
if (!IsCommitmentRequired(llmqParams, nHeight)) {
// no commitment required
return false;
}

uint256 quorumHash = GetQuorumBlockHash(llmqType, nHeight);
uint256 quorumHash = GetQuorumBlockHash(llmqParams, nHeight);
if (quorumHash.IsNull()) {
return false;
}

LOCK(minableCommitmentsCs);

auto k = std::make_pair(llmqType, quorumHash);
auto k = std::make_pair(llmqParams.type, quorumHash);
auto it = minableCommitmentsByQuorum.find(k);
if (it == minableCommitmentsByQuorum.end()) {
// null commitment required
ret = CFinalCommitment(GetLLMQParams(llmqType), quorumHash);
ret = CFinalCommitment(llmqParams, quorumHash);
return true;
}

Expand All @@ -574,12 +573,12 @@ bool CQuorumBlockProcessor::GetMineableCommitment(Consensus::LLMQType llmqType,
return true;
}

bool CQuorumBlockProcessor::GetMineableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret) const
bool CQuorumBlockProcessor::GetMineableCommitmentTx(const Consensus::LLMQParams& llmqParams, int nHeight, CTransactionRef& ret) const
{
AssertLockHeld(cs_main);

CFinalCommitmentTxPayload qc;
if (!GetMineableCommitment(llmqType, nHeight, qc.commitment)) {
if (!GetMineableCommitment(llmqParams, nHeight, qc.commitment)) {
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions src/llmq/blockprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class CQuorumBlockProcessor
void AddMineableCommitment(const CFinalCommitment& fqc);
bool HasMineableCommitment(const uint256& hash) const;
bool GetMineableCommitmentByHash(const uint256& commitmentHash, CFinalCommitment& ret) const;
bool GetMineableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool GetMineableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool GetMineableCommitment(const Consensus::LLMQParams& llmqParams, int nHeight, CFinalCommitment& ret) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool GetMineableCommitmentTx(const Consensus::LLMQParams& llmqParams, int nHeight, CTransactionRef& ret) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);

bool HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const;
CFinalCommitmentPtr GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash) const;
Expand All @@ -66,9 +66,9 @@ class CQuorumBlockProcessor
private:
static bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindex, std::map<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static bool IsMiningPhase(Consensus::LLMQType llmqType, int nHeight);
bool IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static uint256 GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight);
bool IsCommitmentRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static uint256 GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
};

extern CQuorumBlockProcessor* quorumBlockProcessor;
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool CFinalCommitment::Verify(const CBlockIndex* pQuorumBaseBlockIndex, bool che
return false;
}

auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pQuorumBaseBlockIndex);
auto members = CLLMQUtils::GetAllQuorumMembers(llmq_params, pQuorumBaseBlockIndex);
for (size_t i = members.size(); i < llmq_params.size; i++) {
if (validMembers[i]) {
LogPrintfFinalCommitment("invalid validMembers bitset. bit %d should not be set\n", i);
Expand Down
12 changes: 6 additions & 6 deletions src/llmq/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ UniValue CDKGDebugSessionStatus::ToJson(int detailLevel) const
if (detailLevel == 2) {
const CBlockIndex* pindex = WITH_LOCK(cs_main, return LookupBlockIndex(quorumHash));
if (pindex != nullptr) {
dmnMembers = CLLMQUtils::GetAllQuorumMembers( llmqType, pindex);
dmnMembers = CLLMQUtils::GetAllQuorumMembers(GetLLMQParams(llmqType), pindex);
}
}

Expand Down Expand Up @@ -147,23 +147,23 @@ void CDKGDebugManager::ResetLocalSessionStatus(Consensus::LLMQType llmqType)
localStatus.nTime = GetAdjustedTime();
}

void CDKGDebugManager::InitLocalSessionStatus(Consensus::LLMQType llmqType, const uint256& quorumHash, int quorumHeight)
void CDKGDebugManager::InitLocalSessionStatus(const Consensus::LLMQParams& llmqParams, const uint256& quorumHash, int quorumHeight)
{
LOCK(cs);

auto it = localStatus.sessions.find(llmqType);
auto it = localStatus.sessions.find(llmqParams.type);
if (it == localStatus.sessions.end()) {
it = localStatus.sessions.emplace(llmqType, CDKGDebugSessionStatus()).first;
it = localStatus.sessions.emplace(llmqParams.type, CDKGDebugSessionStatus()).first;
}

auto& session = it->second;
session.llmqType = llmqType;
session.llmqType = llmqParams.type;
session.quorumHash = quorumHash;
session.quorumHeight = (uint32_t)quorumHeight;
session.phase = 0;
session.statusBitset = 0;
session.members.clear();
session.members.resize((size_t)GetLLMQParams(llmqType).size);
session.members.resize((size_t)llmqParams.size);
}

void CDKGDebugManager::UpdateLocalSessionStatus(Consensus::LLMQType llmqType, std::function<bool(CDKGDebugSessionStatus& status)>&& func)
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CDKGDebugManager
void GetLocalDebugStatus(CDKGDebugStatus& ret) const;

void ResetLocalSessionStatus(Consensus::LLMQType llmqType);
void InitLocalSessionStatus(Consensus::LLMQType llmqType, const uint256& quorumHash, int quorumHeight);
void InitLocalSessionStatus(const Consensus::LLMQParams& llmqParams, const uint256& quorumHash, int quorumHeight);

void UpdateLocalSessionStatus(Consensus::LLMQType llmqType, std::function<bool(CDKGDebugSessionStatus& status)>&& func);
void UpdateLocalMemberStatus(Consensus::LLMQType llmqType, size_t memberIdx, std::function<bool(CDKGDebugMemberStatus& status)>&& func);
Expand Down
4 changes: 2 additions & 2 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ bool CDKGSession::Init(const CBlockIndex* _pQuorumBaseBlockIndex, const std::vec
}

if (!myProTxHash.IsNull()) {
quorumDKGDebugManager->InitLocalSessionStatus(params.type, m_quorum_base_block_index->GetBlockHash(), m_quorum_base_block_index->nHeight);
relayMembers = CLLMQUtils::GetQuorumRelayMembers(params.type, m_quorum_base_block_index, myProTxHash, true);
quorumDKGDebugManager->InitLocalSessionStatus(params, m_quorum_base_block_index->GetBlockHash(), m_quorum_base_block_index->nHeight);
relayMembers = CLLMQUtils::GetQuorumRelayMembers(params, m_quorum_base_block_index, myProTxHash, true);
}

if (myProTxHash.IsNull()) {
Expand Down
6 changes: 3 additions & 3 deletions src/llmq/dkgsessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pQuorumBaseBlockIndex)
return false;
}

auto mns = CLLMQUtils::GetAllQuorumMembers(params.type, pQuorumBaseBlockIndex);
auto mns = CLLMQUtils::GetAllQuorumMembers(params, pQuorumBaseBlockIndex);

if (!curSession->Init(pQuorumBaseBlockIndex, mns, WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) {
LogPrintf("CDKGSessionManager::%s -- quorum initialization failed for %s\n", __func__, curSession->params.name);
Expand Down Expand Up @@ -518,9 +518,9 @@ void CDKGSessionHandler::HandleDKGRound()
return changed;
});

CLLMQUtils::EnsureQuorumConnections(params.type, pQuorumBaseBlockIndex, curSession->myProTxHash);
CLLMQUtils::EnsureQuorumConnections(params, pQuorumBaseBlockIndex, curSession->myProTxHash);
if (curSession->AreWeMember()) {
CLLMQUtils::AddQuorumProbeConnections(params.type, pQuorumBaseBlockIndex, curSession->myProTxHash);
CLLMQUtils::AddQuorumProbeConnections(params, pQuorumBaseBlockIndex, curSession->myProTxHash);
}

WaitForNextPhase(QuorumPhase_Initialized, QuorumPhase_Contribute, curQuorumHash, []{return false;});
Expand Down
4 changes: 2 additions & 2 deletions src/llmq/dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void CDKGSessionManager::WriteEncryptedContributions(Consensus::LLMQType llmqTyp
bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet) const
{
LOCK(contributionsCacheCs);
auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pQuorumBaseBlockIndex);
auto members = CLLMQUtils::GetAllQuorumMembers(GetLLMQParams(llmqType), pQuorumBaseBlockIndex);

memberIndexesRet.clear();
vvecsRet.clear();
Expand Down Expand Up @@ -344,7 +344,7 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType,

bool CDKGSessionManager::GetEncryptedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const std::vector<bool>& validMembers, const uint256& nProTxHash, std::vector<CBLSIESEncryptedObject<CBLSSecretKey>>& vecRet) const
{
auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pQuorumBaseBlockIndex);
auto members = CLLMQUtils::GetAllQuorumMembers(GetLLMQParams(llmqType), pQuorumBaseBlockIndex);

vecRet.clear();
vecRet.reserve(members.size());
Expand Down
21 changes: 10 additions & 11 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial
}

for (auto& p : Params().GetConsensus().llmqs) {
EnsureQuorumConnections(p.first, pindexNew);
EnsureQuorumConnections(p.second, pindexNew);
}

{
Expand All @@ -276,26 +276,24 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial
TriggerQuorumDataRecoveryThreads(pindexNew);
}

void CQuorumManager::EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexNew) const
void CQuorumManager::EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex* pindexNew) const
{
const auto& llmq_params = GetLLMQParams(llmqType);
auto lastQuorums = ScanQuorums(llmqParams.type, pindexNew, (size_t)llmqParams.keepOldConnections);

auto lastQuorums = ScanQuorums(llmqType, pindexNew, (size_t)llmq_params.keepOldConnections);

auto connmanQuorumsToDelete = g_connman->GetMasternodeQuorums(llmqType);
auto connmanQuorumsToDelete = g_connman->GetMasternodeQuorums(llmqParams.type);

// don't remove connections for the currently in-progress DKG round
int curDkgHeight = pindexNew->nHeight - (pindexNew->nHeight % llmq_params.dkgInterval);
int curDkgHeight = pindexNew->nHeight - (pindexNew->nHeight % llmqParams.dkgInterval);
auto curDkgBlock = pindexNew->GetAncestor(curDkgHeight)->GetBlockHash();
connmanQuorumsToDelete.erase(curDkgBlock);

for (const auto& quorum : lastQuorums) {
if (CLLMQUtils::EnsureQuorumConnections(llmqType, quorum->m_quorum_base_block_index, WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) {
if (CLLMQUtils::EnsureQuorumConnections(llmqParams, quorum->m_quorum_base_block_index, WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) {
continue;
}
if (connmanQuorumsToDelete.count(quorum->qc->quorumHash) > 0) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- removing masternodes quorum connections for quorum %s:\n", __func__, quorum->qc->quorumHash.ToString());
g_connman->RemoveMasternodeQuorumNodes(llmqType, quorum->qc->quorumHash);
g_connman->RemoveMasternodeQuorumNodes(llmqParams.type, quorum->qc->quorumHash);
}
}
}
Expand All @@ -313,8 +311,9 @@ CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType l
}
assert(qc->quorumHash == pQuorumBaseBlockIndex->GetBlockHash());

auto quorum = std::make_shared<CQuorum>(llmq::GetLLMQParams(llmqType), blsWorker);
auto members = CLLMQUtils::GetAllQuorumMembers((Consensus::LLMQType)qc->llmqType, pQuorumBaseBlockIndex);
const auto& llmqParams = llmq::GetLLMQParams(llmqType);
auto quorum = std::make_shared<CQuorum>(llmqParams, blsWorker);
auto members = CLLMQUtils::GetAllQuorumMembers(llmqParams, pQuorumBaseBlockIndex);

quorum->Init(qc, pQuorumBaseBlockIndex, minedBlockHash, members);

Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class CQuorumManager

private:
// all private methods here are cs_main-free
void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexNew) const;
void EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex *pindexNew) const;

CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex) const EXCLUSIVE_LOCKS_REQUIRED(quorumsCacheCs);
bool BuildQuorumContributions(const CFinalCommitmentPtr& fqc, const std::shared_ptr<CQuorum>& quorum) const;
Expand Down
Loading

0 comments on commit a0b68ca

Please sign in to comment.