Skip to content

Commit 2a533fe

Browse files
Merge #7086: feat: minimal improvements to llmq/quorums
41b9283 refactor: inline error handler in `CQuorumManager::ProcessMessage()` (Kittywhiskers Van Gogh) caa5400 refactor: move `mapQuorumDataRequests` to `CQuorumManager`, use `Mutex` (Kittywhiskers Van Gogh) Pull request description: ## Issue being fixed or feature implemented ## What was done? extracted two commits from: #7063 ## How Has This Been Tested? ## Breaking Changes None ## Checklist: _Go over all the following points, and put an `x` in all the boxes that apply._ - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: knst: utACK 41b9283 Tree-SHA512: 1ec93540031be1006ceefd4a2f1445f2edc089958bc8dd6dbefda689bd16ff21804b7bc1a7bbe7bae228a12824c898d43c38a8664100634469bf117349c02b05
2 parents c404597 + 41b9283 commit 2a533fe

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

src/llmq/quorums.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ namespace llmq
3434
static const std::string DB_QUORUM_SK_SHARE = "q_Qsk";
3535
static const std::string DB_QUORUM_QUORUM_VVEC = "q_Qqvvec";
3636

37-
RecursiveMutex cs_data_requests;
38-
static std::unordered_map<CQuorumDataRequestKey, CQuorumDataRequest, StaticSaltedHasher> mapQuorumDataRequests GUARDED_BY(cs_data_requests);
39-
40-
4137
static uint256 MakeQuorumKey(const CQuorum& q)
4238
{
4339
CHashWriter hw(SER_NETWORK, 0);
@@ -707,18 +703,9 @@ size_t CQuorumManager::GetQuorumRecoveryStartOffset(const CQuorum& quorum, gsl::
707703

708704
MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& connman, std::string_view msg_type, CDataStream& vRecv)
709705
{
710-
auto strFunc = __func__;
711-
auto errorHandler = [&](const std::string& strError, int nScore = 10) -> MessageProcessingResult {
712-
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- %s: %s, from peer=%d\n", strFunc, msg_type, strError, pfrom.GetId());
713-
if (nScore > 0) {
714-
return MisbehavingError{nScore};
715-
}
716-
return {};
717-
};
718-
719706
if (msg_type == NetMsgType::QGETDATA) {
720707
if (m_mn_activeman == nullptr || (pfrom.GetVerifiedProRegTxHash().IsNull() && !pfrom.qwatch)) {
721-
return errorHandler("Not a verified masternode or a qwatch connection");
708+
return MisbehavingError{10, "not a verified masternode or a qwatch connection"};
722709
}
723710

724711
CQuorumDataRequest request;
@@ -735,7 +722,7 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
735722
case (CQuorumDataRequest::Errors::QUORUM_NOT_FOUND):
736723
case (CQuorumDataRequest::Errors::MASTERNODE_IS_NO_MEMBER):
737724
case (CQuorumDataRequest::Errors::UNDEFINED):
738-
if (request_limit_exceeded) ret = errorHandler("Request limit exceeded", 25);
725+
if (request_limit_exceeded) ret = MisbehavingError{25, "request limit exceeded"};
739726
break;
740727
case (CQuorumDataRequest::Errors::QUORUM_VERIFICATION_VECTOR_MISSING):
741728
case (CQuorumDataRequest::Errors::ENCRYPTED_CONTRIBUTIONS_MISSING):
@@ -812,7 +799,7 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
812799

813800
if (msg_type == NetMsgType::QDATA) {
814801
if ((m_mn_activeman == nullptr && !m_quorums_watch) || pfrom.GetVerifiedProRegTxHash().IsNull()) {
815-
return errorHandler("Not a verified masternode and -watchquorums is not enabled");
802+
return MisbehavingError{10, "not a verified masternode and -watchquorums is not enabled"};
816803
}
817804

818805
CQuorumDataRequest request;
@@ -823,25 +810,28 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
823810
const CQuorumDataRequestKey key(pfrom.GetVerifiedProRegTxHash(), true, request.GetQuorumHash(), request.GetLLMQType());
824811
auto it = mapQuorumDataRequests.find(key);
825812
if (it == mapQuorumDataRequests.end()) {
826-
return errorHandler("Not requested");
813+
return MisbehavingError{10, "not requested"};
827814
}
828815
if (it->second.IsProcessed()) {
829-
return errorHandler("Already received");
816+
return MisbehavingError(10, "already received");
830817
}
831818
if (request != it->second) {
832-
return errorHandler("Not like requested");
819+
return MisbehavingError(10, "not like requested");
833820
}
834821
it->second.SetProcessed();
835822
}
836823

837824
if (request.GetError() != CQuorumDataRequest::Errors::NONE) {
838-
return errorHandler(strprintf("Error %d (%s)", request.GetError(), request.GetErrorString()), 0);
825+
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- %s: %s, from peer=%d\n", __func__, msg_type, strprintf("Error %d (%s)", request.GetError(), request.GetErrorString()), pfrom.GetId());
826+
return {};
839827
}
840828

841829
CQuorumPtr pQuorum;
842830
{
843831
if (LOCK(cs_map_quorums); !mapQuorumsCache[request.GetLLMQType()].get(request.GetQuorumHash(), pQuorum)) {
844-
return errorHandler("Quorum not found", 0); // Don't bump score because we asked for it
832+
// Don't bump score because we asked for it
833+
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- %s: Quorum not found, from peer=%d\n", __func__, msg_type, pfrom.GetId());
834+
return {};
845835
}
846836
}
847837

@@ -854,7 +844,7 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
854844
if (pQuorum->SetVerificationVector(verificationVector)) {
855845
StartCachePopulatorThread(pQuorum);
856846
} else {
857-
return errorHandler("Invalid quorum verification vector");
847+
return MisbehavingError{10, "invalid quorum verification vector"};
858848
}
859849
}
860850

@@ -863,12 +853,16 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
863853
assert(m_mn_activeman);
864854

865855
if (WITH_LOCK(pQuorum->cs_vvec_shShare, return pQuorum->quorumVvec->size() != size_t(pQuorum->params.threshold))) {
866-
return errorHandler("No valid quorum verification vector available", 0); // Don't bump score because we asked for it
856+
// Don't bump score because we asked for it
857+
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- %s: No valid quorum verification vector available, from peer=%d\n", __func__, msg_type, pfrom.GetId());
858+
return {};
867859
}
868860

869861
int memberIdx = pQuorum->GetMemberIndex(request.GetProTxHash());
870862
if (memberIdx == -1) {
871-
return errorHandler("Not a member of the quorum", 0); // Don't bump score because we asked for it
863+
// Don't bump score because we asked for it
864+
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- %s: Not a member of the quorum, from peer=%d\n", __func__, msg_type, pfrom.GetId());
865+
return {};
872866
}
873867

874868
std::vector<CBLSIESEncryptedObject<CBLSSecretKey>> vecEncrypted;
@@ -878,13 +872,13 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
878872
vecSecretKeys.resize(vecEncrypted.size());
879873
for (const auto i : irange::range(vecEncrypted.size())) {
880874
if (!m_mn_activeman->Decrypt(vecEncrypted[i], memberIdx, vecSecretKeys[i], PROTOCOL_VERSION)) {
881-
return errorHandler("Failed to decrypt");
875+
return MisbehavingError{10, "failed to decrypt"};
882876
}
883877
}
884878

885879
CBLSSecretKey secretKeyShare = blsWorker.AggregateSecretKeys(vecSecretKeys);
886880
if (!pQuorum->SetSecretKeyShare(secretKeyShare, *m_mn_activeman)) {
887-
return errorHandler("Invalid secret key share received");
881+
return MisbehavingError{10, "invalid secret key share received"};
888882
}
889883
}
890884
WITH_LOCK(cs_db, pQuorum->WriteContributions(*db));

src/llmq/quorums.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ class CQuorumManager
251251
mutable Mutex cs_db;
252252
std::unique_ptr<CDBWrapper> db GUARDED_BY(cs_db){nullptr};
253253

254+
mutable Mutex cs_data_requests;
255+
mutable std::unordered_map<CQuorumDataRequestKey, CQuorumDataRequest, StaticSaltedHasher> mapQuorumDataRequests
256+
GUARDED_BY(cs_data_requests);
257+
254258
mutable Mutex cs_map_quorums;
255259
mutable std::map<Consensus::LLMQType, Uint256LruHashMap<CQuorumPtr>> mapQuorumsCache GUARDED_BY(cs_map_quorums);
256260

@@ -294,19 +298,19 @@ class CQuorumManager
294298
void Stop();
295299

296300
void TriggerQuorumDataRecoveryThreads(CConnman& connman, gsl::not_null<const CBlockIndex*> pIndex) const
297-
EXCLUSIVE_LOCKS_REQUIRED(!cs_db, !cs_scan_quorums, !cs_map_quorums);
301+
EXCLUSIVE_LOCKS_REQUIRED(!cs_db, !cs_data_requests, !cs_scan_quorums, !cs_map_quorums);
298302

299303
void UpdatedBlockTip(const CBlockIndex* pindexNew, CConnman& connman, bool fInitialDownload) const
300-
EXCLUSIVE_LOCKS_REQUIRED(!cs_db, !cs_scan_quorums, !cs_map_quorums);
304+
EXCLUSIVE_LOCKS_REQUIRED(!cs_db, !cs_data_requests, !cs_scan_quorums, !cs_map_quorums);
301305

302306
[[nodiscard]] MessageProcessingResult ProcessMessage(CNode& pfrom, CConnman& connman, std::string_view msg_type,
303307
CDataStream& vRecv)
304-
EXCLUSIVE_LOCKS_REQUIRED(!cs_map_quorums, !cs_db);
308+
EXCLUSIVE_LOCKS_REQUIRED(!cs_db, !cs_data_requests, !cs_map_quorums);
305309

306310
static bool HasQuorum(Consensus::LLMQType llmqType, const CQuorumBlockProcessor& quorum_block_processor, const uint256& quorumHash);
307311

308312
bool RequestQuorumData(CNode* pfrom, CConnman& connman, const CQuorum& quorum, uint16_t nDataMask,
309-
const uint256& proTxHash = uint256()) const;
313+
const uint256& proTxHash = uint256()) const EXCLUSIVE_LOCKS_REQUIRED(!cs_data_requests);
310314

311315
// all these methods will lock cs_main for a short period of time
312316
CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const
@@ -341,7 +345,7 @@ class CQuorumManager
341345

342346
void StartCachePopulatorThread(CQuorumCPtr pQuorum) const;
343347
void StartQuorumDataRecoveryThread(CConnman& connman, CQuorumCPtr pQuorum, gsl::not_null<const CBlockIndex*> pIndex,
344-
uint16_t nDataMask) const;
348+
uint16_t nDataMask) const EXCLUSIVE_LOCKS_REQUIRED(!cs_data_requests);
345349

346350
void StartCleanupOldQuorumDataThread(gsl::not_null<const CBlockIndex*> pIndex) const;
347351
void MigrateOldQuorumDB(CEvoDB& evoDb) const EXCLUSIVE_LOCKS_REQUIRED(!cs_db);

0 commit comments

Comments
 (0)