fix(llmq): Fix quorum commitments requirement conditions (#4899)

Wasn't really requiring other commitments for rotation quorums once a single commitment was mined
This commit is contained in:
UdjinM6 2022-07-02 21:02:42 +03:00
parent 24205ccce9
commit a999023cbd
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9
2 changed files with 11 additions and 11 deletions

View File

@ -152,18 +152,18 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
break; break;
} }
bool isCommitmentRequired = IsCommitmentRequired(params, pindex->nHeight); const size_t numCommitmentsRequired = GetNumCommitmentsRequired(params, pindex->nHeight);
const auto numCommitmentsInNewBlock = qcs.count(params.type); const auto numCommitmentsInNewBlock = qcs.count(params.type);
if (!isCommitmentRequired && numCommitmentsInNewBlock > 0) { if (numCommitmentsRequired < numCommitmentsInNewBlock) {
return state.DoS(100, false, REJECT_INVALID, "bad-qc-not-allowed"); return state.DoS(100, false, REJECT_INVALID, "bad-qc-not-allowed");
} }
if (isCommitmentRequired && numCommitmentsInNewBlock == 0) { if (numCommitmentsRequired > numCommitmentsInNewBlock) {
return state.DoS(100, false, REJECT_INVALID, "bad-qc-missing"); return state.DoS(100, false, REJECT_INVALID, "bad-qc-missing");
} }
if (llmq::CLLMQUtils::IsQuorumRotationEnabled(params.type, pindex)) { if (llmq::CLLMQUtils::IsQuorumRotationEnabled(params.type, pindex)) {
LogPrintf("[ProcessBlock] h[%d] isCommitmentRequired[%d] numCommitmentsInNewBlock[%d]\n", pindex->nHeight, isCommitmentRequired, numCommitmentsInNewBlock); LogPrintf("[ProcessBlock] h[%d] numCommitmentsRequired[%d] numCommitmentsInNewBlock[%d]\n", pindex->nHeight, numCommitmentsRequired, numCommitmentsInNewBlock);
} }
} }
@ -431,11 +431,11 @@ bool CQuorumBlockProcessor::IsMiningPhase(const Consensus::LLMQParams& llmqParam
return false; return false;
} }
bool CQuorumBlockProcessor::IsCommitmentRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const size_t CQuorumBlockProcessor::GetNumCommitmentsRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
if (!IsMiningPhase(llmqParams, nHeight)) return false; if (!IsMiningPhase(llmqParams, nHeight)) return 0;
// Note: This function can be called for new blocks // Note: This function can be called for new blocks
assert(nHeight <= ::ChainActive().Height() + 1); assert(nHeight <= ::ChainActive().Height() + 1);
@ -443,14 +443,14 @@ bool CQuorumBlockProcessor::IsCommitmentRequired(const Consensus::LLMQParams& ll
bool rotation_enabled = CLLMQUtils::IsQuorumRotationEnabled(llmqParams.type, pindex); bool rotation_enabled = CLLMQUtils::IsQuorumRotationEnabled(llmqParams.type, pindex);
size_t quorums_num = rotation_enabled ? llmqParams.signingActiveQuorumCount : 1; size_t quorums_num = rotation_enabled ? llmqParams.signingActiveQuorumCount : 1;
size_t ret{0};
for (int quorumIndex = 0; quorumIndex < quorums_num; ++quorumIndex) { for (int quorumIndex = 0; quorumIndex < quorums_num; ++quorumIndex) {
uint256 quorumHash = GetQuorumBlockHash(llmqParams, nHeight, quorumIndex); uint256 quorumHash = GetQuorumBlockHash(llmqParams, nHeight, quorumIndex);
if (quorumHash.IsNull()) return false; if (!quorumHash.IsNull() && !HasMinedCommitment(llmqParams.type, quorumHash)) ++ret;
if (HasMinedCommitment(llmqParams.type, quorumHash)) return false;
} }
return true; return ret;
} }
// WARNING: This method returns uint256() on the first block of the DKG interval (because the block hash is not known yet) // WARNING: This method returns uint256() on the first block of the DKG interval (because the block hash is not known yet)
@ -711,7 +711,7 @@ std::optional<std::vector<CFinalCommitment>> CQuorumBlockProcessor::GetMineableC
std::vector<CFinalCommitment> ret; std::vector<CFinalCommitment> ret;
if (!IsCommitmentRequired(llmqParams, nHeight /*, 0*/)) { if (GetNumCommitmentsRequired(llmqParams, nHeight) == 0) {
// no commitment required // no commitment required
return std::nullopt; return std::nullopt;
} }

View File

@ -70,7 +70,7 @@ private:
static bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindex, std::multimap<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main); static bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindex, std::multimap<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, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool IsCommitmentRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main); size_t GetNumCommitmentsRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static uint256 GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, int nHeight, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_main); static uint256 GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, int nHeight, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
}; };