Skip to content
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

chore(sdk): various sdk fixes #2328

Draft
wants to merge 41 commits into
base: v1.8-dev
Choose a base branch
from
Draft

Conversation

QuantumExplorer
Copy link
Member

@QuantumExplorer QuantumExplorer commented Nov 13, 2024

Issue being fixed or feature implemented

What was done?

How Has This Been Tested?

Breaking Changes

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 added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Summary by CodeRabbit

  • New Features

    • Introduced a new BlsError variant for enhanced error handling related to BLS signatures.
    • Added new modules for handling various voting-related queries.
    • New JSON schema for withdrawal documents, enhancing data structure and validation.
  • Improvements

    • Enhanced BLS signature verification processes with specific implementations.
    • Updated error handling in several methods for improved clarity and robustness.
    • Streamlined handling of public and private keys, enforcing fixed-size array types for better type safety.
  • Bug Fixes

    • Improved error handling for public key conversions and signature verification, ensuring clearer error messages.
  • Documentation

    • Expanded test coverage for query functionalities and edge cases, particularly involving null values.

Copy link
Contributor

coderabbitai bot commented Nov 13, 2024

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request introduces a series of updates across multiple files, primarily focusing on enhancing the handling of BLS signatures and improving error management within the Dash Platform's codebase. Key changes include updates to dependency versions in Cargo.toml, modifications to the NativeBlsModule for public key and signature handling, and enhancements to various validator and quorum structures to utilize a specific BLS implementation. Additionally, several methods and structures have been adapted to improve type safety, particularly regarding private key representations and error handling mechanisms.

Changes

File Path Change Summary
packages/rs-dpp/Cargo.toml Updated dashcore dependency from 0.32.0 to 0.33.3, added serde feature, modified chrono and bincode dependencies, updated once_cell, and specified dev-dependencies.
packages/rs-dpp/src/bls/native_bls.rs Significant updates to public key validation, signature verification, private key handling, and signing methods, enhancing cryptographic operation specificity.
packages/rs-dpp/src/core_types/validator/mod.rs Updated BlsPublicKey type to BlsPublicKey<Bls12381G2Impl> in ValidatorV0Getters and ValidatorV0Setters methods.
packages/rs-dpp/src/core_types/validator/v0/mod.rs Changed public_key field type to BlsPublicKey<Bls12381G2Impl>, updated encoding and decoding methods for public keys.
packages/rs-dpp/src/core_types/validator_set/mod.rs Updated threshold_public_key type to BlsPublicKey<Bls12381G2Impl> in ValidatorSetV0Getters and ValidatorSetV0Setters.
packages/rs-dpp/src/core_types/validator_set/v0/mod.rs Modified threshold_public_key field type to BlsPublicKey<Bls12381G2Impl>, updated encoding and decoding methods.
packages/rs-dpp/src/errors/protocol_error.rs Added BlsError variant to ProtocolError enum for improved error handling related to BLS signatures.
packages/rs-dpp/src/identity/identity_public_key/key_type.rs Updated methods for handling BLS keys, changing return types to fixed-size arrays and enhancing error management.
packages/rs-dpp/src/identity/identity_public_key/methods/hash/mod.rs Introduced address method to IdentityPublicKeyHashMethodsV0, updated validate_private_key_bytes method to use fixed-size arrays.
packages/rs-dpp/src/identity/identity_public_key/methods/hash/v0/mod.rs Added address method to trait, modified validate_private_key_bytes method to use fixed-size arrays.
packages/rs-dpp/src/identity/identity_public_key/random.rs Updated return types in multiple methods to fixed-size arrays for private keys.
packages/rs-dpp/src/identity/identity_public_key/v0/methods/mod.rs Added address method, modified validate_private_key_bytes method to use fixed-size arrays.
packages/rs-dpp/src/identity/identity_public_key/v0/random.rs Changed return types in various methods to fixed-size arrays for private keys.
packages/rs-dpp/src/identity/random.rs Updated return types in methods to fixed-size arrays for private keys.
packages/rs-dpp/src/identity/v0/random.rs Updated return types in methods to fixed-size arrays for private keys.
packages/rs-dpp/src/lib.rs Modified public exports to include bls_signatures, changing from dashcore::bls_signatures to dashcore::blsful.
packages/rs-drive-abci/Cargo.toml Updated dashcore-rpc dependency from v0.15.8 to v0.15.12.
packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs Enhanced handling for operator identity updates, refined identity key management.
packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_quorum_info/v0/mod.rs Updated error handling for BlsPublicKey conversion.
packages/rs-drive-abci/src/execution/platform_events/core_chain_lock/choose_quorum/mod.rs Changed parameter type for quorums to BTreeMap<QuorumHash, BlsPublicKey<Bls12381G2Impl>>.
packages/rs-drive-abci/src/execution/platform_events/core_chain_lock/choose_quorum/v0/mod.rs Updated parameter and return types to BlsPublicKey<Bls12381G2Impl>.
packages/rs-drive-abci/src/execution/platform_events/core_chain_lock/verify_chain_lock_locally/v0/mod.rs Replaced G2Element with Signature type for signature verification.
packages/rs-drive-abci/src/execution/platform_events/core_instant_send_lock/verify_recent_signature_locally/v0/mod.rs Changed signature creation to use Signature<Bls12381G2Impl>.
packages/rs-drive-abci/src/execution/validation/state_transition/common/asset_lock/transaction/fetch_asset_lock_transaction_output_sync/v0/mod.rs Updated hash type handling in asset lock transaction processing.
packages/rs-drive-abci/src/mimic/mod.rs Updated block signing logic to use SignatureSchemes::Basic.
packages/rs-drive-abci/src/mimic/test_quorum.rs Updated ValidatorInQuorum and TestQuorumInfo to use Bls12381G2Impl.
packages/rs-drive-abci/src/platform_types/commit/mod.rs Changed public_key parameter type in verify_signature method to BlsPublicKey<Bls12381G2Impl>.
packages/rs-drive-abci/src/platform_types/commit/v0/mod.rs Updated verify_signature method to use Bls12381G2Impl.
packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs Restricted visibility of PlatformStateForSavingV0 struct and updated field types.
packages/rs-drive-abci/src/platform_types/platform_state/v0/old_structures/mod.rs Introduced new module for managing validator sets.
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/mod.rs Added V1 variant to SignatureVerificationQuorumSetForSaving enum.
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/v0/for_saving.rs Updated public_key field type in QuorumForSavingV0 to bls_signatures::PublicKey.
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/v0/for_saving_v1.rs Introduced SignatureVerificationQuorumSetForSavingV1 struct with conversion implementations.
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/v0/mod.rs Added new module for_saving_v1.
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/v0/quorums.rs Updated public_key field type in VerificationQuorum and modified signing logic.
packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs Updated public_key parameter type in NewValidatorIfMasternodeInState trait.
packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs Updated error handling logic for threshold_public_key comparisons.
packages/rs-drive-abci/src/query/document_query/v0/mod.rs Introduced logging statement in query_documents_v0 method.
packages/rs-drive-abci/src/query/system/current_quorums_info/v0/mod.rs Modified how threshold_public_key is constructed from the validator_set.
packages/rs-drive-abci/tests/strategy_tests/execution.rs Updated private key handling and signature generation processes.
packages/rs-drive-abci/tests/strategy_tests/main.rs Changed import from PrivateKey to SecretKey.
packages/rs-drive-abci/tests/strategy_tests/masternode_list_item_helpers.rs Updated private key generation process to use BlsPrivateKey.
packages/rs-drive-abci/tests/strategy_tests/masternodes.rs Modified private key generation and import statements.
packages/rs-drive-abci/tests/strategy_tests/query.rs Updated ProofVerification struct to use BlsPublicKey<Bls12381G2Impl>.
packages/rs-drive-proof-verifier/src/unproved.rs Enhanced error handling in maybe_from_unproved_with_metadata methods.
packages/rs-drive-proof-verifier/src/verify.rs Updated public key construction and signature handling processes.
packages/rs-drive/src/query/mod.rs Introduced new modules for voting and polling queries.
packages/rs-drive/tests/query_tests.rs Added new test cases for various query scenarios.
packages/rs-drive/tests/supporting_files/contract/withdrawals/withdrawals.json New JSON schema for withdrawal documents added.
packages/rs-sdk/Cargo.toml Updated dashcore-rpc dependency version.
packages/rs-sdk/src/core/dash_core_client.rs Added new error codes and enhanced RPC method error handling.
packages/rs-sdk/src/core/transaction.rs Removed debug logging from wait_for_asset_lock_proof_for_transaction method.
packages/rs-sdk/src/lib.rs Added public export for dapi_grpc module.
packages/simple-signer/Cargo.toml Updated dashcore-rpc dependency version.
packages/simple-signer/src/signer.rs Changed private key fields to fixed-size arrays and updated signing logic.

Possibly related PRs

  • feat(platform): get current quorum info  #2168: The changes in the main PR regarding updates to the Cargo.toml file and dependency management are related to the new RPC method getCurrentQuorumsInfo, which also involves handling quorum data, indicating a connection in terms of functionality and dependency management.
  • chore: move BLS Sigs import to Rust Dash Core #2252: The modifications in the main PR that involve the Cargo.toml file and the handling of BLS signatures are relevant to this PR, which moves the import of BLS signatures to the Rust Dash Core, reflecting a shift in how these dependencies are managed across the project.
  • feat(sdk)!: detect stale nodes #2254: The changes in the main PR regarding the handling of BLS signatures and their integration into the project are related to the detection of stale nodes, as both involve enhancements to the handling of cryptographic operations and error management.
  • fix: document query start to support pagination #2284: The updates in the main PR that improve error handling and functionality in document queries are connected to this PR, which focuses on enhancing pagination support and error handling in document queries, indicating a shared goal of improving query capabilities.

Poem

🐰 In the code where rabbits hop,
Changes made, we’ll never stop.
BLS signatures, now so bright,
With error handling, all feels right.
Dependencies updated, oh what a sight!
Hopping forward, coding through the night! 🌙


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

@QuantumExplorer QuantumExplorer marked this pull request as draft November 13, 2024 08:30
@lklimek lklimek changed the base branch from v1.6-dev to refactor/replaceBLSLibrary November 13, 2024 08:35
@lklimek lklimek self-assigned this Nov 13, 2024
@lklimek lklimek changed the title Test/test without span chore(sdk): various sdk fixes Nov 13, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments.

🛑 Comments failed to post (19)
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/mod.rs (1)

122-123: ⚠️ Potential issue

Fix incorrect documentation comment for V1 variant.

The documentation comment incorrectly states "Version 0" for the V1 variant.

Apply this fix:

-    /// Version 0 of the signature verification quorums
+    /// Version 1 of the signature verification quorums
     V1(SignatureVerificationQuorumSetForSavingV1),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    /// Version 1 of the signature verification quorums
    V1(SignatureVerificationQuorumSetForSavingV1),
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/v0/for_saving.rs (2)

165-168: 🛠️ Refactor suggestion

Consider proper error handling while maintaining compression efficiency

The use of to_compressed() for the public key is good for efficiency. However, similar to the previous conversion, the expect usage could be replaced with proper error handling.

Consider propagating the error while maintaining the compression:

-    public_key: bls_signatures::PublicKey::from_bytes(
-        &quorum.public_key.0.to_compressed(),
-    )
-    .expect("expected to convert between BLS key libraries (to chia)"),
+    public_key: bls_signatures::PublicKey::from_bytes(
+        &quorum.public_key.0.to_compressed(),
+    )
+    .map_err(|e| format!("Failed to convert BLS key to chia: {}", e))?,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

                public_key: bls_signatures::PublicKey::from_bytes(
                    &quorum.public_key.0.to_compressed(),
                )
                .map_err(|e| format!("Failed to convert BLS key to chia: {}", e))?,

148-151: 🛠️ Refactor suggestion

Consider proper error handling for BLS key conversion

The current implementation uses expect which could cause runtime panics. While the conversion between BLS libraries might be considered safe, it's generally better to handle potential errors gracefully.

Consider propagating the error instead:

-    public_key: dpp::bls_signatures::PublicKey::try_from(
-        quorum.public_key.to_bytes().as_slice(),
-    )
-    .expect("expected to convert between BLS key libraries (from chia)"),
+    public_key: dpp::bls_signatures::PublicKey::try_from(
+        quorum.public_key.to_bytes().as_slice(),
+    )
+    .map_err(|e| format!("Failed to convert BLS key from chia: {}", e))?,

Committable suggestion skipped: line range outside the PR's diff.

packages/rs-dpp/src/identity/random.rs (1)

69-70: 💡 Codebase verification

Breaking change detected - requires updates to test files

The change from Vec<u8> to [u8; 32] is a breaking change that affects multiple test files. Found several impacted locations that need to be updated:

  • packages/strategy-tests/src/lib.rs
  • packages/strategy-tests/src/transitions.rs
  • packages/rs-drive-abci/tests/strategy_tests/voting_tests.rs
  • packages/rs-drive-abci/tests/strategy_tests/strategy.rs
  • packages/rs-drive-abci/tests/strategy_tests/main.rs
  • packages/rs-dpp/src/state_transition/serialization.rs

All these files are currently using Vec<_> as the generic type parameter, which won't work with the new [u8; 32] constraint.

🔗 Analysis chain

Approve type constraint change for improved type safety.

The change from Vec<u8> to [u8; 32] for private keys is a good improvement as it:

  • Enforces exact key length at compile-time
  • Provides better memory efficiency with stack allocation
  • Prevents potential buffer overflow attacks

Let's verify the impact of this breaking change:


Let's verify the actual implementation to understand if this is a breaking change.


Let's try a different approach to check the implementation:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any existing implementations or usages that might be affected
rg "random_identity_with_main_keys_with_private_key|random_identities_with_private_keys_with_rng" --type rust

Length of output: 3723


Script:

#!/bin/bash
# Check the implementation of these methods to understand if Vec<u8> is still supported
ast-grep --pattern 'fn random_identity_with_main_keys_with_private_key<I>($$$)'

Length of output: 81


Script:

#!/bin/bash
# Check the content of the files containing these methods
rg -A 10 "fn random_identity_with_main_keys_with_private_key" --type rust

Length of output: 1771

packages/rs-sdk/src/core/transaction.rs (1)

72-78: 💡 Codebase verification

Based on the search results, I can see that tracing is extensively used throughout the codebase, particularly for error handling, debugging, and monitoring. The file under review is part of the transaction handling module where tracing is used consistently with different log levels (error, debug, trace) to provide observability.

Keep the tracing spans but consider adjusting log levels

The tracing spans in this code provide valuable transaction context for debugging. Instead of removing them completely:

  • Keep the transaction ID span as it's crucial for correlation
  • Consider changing debug! to trace! level if the goal is to reduce noise
  • Maintain consistency with the rest of the codebase which uses structured logging

Example locations showing similar patterns:

  • rs-sdk/src/core/transaction.rs: Uses tracing for error handling
  • rs-drive-abci/src/execution/: Uses spans for transaction tracing
  • rs-dapi-client/src/dapi_client.rs: Uses trace spans for request tracking
🔗 Analysis chain

Consider keeping structured logging for production debugging

While removing debug spans reduces noise during testing, completely removing transaction ID tracing might impact our ability to debug production issues. Consider:

  1. Keeping error-level tracing for critical paths
  2. Making tracing conditional using feature flags
  3. Using log levels appropriately (error, warn, info) instead of removing all tracing
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if other files in the codebase still use tracing
rg "tracing::(debug|error|warn|info|trace)" --type rust

Length of output: 37123

packages/rs-drive-abci/src/execution/platform_events/core_chain_lock/verify_chain_lock_locally/v0/mod.rs (1)

171-176: 🛠️ Refactor suggestion

Extract duplicate signature verification logic

The signature verification logic is duplicated. Consider extracting it into a helper function to improve maintainability and ensure consistent error handling.

fn verify_signature(
    signature: &Signature<Bls12381G2Impl>,
    public_key: &PublicKey<Bls12381G2Impl>,
    message_digest: &[u8],
) -> bool {
    match signature.verify(public_key, message_digest) {
        Ok(_) => true,
        Err(e) => {
            tracing::debug!("Signature verification failed: {}", e);
            false
        }
    }
}
packages/rs-drive-abci/tests/strategy_tests/masternodes.rs (1)

212-220: 🛠️ Refactor suggestion

Improve error handling in private key generation

The current implementation uses unwrap() which could panic if the key generation or conversion fails. Consider propagating errors using the ? operator and handling them appropriately.

Here's a suggested improvement:

-        let private_key_operator_bytes = bls_signatures::PrivateKey::generate_dash(rng)
-            .expect("expected to generate a private key")
-            .to_bytes()
-            .to_vec();
-        let private_key_operator = BlsPrivateKey::<Bls12381G2Impl>::from_be_bytes(
-            &private_key_operator_bytes.try_into().unwrap(),
-        )
-        .unwrap();
+        let private_key_operator_bytes = bls_signatures::PrivateKey::generate_dash(rng)
+            .map_err(|e| format!("Failed to generate private key: {}", e))?
+            .to_bytes()
+            .to_vec();
+        let private_key_operator = BlsPrivateKey::<Bls12381G2Impl>::from_be_bytes(
+            &private_key_operator_bytes.try_into()
+                .map_err(|_| "Invalid private key length")?
+        )
+        .map_err(|e| format!("Failed to create BLS private key: {}", e))?;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

        let private_key_operator_bytes = bls_signatures::PrivateKey::generate_dash(rng)
            .map_err(|e| format!("Failed to generate private key: {}", e))?
            .to_bytes()
            .to_vec();
        let private_key_operator = BlsPrivateKey::<Bls12381G2Impl>::from_be_bytes(
            &private_key_operator_bytes.try_into()
                .map_err(|_| "Invalid private key length")?
        )
        .map_err(|e| format!("Failed to create BLS private key: {}", e))?;
        let pub_key_operator = private_key_operator.public_key().0.to_compressed().to_vec();
packages/rs-dpp/src/identity/identity_public_key/random.rs (1)

141-141: 💡 Codebase verification

Based on the search results, I can now make a final determination. The results show:

  1. Multiple test files and implementations explicitly use Vec<_> when calling these methods
  2. The private keys are primarily used via as_slice() for signing operations
  3. The change affects both the core rs-dpp package and its dependents (wasm-dpp, rs-drive-abci)

Breaking change needs to be reverted or handled with version compatibility

The change from Vec<u8> to [u8; 32] is causing compatibility issues as evidenced by:

  • Multiple callers explicitly specify Vec<_> in their type parameters

  • Several packages in the ecosystem depend on the Vec<u8> interface

  • Existing code uses as_slice() for signing operations, which works with Vec<u8>

  • packages/rs-dpp/src/identity/identity_public_key/random.rs: Revert the return type change or implement version-based compatibility

  • packages/rs-drive-abci/tests/strategy_tests/*: Update test files to handle the new type or maintain backward compatibility

  • packages/wasm-dpp/src/**/*: Multiple files need updates to handle fixed-size arrays

The change should either:

  1. Keep Vec<u8> for backward compatibility
  2. Implement a version-based approach where new versions use [u8; 32]
  3. Add conversion methods to maintain compatibility with existing code
🔗 Analysis chain

Breaking change: Private key return type modified from Vec to [u8; 32]

The change from Vec<u8> to [u8; 32] for private keys improves type safety by enforcing a fixed length of 32 bytes, which aligns with common cryptographic standards. However, this is a breaking change that requires careful consideration.

Consider:

  1. Adding a version migration strategy
  2. Documenting this breaking change in CHANGELOG.md
  3. Updating all dependent code to handle fixed-size arrays

Also applies to: 179-179, 275-275, 321-321, 349-349, 387-387, 414-414, 445-445, 492-492, 521-521, 561-561, 590-590, 632-632, 659-659, 706-706, 723-723


Let me analyze the results and generate additional verification scripts to better understand the impact.

The first search shows multiple files using Vec<u8> for private keys, particularly in the wasm-dpp package. I want to verify how these keys are being used and if there are any direct dependencies on the vector nature of the private keys before making a final determination.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any direct usages of these methods that might be affected by the return type change

# Search for direct method calls that might need updates
rg -l "random_.*_key.*_with.*private_key" --type rust

# Search for any stored private keys to ensure they're compatible with 32 bytes
rg "private_key: Vec<u8>" --type rust

Length of output: 2115


Script:

#!/bin/bash
# Check if there are any direct usages of the return value from these random key generation methods
rg -A 3 "random_.*_key.*_with.*private_key" --type rust

# Check if there are any type conversions or transformations of private keys
rg "private_key\.(?:to_vec|into|as_slice)" --type rust

# Look for any test files that might be affected
rg "test.*random_.*_key" --type rust

Length of output: 15493

packages/rs-drive-abci/tests/strategy_tests/main.rs (1)

6-6: 💡 Codebase verification

Inconsistent usage of SecretKey vs PrivateKey found in the codebase

The verification reveals inconsistent usage of the BLS key terminology across the codebase:

  • Several files still use BlsPrivateKey type alias for SecretKey
  • Some files directly use SecretKey without the alias
  • JavaScript files in dashmate still use privateKey terminology

Key files needing alignment:

  • packages/rs-drive-abci/tests/strategy_tests/masternodes.rs
  • packages/rs-drive-abci/tests/strategy_tests/masternode_list_item_helpers.rs
  • packages/rs-drive-abci/src/mimic/test_quorum.rs
  • packages/dashmate/src/core/getBLSPublicKeyFromPrivateKeyHex.js
🔗 Analysis chain

LGTM! Verify the SecretKey usage across tests.

The import change from PrivateKey to SecretKey is consistent with modern cryptographic terminology and aligns with the broader updates across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all BLS-related code consistently uses SecretKey instead of PrivateKey
# Test: Search for any remaining PrivateKey usages that might need updating
rg -A 3 "PrivateKey.*Bls" 

# Test: Confirm SecretKey is used consistently
rg -A 3 "SecretKey.*Bls"

Length of output: 14745

packages/rs-drive-abci/src/platform_types/platform_state/v0/old_structures/mod.rs (1)

12-18: ⚠️ Potential issue

Use TryFrom instead of From for fallible conversions to properly handle errors

Currently, the implementations of the From trait for ValidatorSet, ValidatorSetV0, and ValidatorV0 use unwrap() on functions that can fail (PublicKey::try_from), which can cause panics if the conversion fails. Since these conversions can fail, it's appropriate to implement the TryFrom trait instead, allowing for proper error handling without panicking.

Here's how you might adjust the code:

  • Implement the TryFrom trait for these types.
  • Adjust function signatures to return Result<..., ...> types.
  • Handle errors appropriately in the conversion logic.

Example for ValidatorSetV0:

use std::convert::TryFrom;
use dpp::core_types::validator_set::v0::ValidatorSetV0 as DppValidatorSetV0;
use dpp::bls_signatures::PublicKey as DppPublicKey;
use bls_signatures::PublicKey as BlsPublicKey;

impl TryFrom<ValidatorSetV0> for DppValidatorSetV0 {
    type Error = SomeErrorType; // Define an appropriate error type

    fn try_from(value: ValidatorSetV0) -> Result<Self, Self::Error> {
        let ValidatorSetV0 {
            quorum_hash,
            quorum_index,
            core_height,
            members,
            threshold_public_key,
        } = value;

        let dpp_members = members
            .into_iter()
            .map(|(pro_tx_hash, validator)| {
                Ok((pro_tx_hash, validator.try_into()?))
            })
            .collect::<Result<_, _>>()?;

        let dpp_threshold_public_key = DppPublicKey::try_from(threshold_public_key.to_bytes().as_slice())
            .map_err(|e| /* map or return the error appropriately */)?;

        Ok(Self {
            quorum_hash,
            quorum_index,
            core_height,
            members: dpp_members,
            threshold_public_key: dpp_threshold_public_key,
        })
    }
}

Similarly, update the implementations for ValidatorSet and ValidatorV0 to use TryFrom and handle errors appropriately.

Also applies to: 36-57, 80-103

packages/simple-signer/src/signer.rs (1)

85-93: 🛠️ Refactor suggestion

Use specific error types for BLS key operations

In the BLS signing logic, consider replacing ProtocolError::Generic with the more specific ProtocolError::BlsError. This will improve error clarity and make it easier to handle BLS-related issues.

Apply the following changes to enhance error handling:

- .ok_or(ProtocolError::Generic(
-     "bls private key from bytes isn't correct".to_string(),
- ))?;
+ .ok_or(ProtocolError::BlsError(
+     "Invalid BLS private key bytes: failed to construct SecretKey".to_string(),
+ ))?;

...

- .map_err(|e| ProtocolError::Generic(format!("unable to sign {}", e)))?;
+ .map_err(|e| ProtocolError::BlsError(format!("Unable to sign data: {}", e)))?;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

                let pk = bls_signatures::SecretKey::<Bls12381G2Impl>::from_be_bytes(private_key)
                    .into_option()
                    .ok_or(ProtocolError::BlsError(
                        "Invalid BLS private key bytes: failed to construct SecretKey".to_string(),
                    ))?;
                let signature = pk
                    .sign(SignatureSchemes::Basic, data)
                    .map_err(|e| ProtocolError::BlsError(format!("Unable to sign data: {}", e)))?;
                Ok(signature.as_raw_value().to_compressed().to_vec().into())
packages/rs-dpp/src/signing.rs (1)

66-67: ⚠️ Potential issue

Avoid panics by handling errors from from_compressed

Using expect on from_compressed can cause a panic if the signature bytes are invalid. To ensure robustness, handle the error gracefully and return an appropriate validation result.

Apply this diff to handle the potential error:

-let g2 = <Bls12381G2Impl as Pairing>::Signature::from_compressed(&signature_bytes)
-    .expect("G2 projective");
+let g2 = match <Bls12381G2Impl as Pairing>::Signature::from_compressed(&signature_bytes) {
+    Ok(g2) => g2,
+    Err(e) => {
+        return SimpleConsensusValidationResult::new_with_error(
+            SignatureError::BasicBLSError(BasicBLSError::new(e.to_string())).into(),
+        );
+    }
+};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

                let g2 = match <Bls12381G2Impl as Pairing>::Signature::from_compressed(&signature_bytes) {
                    Ok(g2) => g2,
                    Err(e) => {
                        return SimpleConsensusValidationResult::new_with_error(
                            SignatureError::BasicBLSError(BasicBLSError::new(e.to_string())).into(),
                        );
                    }
                };
packages/rs-drive-abci/src/platform_types/signature_verification_quorum_set/v0/quorums.rs (2)

159-159: ⚠️ Potential issue

Use Debug formatting if Display is not implemented for public_key

The method to_string() requires ThresholdBlsPublicKey<Bls12381G2Impl> to implement the Display trait. If Display is not implemented, this will result in a compilation error. Consider using Debug formatting instead:

- .field("public_key", &self.public_key.to_string())
+ .field("public_key", &format!("{:?}", self.public_key))

Alternatively, implement the Display trait for ThresholdBlsPublicKey<Bls12381G2Impl> if a custom string representation is needed.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            .field("public_key", &format!("{:?}", self.public_key))

210-216: 🛠️ Refactor suggestion

Simplify error handling when deserializing the private key

The current error handling can be made more concise and idiomatic. Since from_be_bytes returns an Option<SecretKey<Bls12381G2Impl>>, you can use ok_or_else to provide the error:

 let private_key =
     bls_signatures::SecretKey::<Bls12381G2Impl>::from_be_bytes(&self.private_key)
-        .into_option()
-        .ok_or(Error::BLSError(
-            dpp::bls_signatures::BlsError::DeserializationError(
-                "Could not deserialize private key".to_string(),
-            ),
-        ))?;
+        .ok_or_else(|| Error::BLSError(
+            dpp::bls_signatures::BlsError::DeserializationError(
+                "Could not deserialize private key".to_string(),
+            ),
+        ))?;

This refactoring makes the code more readable and follows Rust's error handling best practices.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            let private_key =
                bls_signatures::SecretKey::<Bls12381G2Impl>::from_be_bytes(&self.private_key)
                    .ok_or_else(|| Error::BLSError(
                        dpp::bls_signatures::BlsError::DeserializationError(
                            "Could not deserialize private key".to_string(),
                        ),
                    ))?;
packages/rs-drive-abci/src/platform_types/commit/v0/mod.rs (1)

127-131: ⚠️ Potential issue

Avoid including raw signatures in error messages to prevent potential leakage of sensitive data.

Including the raw signature in error messages may expose sensitive information. Consider removing the signature from the error message or replacing it with a hash or an identifier.

Apply this diff to modify the error message:

                 Err(_) => ValidationResult::new_with_error(AbciError::BadCommitSignature(format!(
-                    "commit signature {} is wrong",
-                    signature
+                    "commit signature is invalid"
                 ))),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

        match signature.verify(&public_key, &hash) {
            Ok(_) => ValidationResult::default(),
            Err(_) => ValidationResult::new_with_error(AbciError::BadCommitSignature(format!(
                "commit signature is invalid"
packages/rs-sdk/src/core/dash_core_client.rs (2)

47-53: 🛠️ Refactor suggestion

Optimize the Fibonacci function to prevent stack overflow and improve efficiency

The recursive implementation of the Fibonacci function can cause stack overflows and is inefficient, even with a small MAX_RETRIES. Consider replacing it with an iterative implementation to enhance performance and reliability.

Apply this diff to refactor the function:

 fn fibonacci(n: u32) -> u64 {
-    match n {
-        0 => 0,
-        1 => 1,
-        _ => fibonacci(n - 1) + fibonacci(n - 2),
-    }
+    let mut a = 0;
+    let mut b = 1;
+    for _ in 0..n {
+        let temp = a;
+        a = b;
+        b = temp + b;
+    }
+    a
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

        fn fibonacci(n: u32) -> u64 {
            let mut a = 0;
            let mut b = 1;
            for _ in 0..n {
                let temp = a;
                a = b;
                b = temp + b;
            }
            a
        }

90-90: 🛠️ Refactor suggestion

Avoid using expect to prevent potential panics

Using final_result.expect("expected a final result") might cause a panic if final_result remains None. Although the logic suggests this shouldn't happen, it's safer to handle this case explicitly.

You can modify the code to return an error instead of panicking:

- final_result.expect("expected a final result")
+ match final_result {
+     Some(result) => result,
+     None => Err(ContextProviderError::Generic("Failed to retrieve result after retries".to_string())),
+ }

Committable suggestion skipped: line range outside the PR's diff.

packages/rs-drive-abci/src/mimic/test_quorum.rs (2)

283-283: 🛠️ Refactor suggestion

Encapsulate public_key access when setting quorum_public_key

Similar to the previous comment, avoid accessing the internal .0 field. Use the public interface provided by BlsPublicKey.

Apply this diff:

-                quorum_public_key: public_key.0.to_compressed().to_vec(),
+                quorum_public_key: public_key.to_compressed().to_vec(),

This ensures encapsulation and reduces the risk of future breakages.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            quorum_public_key: public_key.to_compressed().to_vec(),

272-272: 🛠️ Refactor suggestion

Avoid direct access to internal fields of public_key

Accessing the internal field .0 of public_key breaks encapsulation and may lead to maintenance issues if the internal structure changes. Use a public method to obtain the compressed representation.

Apply this diff to encapsulate access:

-                        pub_key_share: Some(public_key.0.to_compressed().to_vec()),
+                        pub_key_share: Some(public_key.to_compressed().to_vec()),

Ensure that the to_compressed() method is publicly accessible on the BlsPublicKey type.

Committable suggestion skipped: line range outside the PR's diff.

@lklimek lklimek changed the base branch from refactor/replaceBLSLibrary to v1.6-dev November 13, 2024 08:38
@lklimek lklimek changed the base branch from v1.6-dev to refactor/replaceBLSLibrary November 13, 2024 08:43
@lklimek lklimek mentioned this pull request Nov 15, 2024
6 tasks
@@ -131,6 +131,8 @@ impl<C> Platform<C> {
&self.config.drive,
));

println!("{:?}", drive_query);
Copy link
Member

Choose a reason for hiding this comment

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

Remove debug output

@@ -4662,6 +4662,8 @@ fn test_dpns_query_start_after_with_null_id() {
)
.expect("query should be built");

println!("{:?}", query);
Copy link
Member

Choose a reason for hiding this comment

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

Remove debug output

Base automatically changed from refactor/replaceBLSLibrary to v1.8-dev December 16, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants