-
Notifications
You must be signed in to change notification settings - Fork 39
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
refactor(drive): remove duplicated withdrawal amount validation #2191
Conversation
WalkthroughThe changes introduce modifications to the Changes
Possibly related PRs
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (4)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/balance/v0/mod.rs (2)
33-37
: Improved balance check logic with overflow protection.The changes enhance the clarity and safety of the balance validation logic. The use of
checked_add
prevents potential overflow issues, which is a great improvement.Consider making the error message more specific:
- "overflow when adding amount and min_leftover_credits_before_processing in identity credit withdrawal" + "overflow when calculating total required balance (amount + fees) for identity credit withdrawal"
48-139
: Comprehensive test coverage with room for improvement.The new test module provides good coverage for both valid and invalid scenarios. The use of
assert_matches
enhances the clarity of test assertions.Consider adding a test case for the overflow scenario:
#[test] fn should_return_error_on_overflow() { let balance = u64::MAX; let amount = u64::MAX; let identity = PartialIdentity { id: Identifier::random(), loaded_public_keys: Default::default(), balance: Some(balance), revision: None, not_found_public_keys: Default::default(), }; let transaction = IdentityCreditWithdrawalTransition::V0(IdentityCreditWithdrawalTransitionV0 { identity_id: identity.id, amount, // ... other fields }); let platform_version = &PLATFORM_V1; let result = transaction .validate_advanced_minimum_balance_pre_check_v0(&identity, platform_version); assert_matches!( result, Err(Error::Execution(ExecutionError::Overflow(_))) ); }This test ensures that the overflow protection we've added is working as expected.
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs (2)
Line range hint
45-49
: Ensure consistent handling of unsupported pooling types.The validation correctly checks that
self.pooling()
is notPooling::Never
. However, after adding the error to the result, the function returns early withreturn Ok(result);
. This means that subsequent validations (e.g., core fee per byte and output script validations) are skipped when pooling is notNever
.Consider allowing the function to continue validating other fields even if the pooling is incorrect, unless it's intentional to halt validation at this point.
Line range hint
53-60
: Clarify the core fee per byte validation logic.The validation checks if
self.core_fee_per_byte()
is a Fibonacci number, and adds an error if it's not. However, the use of the Fibonacci sequence for core fee validation may not be immediately clear to other developers or may require justification.Consider adding a comment explaining why the Fibonacci sequence is being used for
core_fee_per_byte
, or if this is a temporary measure, ensure documentation is updated accordingly.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
- packages/rs-dpp/src/errors/consensus/basic/identity/invalid_identity_credit_withdrawal_transition_output_script_error.rs (1 hunks)
- packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/balance/v0/mod.rs (2 hunks)
- packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/state/v0/mod.rs (1 hunks)
- packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs (2 hunks)
🧰 Additional context used
📓 Learnings (1)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/state/v0/mod.rs (1)
Learnt from: shumkov PR: dashpay/platform#2186 File: packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/mod.rs:48-54 Timestamp: 2024-09-30T12:11:35.148Z Learning: In the identity credit withdrawal transition code, the field `platform_version.drive_abci.validation_and_processing.state_transitions.identity_credit_withdrawal_state_transition.transform_into_action` is not an `Option` type, so handling `None` cases is unnecessary.
🔇 Additional comments (4)
packages/rs-dpp/src/errors/consensus/basic/identity/invalid_identity_credit_withdrawal_transition_output_script_error.rs (1)
Line range hint
1-45
: Maintain focus on PR objectivesWhile the added TODO comment raises a valid performance concern, it doesn't align with the PR's main objective of removing duplicated withdrawal amount validation. To maintain a focused and efficient review process:
- Consider addressing the performance issue in a separate PR or issue.
- Ensure that the changes in this PR directly contribute to removing the duplicated withdrawal amount validation, as stated in the PR objectives.
To confirm that this file is relevant to the PR's objectives, let's check for any references to withdrawal validation:
If the search doesn't yield relevant results, consider removing changes to this file from the current PR to maintain its focus.
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/balance/v0/mod.rs (1)
Line range hint
1-139
: Summary: Changes align with PR objectives and improve code quality.The modifications in this file successfully address the PR's objective of removing duplicated withdrawal amount validation. The implementation of
validate_advanced_minimum_balance_pre_check_v0
has been streamlined and made more robust with overflow protection. The added test module provides good coverage of the main scenarios.The changes improve both the efficiency and safety of the code, aligning well with the stated goals of the pull request. The removal of redundant validation and the addition of comprehensive tests enhance the overall quality of the codebase.
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/state/v0/mod.rs (1)
71-71
: LGTM!Passing
platform.drive
by value instead of by reference is appropriate if thedrive
field implements the necessary traits for ownership transfer. This change simplifies the call totry_from_identity_credit_withdrawal
and should not introduce any issues.packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs (1)
230-257
: Handle the case whenoutput_script
is empty.In the test
should_return_invalid_result_if_output_script_is_not_p2pkh_or_p2sh
, theoutput_script
is initialized with an emptyScriptBuf
. An empty script might not be the only invalid case. There could be scripts that are non-empty but still not P2PKH or P2SH.Consider extending the test to include such scenarios, ensuring that the validation catches all invalid script types.
Run the following script to search for other usages of
CoreScript::new
with potentially invalid scripts:
...onsensus/basic/identity/invalid_identity_credit_withdrawal_transition_output_script_error.rs
Show resolved
Hide resolved
...validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs
Outdated
Show resolved
Hide resolved
...validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs
Outdated
Show resolved
Hide resolved
...onsensus/basic/identity/invalid_identity_credit_withdrawal_transition_output_script_error.rs
Show resolved
Hide resolved
...ion/validation/state_transition/state_transitions/identity_credit_withdrawal/state/v0/mod.rs
Show resolved
Hide resolved
There was a problem hiding this 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 (1)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs (1)
81-337
: LGTM: Comprehensive test suite with a minor suggestionThe test suite for
validate_basic_structure_v1
is well-structured and covers a wide range of scenarios:
- Invalid amounts (too low and too high)
- Invalid pooling
- Invalid core fee
- Invalid output script
- Valid P2PKH output script
- Valid P2SH output script
- Valid transition without output script
The use of
assert_matches!
macro enhances readability and provides detailed error checking. Both positive and negative scenarios are covered, ensuring robust validation.Consider adding a test case for the upper bound of the valid amount range to ensure the validation works correctly at the boundary condition.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs (1 hunks)
🔇 Additional comments (2)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_credit_withdrawal/structure/v1/mod.rs (2)
Line range hint
28-79
: LGTM: Comprehensive validation implementationThe
validate_basic_structure_v1
method provides a thorough validation of theIdentityCreditWithdrawalTransition
. It covers all necessary aspects:
- Amount validation
- Pooling validation
- Core fee validation
- Output script validation
The implementation is well-structured, uses appropriate error types, and follows best practices for error handling and result composition.
Line range hint
1-337
: Summary: Changes align well with PR objectivesThe modifications in this file successfully address the PR objectives:
- The
validate_basic_structure_v1
method provides a single, comprehensive validation for the withdrawal amount, eliminating any potential duplication.- The extensive test suite significantly improves the test coverage for withdrawal state validation.
These changes contribute to streamlining the validation process and enhancing the overall robustness of the codebase. The implementation is well-structured, and the tests are comprehensive, covering both positive and negative scenarios.
Issue being fixed or feature implemented
What was done?
How Has This Been Tested?
Add test for balance and structure validation
Breaking Changes
None
Checklist:
For repository code-owners and collaborators only
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
Tests