Skip to content

Commit ec3a9d4

Browse files
committed
refactor: break circular dependencies llmq/blockprocessor <-> llmq/commitment
1 parent adb1a1d commit ec3a9d4

File tree

6 files changed

+58
-53
lines changed

6 files changed

+58
-53
lines changed

src/llmq/blockprocessor.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,6 @@ static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT_Q_INDEXED = "q_m
4545

4646
static const std::string DB_BEST_BLOCK_UPGRADE = "q_bbu2";
4747

48-
bool BlsCheck::operator()()
49-
{
50-
if (m_pubkeys.size() > 1) {
51-
if (!m_sig.VerifySecureAggregated(m_pubkeys, m_msg_hash)) {
52-
LogPrint(BCLog::LLMQ, "%s\n", m_id_string);
53-
return false;
54-
}
55-
} else if (m_pubkeys.size() == 1) {
56-
if (!m_sig.VerifyInsecure(m_pubkeys.back(), m_msg_hash)) {
57-
LogPrint(BCLog::LLMQ, "%s\n", m_id_string);
58-
return false;
59-
}
60-
} else {
61-
// It is supposed to be at least one public key!
62-
return false;
63-
}
64-
return true;
65-
}
66-
6748
CQuorumBlockProcessor::CQuorumBlockProcessor(CChainState& chainstate, CDeterministicMNManager& dmnman, CEvoDB& evoDb,
6849
CQuorumSnapshotManager& qsnapman) :
6950
m_chainstate(chainstate),
@@ -225,7 +206,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<cons
225206
}
226207

227208
if (fBLSChecks) {
228-
CCheckQueueControl<BlsCheck> queue_control(&m_bls_queue);
209+
CCheckQueueControl<utils::BlsCheck> queue_control(&m_bls_queue);
229210
for (const auto& [_, qc] : qcs) {
230211
if (qc.IsNull()) continue;
231212
const auto* pQuorumBaseBlockIndex = m_chainstate.m_blockman.LookupBlockIndex(qc.quorumHash);

src/llmq/blockprocessor.h

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <bls/bls.h>
1111
#include <checkqueue.h>
1212
#include <llmq/params.h>
13+
#include <llmq/utils.h>
1314
#include <protocol.h>
1415
#include <saltedhasher.h>
1516
#include <sync.h>
@@ -38,33 +39,6 @@ class CQuorumSnapshotManager;
3839

3940
using CFinalCommitmentPtr = std::unique_ptr<CFinalCommitment>;
4041

41-
struct BlsCheck {
42-
CBLSSignature m_sig;
43-
std::vector<CBLSPublicKey> m_pubkeys;
44-
uint256 m_msg_hash;
45-
std::string m_id_string;
46-
47-
BlsCheck() = default;
48-
49-
BlsCheck(CBLSSignature sig, std::vector<CBLSPublicKey> pubkeys, uint256 msg_hash, std::string id_string) :
50-
m_sig(sig),
51-
m_pubkeys(pubkeys),
52-
m_msg_hash(msg_hash),
53-
m_id_string(id_string)
54-
{
55-
}
56-
57-
void swap(BlsCheck& obj)
58-
{
59-
std::swap(m_sig, obj.m_sig);
60-
std::swap(m_pubkeys, obj.m_pubkeys);
61-
std::swap(m_msg_hash, obj.m_msg_hash);
62-
std::swap(m_id_string, obj.m_id_string);
63-
}
64-
65-
bool operator()();
66-
};
67-
6842
class CQuorumBlockProcessor
6943
{
7044
private:
@@ -73,7 +47,7 @@ class CQuorumBlockProcessor
7347
CEvoDB& m_evoDb;
7448
CQuorumSnapshotManager& m_qsnapman;
7549

76-
CCheckQueue<BlsCheck> m_bls_queue{32};
50+
CCheckQueue<utils::BlsCheck> m_bls_queue{32};
7751

7852
mutable Mutex minableCommitmentsCs;
7953
std::map<std::pair<Consensus::LLMQType, uint256>, uint256> minableCommitmentsByQuorum GUARDED_BY(minableCommitmentsCs);

src/llmq/commitment.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <checkqueue.h>
1212
#include <consensus/validation.h>
1313
#include <deploymentstatus.h>
14-
#include <llmq/blockprocessor.h>
1514
#include <llmq/options.h>
1615
#include <llmq/utils.h>
1716
#include <logging.h>
@@ -31,7 +30,7 @@ CFinalCommitment::CFinalCommitment(const Consensus::LLMQParams& params, const ui
3130

3231
bool CFinalCommitment::VerifySignatureAsync(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
3332
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
34-
CCheckQueueControl<BlsCheck>* queue_control) const
33+
CCheckQueueControl<utils::BlsCheck>* queue_control) const
3534
{
3635
auto members = utils::GetAllQuorumMembers(llmqType, dmnman, qsnapman, pQuorumBaseBlockIndex);
3736
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
@@ -69,7 +68,7 @@ bool CFinalCommitment::VerifySignatureAsync(CDeterministicMNManager& dmnman, CQu
6968
std::string members_id_string{
7069
strprintf("CFinalCommitment -- q[%s] invalid aggregated members signature", quorumHash.ToString())};
7170
if (queue_control) {
72-
std::vector<BlsCheck> vChecks;
71+
std::vector<utils::BlsCheck> vChecks;
7372
vChecks.emplace_back(membersSig, memberPubKeys, commitmentHash, members_id_string);
7473
queue_control->Add(vChecks);
7574
} else {
@@ -81,7 +80,7 @@ bool CFinalCommitment::VerifySignatureAsync(CDeterministicMNManager& dmnman, CQu
8180
}
8281
std::string qsig_id_string{strprintf("CFinalCommitment -- q[%s] invalid quorum signature", quorumHash.ToString())};
8382
if (queue_control) {
84-
std::vector<BlsCheck> vChecks;
83+
std::vector<utils::BlsCheck> vChecks;
8584
std::vector<CBLSPublicKey> public_keys;
8685
public_keys.push_back(quorumPublicKey);
8786
vChecks.emplace_back(quorumSig, public_keys, commitmentHash, qsig_id_string);

src/llmq/commitment.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ namespace llmq
3131
{
3232
class CQuorumSnapshotManager;
3333

34+
namespace utils
35+
{
3436
struct BlsCheck;
37+
} // namespace utils
3538
// This message is an aggregation of all received premature commitments and only valid if
3639
// enough (>=threshold) premature commitments were aggregated
3740
// This is mined on-chain as part of TRANSACTION_QUORUM_COMMITMENT
@@ -71,7 +74,7 @@ class CFinalCommitment
7174

7275
bool VerifySignatureAsync(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
7376
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
74-
CCheckQueueControl<BlsCheck>* queue_control) const;
77+
CCheckQueueControl<utils::BlsCheck>* queue_control) const;
7578
bool Verify(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
7679
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool checkSigs) const;
7780
bool VerifyNull() const;

src/llmq/utils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,25 @@ void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, CConnman
882882
}
883883
}
884884

885+
bool BlsCheck::operator()()
886+
{
887+
if (m_pubkeys.size() > 1) {
888+
if (!m_sig.VerifySecureAggregated(m_pubkeys, m_msg_hash)) {
889+
LogPrint(BCLog::LLMQ, "%s\n", m_id_string);
890+
return false;
891+
}
892+
} else if (m_pubkeys.size() == 1) {
893+
if (!m_sig.VerifyInsecure(m_pubkeys.back(), m_msg_hash)) {
894+
LogPrint(BCLog::LLMQ, "%s\n", m_id_string);
895+
return false;
896+
}
897+
} else {
898+
// It is supposed to be at least one public key!
899+
return false;
900+
}
901+
return true;
902+
}
903+
885904
template <typename CacheType>
886905
void InitQuorumsCache(CacheType& cache, bool limit_by_connections)
887906
{

src/llmq/utils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_LLMQ_UTILS_H
66
#define BITCOIN_LLMQ_UTILS_H
77

8+
#include <bls/bls.h>
89
#include <gsl/pointers.h>
910
#include <llmq/params.h>
1011
#include <saltedhasher.h>
@@ -56,8 +57,36 @@ void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, CConnman
5657
const CSporkManager& sporkman, const CDeterministicMNList& tip_mn_list,
5758
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& myProTxHash);
5859

60+
struct BlsCheck {
61+
CBLSSignature m_sig;
62+
std::vector<CBLSPublicKey> m_pubkeys;
63+
uint256 m_msg_hash;
64+
std::string m_id_string;
65+
66+
BlsCheck() = default;
67+
68+
BlsCheck(CBLSSignature sig, std::vector<CBLSPublicKey> pubkeys, uint256 msg_hash, std::string id_string) :
69+
m_sig(sig),
70+
m_pubkeys(pubkeys),
71+
m_msg_hash(msg_hash),
72+
m_id_string(id_string)
73+
{
74+
}
75+
76+
void swap(BlsCheck& obj)
77+
{
78+
std::swap(m_sig, obj.m_sig);
79+
std::swap(m_pubkeys, obj.m_pubkeys);
80+
std::swap(m_msg_hash, obj.m_msg_hash);
81+
std::swap(m_id_string, obj.m_id_string);
82+
}
83+
84+
bool operator()();
85+
};
86+
5987
template <typename CacheType>
6088
void InitQuorumsCache(CacheType& cache, bool limit_by_connections = true);
89+
6190
} // namespace utils
6291
} // namespace llmq
6392

0 commit comments

Comments
 (0)