Skip to content

Commit 74e54b8

Browse files
Merge #6292: refactor: remove dependency of llmq/chainlocks, llmq/quorum_block_processor, ehf_signals on PeerManager
c77216e docs: explain meaning of MessageProcessingResult's members (Konstantin Akimov) d0f1778 refactor: drop dependency of EhfSignals on PeerManager (Konstantin Akimov) 1d13f01 refactor: remove dependency of QuorumBlockProcessor on PeerManager (Konstantin Akimov) f1c6d17 refactor: remove dependency of chainlocks on PeerManager (Konstantin Akimov) 5383421 refactor: HandleNewRecoveredSig return PostProcessingMessage (Konstantin Akimov) 09565fe refactor: new type of message processing result for resolving circular dependency over PeerManager (Konstantin Akimov) d54b3ee refactor: move ChainLocksSigningEnabled from header to cpp file as static function (Konstantin Akimov) cab700a refactor: remove unused include spork.h from validation.cpp (Konstantin Akimov) Pull request description: ## Issue being fixed or feature implemented It reduces circular dependencies and simplify code. ## What was done? Removed circular dependency over PeerManager for llmq::QuorumBlockProcessor, llmq::CEhfSignals, llmq::ChainLocks, and several extra useful refactorings: see commits. ## How Has This Been Tested? Run `test/lint/lint-circular-dependencies.sh` ## Breaking Changes N/A ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone ACKs for top commit: UdjinM6: utACK c77216e PastaPastaPasta: utACK c77216e Tree-SHA512: e01829a694c9bfbbe70bc346ef5949b5e9e4532560f4c40ee292952f05f0fd23ecf4bd978e918f74dd3422c1b90231fd7d9984f491f3ab8f7eb08072540406b4
2 parents e5daf4a + c77216e commit 74e54b8

26 files changed

+183
-154
lines changed

src/coinjoin/client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <coinjoin/util.h>
99
#include <coinjoin/coinjoin.h>
1010

11-
#include <net_types.h>
11+
#include <protocol.h>
1212
#include <util/ranges.h>
1313
#include <util/translation.h>
1414

src/coinjoin/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <coinjoin/coinjoin.h>
99

10-
#include <net_types.h>
10+
#include <protocol.h>
1111

1212
class CActiveMasternodeManager;
1313
class CChainState;

src/evo/mnauth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define BITCOIN_EVO_MNAUTH_H
77

88
#include <bls/bls.h>
9-
#include <net_types.h>
9+
#include <protocol.h>
1010
#include <serialize.h>
1111

1212
class CActiveMasternodeManager;

src/governance/governance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <cachemap.h>
1212
#include <cachemultimap.h>
13-
#include <net_types.h>
13+
#include <protocol.h>
1414
#include <util/check.h>
1515

1616
#include <optional>

src/llmq/blockprocessor.cpp

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <consensus/validation.h>
1717
#include <deploymentstatus.h>
1818
#include <net.h>
19-
#include <net_processing.h>
2019
#include <primitives/block.h>
2120
#include <primitives/transaction.h>
2221
#include <saltedhasher.h>
@@ -44,14 +43,16 @@ static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT_Q_INDEXED = "q_m
4443

4544
static const std::string DB_BEST_BLOCK_UPGRADE = "q_bbu2";
4645

47-
CQuorumBlockProcessor::CQuorumBlockProcessor(CChainState& chainstate, CDeterministicMNManager& dmnman, CEvoDB& evoDb,
48-
const std::unique_ptr<PeerManager>& peerman) :
49-
m_chainstate(chainstate), m_dmnman(dmnman), m_evoDb(evoDb), m_peerman(peerman)
46+
CQuorumBlockProcessor::CQuorumBlockProcessor(CChainState& chainstate, CDeterministicMNManager& dmnman, CEvoDB& evoDb) :
47+
m_chainstate(chainstate),
48+
m_dmnman(dmnman),
49+
m_evoDb(evoDb)
5050
{
5151
utils::InitQuorumsCache(mapHasMinedCommitmentCache);
5252
}
5353

54-
PeerMsgRet CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_view msg_type, CDataStream& vRecv)
54+
MessageProcessingResult CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_view msg_type,
55+
CDataStream& vRecv)
5556
{
5657
if (msg_type != NetMsgType::QFCOMMITMENT) {
5758
return {};
@@ -60,19 +61,21 @@ PeerMsgRet CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_
6061
CFinalCommitment qc;
6162
vRecv >> qc;
6263

63-
WITH_LOCK(::cs_main, Assert(m_peerman)->EraseObjectRequest(peer.GetId(),
64-
CInv(MSG_QUORUM_FINAL_COMMITMENT, ::SerializeHash(qc))));
64+
MessageProcessingResult ret;
65+
ret.m_to_erase = CInv{MSG_QUORUM_FINAL_COMMITMENT, ::SerializeHash(qc)};
6566

6667
if (qc.IsNull()) {
6768
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- null commitment from peer=%d\n", __func__, peer.GetId());
68-
return tl::unexpected{100};
69+
ret.m_error = MisbehavingError{100};
70+
return ret;
6971
}
7072

7173
const auto& llmq_params_opt = Params().GetLLMQ(qc.llmqType);
7274
if (!llmq_params_opt.has_value()) {
7375
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- invalid commitment type %d from peer=%d\n", __func__,
7476
ToUnderlying(qc.llmqType), peer.GetId());
75-
return tl::unexpected{100};
77+
ret.m_error = MisbehavingError{100};
78+
return ret;
7679
}
7780
auto type = qc.llmqType;
7881

@@ -86,32 +89,33 @@ PeerMsgRet CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_
8689
qc.quorumHash.ToString(), peer.GetId());
8790
// can't really punish the node here, as we might simply be the one that is on the wrong chain or not
8891
// fully synced
89-
return {};
92+
return ret;
9093
}
9194
if (m_chainstate.m_chain.Tip()->GetAncestor(pQuorumBaseBlockIndex->nHeight) != pQuorumBaseBlockIndex) {
9295
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- block %s not in active chain, peer=%d\n", __func__,
9396
qc.quorumHash.ToString(), peer.GetId());
9497
// same, can't punish
95-
return {};
98+
return ret;
9699
}
97100
if (int quorumHeight = pQuorumBaseBlockIndex->nHeight - (pQuorumBaseBlockIndex->nHeight % llmq_params_opt->dkgInterval) + int(qc.quorumIndex);
98101
quorumHeight != pQuorumBaseBlockIndex->nHeight) {
99102
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- block %s is not the first block in the DKG interval, peer=%d\n", __func__,
100103
qc.quorumHash.ToString(), peer.GetId());
101-
return tl::unexpected{100};
104+
ret.m_error = MisbehavingError{100};
105+
return ret;
102106
}
103107
if (pQuorumBaseBlockIndex->nHeight < (m_chainstate.m_chain.Height() - llmq_params_opt->dkgInterval)) {
104108
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- block %s is too old, peer=%d\n", __func__,
105109
qc.quorumHash.ToString(), peer.GetId());
106110
// TODO: enable punishment in some future version when all/most nodes are running with this fix
107-
// return tl::unexpected{100};
108-
return {};
111+
// ret.m_error = MisbehavingError{100};
112+
return ret;
109113
}
110114
if (HasMinedCommitment(type, qc.quorumHash)) {
111115
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- commitment for quorum hash[%s], type[%d], quorumIndex[%d] is already mined, peer=%d\n",
112116
__func__, qc.quorumHash.ToString(), ToUnderlying(type), qc.quorumIndex, peer.GetId());
113117
// NOTE: do not punish here
114-
return {};
118+
return ret;
115119
}
116120
}
117121

@@ -124,7 +128,7 @@ PeerMsgRet CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_
124128
if (it != minableCommitmentsByQuorum.end()) {
125129
auto jt = minableCommitments.find(it->second);
126130
if (jt != minableCommitments.end() && jt->second.CountSigners() <= qc.CountSigners()) {
127-
return {};
131+
return ret;
128132
}
129133
}
130134
}
@@ -133,14 +137,15 @@ PeerMsgRet CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_
133137
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- commitment for quorum %s:%d is not valid quorumIndex[%d] nversion[%d], peer=%d\n",
134138
__func__, qc.quorumHash.ToString(),
135139
ToUnderlying(qc.llmqType), qc.quorumIndex, qc.nVersion, peer.GetId());
136-
return tl::unexpected{100};
140+
ret.m_error = MisbehavingError{100};
141+
return ret;
137142
}
138143

139144
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- received commitment for quorum %s:%d, validMembers=%d, signers=%d, peer=%d\n", __func__,
140145
qc.quorumHash.ToString(), ToUnderlying(qc.llmqType), qc.CountValidMembers(), qc.CountSigners(), peer.GetId());
141146

142-
AddMineableCommitmentAndRelay(qc);
143-
return {};
147+
ret.m_inventory = AddMineableCommitment(qc);
148+
return ret;
144149
}
145150

146151
bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, BlockValidationState& state, bool fJustCheck, bool fBLSChecks)
@@ -637,7 +642,7 @@ bool CQuorumBlockProcessor::HasMineableCommitment(const uint256& hash) const
637642
return minableCommitments.count(hash) != 0;
638643
}
639644

640-
std::optional<uint256> CQuorumBlockProcessor::AddMineableCommitment(const CFinalCommitment& fqc)
645+
std::optional<CInv> CQuorumBlockProcessor::AddMineableCommitment(const CFinalCommitment& fqc)
641646
{
642647
const uint256 commitmentHash = ::SerializeHash(fqc);
643648

@@ -663,17 +668,7 @@ std::optional<uint256> CQuorumBlockProcessor::AddMineableCommitment(const CFinal
663668
return false;
664669
}();
665670

666-
return relay ? std::make_optional(commitmentHash) : std::nullopt;
667-
}
668-
669-
void CQuorumBlockProcessor::AddMineableCommitmentAndRelay(const CFinalCommitment& fqc)
670-
{
671-
const auto commitmentHashOpt = AddMineableCommitment(fqc);
672-
// We only relay the new commitment if it's new or better then the old one
673-
if (commitmentHashOpt) {
674-
CInv inv(MSG_QUORUM_FINAL_COMMITMENT, *commitmentHashOpt);
675-
Assert(m_peerman)->RelayInv(inv);
676-
}
671+
return relay ? std::make_optional(CInv{MSG_QUORUM_FINAL_COMMITMENT, commitmentHash}) : std::nullopt;
677672
}
678673

679674
bool CQuorumBlockProcessor::GetMineableCommitmentByHash(const uint256& commitmentHash, llmq::CFinalCommitment& ret) const

src/llmq/blockprocessor.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#include <chain.h>
1111
#include <consensus/params.h>
12-
#include <net_types.h>
1312
#include <primitives/block.h>
13+
#include <protocol.h>
1414
#include <saltedhasher.h>
1515
#include <sync.h>
1616

@@ -26,7 +26,6 @@ class CDataStream;
2626
class CDeterministicMNManager;
2727
class CEvoDB;
2828
class CNode;
29-
class PeerManager;
3029

3130
extern RecursiveMutex cs_main;
3231

@@ -42,7 +41,6 @@ class CQuorumBlockProcessor
4241
CChainState& m_chainstate;
4342
CDeterministicMNManager& m_dmnman;
4443
CEvoDB& m_evoDb;
45-
const std::unique_ptr<PeerManager>& m_peerman;
4644

4745
mutable Mutex minableCommitmentsCs;
4846
std::map<std::pair<Consensus::LLMQType, uint256>, uint256> minableCommitmentsByQuorum GUARDED_BY(minableCommitmentsCs);
@@ -51,15 +49,15 @@ class CQuorumBlockProcessor
5149
mutable std::map<Consensus::LLMQType, unordered_lru_cache<uint256, bool, StaticSaltedHasher>> mapHasMinedCommitmentCache GUARDED_BY(minableCommitmentsCs);
5250

5351
public:
54-
explicit CQuorumBlockProcessor(CChainState& chainstate, CDeterministicMNManager& dmnman, CEvoDB& evoDb,
55-
const std::unique_ptr<PeerManager>& peerman);
52+
explicit CQuorumBlockProcessor(CChainState& chainstate, CDeterministicMNManager& dmnman, CEvoDB& evoDb);
5653

57-
PeerMsgRet ProcessMessage(const CNode& peer, std::string_view msg_type, CDataStream& vRecv);
54+
MessageProcessingResult ProcessMessage(const CNode& peer, std::string_view msg_type, CDataStream& vRecv);
5855

5956
bool ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, BlockValidationState& state, bool fJustCheck, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
6057
bool UndoBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
6158

62-
void AddMineableCommitmentAndRelay(const CFinalCommitment& fqc);
59+
//! it returns hash of commitment if it should be relay, otherwise nullopt
60+
std::optional<CInv> AddMineableCommitment(const CFinalCommitment& fqc);
6361
bool HasMineableCommitment(const uint256& hash) const;
6462
bool GetMineableCommitmentByHash(const uint256& commitmentHash, CFinalCommitment& ret) const;
6563
std::optional<std::vector<CFinalCommitment>> GetMineableCommitments(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@@ -74,8 +72,6 @@ class CQuorumBlockProcessor
7472
std::vector<std::pair<int, const CBlockIndex*>> GetLastMinedCommitmentsPerQuorumIndexUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t cycle) const;
7573
std::optional<const CBlockIndex*> GetLastMinedCommitmentsByQuorumIndexUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, int quorumIndex, size_t cycle) const;
7674
private:
77-
//! it returns hash of commitment if it should be relay, otherwise nullopt
78-
std::optional<uint256> AddMineableCommitment(const CFinalCommitment& fqc);
7975
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);
8076
bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, BlockValidationState& state, bool fJustCheck, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
8177
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

src/llmq/chainlocks.cpp

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <chainparams.h>
1212
#include <consensus/validation.h>
1313
#include <masternode/sync.h>
14-
#include <net_processing.h>
1514
#include <node/blockstorage.h>
1615
#include <node/ui_interface.h>
1716
#include <scheduler.h>
@@ -23,25 +22,29 @@
2322
#include <validation.h>
2423
#include <validationinterface.h>
2524

25+
static bool ChainLocksSigningEnabled(const CSporkManager& sporkman)
26+
{
27+
return sporkman.GetSporkValue(SPORK_19_CHAINLOCKS_ENABLED) == 0;
28+
}
29+
2630
namespace llmq
2731
{
2832
std::unique_ptr<CChainLocksHandler> chainLocksHandler;
2933

3034
CChainLocksHandler::CChainLocksHandler(CChainState& chainstate, CQuorumManager& _qman, CSigningManager& _sigman,
3135
CSigSharesManager& _shareman, CSporkManager& sporkman, CTxMemPool& _mempool,
32-
const CMasternodeSync& mn_sync, const std::unique_ptr<PeerManager>& peerman,
33-
bool is_masternode) :
36+
const CMasternodeSync& mn_sync, bool is_masternode) :
3437
m_chainstate(chainstate),
3538
qman(_qman),
3639
sigman(_sigman),
3740
shareman(_shareman),
3841
spork_manager(sporkman),
3942
mempool(_mempool),
4043
m_mn_sync(mn_sync),
41-
m_peerman(peerman),
4244
m_is_masternode{is_masternode},
4345
scheduler(std::make_unique<CScheduler>()),
44-
scheduler_thread(std::make_unique<std::thread>(std::thread(util::TraceThread, "cl-schdlr", [&] { scheduler->serviceQueue(); })))
46+
scheduler_thread(
47+
std::make_unique<std::thread>(std::thread(util::TraceThread, "cl-schdlr", [&] { scheduler->serviceQueue(); })))
4548
{
4649
}
4750

@@ -93,31 +96,11 @@ CChainLockSig CChainLocksHandler::GetBestChainLock() const
9396
return bestChainLock;
9497
}
9598

96-
PeerMsgRet CChainLocksHandler::ProcessMessage(const CNode& pfrom, const std::string& msg_type, CDataStream& vRecv)
97-
{
98-
if (!AreChainLocksEnabled(spork_manager)) {
99-
return {};
100-
}
101-
102-
if (msg_type == NetMsgType::CLSIG) {
103-
CChainLockSig clsig;
104-
vRecv >> clsig;
105-
106-
return ProcessNewChainLock(pfrom.GetId(), clsig, ::SerializeHash(clsig));
107-
}
108-
return {};
109-
}
110-
111-
PeerMsgRet CChainLocksHandler::ProcessNewChainLock(const NodeId from, const llmq::CChainLockSig& clsig, const uint256& hash)
99+
MessageProcessingResult CChainLocksHandler::ProcessNewChainLock(const NodeId from, const llmq::CChainLockSig& clsig,
100+
const uint256& hash)
112101
{
113102
CheckActiveState();
114103

115-
CInv clsigInv(MSG_CLSIG, hash);
116-
117-
if (from != -1) {
118-
WITH_LOCK(::cs_main, Assert(m_peerman)->EraseObjectRequest(from, clsigInv));
119-
}
120-
121104
{
122105
LOCK(cs);
123106
if (!seenChainLocks.emplace(hash, GetTimeMillis()).second) {
@@ -133,7 +116,7 @@ PeerMsgRet CChainLocksHandler::ProcessNewChainLock(const NodeId from, const llmq
133116
if (const auto ret = VerifyChainLock(clsig); ret != VerifyRecSigStatus::Valid) {
134117
LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- invalid CLSIG (%s), status=%d peer=%d\n", __func__, clsig.ToString(), ToUnderlying(ret), from);
135118
if (from != -1) {
136-
return tl::unexpected{10};
119+
return MisbehavingError{10};
137120
}
138121
return {};
139122
}
@@ -162,14 +145,12 @@ PeerMsgRet CChainLocksHandler::ProcessNewChainLock(const NodeId from, const llmq
162145
// Note: make sure to still relay clsig further.
163146
}
164147

165-
// Note: do not hold cs while calling RelayInv
166-
AssertLockNotHeld(cs);
167-
Assert(m_peerman)->RelayInv(clsigInv);
148+
CInv clsigInv(MSG_CLSIG, hash);
168149

169150
if (pindex == nullptr) {
170151
// we don't know the block/header for this CLSIG yet, so bail out for now
171152
// when the block or the header later comes in, we will enforce the correct chain
172-
return {};
153+
return clsigInv;
173154
}
174155

175156
scheduler->scheduleFromNow([&]() {
@@ -179,7 +160,7 @@ PeerMsgRet CChainLocksHandler::ProcessNewChainLock(const NodeId from, const llmq
179160

180161
LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- processed new CLSIG (%s), peer=%d\n",
181162
__func__, clsig.ToString(), from);
182-
return {};
163+
return clsigInv;
183164
}
184165

185166
void CChainLocksHandler::AcceptedBlockHeader(gsl::not_null<const CBlockIndex*> pindexNew)
@@ -520,10 +501,10 @@ void CChainLocksHandler::EnforceBestChainLock()
520501
uiInterface.NotifyChainLock(clsig->getBlockHash().ToString(), clsig->getHeight());
521502
}
522503

523-
void CChainLocksHandler::HandleNewRecoveredSig(const llmq::CRecoveredSig& recoveredSig)
504+
MessageProcessingResult CChainLocksHandler::HandleNewRecoveredSig(const llmq::CRecoveredSig& recoveredSig)
524505
{
525506
if (!isEnabled) {
526-
return;
507+
return {};
527508
}
528509

529510
CChainLockSig clsig;
@@ -532,17 +513,17 @@ void CChainLocksHandler::HandleNewRecoveredSig(const llmq::CRecoveredSig& recove
532513

533514
if (recoveredSig.getId() != lastSignedRequestId || recoveredSig.getMsgHash() != lastSignedMsgHash) {
534515
// this is not what we signed, so lets not create a CLSIG for it
535-
return;
516+
return {};
536517
}
537518
if (bestChainLock.getHeight() >= lastSignedHeight) {
538519
// already got the same or a better CLSIG through the CLSIG message
539-
return;
520+
return {};
540521
}
541522

542523

543524
clsig = CChainLockSig(lastSignedHeight, lastSignedMsgHash, recoveredSig.sig.Get());
544525
}
545-
ProcessNewChainLock(-1, clsig, ::SerializeHash(clsig));
526+
return ProcessNewChainLock(-1, clsig, ::SerializeHash(clsig));
546527
}
547528

548529
bool CChainLocksHandler::HasChainLock(int nHeight, const uint256& blockHash) const
@@ -678,9 +659,4 @@ bool AreChainLocksEnabled(const CSporkManager& sporkman)
678659
return sporkman.IsSporkActive(SPORK_19_CHAINLOCKS_ENABLED);
679660
}
680661

681-
bool ChainLocksSigningEnabled(const CSporkManager& sporkman)
682-
{
683-
return sporkman.GetSporkValue(SPORK_19_CHAINLOCKS_ENABLED) == 0;
684-
}
685-
686662
} // namespace llmq

0 commit comments

Comments
 (0)