Skip to content

Commit 41b9283

Browse files
kwvgPastaPastaPasta
authored andcommitted
refactor: inline error handler in CQuorumManager::ProcessMessage()
1 parent caa5400 commit 41b9283

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

src/llmq/quorums.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -703,18 +703,9 @@ size_t CQuorumManager::GetQuorumRecoveryStartOffset(const CQuorum& quorum, gsl::
703703

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

720711
CQuorumDataRequest request;
@@ -731,7 +722,7 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
731722
case (CQuorumDataRequest::Errors::QUORUM_NOT_FOUND):
732723
case (CQuorumDataRequest::Errors::MASTERNODE_IS_NO_MEMBER):
733724
case (CQuorumDataRequest::Errors::UNDEFINED):
734-
if (request_limit_exceeded) ret = errorHandler("Request limit exceeded", 25);
725+
if (request_limit_exceeded) ret = MisbehavingError{25, "request limit exceeded"};
735726
break;
736727
case (CQuorumDataRequest::Errors::QUORUM_VERIFICATION_VECTOR_MISSING):
737728
case (CQuorumDataRequest::Errors::ENCRYPTED_CONTRIBUTIONS_MISSING):
@@ -808,7 +799,7 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
808799

809800
if (msg_type == NetMsgType::QDATA) {
810801
if ((m_mn_activeman == nullptr && !m_quorums_watch) || pfrom.GetVerifiedProRegTxHash().IsNull()) {
811-
return errorHandler("Not a verified masternode and -watchquorums is not enabled");
802+
return MisbehavingError{10, "not a verified masternode and -watchquorums is not enabled"};
812803
}
813804

814805
CQuorumDataRequest request;
@@ -819,25 +810,28 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
819810
const CQuorumDataRequestKey key(pfrom.GetVerifiedProRegTxHash(), true, request.GetQuorumHash(), request.GetLLMQType());
820811
auto it = mapQuorumDataRequests.find(key);
821812
if (it == mapQuorumDataRequests.end()) {
822-
return errorHandler("Not requested");
813+
return MisbehavingError{10, "not requested"};
823814
}
824815
if (it->second.IsProcessed()) {
825-
return errorHandler("Already received");
816+
return MisbehavingError(10, "already received");
826817
}
827818
if (request != it->second) {
828-
return errorHandler("Not like requested");
819+
return MisbehavingError(10, "not like requested");
829820
}
830821
it->second.SetProcessed();
831822
}
832823

833824
if (request.GetError() != CQuorumDataRequest::Errors::NONE) {
834-
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 {};
835827
}
836828

837829
CQuorumPtr pQuorum;
838830
{
839831
if (LOCK(cs_map_quorums); !mapQuorumsCache[request.GetLLMQType()].get(request.GetQuorumHash(), pQuorum)) {
840-
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 {};
841835
}
842836
}
843837

@@ -850,7 +844,7 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
850844
if (pQuorum->SetVerificationVector(verificationVector)) {
851845
StartCachePopulatorThread(pQuorum);
852846
} else {
853-
return errorHandler("Invalid quorum verification vector");
847+
return MisbehavingError{10, "invalid quorum verification vector"};
854848
}
855849
}
856850

@@ -859,12 +853,16 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
859853
assert(m_mn_activeman);
860854

861855
if (WITH_LOCK(pQuorum->cs_vvec_shShare, return pQuorum->quorumVvec->size() != size_t(pQuorum->params.threshold))) {
862-
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 {};
863859
}
864860

865861
int memberIdx = pQuorum->GetMemberIndex(request.GetProTxHash());
866862
if (memberIdx == -1) {
867-
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 {};
868866
}
869867

870868
std::vector<CBLSIESEncryptedObject<CBLSSecretKey>> vecEncrypted;
@@ -874,13 +872,13 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
874872
vecSecretKeys.resize(vecEncrypted.size());
875873
for (const auto i : irange::range(vecEncrypted.size())) {
876874
if (!m_mn_activeman->Decrypt(vecEncrypted[i], memberIdx, vecSecretKeys[i], PROTOCOL_VERSION)) {
877-
return errorHandler("Failed to decrypt");
875+
return MisbehavingError{10, "failed to decrypt"};
878876
}
879877
}
880878

881879
CBLSSecretKey secretKeyShare = blsWorker.AggregateSecretKeys(vecSecretKeys);
882880
if (!pQuorum->SetSecretKeyShare(secretKeyShare, *m_mn_activeman)) {
883-
return errorHandler("Invalid secret key share received");
881+
return MisbehavingError{10, "invalid secret key share received"};
884882
}
885883
}
886884
WITH_LOCK(cs_db, pQuorum->WriteContributions(*db));

0 commit comments

Comments
 (0)