Skip to content

Commit 91d861c

Browse files
committed
refactor: partial de-globalization of quorumManager
1 parent c8c1cb0 commit 91d861c

19 files changed

+90
-67
lines changed

src/evo/mnhftx.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <llmq/commitment.h>
99
#include <llmq/signing.h>
1010
#include <llmq/utils.h>
11-
#include <llmq/quorums.h>
1211

1312
#include <chain.h>
1413
#include <chainparams.h>
@@ -18,7 +17,7 @@
1817

1918
extern const std::string CBLSIG_REQUESTID_PREFIX = "clsig";
2019

21-
bool MNHFTx::Verify(const CBlockIndex* pQuorumIndex) const
20+
bool MNHFTx::Verify(const CBlockIndex* pQuorumIndex, const llmq::CQuorumManager& quorumManager) const
2221
{
2322
if (nVersion == 0 || nVersion > (llmq::utils::IsV19Active(pQuorumIndex) ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION)) {
2423
return false;
@@ -30,11 +29,11 @@ bool MNHFTx::Verify(const CBlockIndex* pQuorumIndex) const
3029
int signOffset{llmq_params_opt->dkgInterval};
3130

3231
const uint256 requestId = ::SerializeHash(std::make_pair(CBLSIG_REQUESTID_PREFIX, pQuorumIndex->nHeight));
33-
return llmq::CSigningManager::VerifyRecoveredSig(llmqType, *llmq::quorumManager, pQuorumIndex->nHeight, requestId, pQuorumIndex->GetBlockHash(), sig, 0) ||
34-
llmq::CSigningManager::VerifyRecoveredSig(llmqType, *llmq::quorumManager, pQuorumIndex->nHeight, requestId, pQuorumIndex->GetBlockHash(), sig, signOffset);
32+
return llmq::CSigningManager::VerifyRecoveredSig(llmqType, quorumManager, pQuorumIndex->nHeight, requestId, pQuorumIndex->GetBlockHash(), sig, 0) ||
33+
llmq::CSigningManager::VerifyRecoveredSig(llmqType, quorumManager, pQuorumIndex->nHeight, requestId, pQuorumIndex->GetBlockHash(), sig, signOffset);
3534
}
3635

37-
bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
36+
bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const llmq::CQuorumManager& quorumManager, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
3837
{
3938
MNHFTxPayload mnhfTx;
4039
if (!GetTxPayload(tx, mnhfTx)) {
@@ -59,7 +58,7 @@ bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValida
5958
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-type");
6059
}
6160

62-
if (!mnhfTx.signal.Verify(pindexQuorum)) {
61+
if (!mnhfTx.signal.Verify(pindexQuorum, quorumManager)) {
6362
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-invalid");
6463
}
6564

src/evo/mnhftx.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class CBlockIndex;
1515
class TxValidationState;
1616
extern RecursiveMutex cs_main;
1717

18+
namespace llmq {
19+
class CQuorumManager;
20+
} // namespace llmq
21+
1822
// mnhf signal special transaction
1923
class MNHFTx
2024
{
@@ -27,7 +31,7 @@ class MNHFTx
2731
CBLSSignature sig;
2832

2933
MNHFTx() = default;
30-
bool Verify(const CBlockIndex* pQuorumIndex) const;
34+
bool Verify(const CBlockIndex* pQuorumIndex, const llmq::CQuorumManager& quorumManager) const;
3135

3236
SERIALIZE_METHODS(MNHFTx, obj)
3337
{
@@ -72,6 +76,6 @@ class MNHFTxPayload
7276
}
7377
};
7478

75-
bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
79+
bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const llmq::CQuorumManager& quorumManager, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
7680

7781
#endif // BITCOIN_EVO_MNHFTX_H

src/evo/specialtxman.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
#include <hash.h>
1414
#include <llmq/blockprocessor.h>
1515
#include <llmq/commitment.h>
16+
#include <llmq/context.h>
1617
#include <llmq/utils.h>
1718
#include <primitives/block.h>
1819
#include <validation.h>
1920

20-
bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, bool check_sigs, TxValidationState& state)
21+
namespace llmq {
22+
class CChainLocksHandler;
23+
} // namespace llmq
24+
25+
bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, LLMQContext& llmq_ctx, bool check_sigs, TxValidationState& state)
2126
{
2227
AssertLockHeld(cs_main);
2328

@@ -43,7 +48,7 @@ bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const
4348
case TRANSACTION_QUORUM_COMMITMENT:
4449
return llmq::CheckLLMQCommitment(tx, pindexPrev, state);
4550
case TRANSACTION_MNHF_SIGNAL:
46-
return pindexPrev->nHeight + 1 >= Params().GetConsensus().DIP0024Height && CheckMNHFTx(tx, pindexPrev, state);
51+
return pindexPrev->nHeight + 1 >= Params().GetConsensus().DIP0024Height && CheckMNHFTx(tx, pindexPrev, *llmq_ctx.qman, state);
4752
}
4853
} catch (const std::exception& e) {
4954
LogPrintf("%s -- failed: %s\n", __func__, e.what());
@@ -99,10 +104,12 @@ bool UndoSpecialTx(const CTransaction& tx, const CBlockIndex* pindex)
99104
return false;
100105
}
101106

102-
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumBlockProcessor& quorum_block_processor, const llmq::CChainLocksHandler& chainlock_handler,
103-
const CCoinsViewCache& view, bool fJustCheck, bool fCheckCbTxMerleRoots, BlockValidationState& state)
107+
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, const CCoinsViewCache& view,
108+
LLMQContext& llmq_ctx, bool fJustCheck, bool fCheckCbTxMerleRoots, BlockValidationState& state)
104109
{
105110
AssertLockHeld(cs_main);
111+
llmq::CQuorumBlockProcessor& quorum_block_processor = *llmq_ctx.quorum_block_processor;
112+
const llmq::CChainLocksHandler& chainlock_handler = *llmq_ctx.clhandler;
106113

107114
try {
108115
static int64_t nTimeLoop = 0;
@@ -117,7 +124,7 @@ bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, ll
117124
TxValidationState tx_state;
118125
// At this moment CheckSpecialTx() and ProcessSpecialTx() may fail by 2 possible ways:
119126
// consensus failures and "TX_BAD_SPECIAL"
120-
if (!CheckSpecialTx(*ptr_tx, pindex->pprev, view, fCheckCbTxMerleRoots, tx_state)) {
127+
if (!CheckSpecialTx(*ptr_tx, pindex->pprev, view, llmq_ctx, fCheckCbTxMerleRoots, tx_state)) {
121128
assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS || tx_state.GetResult() == TxValidationResult::TX_BAD_SPECIAL);
122129
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
123130
strprintf("Special Transaction check failed (tx hash %s) %s", ptr_tx->GetHash().ToString(), tx_state.GetDebugMessage()));
@@ -133,7 +140,7 @@ bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, ll
133140
nTimeLoop += nTime2 - nTime1;
134141
LogPrint(BCLog::BENCHMARK, " - Loop: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeLoop * 0.000001);
135142

136-
if (!quorum_block_processor.ProcessBlock(block, pindex, state, fJustCheck, fCheckCbTxMerleRoots)) {
143+
if (!quorum_block_processor.ProcessBlock(block, pindex, *llmq_ctx.qman, fJustCheck, fCheckCbTxMerleRoots, state)) {
137144
// pass the state returned by the function above
138145
return false;
139146
}
@@ -183,10 +190,10 @@ bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, ll
183190
return true;
184191
}
185192

186-
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumBlockProcessor& quorum_block_processor)
193+
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, LLMQContext& llmq_ctx)
187194
{
188195
AssertLockHeld(cs_main);
189-
196+
llmq::CQuorumBlockProcessor& quorum_block_processor = *llmq_ctx.quorum_block_processor;
190197
auto bls_legacy_scheme = bls::bls_legacy_scheme.load();
191198

192199
try {
@@ -208,7 +215,7 @@ bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, llmq:
208215
return false;
209216
}
210217

211-
if (!quorum_block_processor.UndoBlock(block, pindex)) {
218+
if (!quorum_block_processor.UndoBlock(block, pindex, *llmq_ctx.qman)) {
212219
return false;
213220
}
214221
} catch (const std::exception& e) {

src/evo/specialtxman.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ class BlockValidationState;
1313
class CBlock;
1414
class CBlockIndex;
1515
class CCoinsViewCache;
16+
class LLMQContext;
1617
class TxValidationState;
17-
namespace llmq {
18-
class CQuorumBlockProcessor;
19-
class CChainLocksHandler;
20-
} // namespace llmq
2118

2219
extern RecursiveMutex cs_main;
2320

24-
bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, bool check_sigs, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
25-
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumBlockProcessor& quorum_block_processor, const llmq::CChainLocksHandler& chainlock_handler,
26-
const CCoinsViewCache& view, bool fJustCheck, bool fCheckCbTxMerleRoots, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
27-
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumBlockProcessor& quorum_block_processor) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
21+
bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, LLMQContext& llmq_ctx, bool check_sigs, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
22+
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, const CCoinsViewCache& view,
23+
LLMQContext& llmq_ctx, bool fJustCheck, bool fCheckCbTxMerleRoots,
24+
BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
25+
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, LLMQContext& llmq_ctx) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
2826

2927
#endif // BITCOIN_EVO_SPECIALTXMAN_H

src/llmq/blockprocessor.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
#include <map>
2727

28-
static void PreComputeQuorumMembers(const CBlockIndex* pindex, bool reset_cache = false)
28+
namespace llmq {
29+
class CQuorumManager;
30+
} // namespace llmq
31+
32+
static void PreComputeQuorumMembers(const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager, bool reset_cache = false)
2933
{
30-
for (const Consensus::LLMQParams& params : llmq::utils::GetEnabledQuorumParams(pindex->pprev)) {
34+
for (const Consensus::LLMQParams& params : llmq::utils::GetEnabledQuorumParams(pindex->pprev, quorumManager)) {
3135
if (llmq::utils::IsQuorumRotationEnabled(params, pindex) && (pindex->nHeight % params.dkgInterval == 0)) {
3236
llmq::utils::GetAllQuorumMembers(params.type, pindex, reset_cache);
3337
}
@@ -145,7 +149,7 @@ void CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_view m
145149
AddMineableCommitment(qc);
146150
}
147151

148-
bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex* pindex, BlockValidationState& state, bool fJustCheck, bool fBLSChecks)
152+
bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager, bool fJustCheck, bool fBLSChecks, BlockValidationState& state)
149153
{
150154
AssertLockHeld(cs_main);
151155

@@ -157,7 +161,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
157161
return true;
158162
}
159163

160-
PreComputeQuorumMembers(pindex);
164+
PreComputeQuorumMembers(pindex, quorumManager);
161165

162166
std::multimap<Consensus::LLMQType, CFinalCommitment> qcs;
163167
if (!GetCommitmentsFromBlock(block, pindex, qcs, state)) {
@@ -168,7 +172,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
168172
// until the first non-null commitment has been mined. After the non-null commitment, no other commitments are
169173
// allowed, including null commitments.
170174
// Note: must only check quorums that were enabled at the _previous_ block height to match mining logic
171-
for (const Consensus::LLMQParams& params : utils::GetEnabledQuorumParams(pindex->pprev)) {
175+
for (const Consensus::LLMQParams& params : utils::GetEnabledQuorumParams(pindex->pprev, quorumManager)) {
172176
// skip these checks when replaying blocks after the crash
173177
if (::ChainActive().Tip() == nullptr) {
174178
break;
@@ -309,11 +313,11 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
309313
return true;
310314
}
311315

312-
bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, const CBlockIndex* pindex)
316+
bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager)
313317
{
314318
AssertLockHeld(cs_main);
315319

316-
PreComputeQuorumMembers(pindex, true);
320+
PreComputeQuorumMembers(pindex, quorumManager, true);
317321

318322
std::multimap<Consensus::LLMQType, CFinalCommitment> qcs;
319323
BlockValidationState dummy;

src/llmq/blockprocessor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ namespace llmq
3030
class CFinalCommitment;
3131
using CFinalCommitmentPtr = std::unique_ptr<CFinalCommitment>;
3232

33+
class CQuorumManager;
34+
3335
class CQuorumBlockProcessor
3436
{
3537
private:
@@ -52,8 +54,8 @@ class CQuorumBlockProcessor
5254

5355
void ProcessMessage(const CNode& peer, std::string_view msg_type, CDataStream& vRecv);
5456

55-
bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, BlockValidationState& state, bool fJustCheck, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
56-
bool UndoBlock(const CBlock& block, const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
57+
bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager, bool fJustCheck, bool fBLSChecks, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
58+
bool UndoBlock(const CBlock& block, const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
5759

5860
void AddMineableCommitment(const CFinalCommitment& fqc);
5961
bool HasMineableCommitment(const uint256& hash) const;

src/llmq/chainlocks.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <llmq/chainlocks.h>
66
#include <llmq/instantsend.h>
7-
#include <llmq/quorums.h>
87
#include <llmq/utils.h>
98
#include <llmq/signing_shares.h>
109

src/llmq/dkgsessionmgr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
#include <llmq/debug.h>
66
#include <llmq/dkgsessionmgr.h>
7-
#include <llmq/quorums.h>
87
#include <llmq/utils.h>
98

109
#include <evo/deterministicmns.h>
1110

1211
#include <chainparams.h>
1312
#include <net_processing.h>
13+
#include <saltedhasher.h>
1414
#include <spork.h>
15+
#include <unordered_lru_cache.h>
1516
#include <util/irange.h>
1617
#include <util/underlying.h>
1718
#include <validation.h>

src/llmq/utils.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,25 +1031,13 @@ bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, const CQuorumMana
10311031
assert(false);
10321032
}
10331033

1034-
std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(const CBlockIndex* pindex)
1035-
{
1036-
std::vector<Consensus::LLMQType> ret;
1037-
ret.reserve(Params().GetConsensus().llmqs.size());
1038-
for (const auto& params : Params().GetConsensus().llmqs) {
1039-
if (IsQuorumTypeEnabled(params.type, *llmq::quorumManager, pindex)) {
1040-
ret.push_back(params.type);
1041-
}
1042-
}
1043-
return ret;
1044-
}
1045-
1046-
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(const CBlockIndex* pindex)
1034+
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager)
10471035
{
10481036
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> ret;
10491037
ret.reserve(Params().GetConsensus().llmqs.size());
10501038

10511039
std::copy_if(Params().GetConsensus().llmqs.begin(), Params().GetConsensus().llmqs.end(), std::back_inserter(ret),
1052-
[&pindex](const auto& params){return IsQuorumTypeEnabled(params.type, *llmq::quorumManager, pindex);});
1040+
[&pindex, &quorumManager](const auto& params){return IsQuorumTypeEnabled(params.type, quorumManager, pindex);});
10531041

10541042
return ret;
10551043
}

src/llmq/utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ bool IsQuorumActive(Consensus::LLMQType llmqType, const CQuorumManager& qman, co
6666
bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, const CQuorumManager& qman, const CBlockIndex* pindex);
6767
bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, const CQuorumManager& qman, const CBlockIndex* pindex, std::optional<bool> optDIP0024IsActive, std::optional<bool> optHaveDIP0024Quorums);
6868

69-
std::vector<Consensus::LLMQType> GetEnabledQuorumTypes(const CBlockIndex* pindex);
70-
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(const CBlockIndex* pindex);
69+
std::vector<std::reference_wrapper<const Consensus::LLMQParams>> GetEnabledQuorumParams(const CBlockIndex* pindex, llmq::CQuorumManager& quorumManager);
7170

7271
bool IsQuorumRotationEnabled(const Consensus::LLMQParams& llmqParams, const CBlockIndex* pindex);
7372
Consensus::LLMQType GetInstantSendLLMQType(const CQuorumManager& qman, const CBlockIndex* pindex);

0 commit comments

Comments
 (0)