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

fix(platform): fixed Platform State deserialization issue #2227

Merged
merged 5 commits into from
Oct 8, 2024

Conversation

QuantumExplorer
Copy link
Member

@QuantumExplorer QuantumExplorer commented Oct 8, 2024

Issue being fixed or feature implemented

Because of how PlatformStateForSerialization was serialized it would store the fee version. Since fee version can include new fields this was a big no no.

What was done?

Modified PlatformState and introduced a new PlatformStateForSerializationV1 that fixes the issue and also serializes more efficiently.
We also changed validator sets to be serialized with Bincode instead of Serde.

How Has This Been Tested?

Ran tests.

Breaking Changes

Fixing an issue, not breaking.

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

Release Notes

  • New Features

    • Enhanced fee version management across various modules, improving efficiency and clarity.
    • Introduced a new variant for platform state management, expanding versioning capabilities.
  • Bug Fixes

    • Improved type safety and clarity in encoding and decoding processes for ValidatorV0 and ValidatorSetV0 structs.
  • Tests

    • Added new test modules to validate serialization and deserialization processes, ensuring data integrity for ValidatorV0, ValidatorSetV0, and platform state structures.
  • Documentation

    • Updated comments and naming conventions for clarity, particularly related to platform state structures.

Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

Walkthrough

The pull request introduces several modifications across multiple modules, focusing on enhancing the encoding and decoding processes for various structs, specifically ValidatorV0, ValidatorSetV0, and fee-related structures. Key changes include the transition from vector-based to fixed-size array representations for certain fields, the introduction of new types for fee version management, and the addition of test modules to ensure data integrity. Additionally, several naming conventions have been updated for clarity, particularly regarding platform state structures and fee versions.

Changes

File Path Change Summary
packages/rs-dpp/src/core_types/validator/v0/mod.rs Updated ValidatorV0 struct's encoding/decoding for pro_tx_hash, node_id, and public_key. Added test module mod tests with test_serialize_deserialize_validator_v0().
packages/rs-dpp/src/core_types/validator_set/v0/mod.rs Modified ValidatorSetV0 struct's encoding/decoding for quorum_hash and threshold_public_key. Added test module with test_serialize_deserialize_validator_set_v0().
packages/rs-dpp/src/fee/default_costs/mod.rs Changed CachedEpochIndexFeeVersions type to hold references. Introduced EpochIndexFeeVersionsForStorage and CachedEpochIndexFeeVersionsFieldsBeforeVersion4. Updated active_fee_version method signature.
packages/rs-drive-abci/src/platform_types/platform_state/mod.rs Introduced V1(PlatformStateForSavingV1) variant in PlatformStateForSaving. Removed deprecated method current_protocol_version_in_consensus.
packages/rs-drive-abci/src/test/fixture/mod.rs Added new module platform_state.

Possibly related PRs

Suggested reviewers

  • shumkov
  • lklimek

Poem

🐇 In the meadow where bunnies play,
New structs and tests come out to stay.
With fees and versions all in line,
Our code hops forward, oh so fine!
Encoding, decoding, we cheer with glee,
For a brighter future, just wait and see! 🌼


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.

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.

Actionable comments posted: 12

🧹 Outside diff range and nitpick comments (18)
packages/rs-dpp/src/fee/fee_result/refunds.rs (1)

188-189: LGTM: Test setup updated correctly with a minor suggestion.

The initialization of EPOCH_CHANGE_FEE_VERSION_TEST has been updated to use FeeVersion::first(), which is consistent with the import change and the shift to FeeVersion. This modification simplifies the code while maintaining the same functionality.

Consider updating the variable name to reflect the new type:

-static EPOCH_CHANGE_FEE_VERSION_TEST: Lazy<CachedEpochIndexFeeVersions> =
+static EPOCH_CHANGE_FEE_VERSIONS_TEST: Lazy<CachedEpochIndexFeeVersions> =
     Lazy::new(|| BTreeMap::from([(0, FeeVersion::first())]));

This change would make the variable name more accurate and consistent with its new content.

packages/rs-dpp/src/core_types/validator/v0/mod.rs (2)

255-296: Excellent addition of serialization/deserialization test

The new test module with the test_serialize_deserialize_validator_v0 function is a valuable addition to ensure the correctness of the ValidatorV0 serialization and deserialization process. The test comprehensively covers all fields of the ValidatorV0 struct and verifies that the deserialized instance matches the original, which is crucial for maintaining data integrity.

To further enhance the test coverage, consider adding the following:

  1. Test cases with edge values (e.g., empty strings, minimum/maximum port numbers).
  2. A test case where public_key is None to ensure proper handling of optional fields.
  3. Explicit checks for individual field values after deserialization, in addition to the overall struct equality check.

Would you like me to provide example code for these additional test cases?


Line range hint 1-296: Consistent with PR objectives and summary

The changes in this file align well with the PR objectives of fixing a Platform State deserialization issue. The modifications to the Encode and Decode implementations for ValidatorV0, along with the new test case, directly address potential deserialization problems and ensure data integrity.

To further improve the code:

Consider adding a brief comment at the top of the file or above the ValidatorV0 struct explaining its role in the Platform State and how these changes contribute to resolving the deserialization issue mentioned in the PR title. This would provide valuable context for future developers working with this code.

Would you like me to provide an example of such a comment?

packages/rs-platform-version/src/version/drive_abci_versions.rs (1)

77-81: Consider adding Rust doc comments

To improve code documentation and maintainability, consider adding Rust doc comments to the DriveAbciStructureVersions struct and its fields. This will provide better context and understanding for developers working with this code in the future.

Example:

/// Represents the versions of various Drive ABCI structures
pub struct DriveAbciStructureVersions {
    /// Version of the platform state structure
    pub platform_state_structure: FeatureVersion,
    /// Default version of the platform state structure for saving
    pub platform_state_for_saving_structure_default: FeatureVersion,
    // ... (add comments for other fields)
}
packages/rs-platform-version/src/version/v3.rs (1)

612-612: Approved: Field renaming for clarity

The change from platform_state_for_saving_structure to platform_state_for_saving_structure_default improves clarity by explicitly indicating that this is a default value. This is a good practice for maintaining clear and self-documenting code.

Consider updating any related documentation or comments to reflect this change and explain the significance of the "_default" suffix if it's not already clear in the surrounding context.

packages/rs-platform-version/src/version/fee/processing/mod.rs (1)

18-22: Improve clarity of the deserialization issue comments

The comments explaining the purpose of FeeProcessingVersionFieldsBeforeVersion1Point4 could be clearer for future maintainers. Consider rephrasing them for better understanding.

Apply this diff to enhance the comments:

-// This is type only meant for deserialization because of an issue
-// The issue was that the platform state was stored with FeeVersions in it before version 1.4
-// When we would add new fields we would be unable to deserialize
-// This FeeProcessingVersionFieldsBeforeVersion4 is how things were before version 1.4 was released
+// This struct is only meant for deserialization due to a compatibility issue.
+// Prior to version 1.4, the platform state was stored with FeeVersions without the `perform_network_threshold_signing` field.
+// Adding new fields caused deserialization failures.
+// `FeeProcessingVersionFieldsBeforeVersion1Point4` represents the old struct layout before version 1.4.
packages/rs-platform-version/src/version/fee/mod.rs (1)

41-74: Consider handling empty FEE_VERSIONS gracefully

In the first and latest methods, using expect will cause a panic if FEE_VERSIONS is empty. Although it's expected to have at least one fee version, consider adding a more graceful error handling or a descriptive error message to prevent potential runtime panics.

packages/rs-dpp/src/core_types/validator_set/v0/mod.rs (3)

333-335: Handle potential decoding errors in tests

While using .unwrap() is acceptable in tests, providing a clear error message with .expect() can improve test diagnostics if the decoding fails unexpectedly.

Apply this diff to enhance error handling:

             // Deserialize the data back into a ValidatorSetV0 instance
             let decoded: ValidatorSetV0 = bincode::decode_from_slice(&encoded, config::standard())
-                .unwrap()
+                .expect("Failed to decode ValidatorSetV0")
                 .0;

Line range hint 99-126: Ensure consistent handling of decoding errors

In the Decode implementation, consider providing more detailed error messages or handling specific error cases to aid in debugging if decoding fails.

For example, when decoding ProTxHash and BlsPublicKey, you can include the underlying error message or context.

Modify the error handling as follows:

                 let key = ProTxHash::from_slice(&key_bytes).map_err(|e| {
                     bincode::error::DecodeError::OtherString(format!(
-                        "Failed to decode ProTxHash"
+                        "Failed to decode ProTxHash: {}",
+                        e
                     ))
                 })?;

And similarly for threshold_public_key:

             let threshold_public_key = BlsPublicKey::from_bytes(&bytes).map_err(|e| {
                 bincode::error::DecodeError::OtherString(format!(
-                    "Failed to decode BlsPublicKey"
+                    "Failed to decode BlsPublicKey: {}",
+                    e
                 ))
             })?;

140-141: Remove redundant comments

The comments at lines 140-141 duplicate information that is already clear from the code. Removing them can improve code readability.

Apply this diff:

-            // Decode the quorum hash directly as a [u8; 32] array
             let quorum_hash = <[u8; 32]>::decode(decoder)?;
-            // Decode quorum_index as Option<u32>
             let quorum_index = Option::<u32>::decode(decoder)?;
packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/tests.rs (6)

Line range hint 54-58: Remove unused variable altered_document

The variable altered_document is created and modified but never used within the setup_join_contract_document function. Removing it will improve code clarity and reduce unnecessary code.

Apply this diff to remove the unused code:

@@ -54,6 +54,0 @@
-    let mut altered_document = document.clone();
-
-    altered_document.increment_revision().unwrap();
-    altered_document.set("displayName", "Ivan".into());
-    altered_document.set("avatarUrl", "http://test.com/dog.jpg".into());

Line range hint 128-133: Remove unused variable altered_document

The variable altered_document is created and modified but never used within the setup_initial_document function. Removing it will enhance code readability and eliminate unnecessary operations.

Apply this diff to remove the unused code:

@@ -128,6 +128,0 @@
-    let mut altered_document = document.clone();
-
-    altered_document.increment_revision().unwrap();
-    altered_document.set("displayName", "Samuel".into());
-    altered_document.set("avatarUrl", "http://test.com/cat.jpg".into());

Line range hint 303-307: Clarify refund percentage calculation in assertions

The assertions for refund amounts in test_document_refund_immediate compare the refund to be more than 99% and less than 100% of the insertion fee. Consider calculating the exact expected refund percentage to make the test assertions clearer and more precise.

You could update the assertions as follows:

-assert!(refund_amount > lower_bound, "expected the refund amount to be more than 99% of the storage cost, as it is for just one out of 2000 epochs");
-assert!(
-    refund_amount < insertion_fee_result.storage_fee,
-    "expected the refund amount to be less than the insertion cost"
-);
+let expected_refund = insertion_fee_result.storage_fee * refund_factor / 100;
+assert_eq!(
+    refund_amount, expected_refund,
+    "expected the refund amount to be exactly {}% of the storage cost", refund_factor
+);

Line range hint 247-248: Avoid magic numbers for identity setup

The value 958 used in setup_identity_with_system_credits may not be immediately clear. Consider defining a constant or adding a comment to explain its significance.

For example:

// 958 is the unique identity index for this test
let (identity, signer, key) =
    setup_identity_with_system_credits(&mut platform, 958, dash_to_credits!(1));

Line range hint 486-490: Consider edge case handling for zero refunds

In test_document_refund_after_50_years, the refund amount is expected to be zero. It's good to assert this explicitly, but also consider handling any potential edge cases where floating-point precision or integer division could affect the refund calculation.

Ensure that the refund calculation correctly handles long durations without unexpected rounding errors.


Line range hint 243-244: Redundant platform state loading

The platform_state is loaded but not used before being reloaded later in the tests. Consider removing the redundant loading to streamline the code.

Apply this diff to remove the unused variable:

-    let platform_state = platform.state.load();
packages/rs-drive/tests/query_tests.rs (2)

Line range hint 149-183: Refactor setup_family_tests_with_nulls to Eliminate Code Duplication

The setup_family_tests_with_nulls function shares significant similarities with setup_family_tests. To adhere to the DRY (Don't Repeat Yourself) principle, consider refactoring the common code into a shared helper function or parameterizing the differences. This will enhance maintainability and reduce potential errors from duplicated code.


Line range hint 184-221: Refactor setup_family_tests_only_first_name_index to Reduce Redundancy

Similar to the previous function, setup_family_tests_only_first_name_index duplicates much of the logic from setup_family_tests. Refactoring common functionality into a shared utility function or using parameters to handle variations can improve code readability and maintenance.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5aed75a and e11bfa5.

📒 Files selected for processing (23)
  • packages/rs-dpp/src/core_types/validator/v0/mod.rs (5 hunks)
  • packages/rs-dpp/src/core_types/validator_set/v0/mod.rs (7 hunks)
  • packages/rs-dpp/src/fee/default_costs/mod.rs (3 hunks)
  • packages/rs-dpp/src/fee/fee_result/refunds.rs (1 hunks)
  • packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/tests.rs (1 hunks)
  • packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/upgrade_protocol_version/v0/mod.rs (1 hunks)
  • packages/rs-drive-abci/src/platform_types/platform_state/mod.rs (4 hunks)
  • packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs (4 hunks)
  • packages/rs-drive/src/drive/document/delete/mod.rs (1 hunks)
  • packages/rs-drive/src/drive/document/insert/mod.rs (1 hunks)
  • packages/rs-drive/src/drive/document/update/mod.rs (1 hunks)
  • packages/rs-drive/tests/query_tests.rs (3 hunks)
  • packages/rs-drive/tests/query_tests_history.rs (3 hunks)
  • packages/rs-platform-version/src/version/drive_abci_versions.rs (1 hunks)
  • packages/rs-platform-version/src/version/fee/mod.rs (3 hunks)
  • packages/rs-platform-version/src/version/fee/processing/mod.rs (2 hunks)
  • packages/rs-platform-version/src/version/fee/v1.rs (1 hunks)
  • packages/rs-platform-version/src/version/mocks/v2_test.rs (1 hunks)
  • packages/rs-platform-version/src/version/mocks/v3_test.rs (1 hunks)
  • packages/rs-platform-version/src/version/v1.rs (1 hunks)
  • packages/rs-platform-version/src/version/v2.rs (1 hunks)
  • packages/rs-platform-version/src/version/v3.rs (1 hunks)
  • packages/rs-platform-version/src/version/v4.rs (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • packages/rs-platform-version/src/version/mocks/v2_test.rs
  • packages/rs-platform-version/src/version/mocks/v3_test.rs
  • packages/rs-platform-version/src/version/v1.rs
🧰 Additional context used
📓 Learnings (1)
packages/rs-platform-version/src/version/drive_abci_versions.rs (1)
Learnt from: shumkov
PR: dashpay/platform#2182
File: packages/rs-platform-version/src/version/drive_abci_versions.rs:116-121
Timestamp: 2024-09-29T13:13:54.230Z
Learning: Adding Rust doc comments to structs and fields is recommended to enhance clarity and documentation.
🔇 Additional comments (38)
packages/rs-platform-version/src/version/fee/v1.rs (1)

11-11: LGTM! Verify related code for consistency.

The addition of the fee_version_number field is a good practice for versioning and its value is consistent with the constant name FEE_VERSION1. This change enhances the clarity and maintainability of the fee versioning system.

Please ensure that:

  1. The FeeVersion struct definition has been updated to include this new field.
  2. Any code that constructs or uses FeeVersion instances has been updated accordingly.
  3. This change is part of a broader versioning strategy for fee-related structures.

Run the following script to verify the FeeVersion struct definition and its usage:

✅ Verification successful

FeeVersion struct and its usage have been successfully verified.

The fee_version_number field has been correctly added to the FeeVersion struct in mod.rs, and all instances in v1.rs have been updated accordingly.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify FeeVersion struct definition and usage

# Test 1: Check FeeVersion struct definition
echo "Checking FeeVersion struct definition:"
rg --type rust "struct FeeVersion" -A 10

# Test 2: Check FeeVersion usage
echo "\nChecking FeeVersion usage:"
rg --type rust "FeeVersion\s*[{=]" -A 5

Length of output: 5626

packages/rs-dpp/src/fee/fee_result/refunds.rs (2)

185-185: LGTM: Import statement updated correctly.

The import statement has been updated to use FeeVersion from platform_version::version::fee, which is consistent with the changes in the test setup. This modification aligns with the shift from PlatformVersion to FeeVersion for fee version management.


185-189: Verify consistency of FeeVersion changes across the codebase.

The changes in this file are part of a larger refactoring effort to use FeeVersion instead of PlatformVersion. To ensure consistency and completeness of this refactoring:

Run the following script to check for any remaining instances of PlatformVersion related to fee calculations:

This script will help identify any remaining instances of PlatformVersion that might need to be updated to FeeVersion, ensuring consistency across the codebase.

packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/upgrade_protocol_version/v0/mod.rs (1)

102-104: Improved fee version comparison logic

The changes to the fee version comparison are well-implemented and offer several benefits:

  1. Enhanced efficiency: By comparing fee_version_number directly instead of cloning the entire fee version, unnecessary memory allocation is avoided.
  2. Improved readability: The comparison is now more explicit, making the intention clearer.
  3. Maintained functionality: The logic still ensures that a new fee version is only inserted when it differs from the last one.

These modifications contribute to better performance and code quality without altering the overall behavior of the method.

Also applies to: 107-107, 114-114

packages/rs-dpp/src/core_types/validator/v0/mod.rs (3)

50-51: Optimization: Improved encoding for pro_tx_hash and node_id

The changes from to_byte_array().to_vec() to as_byte_array() for pro_tx_hash and node_id encoding are good optimizations. This modification avoids unnecessary allocation by returning a reference to the underlying byte array instead of creating a new Vec<u8>. This improvement enhances performance and reduces memory usage during the encoding process.

Also applies to: 68-69


89-96: Enhanced type safety: Use of fixed-size arrays in decoding

The changes to use fixed-size arrays ([u8; 32] for pro_tx_hash, [u8; 48] for public_key, and [u8; 20] for node_id) instead of Vec<u8> in the decoding process are excellent improvements. These modifications offer several benefits:

  1. Improved type safety: The use of fixed-size arrays ensures that the decoded data always has the correct size, preventing potential issues with incorrect data lengths.
  2. Better alignment with cryptographic practices: Fixed-size arrays are preferred over dynamic-sized vectors when handling cryptographic primitives.
  3. Potential performance improvements: Fixed-size arrays can lead to better performance as they are stack-allocated and have a known size at compile-time.

These changes contribute to a more robust and reliable decoding process.

Also applies to: 108-110


Line range hint 1-296: Well-structured and complete implementation

The overall structure and completeness of the file are commendable. The changes focus on improving the Encode and Decode implementations for ValidatorV0, with the addition of a comprehensive test. These modifications are consistent and well-integrated with the existing code.

The file maintains a clear organization with separate trait implementations for Encode, Decode, Debug, ValidatorV0Getters, and ValidatorV0Setters. This separation of concerns enhances readability and maintainability.

The unchanged parts of the file, including the struct definition and other trait implementations, appear to be complete and well-structured. There are no apparent inconsistencies or missing implementations.

packages/rs-platform-version/src/version/drive_abci_versions.rs (2)

80-80: LGTM: Field renamed for clarity

The renaming of platform_state_for_saving_structure to platform_state_for_saving_structure_default improves clarity by indicating that this field represents a default value. This change enhances code readability and understanding.


80-80: Verify impact of field renaming

The renaming of platform_state_for_saving_structure to platform_state_for_saving_structure_default might affect other parts of the codebase that reference this field. Let's verify the impact of this change.

✅ Verification successful

Verified: Field Renaming Impact

The renaming of platform_state_for_saving_structure to platform_state_for_saving_structure_default has been successfully reflected across the codebase. No references to the old field name were found, and the new field name is used consistently in the following files:

  • packages/rs-platform-version/src/version/v4.rs
  • packages/rs-platform-version/src/version/v1.rs
  • packages/rs-platform-version/src/version/v2.rs
  • packages/rs-platform-version/src/version/mocks/v3_test.rs
  • packages/rs-platform-version/src/version/mocks/v2_test.rs
  • packages/rs-platform-version/src/version/v3.rs
  • packages/rs-platform-version/src/version/drive_abci_versions.rs
  • packages/rs-drive-abci/src/platform_types/platform_state/mod.rs
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for usages of the old and new field names across the codebase

echo "Searching for old field name usage:"
rg --type rust "platform_state_for_saving_structure[^_]"

echo "Searching for new field name usage:"
rg --type rust "platform_state_for_saving_structure_default"

Length of output: 1207

packages/rs-drive/src/drive/document/insert/mod.rs (2)

63-63: LGTM: New import added for FeeVersion

The new import use dpp::version::fee::FeeVersion; is correctly placed and appears to be necessary for the changes in this file.


67-67: Verify the impact of using FeeVersion::first() instead of PlatformVersion

The initialization of EPOCH_CHANGE_FEE_VERSION_TEST has been updated to use FeeVersion::first() directly instead of PlatformVersion::first().fee_version.clone(). While this change simplifies the code, it's important to ensure that it doesn't alter the expected behavior in the rest of the system.

Please confirm that this change:

  1. Maintains the same fee version as before
  2. Doesn't break any existing functionality that relies on this static variable
  3. Is consistent with the intended design of the fee version system

You can use the following script to check for any other usages of PlatformVersion::first().fee_version that might need to be updated:

✅ Verification successful

Change Verified: No other instances of PlatformVersion::first().fee_version found

The verification confirms that there are no other usages of PlatformVersion::first().fee_version in the codebase. Therefore, using FeeVersion::first() directly does not affect other parts of the system and maintains the intended behavior.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other occurrences of PlatformVersion::first().fee_version
rg "PlatformVersion::first\(\)\.fee_version" --type rust

Length of output: 58


Script:

#!/bin/bash
# Search for any occurrences of 'PlatformVersion' accessing 'fee_version' in Rust files
rg "PlatformVersion\s*::\s*first\(\)\s*\.s*fee_version" --type rust

Length of output: 69


Script:

#!/bin/bash
# Search for any occurrences of 'PlatformVersion' accessing 'fee_version' in Rust files with corrected regex
rg "PlatformVersion\s*::\s*first\(\)\s*\.\s*fee_version" --type rust

Length of output: 70

packages/rs-drive/tests/query_tests_history.rs (3)

56-56: LGTM: New import for FeeVersion.

The addition of the FeeVersion import is correct and necessary for the changes made in the setup and test_query_historical functions.


264-265: LGTM: Consistent FeeVersion initialization in test function.

The change to use FeeVersion::first() in the test_query_historical function is consistent with the earlier modification in the setup function. This maintains code consistency and simplifies the initialization process.


170-171: LGTM: Simplified FeeVersion initialization.

The change to use FeeVersion::first() directly is an improvement. It simplifies the code and is more efficient.

Please verify if similar changes are needed elsewhere in the codebase for consistency. You can use the following script to check for other occurrences:

✅ Verification successful

Verified: Isolated FeeVersion initialization.

No other occurrences of PlatformVersion::first().fee_version were found, indicating that the change is isolated.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other occurrences of PlatformVersion::first().fee_version
rg "PlatformVersion::first\(\)\.fee_version" --type rust

Length of output: 58

packages/rs-platform-version/src/version/v2.rs (1)

605-605: Approve renaming with a suggestion for impact verification.

The renaming of platform_state_for_saving_structure to platform_state_for_saving_structure_default looks good. This change improves clarity by indicating that this is a default structure.

To ensure this change doesn't have unintended consequences, please run the following script to check for any other occurrences of the old field name:

This will help verify that all relevant code has been updated to use the new field name.

✅ Verification successful

Renaming Verified Successfully.

All occurrences of platform_state_for_saving_structure have been updated to platform_state_for_saving_structure_default. No references to the old field name were found outside the intended changes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other occurrences of the old field name

# Search for the old field name in all Rust files
rg --type rust "platform_state_for_saving_structure[^_]"

# Search for the new field name to confirm it's used consistently
rg --type rust "platform_state_for_saving_structure_default"

Length of output: 1045

packages/rs-platform-version/src/version/v3.rs (1)

Line range hint 1-1043: Overall assessment: Minor change with potential wider implications

The change in this file is minimal, involving only the renaming of a single field in the DriveAbciStructureVersions struct. While this change improves code clarity, it's important to consider its context within the larger PLATFORM_V3 constant, which defines crucial version and configuration information for the platform.

To ensure this change doesn't have unintended consequences, please run the following verification steps:

These checks will help ensure that the renaming has been consistently applied across the codebase and that any related tests have been updated accordingly.

✅ Verification successful

Verification Successful: Field Renaming Consistent

All references to platform_state_for_saving_structure have been correctly updated to platform_state_for_saving_structure_default across the codebase and associated tests. No issues or inconsistencies were found during the verification process.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any references to the old field name and verify the usage of the new field name

# Search for any remaining references to the old field name
echo "Searching for old field name references:"
rg "platform_state_for_saving_structure[^_]" --type rust

# Search for uses of the new field name
echo "Searching for new field name usage:"
rg "platform_state_for_saving_structure_default" --type rust

# Check for any tests that might need updating
echo "Checking for potentially affected tests:"
rg "platform_state_for_saving_structure" --type rust --glob "*test*.rs"

Length of output: 1616

packages/rs-platform-version/src/version/fee/processing/mod.rs (3)

15-15: Verify the reintroduction of perform_network_threshold_signing

The field perform_network_threshold_signing has been reintroduced in the FeeProcessingVersion struct. Please ensure that this change is intentional and that all serialization and deserialization logic correctly handle this additional field, especially concerning backward compatibility.


33-50: Ensure accurate field mapping in the conversion implementation

In the impl From<FeeProcessingVersionFieldsBeforeVersion1Point4> for FeeProcessingVersion, verify that all fields are correctly mapped from the old struct to the new one. Specifically, confirm that perform_network_threshold_signing is set appropriately using FEE_VERSION1.processing.perform_network_threshold_signing.


47-49: Confirm the default value's suitability for older data

Setting perform_network_threshold_signing to FEE_VERSION1.processing.perform_network_threshold_signing may have implications for data deserialized from versions prior to 1.4. Please confirm that this default value is appropriate and does not introduce inconsistencies or errors with older platform states.

packages/rs-platform-version/src/version/fee/mod.rs (4)

23-24: Addition of FeeVersionNumber type alias enhances clarity

Defining FeeVersionNumber as a type alias for u32 improves code readability and maintainability by providing semantic meaning to the version numbers used.


25-26: Initialization of FEE_VERSIONS constant is appropriate

The FEE_VERSIONS constant is correctly initialized with FEE_VERSION1, setting up the framework for managing multiple fee versions.


93-105: Confirm hardcoded fee_version_number in conversion from older versions

In the From<FeeVersionFieldsBeforeVersion4> implementation, the fee_version_number is hardcoded to 1. Please verify that this accurately represents the version of the older data and that this is the intended behavior.

To ensure that all older serialized FeeVersion instances correspond to version 1, consider checking the historical data or documentation.


Line range hint 29-38: Ensure backward compatibility with the added fee_version_number field

Adding the fee_version_number field to the FeeVersion struct may affect serialization and deserialization of existing data. Please verify that backward compatibility is maintained and that older serialized data can be deserialized without errors.

To confirm that deserialization handles the new field correctly, you can search for deserialization implementations of FeeVersion using the following script:

packages/rs-drive-abci/src/platform_types/platform_state/mod.rs (2)

7-8: Imports updated to include PlatformStateForSavingV1

The new type PlatformStateForSavingV1 has been added to the imports, which is necessary for handling the new version in the platform state saving logic.


45-46: Added new variant V1 to PlatformStateForSaving enum

The addition of V1(PlatformStateForSavingV1) to the PlatformStateForSaving enum allows the code to handle a new version of platform state data. This maintains consistency in versioning and is appropriate for the extended functionality.

packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/tests.rs (3)

798-798: Check insertion into previous_fee_versions

In test_document_refund_after_10_epochs_on_different_fee_version_increasing_fees, ensure that inserting into previous_fee_versions with key 5 is correct. Verify that the key corresponds to the intended epoch or version when the fee change occurred.

Please confirm that the fee version 5 aligns with the platform's versioning for fee changes.


Line range hint 356-359: Verify correctness of epoch advancement

When fast-forwarding to a future block in test_document_refund_after_a_year, ensure that the parameters accurately represent a year's advancement based on the platform's epoch duration.

Run the following script to confirm the number of epochs in a year:

#!/bin/bash
# Description: Verify the number of epochs corresponding to one year

# Assuming epoch duration is known, calculate epochs per year
echo "scale=2; (365 * 24 * 60 * 60) / <epoch_duration_in_seconds>" | bc

Replace <epoch_duration_in_seconds> with the actual epoch duration used in the platform.


Line range hint 275-280: Ensure consistent usage of platform state

In the process_state_transitions call within test_document_refund_immediate, the BlockInfo::default() is used. Confirm if the default block info accurately reflects the intended test conditions, especially regarding the block height and time.

Run the following script to check for other tests that might need updated block info:

✅ Verification successful


The usage of BlockInfo::default() is consistent across multiple test files, ensuring standardized block height and time settings in your tests. No issues were found regarding its usage in the process_state_transitions calls within test_document_refund_immediate.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for `process_state_transitions` calls using `BlockInfo::default()`

# Test: Find occurrences where `BlockInfo::default()` is used
rg --type rust -A 3 $'process_state_transitions' | rg 'BlockInfo::default\(\)'

Length of output: 241


Script:

#!/bin/bash
# Description: Search for `process_state_transitions` calls using `BlockInfo::default()`

# Test: Find occurrences where `BlockInfo::default()` is used in `process_state_transitions` calls
rg --type rust 'process_state_transitions.*BlockInfo::default\(\)'

Length of output: 68


Script:

#!/bin/bash
# Description: Search for all usages of `BlockInfo::default()` in Rust files

rg --type rust 'BlockInfo::default\(\)' -A 2

Length of output: 114837

packages/rs-drive/src/drive/document/delete/mod.rs (1)

79-83: Ensure Consistent Fee Version Initialization

By replacing PlatformVersion::first().fee_version.clone() with FeeVersion::first() in the initialization of EPOCH_CHANGE_FEE_VERSION_TEST, ensure that the fee version used is consistent with the platform's intended configuration. If FeeVersion::first() and PlatformVersion::first().fee_version.clone() return different values, this might lead to inconsistencies in fee calculations during tests.

To confirm that both approaches yield the same fee version, consider comparing their returned values. Here's a shell script to check the outputs:

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

161-162: Update to previous_fee_versions in PlatformStateForSavingV0 is Appropriate

The modification of previous_fee_versions to use CachedEpochIndexFeeVersionsFieldsBeforeVersion4 aligns with the changes in fee version management and ensures backward compatibility.


164-197: Addition of PlatformStateForSavingV1 Struct Enhances Serialization

The new PlatformStateForSavingV1 struct correctly incorporates updated types for previous_fee_versions, improving versioning and serialization capabilities during state transitions.


Line range hint 200-248: TryFrom<PlatformStateV0> Implementation for PlatformStateForSavingV1 is Correct

The conversion logic properly maps the fields from PlatformStateV0 to PlatformStateForSavingV1, including the transformation of previous_fee_versions into the new structure.


244-248: Efficient Mapping of previous_fee_versions

The mapping from CachedEpochIndexFeeVersions to EpochIndexFeeVersionsForStorage by extracting fee_version_number is handled correctly, ensuring data integrity.

packages/rs-platform-version/src/version/v4.rs (1)

607-607: Ensure all references to the renamed field are updated

The field platform_state_for_saving_structure has been renamed to platform_state_for_saving_structure_default. Please verify that all usages of the old field name have been updated throughout the codebase to avoid any potential issues.

Run the following script to check for any remaining references to platform_state_for_saving_structure:

✅ Verification successful

All references to platform_state_for_saving_structure have been successfully updated to platform_state_for_saving_structure_default.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find usages of `platform_state_for_saving_structure` excluding the current file

# Search for the old field name in Rust files, excluding the current file under review
rg 'platform_state_for_saving_structure' --type rust --ignore-case --glob '!packages/rs-platform-version/src/version/v4.rs'

Length of output: 942

packages/rs-drive/src/drive/document/update/mod.rs (2)

68-68: Adding 'FeeVersion' Import

The import of FeeVersion from dpp::version::fee is necessary due to its usage in the code below. This addition is appropriate and ensures that the FeeVersion type is available for initializing EPOCH_CHANGE_FEE_VERSION_TEST.


73-73: Updating Initialization of 'EPOCH_CHANGE_FEE_VERSION_TEST'

Replacing PlatformVersion::first().fee_version.clone() with FeeVersion::first() simplifies the initialization by directly accessing the fee version without relying on the platform version. Ensure that FeeVersion::first() returns the intended initial fee version to maintain consistent behavior.

You can verify that both methods return the same fee version by comparing their implementations:

packages/rs-drive/tests/query_tests.rs (2)

78-78: Import of FeeVersion Is Necessary and Appropriate

The addition of use dpp::version::fee::FeeVersion; is appropriate as FeeVersion is utilized in the updated initializations of epoch_change_fee_version_test. This import ensures the code compiles correctly with the new fee version handling.


2309-2309: Simplify epoch_change_fee_version_test Initialization

The initialization of epoch_change_fee_version_test has been updated to use FeeVersion::first(), which simplifies the code. Ensure that this change is consistent with the application's fee versioning logic and that any dependencies are updated accordingly.

@QuantumExplorer QuantumExplorer added this to the v1.4.0 milestone Oct 8, 2024
@QuantumExplorer QuantumExplorer added the bug Something isn't working label Oct 8, 2024
@QuantumExplorer QuantumExplorer self-assigned this Oct 8, 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.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
packages/rs-drive-abci/src/platform_types/platform_state/mod.rs (3)

7-7: LGTM! Consider updating documentation for the new version.

The addition of PlatformStateForSavingV1 and the new V1 variant in the PlatformStateForSaving enum are good changes for introducing a new version while maintaining backward compatibility.

Consider updating the documentation for PlatformStateForSaving to explain the differences between V0 and V1, and when each version should be used.

Also applies to: 45-46


141-145: LGTM! Consider improving error messages for clarity.

The implementation correctly handles the new PlatformStateForSavingV1 format while maintaining compatibility with version 0 of the platform state structure. This aligns with the retroactive nature of the changes, as mentioned in the retrieved learning.

Consider updating the error messages in both match arms to include more context about the version mismatch. For example:

Err(Error::Execution(ExecutionError::UnknownVersionMismatch {
    method: "PlatformState::try_from_platform_versioned(PlatformStateForSavingV1)".to_string(),
    known_versions: vec![0],
    received: version,
    additional_info: "Only version 0 of PlatformState is supported, regardless of PlatformStateForSaving version.".to_string(),
})),

This would provide more clarity about why only version 0 is handled, even for V1 serialization.

Also applies to: 177-193


551-571: LGTM! Consider expanding test coverage.

The addition of this test module is valuable, as it verifies the deserialization process using real testnet data. This helps ensure compatibility with existing serialized states.

Consider the following improvements to enhance the test coverage:

  1. Add assertions to verify the deserialized state's contents, not just that it deserializes without errors.
  2. Include tests for error cases, such as attempting to deserialize an invalid or corrupted state.
  3. If possible, add tests for deserializing both V0 and V1 formats to ensure full compatibility.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e11bfa5 and cba7406.

📒 Files selected for processing (2)
  • packages/rs-drive-abci/src/platform_types/platform_state/mod.rs (5 hunks)
  • packages/rs-drive-abci/src/test/fixture/mod.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/rs-drive-abci/src/test/fixture/mod.rs
🧰 Additional context used
📓 Learnings (1)
packages/rs-drive-abci/src/platform_types/platform_state/mod.rs (1)
Learnt from: QuantumExplorer
PR: dashpay/platform#2227
File: packages/rs-drive-abci/src/platform_types/platform_state/mod.rs:141-141
Timestamp: 2024-10-08T13:28:03.529Z
Learning: When converting `PlatformStateV0` to `PlatformStateForSavingV1` in `packages/rs-drive-abci/src/platform_types/platform_state/mod.rs`, only version `0` needs to be handled in the match on `platform_state_for_saving_structure_default` because the changes are retroactive.

@QuantumExplorer
Copy link
Member Author

@shumkov please review when you can.

@QuantumExplorer QuantumExplorer changed the title fix: fixed Platform State deserialization issue fix(platform): fixed Platform State deserialization issue Oct 8, 2024
@QuantumExplorer QuantumExplorer merged commit eaf8c12 into v1.4-dev Oct 8, 2024
79 checks passed
@QuantumExplorer QuantumExplorer deleted the fix/PlatformStateSerializationIssue branch October 8, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants