Skip to content

fix: missing mutex for bls::operator== #6722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

knst
Copy link
Collaborator

@knst knst commented Jun 16, 2025

Issue being fixed or feature implemented

Somehow, CBLSLazyWrapper works correctly till now and does not cause any failures even CBLSLazyWrapper::operator==() doesn't have mutex inside. Let's fix it!

What was done?

Added mutex to operator== for CBLSWrapper; added ptrs comparision to operator== in case of compare A==A to avoid deadlock.

How Has This Been Tested?

Run unit / functional tests.

Breaking Changes

N/A

Checklist:

  • 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
  • I have assigned this pull request to a milestone

@knst knst added this to the 23 milestone Jun 16, 2025
@knst knst requested a review from PastaPastaPasta June 16, 2025 10:11
@knst
Copy link
Collaborator Author

knst commented Jun 16, 2025

potentially it is not really related changes, and each commit can go to it's own PR.
But so far I did performance measure once for all commits together.

I am open for suggestions to split it to several PRs.

Copy link

coderabbitai bot commented Jun 16, 2025

Walkthrough

The changes update several components related to BLS signature handling and deterministic masternode state management. In the BLS wrapper class, internal buffer initialization and synchronization logic are revised, including always copying buffers in the assignment operator, refining serialization and equality checks to improve thread safety, and removing the SetLegacy method. Correspondingly, deserialization logic in the deterministic masternode state diff class is updated to eliminate calls to the now-removed SetLegacy method. Additionally, in the signature shares manager, redundant local variable usage is removed when handling single-node quorum signature recovery, simplifying the code. No public API changes are introduced except for the removal of SetLegacy.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee34525 and 52abb92.

📒 Files selected for processing (3)
  • src/bls/bls.h (5 hunks)
  • src/evo/dmnstate.h (0 hunks)
  • src/llmq/signing_shares.cpp (1 hunks)
💤 Files with no reviewable changes (1)
  • src/evo/dmnstate.h
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: linux64-build / Build source
  • GitHub Check: linux64_tsan-build / Build source
  • GitHub Check: linux64_multiprocess-build / Build source
  • GitHub Check: mac-build / Build source
  • GitHub Check: arm-linux-build / Build source
  • GitHub Check: linux64_nowallet-build / Build source
🔇 Additional comments (4)
src/bls/bls.h (3)

393-396: vecBytes now default-initialised – good call
Zero-initialising the backing buffer removes a harmless UB corner-case during memcmp/ranges::all_of checks.


510-528: Equality operator safer, but pay attention to Get() after unlock
Using std::scoped_lock eliminates the previously missing synchronisation – nice.
Minor: Get() is called after releasing both locks; if another thread mutates either object between unlock and Get(), equality might flip. Usually harmless, but if strict consistency is required keep the lock until the final comparison.


479-486: Set() now wipes the stale buffer
Clearing vecBytes after assigning a fresh object avoids accidental legacy re-serialisation – good improvement.

src/llmq/signing_shares.cpp (1)

790-796: Constructor now receives CBLSLazySignature – check overloads
Previously a temporary CBLSSignature was passed; now sigShare.sigShare (type CBLSLazySignature) goes directly to CRecoveredSig.
If CRecoveredSig only has a ctor taking CBLSSignature, this won’t compile.
Please confirm an overload or implicit conversion exists; otherwise re-introduce:

auto rs = std::make_shared<CRecoveredSig>(…, sigShare.sigShare.Get());
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

UdjinM6
UdjinM6 previously approved these changes Jun 16, 2025
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK 52abb92

@@ -255,7 +255,6 @@ class CDeterministicMNStateDiff

if (read_pubkey) {
SER_READ(obj, obj.fields |= Field_nVersion);
SER_READ(obj, obj.state.pubKeyOperator.SetLegacy(obj.state.nVersion == ProTxVersion::LegacyBLS));
Copy link
Member

Choose a reason for hiding this comment

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

how does this not break stuff? Also, seems unrelated to "missing mutex"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's strange, that it indeed breaks stuff (see Udjin's comment).

Un-serialization of CBLSLazyWrapper set legacy flag and second call of SetLegacy looks llke double work

    template<typename Stream>
    inline void Unserialize(Stream& s, const bool specificLegacyScheme) const
    {   
        std::unique_lock<std::mutex> l(mutex);
        s.read(AsWritableBytes(Span{vecBytes.data(), BLSObject::SerSize}));
        bufValid = std::any_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c != 0; });
        bufLegacyScheme = specificLegacyScheme;
        objInitialized = false;
        hash.SetNull();
    }

once it's deserialized, proper flag should set already:

                    READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.state.pubKeyOperator), obj.state.nVersion == ProTxVersion::LegacyBLS));

That's strange.

I removed all non-related commits from PR

src/bls/bls.h Outdated
Comment on lines 429 to 430
} else {
obj.Reset();
Copy link
Member

Choose a reason for hiding this comment

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

why drop this?

src/bls/bls.h Outdated
Comment on lines 438 to 449
if (!objInitialized) {
if (bufValid && bufLegacyScheme != specificLegacyScheme) {
obj.SetBytes(vecBytes, bufLegacyScheme);
vecBytes = obj.ToBytes(specificLegacyScheme);
}
} else {
if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
vecBytes = obj.ToBytes(specificLegacyScheme);
bufValid = true;
bufLegacyScheme = specificLegacyScheme;
hash.SetNull();
}
Copy link
Member

Choose a reason for hiding this comment

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

why re-work this?

@UdjinM6 UdjinM6 dismissed their stale review June 17, 2025 05:45

protx listdiff 850325 850326

check updatedMNs

develop:

{
  "892048b276a248b44e0e0c498fe0133e19a9a19ff03d2a6201779759a9e597fc": {
    "version": 2,
    "pubKeyOperator": "a7676e9a8ef4eaafcf47451801388500aaa1c1994c5df1619eb3b54b83dfab28c7969b262454c0397fe6fc14dc8c62d9"
  }
},

this PR:

{
"892048b276a248b44e0e0c498fe0133e19a9a19ff03d2a6201779759a9e597fc": {
"version": 2,
"pubKeyOperator": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
},

@UdjinM6
Copy link

UdjinM6 commented Jun 17, 2025

Let's keep it focused maybe and keep a488c8d only

Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK a488c8d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants