Skip to content

Commit 303c6bb

Browse files
committed
refactor: drop usage of chainstate globals in llmq logic
1 parent fa20718 commit 303c6bb

17 files changed

+57
-45
lines changed

src/evo/specialtxman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const Chainstat
4646
case TRANSACTION_COINBASE:
4747
return CheckCbTx(tx, pindexPrev, state);
4848
case TRANSACTION_QUORUM_COMMITMENT:
49-
return llmq::CheckLLMQCommitment(dmnman, tx, pindexPrev, state);
49+
return llmq::CheckLLMQCommitment(dmnman, chainman, tx, pindexPrev, state);
5050
case TRANSACTION_MNHF_SIGNAL:
5151
if (!DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_V20)) {
5252
return state.Invalid(TxValidationResult::TX_CONSENSUS, "mnhf-before-v20");

src/llmq/blockprocessor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
257257
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-dup");
258258
}
259259

260-
if (!IsMiningPhase(llmq_params, nHeight)) {
260+
if (!IsMiningPhase(llmq_params, m_chainstate.m_chain, nHeight)) {
261261
// should not happen as it's already handled in ProcessBlock
262262
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-height");
263263
}
@@ -385,12 +385,12 @@ bool CQuorumBlockProcessor::GetCommitmentsFromBlock(const CBlock& block, gsl::no
385385
return true;
386386
}
387387

388-
bool CQuorumBlockProcessor::IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight)
388+
bool CQuorumBlockProcessor::IsMiningPhase(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight)
389389
{
390390
AssertLockHeld(cs_main);
391391

392392
// Note: This function can be called for new blocks
393-
assert(nHeight <= ::ChainActive().Height() + 1);
393+
assert(nHeight <= active_chain.Height() + 1);
394394

395395
int quorumCycleStartHeight = nHeight - (nHeight % llmqParams.dkgInterval);
396396
int quorumCycleMiningStartHeight = quorumCycleStartHeight + llmqParams.dkgMiningWindowStart;
@@ -409,7 +409,7 @@ size_t CQuorumBlockProcessor::GetNumCommitmentsRequired(const Consensus::LLMQPar
409409
{
410410
AssertLockHeld(cs_main);
411411

412-
if (!IsMiningPhase(llmqParams, nHeight)) return 0;
412+
if (!IsMiningPhase(llmqParams, m_chainstate.m_chain, nHeight)) return 0;
413413

414414
// Note: This function can be called for new blocks
415415
assert(nHeight <= m_chainstate.m_chain.Height() + 1);

src/llmq/blockprocessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CQuorumBlockProcessor
7676
private:
7777
static bool GetCommitmentsFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, std::multimap<Consensus::LLMQType, CFinalCommitment>& ret, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
7878
bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, BlockValidationState& state, bool fJustCheck, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
79-
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
79+
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
8080
size_t GetNumCommitmentsRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
8181
static uint256 GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
8282
};

src/llmq/chainlocks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ bool CChainLocksHandler::VerifyChainLock(const CChainLockSig& clsig) const
555555
{
556556
const auto llmqType = Params().GetConsensus().llmqTypeChainLocks;
557557
const uint256 nRequestId = ::SerializeHash(std::make_pair(llmq::CLSIG_REQUESTID_PREFIX, clsig.getHeight()));
558-
return llmq::VerifyRecoveredSig(llmqType, qman, clsig.getHeight(), nRequestId, clsig.getBlockHash(), clsig.getSig());
558+
return llmq::VerifyRecoveredSig(llmqType, m_chainstate.m_chain, qman, clsig.getHeight(), nRequestId, clsig.getBlockHash(), clsig.getSig());
559559
}
560560

561561
bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockHash) const

src/llmq/commitment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ bool CFinalCommitment::VerifySizes(const Consensus::LLMQParams& params) const
166166
return true;
167167
}
168168

169-
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state)
169+
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state)
170170
{
171171
const auto opt_qcTx = GetTxPayload<CFinalCommitmentTxPayload>(tx);
172172
if (!opt_qcTx) {
@@ -200,7 +200,7 @@ bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx
200200
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-height");
201201
}
202202

203-
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return g_chainman.m_blockman.LookupBlockIndex(qcTx.commitment.quorumHash));
203+
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(qcTx.commitment.quorumHash));
204204
if (pQuorumBaseBlockIndex == nullptr) {
205205
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-quorum-hash");
206206
}

src/llmq/commitment.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
class CBlockIndex;
2020
class CDeterministicMNManager;
21+
class ChainstateManager;
2122
class TxValidationState;
2223

2324
namespace llmq
@@ -172,7 +173,7 @@ class CFinalCommitmentTxPayload
172173
}
173174
};
174175

175-
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state);
176+
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state);
176177

177178
uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256& blockHash, const std::vector<bool>& validMembers, const CBLSPublicKey& pubKey, const uint256& vvecHash);
178179

src/llmq/context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LLMQContext::LLMQContext(CChainState& chainstate, CConnman& connman, CDeterminis
2727
quorum_block_processor{std::make_unique<llmq::CQuorumBlockProcessor>(chainstate, dmnman, evo_db, peerman)},
2828
qdkgsman{std::make_unique<llmq::CDKGSessionManager>(*bls_worker, chainstate, connman, dmnman, *dkg_debugman, mn_metaman, *quorum_block_processor, mn_activeman, sporkman, peerman, unit_tests, wipe)},
2929
qman{std::make_unique<llmq::CQuorumManager>(*bls_worker, chainstate, connman, dmnman, *qdkgsman, evo_db, *quorum_block_processor, mn_activeman, mn_sync, sporkman)},
30-
sigman{std::make_unique<llmq::CSigningManager>(connman, mn_activeman, *qman, peerman, unit_tests, wipe)},
30+
sigman{std::make_unique<llmq::CSigningManager>(connman, mn_activeman, chainstate, *qman, peerman, unit_tests, wipe)},
3131
shareman{std::make_unique<llmq::CSigSharesManager>(connman, *sigman, mn_activeman, *qman, sporkman, peerman)},
3232
clhandler{[&]() -> llmq::CChainLocksHandler* const {
3333
assert(llmq::chainLocksHandler == nullptr);

src/llmq/ehf_signals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void CEHFSignalsHandler::trySignEHFSignal(int bit, const CBlockIndex* const pind
8686
return;
8787
}
8888

89-
const auto quorum = llmq::SelectQuorumForSigning(llmq_params_opt.value(), qman, requestId);
89+
const auto quorum = llmq::SelectQuorumForSigning(llmq_params_opt.value(), chainstate.m_chain, qman, requestId);
9090
if (!quorum) {
9191
LogPrintf("CEHFSignalsHandler::trySignEHFSignal no quorum for id=%s\n", requestId.ToString());
9292
return;

src/llmq/instantsend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPend
920920
nSignHeight = blockIndex->nHeight + dkgInterval - 1;
921921
}
922922

923-
auto quorum = llmq::SelectQuorumForSigning(llmq_params, qman, id, nSignHeight, signOffset);
923+
auto quorum = llmq::SelectQuorumForSigning(llmq_params, m_chainstate.m_chain, qman, id, nSignHeight, signOffset);
924924
if (!quorum) {
925925
// should not happen, but if one fails to select, all others will also fail to select
926926
return {};

src/llmq/quorums.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,25 +1107,26 @@ void CQuorumManager::StartCleanupOldQuorumDataThread(const CBlockIndex* pIndex)
11071107
});
11081108
}
11091109

1110-
CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, const CQuorumManager& quorum_manager, const uint256& selectionHash, int signHeight, int signOffset)
1110+
CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, const CChain& active_chain, const CQuorumManager& qman,
1111+
const uint256& selectionHash, int signHeight, int signOffset)
11111112
{
11121113
size_t poolSize = llmq_params.signingActiveQuorumCount;
11131114

11141115
CBlockIndex* pindexStart;
11151116
{
11161117
LOCK(cs_main);
11171118
if (signHeight == -1) {
1118-
signHeight = ::ChainActive().Height();
1119+
signHeight = active_chain.Height();
11191120
}
11201121
int startBlockHeight = signHeight - signOffset;
1121-
if (startBlockHeight > ::ChainActive().Height() || startBlockHeight < 0) {
1122+
if (startBlockHeight > active_chain.Height() || startBlockHeight < 0) {
11221123
return {};
11231124
}
1124-
pindexStart = ::ChainActive()[startBlockHeight];
1125+
pindexStart = active_chain[startBlockHeight];
11251126
}
11261127

11271128
if (IsQuorumRotationEnabled(llmq_params, pindexStart)) {
1128-
auto quorums = quorum_manager.ScanQuorums(llmq_params.type, pindexStart, poolSize);
1129+
auto quorums = qman.ScanQuorums(llmq_params.type, pindexStart, poolSize);
11291130
if (quorums.empty()) {
11301131
return nullptr;
11311132
}
@@ -1149,7 +1150,7 @@ CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, con
11491150
}
11501151
return *itQuorum;
11511152
} else {
1152-
auto quorums = quorum_manager.ScanQuorums(llmq_params.type, pindexStart, poolSize);
1153+
auto quorums = qman.ScanQuorums(llmq_params.type, pindexStart, poolSize);
11531154
if (quorums.empty()) {
11541155
return nullptr;
11551156
}
@@ -1168,11 +1169,13 @@ CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, con
11681169
}
11691170
}
11701171

1171-
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const CQuorumManager& quorum_manager, int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig, const int signOffset)
1172+
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const CChain& active_chain, const CQuorumManager& qman,
1173+
int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig,
1174+
const int signOffset)
11721175
{
11731176
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
11741177
assert(llmq_params_opt.has_value());
1175-
auto quorum = SelectQuorumForSigning(llmq_params_opt.value(), quorum_manager, id, signedAtHeight, signOffset);
1178+
auto quorum = SelectQuorumForSigning(llmq_params_opt.value(), active_chain, qman, id, signedAtHeight, signOffset);
11761179
if (!quorum) {
11771180
return false;
11781181
}

0 commit comments

Comments
 (0)