Skip to content

Commit cf33efc

Browse files
committed
Move SelectQuorumForSigning into CSigningManager and make it height based
1 parent 4026ea2 commit cf33efc

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

src/llmq/quorums.cpp

-29
Original file line numberDiff line numberDiff line change
@@ -333,35 +333,6 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp
333333
return result;
334334
}
335335

336-
CQuorumCPtr CQuorumManager::SelectQuorum(Consensus::LLMQType llmqType, const uint256& selectionHash)
337-
{
338-
LOCK(cs_main);
339-
return SelectQuorum(llmqType, chainActive.Tip()->GetBlockHash(), selectionHash);
340-
}
341-
342-
CQuorumCPtr CQuorumManager::SelectQuorum(Consensus::LLMQType llmqType, const uint256& startBlock, const uint256& selectionHash)
343-
{
344-
auto& llmqParams = Params().GetConsensus().llmqs.at(llmqType);
345-
size_t poolSize = (size_t)llmqParams.signingActiveQuorumCount;
346-
347-
auto quorums = ScanQuorums(llmqType, startBlock, poolSize);
348-
if (quorums.empty()) {
349-
return nullptr;
350-
}
351-
352-
std::vector<std::pair<uint256, size_t>> scores;
353-
scores.reserve(quorums.size());
354-
for (size_t i = 0; i < quorums.size(); i++) {
355-
CHashWriter h(SER_NETWORK, 0);
356-
h << (uint8_t)llmqType;
357-
h << quorums[i]->quorumHash;
358-
h << selectionHash;
359-
scores.emplace_back(h.GetHash(), i);
360-
}
361-
std::sort(scores.begin(), scores.end());
362-
return quorums[scores.front().second];
363-
}
364-
365336
CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash)
366337
{
367338
AssertLockHeld(cs_main);

src/llmq/quorums.h

-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ class CQuorumManager
9999
CQuorumCPtr GetNewestQuorum(Consensus::LLMQType llmqType);
100100
std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount);
101101
std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, const uint256& startBlock, size_t maxCount);
102-
CQuorumCPtr SelectQuorum(Consensus::LLMQType llmqType, const uint256& selectionHash);
103-
CQuorumCPtr SelectQuorum(Consensus::LLMQType llmqType, const uint256& startBlock, const uint256& selectionHash);
104102

105103
private:
106104
void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexNew);

src/llmq/quorums_signing.cpp

+41-3
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,18 @@ bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, const uint
496496
db.WriteVoteForId(llmqType, id, msgHash);
497497
}
498498

499+
int tipHeight;
500+
{
501+
LOCK(cs_main);
502+
tipHeight = chainActive.Height();
503+
}
504+
499505
// This might end up giving different results on different members
500506
// This might happen when we are on the brink of confirming a new quorum
501507
// This gives a slight risk of not getting enough shares to recover a signature
502508
// But at least it shouldn't be possible to get conflicting recovered signatures
503509
// TODO fix this by re-signing when the next block arrives, but only when that block results in a change of the quorum list and no recovered signature has been created in the mean time
504-
CQuorumCPtr quorum = quorumManager->SelectQuorum(llmqType, id);
510+
CQuorumCPtr quorum = SelectQuorumForSigning(llmqType, tipHeight, id);
505511
if (!quorum) {
506512
LogPrintf("CSigningManager::%s -- failed to select quorum. id=%s, msgHash=%s\n", __func__, id.ToString(), msgHash.ToString());
507513
return false;
@@ -548,11 +554,43 @@ bool CSigningManager::IsConflicting(Consensus::LLMQType llmqType, const uint256&
548554
return false;
549555
}
550556

551-
bool CSigningManager::VerifyRecoveredSig(Consensus::LLMQType llmqType, const uint256& signedAtTip, const uint256& id, const uint256& msgHash, const CBLSSignature& sig)
557+
CQuorumCPtr CSigningManager::SelectQuorumForSigning(Consensus::LLMQType llmqType, int signHeight, const uint256& selectionHash)
558+
{
559+
auto& llmqParams = Params().GetConsensus().llmqs.at(llmqType);
560+
size_t poolSize = (size_t)llmqParams.signingActiveQuorumCount;
561+
562+
uint256 startBlock;
563+
{
564+
LOCK(cs_main);
565+
if (signHeight > chainActive.Height()) {
566+
return nullptr;
567+
}
568+
startBlock = chainActive[signHeight]->GetBlockHash();
569+
}
570+
571+
auto quorums = quorumManager->ScanQuorums(llmqType, startBlock, poolSize);
572+
if (quorums.empty()) {
573+
return nullptr;
574+
}
575+
576+
std::vector<std::pair<uint256, size_t>> scores;
577+
scores.reserve(quorums.size());
578+
for (size_t i = 0; i < quorums.size(); i++) {
579+
CHashWriter h(SER_NETWORK, 0);
580+
h << (uint8_t)llmqType;
581+
h << quorums[i]->quorumHash;
582+
h << selectionHash;
583+
scores.emplace_back(h.GetHash(), i);
584+
}
585+
std::sort(scores.begin(), scores.end());
586+
return quorums[scores.front().second];
587+
}
588+
589+
bool CSigningManager::VerifyRecoveredSig(Consensus::LLMQType llmqType, int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig)
552590
{
553591
auto& llmqParams = Params().GetConsensus().llmqs.at(Params().GetConsensus().llmqTypeForChainLocks);
554592

555-
auto quorum = quorumManager->SelectQuorum(llmqParams.type, signedAtTip, id);
593+
auto quorum = SelectQuorumForSigning(llmqParams.type, signedAtHeight, id);
556594
if (!quorum) {
557595
return false;
558596
}

src/llmq/quorums_signing.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ class CSigningManager
147147
bool HasRecoveredSigForSession(const uint256& signHash);
148148
bool IsConflicting(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash);
149149

150+
CQuorumCPtr SelectQuorumForSigning(Consensus::LLMQType llmqType, int signHeight, const uint256& selectionHash);
151+
150152
// Verifies a recovered sig that was signed while the chain tip was at signedAtTip
151-
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const uint256& signedAtTip, const uint256& id, const uint256& msgHash, const CBLSSignature& sig);
153+
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig);
152154
};
153155

154156
extern CSigningManager* quorumSigningManager;

0 commit comments

Comments
 (0)