Skip to content

Commit 3ba6daa

Browse files
committed
refactor: use UtilParameters in more LLMQ code
1 parent ff71875 commit 3ba6daa

File tree

7 files changed

+45
-56
lines changed

7 files changed

+45
-56
lines changed

src/evo/specialtxman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, llmq::CQuorumSn
130130
}
131131
}
132132
case TRANSACTION_QUORUM_COMMITMENT:
133-
return llmq::CheckLLMQCommitment(dmnman, qsnapman, chainman, tx, pindexPrev, state);
133+
return llmq::CheckLLMQCommitment({dmnman, qsnapman, chainman, pindexPrev}, tx, state);
134134
case TRANSACTION_MNHF_SIGNAL:
135135
return CheckMNHFTx(chainman, qman, tx, pindexPrev, state);
136136
case TRANSACTION_ASSET_LOCK:

src/llmq/blockprocessor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ MessageProcessingResult CQuorumBlockProcessor::ProcessMessage(const CNode& peer,
145145
}
146146
}
147147

148-
if (!qc.Verify(m_dmnman, m_qsnapman, m_chainstate.m_chainman, pQuorumBaseBlockIndex, /*checkSigs=*/true)) {
148+
if (!qc.Verify({m_dmnman, m_qsnapman, m_chainstate.m_chainman, pQuorumBaseBlockIndex}, /*checkSigs=*/true)) {
149149
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- commitment for quorum %s:%d is not valid quorumIndex[%d] nversion[%d], peer=%d\n",
150150
__func__, qc.quorumHash.ToString(),
151151
ToUnderlying(qc.llmqType), qc.quorumIndex, qc.nVersion, peer.GetId());
@@ -215,7 +215,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<cons
215215
pindex->nHeight, qc.quorumHash.ToString());
216216
return false;
217217
}
218-
qc.VerifySignatureAsync(m_dmnman, m_qsnapman, m_chainstate.m_chainman, pQuorumBaseBlockIndex, &queue_control);
218+
qc.VerifySignatureAsync({m_dmnman, m_qsnapman, m_chainstate.m_chainman, pQuorumBaseBlockIndex}, &queue_control);
219219
}
220220

221221
if (!queue_control.Wait()) {
@@ -337,7 +337,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
337337
}
338338

339339
// we don't validate signatures here; they already validated on previous step
340-
if (!qc.Verify(m_dmnman, m_qsnapman, m_chainstate.m_chainman, pQuorumBaseBlockIndex, /*checksigs=*/false)) {
340+
if (!qc.Verify({m_dmnman, m_qsnapman, m_chainstate.m_chainman, pQuorumBaseBlockIndex}, /*checksigs=*/false)) {
341341
LogPrint(BCLog::LLMQ, /* Continued */
342342
"%s -- height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, validMembers=%d, "
343343
"quorumPublicKey=%s qc verify failed.\n",

src/llmq/commitment.cpp

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ CFinalCommitment::CFinalCommitment(const Consensus::LLMQParams& params, const ui
2828
{
2929
}
3030

31-
bool CFinalCommitment::VerifySignatureAsync(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
32-
const ChainstateManager& chainman,
33-
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
34-
CCheckQueueControl<utils::BlsCheck>* queue_control) const
31+
bool CFinalCommitment::VerifySignatureAsync(const llmq::UtilParameters& util_params, CCheckQueueControl<utils::BlsCheck>* queue_control) const
3532
{
36-
auto members = utils::GetAllQuorumMembers(llmqType, {dmnman, qsnapman, chainman, pQuorumBaseBlockIndex});
33+
auto members = utils::GetAllQuorumMembers(llmqType, util_params);
3734
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
3835
if (!llmq_params_opt.has_value()) {
3936
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid llmqType=%d\n", quorumHash.ToString(),
@@ -96,9 +93,7 @@ bool CFinalCommitment::VerifySignatureAsync(CDeterministicMNManager& dmnman, CQu
9693
}
9794

9895

99-
bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
100-
const ChainstateManager& chainman,
101-
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool checkSigs) const
96+
bool CFinalCommitment::Verify(const llmq::UtilParameters& util_params, bool checkSigs) const
10297
{
10398
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
10499
if (!llmq_params_opt.has_value()) {
@@ -107,19 +102,19 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotMa
107102
}
108103
const auto& llmq_params = llmq_params_opt.value();
109104

110-
const uint16_t expected_nversion{CFinalCommitment::GetVersion(IsQuorumRotationEnabled(llmq_params, pQuorumBaseBlockIndex),
111-
DeploymentActiveAfter(pQuorumBaseBlockIndex, chainman.GetConsensus(), Consensus::DEPLOYMENT_V19))};
105+
const uint16_t expected_nversion{CFinalCommitment::GetVersion(IsQuorumRotationEnabled(llmq_params, util_params.m_base_index),
106+
DeploymentActiveAfter(util_params.m_base_index, util_params.m_chainman.GetConsensus(), Consensus::DEPLOYMENT_V19))};
112107
if (nVersion == 0 || nVersion != expected_nversion) {
113108
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid nVersion=%d expected=%d\n", quorumHash.ToString(), nVersion, expected_nversion);
114109
return false;
115110
}
116111

117-
if (pQuorumBaseBlockIndex->GetBlockHash() != quorumHash) {
112+
if (util_params.m_base_index->GetBlockHash() != quorumHash) {
118113
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorumHash\n", quorumHash.ToString());
119114
return false;
120115
}
121116

122-
if ((pQuorumBaseBlockIndex->nHeight % llmq_params.dkgInterval) != quorumIndex) {
117+
if ((util_params.m_base_index->nHeight % llmq_params.dkgInterval) != quorumIndex) {
123118
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorumIndex=%d\n", quorumHash.ToString(), quorumIndex);
124119
return false;
125120
}
@@ -152,7 +147,7 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotMa
152147
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid vvecSig\n", quorumHash.ToString());
153148
return false;
154149
}
155-
auto members = utils::GetAllQuorumMembers(llmqType, {dmnman, qsnapman, chainman, pQuorumBaseBlockIndex});
150+
auto members = utils::GetAllQuorumMembers(llmqType, util_params);
156151
if (LogAcceptDebug(BCLog::LLMQ)) {
157152
std::stringstream ss;
158153
std::stringstream ss2;
@@ -176,7 +171,7 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotMa
176171

177172
// sigs are only checked when the block is processed
178173
if (checkSigs) {
179-
if (!VerifySignatureAsync(dmnman, qsnapman, chainman, pQuorumBaseBlockIndex, nullptr)) {
174+
if (!VerifySignatureAsync(util_params, /*queue_control=*/nullptr)) {
180175
return false;
181176
}
182177
}
@@ -215,20 +210,18 @@ bool CFinalCommitment::VerifySizes(const Consensus::LLMQParams& params) const
215210
return true;
216211
}
217212

218-
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
219-
const ChainstateManager& chainman, const CTransaction& tx,
220-
gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state)
213+
bool CheckLLMQCommitment(const llmq::UtilParameters& util_params, const CTransaction& tx, TxValidationState& state)
221214
{
222215
const auto opt_qcTx = GetTxPayload<CFinalCommitmentTxPayload>(tx);
223216
if (!opt_qcTx) {
224-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] GetTxPayload LLMQCommitment failed\n", pindexPrev->nHeight);
217+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] GetTxPayload LLMQCommitment failed\n", util_params.m_base_index->nHeight);
225218
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-payload");
226219
}
227220
auto& qcTx = *opt_qcTx;
228221

229222
const auto& llmq_params_opt = Params().GetLLMQ(qcTx.commitment.llmqType);
230223
if (!llmq_params_opt.has_value()) {
231-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] GetLLMQ failed for llmqType[%d]\n", pindexPrev->nHeight, ToUnderlying(qcTx.commitment.llmqType));
224+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] GetLLMQ failed for llmqType[%d]\n", util_params.m_base_index->nHeight, ToUnderlying(qcTx.commitment.llmqType));
232225
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-commitment-type");
233226
}
234227

@@ -242,40 +235,39 @@ bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, CQuorumSnapshotManager
242235
}
243236

244237
if (qcTx.nVersion == 0 || qcTx.nVersion > CFinalCommitmentTxPayload::CURRENT_VERSION) {
245-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.nVersion[%d]\n", pindexPrev->nHeight, qcTx.nVersion);
238+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.nVersion[%d]\n", util_params.m_base_index->nHeight, qcTx.nVersion);
246239
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-version");
247240
}
248241

249-
if (qcTx.nHeight != uint32_t(pindexPrev->nHeight + 1)) {
250-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.nHeight[%d]\n", pindexPrev->nHeight, qcTx.nHeight);
242+
if (qcTx.nHeight != uint32_t(util_params.m_base_index->nHeight + 1)) {
243+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.nHeight[%d]\n", util_params.m_base_index->nHeight, qcTx.nHeight);
251244
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-height");
252245
}
253246

254-
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(qcTx.commitment.quorumHash));
247+
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(::cs_main, return util_params.m_chainman.m_blockman.LookupBlockIndex(qcTx.commitment.quorumHash));
255248
if (pQuorumBaseBlockIndex == nullptr) {
256249
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-quorum-hash");
257250
}
258251

259-
260-
if (pQuorumBaseBlockIndex != pindexPrev->GetAncestor(pQuorumBaseBlockIndex->nHeight)) {
252+
if (pQuorumBaseBlockIndex != util_params.m_base_index->GetAncestor(pQuorumBaseBlockIndex->nHeight)) {
261253
// not part of active chain
262254
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-quorum-hash");
263255
}
264256

265257
if (qcTx.commitment.IsNull()) {
266258
if (!qcTx.commitment.VerifyNull()) {
267-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.commitment[%s] VerifyNull failed\n", pindexPrev->nHeight, qcTx.commitment.quorumHash.ToString());
259+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.commitment[%s] VerifyNull failed\n", util_params.m_base_index->nHeight, qcTx.commitment.quorumHash.ToString());
268260
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-invalid-null");
269261
}
270262
return true;
271263
}
272264

273-
if (!qcTx.commitment.Verify(dmnman, qsnapman, chainman, pQuorumBaseBlockIndex, false)) {
274-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.commitment[%s] Verify failed\n", pindexPrev->nHeight, qcTx.commitment.quorumHash.ToString());
265+
if (!qcTx.commitment.Verify(util_params.replace_index(pQuorumBaseBlockIndex), false)) {
266+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.commitment[%s] Verify failed\n", util_params.m_base_index->nHeight, qcTx.commitment.quorumHash.ToString());
275267
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-invalid");
276268
}
277269

278-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] CheckLLMQCommitment VALID\n", pindexPrev->nHeight);
270+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] CheckLLMQCommitment VALID\n", util_params.m_base_index->nHeight);
279271

280272
return true;
281273
}

src/llmq/commitment.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class TxValidationState;
3030
template <typename T>
3131
class CCheckQueueControl;
3232
struct RPCResult;
33-
3433
namespace llmq {
3534
class CQuorumSnapshotManager;
35+
struct UtilParameters;
3636
namespace utils {
3737
struct BlsCheck;
3838
} // namespace utils
39+
} // namespace llmq
3940

41+
namespace llmq {
4042
// This message is an aggregation of all received premature commitments and only valid if
4143
// enough (>=threshold) premature commitments were aggregated
4244
// This is mined on-chain as part of TRANSACTION_QUORUM_COMMITMENT
@@ -74,11 +76,8 @@ class CFinalCommitment
7476
return int(std::count(validMembers.begin(), validMembers.end(), true));
7577
}
7678

77-
bool VerifySignatureAsync(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
78-
const ChainstateManager& chainman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
79-
CCheckQueueControl<utils::BlsCheck>* queue_control) const;
80-
bool Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman, const ChainstateManager& chainman,
81-
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool checkSigs) const;
79+
bool VerifySignatureAsync(const llmq::UtilParameters& util_params, CCheckQueueControl<utils::BlsCheck>* queue_control) const;
80+
bool Verify(const llmq::UtilParameters& util_params, bool checkSigs) const;
8281
bool VerifyNull() const;
8382
bool VerifySizes(const Consensus::LLMQParams& params) const;
8483

@@ -164,9 +163,7 @@ class CFinalCommitmentTxPayload
164163
[[nodiscard]] UniValue ToJson() const;
165164
};
166165

167-
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
168-
const ChainstateManager& chainman, const CTransaction& tx,
169-
gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state);
166+
bool CheckLLMQCommitment(const llmq::UtilParameters& util_params, const CTransaction& tx, TxValidationState& state);
170167

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

src/llmq/dkgsession.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ std::vector<CFinalCommitment> CDKGSession::FinalizeCommitments()
12801280
t2.stop();
12811281

12821282
cxxtimer::Timer t3(true);
1283-
if (!fqc.Verify(m_dmnman, m_qsnapman, m_chainman, m_quorum_base_block_index, true)) {
1283+
if (!fqc.Verify({m_dmnman, m_qsnapman, m_chainman, m_quorum_base_block_index}, true)) {
12841284
logger.Batch("failed to verify final commitment");
12851285
continue;
12861286
}
@@ -1343,7 +1343,7 @@ CFinalCommitment CDKGSession::FinalizeSingleCommitment()
13431343
fqc.quorumSig = fqc.membersSig;
13441344
}
13451345

1346-
if (!fqc.Verify(m_dmnman, m_qsnapman, m_chainman, m_quorum_base_block_index, true)) {
1346+
if (!fqc.Verify({m_dmnman, m_qsnapman, m_chainman, m_quorum_base_block_index}, true)) {
13471347
logger.Batch("failed to verify final commitment");
13481348
assert(false);
13491349
}

src/llmq/utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void BuildQuorumSnapshot(const Consensus::LLMQParams& llmqParams, const Consensu
329329
}
330330
}
331331

332-
std::vector<QuorumMembers> BuildNewQuorumQuarterMembers(const Consensus::LLMQParams& llmqParams, const llmq::utils::UtilParameters& util_params,
332+
std::vector<QuorumMembers> BuildNewQuorumQuarterMembers(const Consensus::LLMQParams& llmqParams, const llmq::UtilParameters& util_params,
333333
const CDeterministicMNList& allMns, const PreviousQuorumQuarters& previousQuarters)
334334
{
335335
if (!llmqParams.useRotation || util_params.m_base_index->nHeight % llmqParams.dkgInterval != 0) {
@@ -442,7 +442,7 @@ std::vector<QuorumMembers> BuildNewQuorumQuarterMembers(const Consensus::LLMQPar
442442
return quarterQuorumMembers;
443443
}
444444

445-
std::vector<QuorumMembers> ComputeQuorumMembersByQuarterRotation(const Consensus::LLMQParams& llmqParams, const llmq::utils::UtilParameters& util_params)
445+
std::vector<QuorumMembers> ComputeQuorumMembersByQuarterRotation(const Consensus::LLMQParams& llmqParams, const llmq::UtilParameters& util_params)
446446
{
447447
const int cycleLength = llmqParams.dkgInterval;
448448
if (!llmqParams.useRotation || util_params.m_base_index->nHeight % llmqParams.dkgInterval != 0) {

src/llmq/utils.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class CQuorumSnapshotManager;
3131
} // namespace llmq
3232

3333
namespace llmq {
34+
struct UtilParameters {
35+
CDeterministicMNManager& m_dmnman;
36+
CQuorumSnapshotManager& m_qsnapman;
37+
const ChainstateManager& m_chainman;
38+
gsl::not_null<const CBlockIndex*> m_base_index;
39+
40+
public:
41+
UtilParameters replace_index(gsl::not_null<const CBlockIndex*> base_index) const { return {m_dmnman, m_qsnapman, m_chainman, base_index}; }
42+
};
43+
3444
namespace utils {
3545
struct BlsCheck {
3646
CBLSSignature m_sig;
@@ -53,16 +63,6 @@ std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType,
5363
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
5464
size_t memberCount, size_t connectionCount);
5565

56-
struct UtilParameters {
57-
CDeterministicMNManager& m_dmnman;
58-
CQuorumSnapshotManager& m_qsnapman;
59-
const ChainstateManager& m_chainman;
60-
gsl::not_null<const CBlockIndex*> m_base_index;
61-
62-
public:
63-
UtilParameters replace_index(gsl::not_null<const CBlockIndex*> base_index) const { return {m_dmnman, m_qsnapman, m_chainman, base_index}; }
64-
};
65-
6666
// includes members which failed DKG
6767
std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqType, const UtilParameters& util_params,
6868
bool reset_cache = false);

0 commit comments

Comments
 (0)