Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/llmq/signing_shares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,11 @@ std::shared_ptr<CRecoveredSig> CSigSharesManager::TryRecoverSig(const CQuorum& q
return nullptr;
}

std::vector<CBLSSignature> sigSharesForRecovery;
// For the multi-member case, collect lazy signatures under the lock and defer the expensive
// BLS deserialization (Get) until after the lock is released to minimize time spent holding cs.
std::vector<CBLSLazySignature> lazySignatures;
std::vector<CBLSId> idsForRecovery;

{
LOCK(cs);

Expand All @@ -646,8 +649,8 @@ std::shared_ptr<CRecoveredSig> CSigSharesManager::TryRecoverSig(const CQuorum& q
return nullptr;
}

std::shared_ptr<CRecoveredSig> singleMemberRecoveredSig;
if (quorum.params.is_single_member()) {
// Single share means a single Get(); no contention benefit to deferring it, so recover inline.
if (sigSharesForSignHash->empty()) {
LogPrint(BCLog::LLMQ_SIGS, /* Continued */
"CSigSharesManager::%s -- impossible to recover single-node signature - no shares yet. id=%s, "
Expand All @@ -656,29 +659,34 @@ std::shared_ptr<CRecoveredSig> CSigSharesManager::TryRecoverSig(const CQuorum& q
return nullptr;
}
const auto& sigShare = sigSharesForSignHash->begin()->second;
CBLSSignature recoveredSig = sigShare.sigShare.Get();
LogPrint(BCLog::LLMQ_SIGS, "CSigSharesManager::%s -- recover single-node signature. id=%s, msgHash=%s\n",
__func__, id.ToString(), msgHash.ToString());

singleMemberRecoveredSig = std::make_shared<CRecoveredSig>(quorum.params.type, quorum.qc->quorumHash, id, msgHash,
recoveredSig);
return std::make_shared<CRecoveredSig>(quorum.params.type, quorum.qc->quorumHash, id, msgHash,
sigShare.sigShare.Get());
}

sigSharesForRecovery.reserve((size_t) quorum.params.threshold);
// Collect lazy signatures and IDs under the lock (cheap); materialize them outside.
lazySignatures.reserve((size_t) quorum.params.threshold);
idsForRecovery.reserve((size_t) quorum.params.threshold);
for (auto it = sigSharesForSignHash->begin(); it != sigSharesForSignHash->end() && sigSharesForRecovery.size() < size_t(quorum.params.threshold); ++it) {
for (auto it = sigSharesForSignHash->begin();
it != sigSharesForSignHash->end() && lazySignatures.size() < size_t(quorum.params.threshold);
++it) {
const auto& sigShare = it->second;
sigSharesForRecovery.emplace_back(sigShare.sigShare.Get());
lazySignatures.emplace_back(sigShare.sigShare); // Collect lazy wrapper, defer BLS deserialization
idsForRecovery.emplace_back(quorum.members[sigShare.getQuorumMember()]->proTxHash);
}

// check if we can recover the final signature
if (sigSharesForRecovery.size() < size_t(quorum.params.threshold)) {
if (lazySignatures.size() < size_t(quorum.params.threshold)) {
return nullptr;
}
if (quorum.params.is_single_member()) {
return singleMemberRecoveredSig; // end of single-quorum processing
}
} // Release lock before expensive materialization

// Materialize signatures outside the critical section (expensive BLS operations)
std::vector<CBLSSignature> sigSharesForRecovery;
sigSharesForRecovery.reserve(lazySignatures.size());
for (const auto& lazySig : lazySignatures) {
sigSharesForRecovery.emplace_back(lazySig.Get()); // Expensive, but outside lock

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is copy of bls signature indeed that expensive?

Maybe there's a bug in our bls's wrapper which calls "is-valid" or "serialize&deserialize" for each constructor copy?

Just copy of 500bytes are not that expensive; I think we should improve performance of our bls-wrapper rather than do this refactoring... Do I miss anything?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a copy; look into the implementation of .Get()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See:

dash/src/bls/bls.h

Lines 488 to 508 in a488c8d

const BLSObject& Get() const
{
std::unique_lock<std::mutex> l(mutex);
static BLSObject invalidObj;
if (!bufValid && !objInitialized) {
return invalidObj;
}
if (!objInitialized) {
obj.SetBytes(vecBytes, bufLegacyScheme);
if (!obj.IsValid()) {
bufValid = false;
return invalidObj;
}
if (!obj.CheckMalleable(vecBytes, bufLegacyScheme)) {
bufValid = false;
return invalidObj;
}
objInitialized = true;
}
return obj;
}

I'd love ways to increase performance here, but it is quite expensive as is.

}

// now recover it
Expand Down
Loading