Skip to content

Commit 76e4e72

Browse files
committed
refactor: split quorum commitment verification and its signatures
1 parent 8fc24bd commit 76e4e72

File tree

3 files changed

+68
-33
lines changed

3 files changed

+68
-33
lines changed

src/llmq/blockprocessor.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,21 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<cons
196196
}
197197
}
198198

199+
if (fBLSChecks) {
200+
for (const auto& [_, qc] : qcs) {
201+
if (qc.IsNull()) continue;
202+
const auto* pQuorumBaseBlockIndex = m_chainstate.m_blockman.LookupBlockIndex(qc.quorumHash);
203+
if (!qc.VerifySignature(m_dmnman, m_qsnapman, pQuorumBaseBlockIndex)) {
204+
LogPrintf("[ProcessBlock] failed h[%d] llmqType[%d] version[%d] quorumIndex[%d] quorumHash[%s]\n",
205+
pindex->nHeight, ToUnderlying(qc.llmqType), qc.nVersion, qc.quorumIndex,
206+
qc.quorumHash.ToString());
207+
return false;
208+
}
209+
}
210+
}
211+
199212
for (const auto& [_, qc] : qcs) {
200-
if (!ProcessCommitment(pindex->nHeight, blockHash, qc, state, fJustCheck, fBLSChecks)) {
213+
if (!ProcessCommitment(pindex->nHeight, blockHash, qc, state, fJustCheck, false)) {
201214
LogPrintf("[ProcessBlock] failed h[%d] llmqType[%d] version[%d] quorumIndex[%d] quorumHash[%s]\n", pindex->nHeight, ToUnderlying(qc.llmqType), qc.nVersion, qc.quorumIndex, qc.quorumHash.ToString());
202215
return false;
203216
}

src/llmq/commitment.cpp

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,57 @@ CFinalCommitment::CFinalCommitment(const Consensus::LLMQParams& params, const ui
2727
{
2828
}
2929

30+
bool CFinalCommitment::VerifySignature(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
31+
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex) const
32+
{
33+
auto members = utils::GetAllQuorumMembers(llmqType, dmnman, qsnapman, pQuorumBaseBlockIndex);
34+
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
35+
if (!llmq_params_opt.has_value()) {
36+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid llmqType=%d\n", quorumHash.ToString(),
37+
ToUnderlying(llmqType));
38+
return false;
39+
}
40+
const auto& llmq_params = llmq_params_opt.value();
41+
42+
uint256 commitmentHash = BuildCommitmentHash(llmq_params.type, quorumHash, validMembers, quorumPublicKey,
43+
quorumVvecHash);
44+
if (LogAcceptDebug(BCLog::LLMQ)) {
45+
std::stringstream ss3;
46+
for (const auto& mn : members) {
47+
ss3 << mn->proTxHash.ToString().substr(0, 4) << " | ";
48+
}
49+
LogPrint(BCLog::LLMQ, "CFinalCommitment::%s members[%s] quorumPublicKey[%s] commitmentHash[%s]\n", __func__,
50+
ss3.str(), quorumPublicKey.ToString(), commitmentHash.ToString());
51+
}
52+
if (llmq_params.size == 1) {
53+
LogPrintf("pubkey operator: %s\n", members[0]->pdmnState->pubKeyOperator.Get().ToString());
54+
if (!membersSig.VerifyInsecure(members[0]->pdmnState->pubKeyOperator.Get(), commitmentHash)) {
55+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid member signature\n", quorumHash.ToString());
56+
return false;
57+
}
58+
} else {
59+
std::vector<CBLSPublicKey> memberPubKeys;
60+
for (const auto i : irange::range(members.size())) {
61+
if (!signers[i]) {
62+
continue;
63+
}
64+
memberPubKeys.emplace_back(members[i]->pdmnState->pubKeyOperator.Get());
65+
}
66+
67+
if (!membersSig.VerifySecureAggregated(memberPubKeys, commitmentHash)) {
68+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid aggregated members signature\n",
69+
quorumHash.ToString());
70+
return false;
71+
}
72+
}
73+
if (!quorumSig.VerifyInsecure(quorumPublicKey, commitmentHash)) {
74+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorum signature\n", quorumHash.ToString());
75+
return false;
76+
}
77+
return true;
78+
}
79+
80+
3081
bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
3182
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool checkSigs) const
3283
{
@@ -106,38 +157,7 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotMa
106157

107158
// sigs are only checked when the block is processed
108159
if (checkSigs) {
109-
uint256 commitmentHash = BuildCommitmentHash(llmq_params.type, quorumHash, validMembers, quorumPublicKey, quorumVvecHash);
110-
if (LogAcceptDebug(BCLog::LLMQ)) {
111-
std::stringstream ss3;
112-
for (const auto &mn: members) {
113-
ss3 << mn->proTxHash.ToString().substr(0, 4) << " | ";
114-
}
115-
LogPrint(BCLog::LLMQ, "CFinalCommitment::%s members[%s] quorumPublicKey[%s] commitmentHash[%s]\n",
116-
__func__, ss3.str(), quorumPublicKey.ToString(), commitmentHash.ToString());
117-
}
118-
if (llmq_params.size == 1) {
119-
LogPrintf("pubkey operator: %s\n", members[0]->pdmnState->pubKeyOperator.Get().ToString());
120-
if (!membersSig.VerifyInsecure(members[0]->pdmnState->pubKeyOperator.Get(), commitmentHash)) {
121-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid member signature\n", quorumHash.ToString());
122-
return false;
123-
}
124-
} else {
125-
std::vector<CBLSPublicKey> memberPubKeys;
126-
for (const auto i : irange::range(members.size())) {
127-
if (!signers[i]) {
128-
continue;
129-
}
130-
memberPubKeys.emplace_back(members[i]->pdmnState->pubKeyOperator.Get());
131-
}
132-
133-
if (!membersSig.VerifySecureAggregated(memberPubKeys, commitmentHash)) {
134-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid aggregated members signature\n",
135-
quorumHash.ToString());
136-
return false;
137-
}
138-
}
139-
if (!quorumSig.VerifyInsecure(quorumPublicKey, commitmentHash)) {
140-
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorum signature\n", quorumHash.ToString());
160+
if (!VerifySignature(dmnman, qsnapman, pQuorumBaseBlockIndex)) {
141161
return false;
142162
}
143163
}

src/llmq/commitment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class CFinalCommitment
6767
return int(std::count(validMembers.begin(), validMembers.end(), true));
6868
}
6969

70+
bool VerifySignature(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
71+
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex) const;
7072
bool Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
7173
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool checkSigs) const;
7274
bool VerifyNull() const;

0 commit comments

Comments
 (0)