Skip to content

Commit 0e4c80b

Browse files
committed
refactor: duplicated code for single-node quorums and regular quorums inside TryRecoverSig
1 parent 489090b commit 0e4c80b

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

src/llmq/signing_shares.cpp

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -789,35 +789,50 @@ void CSigSharesManager::ProcessSigShare(const CSigShare& sigShare, const CQuorum
789789
}
790790

791791
if (canTryRecovery) {
792-
TryRecoverSig(*quorum, sigShare.getId(), sigShare.getMsgHash());
792+
auto rs = TryRecoverSig(*quorum, sigShare.getId(), sigShare.getMsgHash());
793+
if (rs != nullptr) {
794+
if (sigman.ProcessRecoveredSig(rs)) {
795+
// TODO: remove duplicated code with NetSigning
796+
auto listeners = sigman.GetListeners();
797+
for (auto& l : listeners) {
798+
m_peerman.PostProcessMessage(l->HandleNewRecoveredSig(*rs));
799+
}
800+
801+
bool proactive_relay = rs->getLlmqType() != Consensus::LLMQType::LLMQ_100_67 &&
802+
rs->getLlmqType() != Consensus::LLMQType::LLMQ_400_60 &&
803+
rs->getLlmqType() != Consensus::LLMQType::LLMQ_400_85;
804+
GetMainSignals().NotifyRecoveredSig(rs, rs->GetHash().ToString(), proactive_relay);
805+
}
806+
}
793807
}
794808
}
795809

796-
void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id, const uint256& msgHash)
810+
std::shared_ptr<CRecoveredSig> CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
811+
const uint256& msgHash)
797812
{
798813
if (sigman.HasRecoveredSigForId(quorum.params.type, id)) {
799-
return;
814+
return nullptr;
800815
}
801816

802817
std::vector<CBLSSignature> sigSharesForRecovery;
803818
std::vector<CBLSId> idsForRecovery;
804-
std::shared_ptr<CRecoveredSig> singleMemberRecoveredSig;
805819
{
806820
LOCK(cs);
807821

808822
auto signHash = SignHash(quorum.params.type, quorum.qc->quorumHash, id, msgHash).Get();
809823
const auto* sigSharesForSignHash = sigShares.GetAllForSignHash(signHash);
810824
if (sigSharesForSignHash == nullptr) {
811-
return;
825+
return nullptr;
812826
}
813827

828+
std::shared_ptr<CRecoveredSig> singleMemberRecoveredSig;
814829
if (quorum.params.is_single_member()) {
815830
if (sigSharesForSignHash->empty()) {
816831
LogPrint(BCLog::LLMQ_SIGS, /* Continued */
817832
"CSigSharesManager::%s -- impossible to recover single-node signature - no shares yet. id=%s, "
818833
"msgHash=%s\n",
819834
__func__, id.ToString(), msgHash.ToString());
820-
return;
835+
return nullptr;
821836
}
822837
const auto& sigShare = sigSharesForSignHash->begin()->second;
823838
CBLSSignature recoveredSig = sigShare.sigShare.Get();
@@ -838,23 +853,11 @@ void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
838853

839854
// check if we can recover the final signature
840855
if (sigSharesForRecovery.size() < size_t(quorum.params.threshold)) {
841-
return;
856+
return nullptr;
842857
}
843-
}
844-
845-
// Handle single-member quorum case after releasing the lock
846-
if (singleMemberRecoveredSig) {
847-
if (sigman.ProcessRecoveredSig(singleMemberRecoveredSig)) {
848-
// TODO: remove duplicated code with NetSigning
849-
auto listeners = sigman.GetListeners();
850-
for (auto& l : listeners) {
851-
m_peerman.PostProcessMessage(l->HandleNewRecoveredSig(*singleMemberRecoveredSig));
852-
}
853-
854-
GetMainSignals().NotifyRecoveredSig(singleMemberRecoveredSig,
855-
singleMemberRecoveredSig->GetHash().ToString(), false);
858+
if (quorum.params.is_single_member()) {
859+
return singleMemberRecoveredSig; // end of single-quorum processing
856860
}
857-
return; // end of single-quorum processing
858861
}
859862

860863
// now recover it
@@ -863,7 +866,7 @@ void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
863866
if (!recoveredSig.Recover(sigSharesForRecovery, idsForRecovery)) {
864867
LogPrint(BCLog::LLMQ_SIGS, "CSigSharesManager::%s -- failed to recover signature. id=%s, msgHash=%s, time=%d\n", __func__,
865868
id.ToString(), msgHash.ToString(), t.count());
866-
return;
869+
return nullptr;
867870
}
868871

869872
LogPrint(BCLog::LLMQ_SIGS, "CSigSharesManager::%s -- recovered signature. id=%s, msgHash=%s, time=%d\n", __func__,
@@ -881,22 +884,10 @@ void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
881884
// this should really not happen as we have verified all signature shares before
882885
LogPrintf("CSigSharesManager::%s -- own recovered signature is invalid. id=%s, msgHash=%s\n", __func__,
883886
id.ToString(), msgHash.ToString());
884-
return;
887+
return nullptr;
885888
}
886889
}
887-
888-
if (sigman.ProcessRecoveredSig(rs)) {
889-
// TODO: remove duplicated code with NetSigning
890-
auto listeners = sigman.GetListeners();
891-
for (auto& l : listeners) {
892-
m_peerman.PostProcessMessage(l->HandleNewRecoveredSig(*rs));
893-
}
894-
895-
auto proactive_relay = rs->getLlmqType() != Consensus::LLMQType::LLMQ_100_67 &&
896-
rs->getLlmqType() != Consensus::LLMQType::LLMQ_400_60 &&
897-
rs->getLlmqType() != Consensus::LLMQType::LLMQ_400_85;
898-
GetMainSignals().NotifyRecoveredSig(rs, rs->GetHash().ToString(), proactive_relay);
899-
}
890+
return rs;
900891
}
901892

902893
CDeterministicMNCPtr CSigSharesManager::SelectMemberForRecovery(const CQuorum& quorum, const uint256 &id, int attempt)

src/llmq/signing_shares.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ class CSigSharesManager : public CRecoveredSigsListener
479479
EXCLUSIVE_LOCKS_REQUIRED(!cs);
480480

481481
void ProcessSigShare(const CSigShare& sigShare, const CQuorumCPtr& quorum) EXCLUSIVE_LOCKS_REQUIRED(!cs);
482-
void TryRecoverSig(const CQuorum& quorum, const uint256& id, const uint256& msgHash) EXCLUSIVE_LOCKS_REQUIRED(!cs);
482+
std::shared_ptr<CRecoveredSig> TryRecoverSig(const CQuorum& quorum, const uint256& id, const uint256& msgHash)
483+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
483484

484485
bool GetSessionInfoByRecvId(NodeId nodeId, uint32_t sessionId, CSigSharesNodeState::SessionInfo& retInfo)
485486
EXCLUSIVE_LOCKS_REQUIRED(!cs);

0 commit comments

Comments
 (0)