Skip to content

Commit 2127a42

Browse files
UdjinM6codablock
authored andcommitted
Further refactoring of CQuorumBlockProcessor (#2545)
* Switch GetQuorumBlockHash from CBlockIndex* to nHeight * `pindexPrev -> pindex` for ProcessCommitment * Switch IsCommitmentRequired from CBlockIndex* to block height * Switch GetMinableCommitment/Tx from CBlockIndex* to block height * Add `AssertLockHeld(cs_main);` Co-Authored-By: UdjinM6 <UdjinM6@users.noreply.github.com>
1 parent 09730e1 commit 2127a42

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/llmq/quorums_blockprocessor.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
127127

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

132132
if (hasCommitmentInNewBlock && !isCommitmentRequired) {
133133
// If we're either not in the mining phase or a non-null commitment was mined already, reject the block
@@ -143,18 +143,18 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
143143

144144
for (auto& p : qcs) {
145145
auto& qc = p.second;
146-
if (!ProcessCommitment(pindex->pprev, qc, state)) {
146+
if (!ProcessCommitment(pindex, qc, state)) {
147147
return false;
148148
}
149149
}
150150
return true;
151151
}
152152

153-
bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindexPrev, const CFinalCommitment& qc, CValidationState& state)
153+
bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindex, const CFinalCommitment& qc, CValidationState& state)
154154
{
155155
auto& params = Params().GetConsensus().llmqs.at((Consensus::LLMQType)qc.llmqType);
156156

157-
uint256 quorumHash = GetQuorumBlockHash((Consensus::LLMQType)qc.llmqType, pindexPrev);
157+
uint256 quorumHash = GetQuorumBlockHash((Consensus::LLMQType)qc.llmqType, pindex->nHeight);
158158
if (quorumHash.IsNull()) {
159159
return state.DoS(100, false, REJECT_INVALID, "bad-qc-block");
160160
}
@@ -174,7 +174,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindexPrev, con
174174
return state.DoS(100, false, REJECT_INVALID, "bad-qc-dup");
175175
}
176176

177-
if (!IsMiningPhase(params.type, pindexPrev->nHeight + 1)) {
177+
if (!IsMiningPhase(params.type, pindex->nHeight)) {
178178
// should not happen as it's already handled in ProcessBlock
179179
return state.DoS(100, false, REJECT_INVALID, "bad-qc-height");
180180
}
@@ -264,7 +264,7 @@ bool CQuorumBlockProcessor::IsMiningPhase(Consensus::LLMQType llmqType, int nHei
264264
return false;
265265
}
266266

267-
bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev)
267+
bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight)
268268
{
269269
// BEGIN TEMPORARY CODE
270270
bool allowMissingQc = false;
@@ -278,19 +278,19 @@ bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, c
278278
LOCK(cs_main);
279279
const auto& consensus = Params().GetConsensus();
280280
if (consensus.nTemporaryTestnetForkDIP3Height != 0 &&
281-
pindexPrev->nHeight + 1 > consensus.nTemporaryTestnetForkDIP3Height &&
282-
pindexPrev->nHeight + 1 < consensus.nTemporaryTestnetForkHeight &&
281+
nHeight > consensus.nTemporaryTestnetForkDIP3Height &&
282+
nHeight < consensus.nTemporaryTestnetForkHeight &&
283283
chainActive[consensus.nTemporaryTestnetForkDIP3Height]->GetBlockHash() == consensus.nTemporaryTestnetForkDIP3BlockHash) {
284284
allowMissingQc = true;
285285
}
286286
}
287287
// END TEMPORARY CODE
288288

289-
uint256 quorumHash = GetQuorumBlockHash(llmqType, pindexPrev);
289+
uint256 quorumHash = GetQuorumBlockHash(llmqType, nHeight);
290290

291291
// perform extra check for quorumHash.IsNull as the quorum hash is unknown for the first block of a session
292292
// this is because the currently processed block's hash will be the quorumHash of this session
293-
bool isMiningPhase = !quorumHash.IsNull() && IsMiningPhase(llmqType, pindexPrev->nHeight + 1);
293+
bool isMiningPhase = !quorumHash.IsNull() && IsMiningPhase(llmqType, nHeight);
294294

295295
// did we already mine a non-null commitment for this session?
296296
bool hasMinedCommitment = !quorumHash.IsNull() && HasMinedCommitment(llmqType, quorumHash);
@@ -299,18 +299,18 @@ bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, c
299299
}
300300

301301
// WARNING: This method returns uint256() on the first block of the DKG interval (because the block hash is not known yet)
302-
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev)
302+
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight)
303303
{
304+
AssertLockHeld(cs_main);
305+
304306
auto& params = Params().GetConsensus().llmqs.at(llmqType);
305307

306-
int nHeight = pindexPrev->nHeight + 1;
307308
int quorumStartHeight = nHeight - (nHeight % params.dkgInterval);
308-
if (quorumStartHeight >= pindexPrev->nHeight) {
309+
uint256 quorumBlockHash;
310+
if (!GetBlockHash(quorumBlockHash, quorumStartHeight)) {
309311
return uint256();
310312
}
311-
auto quorumIndex = pindexPrev->GetAncestor(quorumStartHeight);
312-
assert(quorumIndex);
313-
return quorumIndex->GetBlockHash();
313+
return quorumBlockHash;
314314
}
315315

316316
bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash)
@@ -376,16 +376,16 @@ bool CQuorumBlockProcessor::GetMinableCommitmentByHash(const uint256& commitment
376376

377377
// Will return false if no commitment should be mined
378378
// Will return true and a null commitment if no minable commitment is known and none was mined yet
379-
bool CQuorumBlockProcessor::GetMinableCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev, CFinalCommitment& ret)
379+
bool CQuorumBlockProcessor::GetMinableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret)
380380
{
381381
AssertLockHeld(cs_main);
382382

383-
if (!IsCommitmentRequired(llmqType, pindexPrev)) {
383+
if (!IsCommitmentRequired(llmqType, nHeight)) {
384384
// no commitment required
385385
return false;
386386
}
387387

388-
uint256 quorumHash = GetQuorumBlockHash(llmqType, pindexPrev);
388+
uint256 quorumHash = GetQuorumBlockHash(llmqType, nHeight);
389389
if (quorumHash.IsNull()) {
390390
return false;
391391
}
@@ -405,16 +405,16 @@ bool CQuorumBlockProcessor::GetMinableCommitment(Consensus::LLMQType llmqType, c
405405
return true;
406406
}
407407

408-
bool CQuorumBlockProcessor::GetMinableCommitmentTx(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev, CTransactionRef& ret)
408+
bool CQuorumBlockProcessor::GetMinableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret)
409409
{
410410
AssertLockHeld(cs_main);
411411

412412
CFinalCommitmentTxPayload qc;
413-
if (!GetMinableCommitment(llmqType, pindexPrev, qc.commitment)) {
413+
if (!GetMinableCommitment(llmqType, nHeight, qc.commitment)) {
414414
return false;
415415
}
416416

417-
qc.nHeight = pindexPrev->nHeight + 1;
417+
qc.nHeight = nHeight;
418418

419419
CMutableTransaction tx;
420420
tx.nVersion = 3;

src/llmq/quorums_blockprocessor.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ class CQuorumBlockProcessor
4040
void AddMinableCommitment(const CFinalCommitment& fqc);
4141
bool HasMinableCommitment(const uint256& hash);
4242
bool GetMinableCommitmentByHash(const uint256& commitmentHash, CFinalCommitment& ret);
43-
bool GetMinableCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev, CFinalCommitment& ret);
44-
bool GetMinableCommitmentTx(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev, CTransactionRef& ret);
43+
bool GetMinableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret);
44+
bool GetMinableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret);
4545

4646
bool HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash);
4747
bool GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, CFinalCommitment& ret);
4848

4949
private:
5050
bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, std::map<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state);
51-
bool ProcessCommitment(const CBlockIndex* pindexPrev, const CFinalCommitment& qc, CValidationState& state);
51+
bool ProcessCommitment(const CBlockIndex* pindex, const CFinalCommitment& qc, CValidationState& state);
5252
bool IsMiningPhase(Consensus::LLMQType llmqType, int nHeight);
53-
bool IsCommitmentRequired(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev);
54-
uint256 GetQuorumBlockHash(Consensus::LLMQType llmqType, const CBlockIndex* pindexPrev);
53+
bool IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight);
54+
uint256 GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight);
5555
};
5656

5757
extern CQuorumBlockProcessor* quorumBlockProcessor;

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
155155
if (fDIP0003Active_context) {
156156
for (auto& p : Params().GetConsensus().llmqs) {
157157
CTransactionRef qcTx;
158-
if (llmq::quorumBlockProcessor->GetMinableCommitmentTx(p.first, pindexPrev, qcTx)) {
158+
if (llmq::quorumBlockProcessor->GetMinableCommitmentTx(p.first, nHeight, qcTx)) {
159159
pblock->vtx.emplace_back(qcTx);
160160
pblocktemplate->vTxFees.emplace_back(0);
161161
pblocktemplate->vTxSigOps.emplace_back(0);

0 commit comments

Comments
 (0)