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: check if token is paused on token transfers #2458

Merged
merged 3 commits into from
Feb 13, 2025

Conversation

pauldelucia
Copy link
Member

@pauldelucia pauldelucia commented Feb 13, 2025

Issue being fixed or feature implemented

We weren't checking if tokens were paused when doing token transfers

What was done?

Add a check and new error type

How Has This Been Tested?

Confirmed to work as intended using DET and local network

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
    • Enhanced validation for token operations now clearly indicates when a token is paused, blocking actions such as transfers.
    • Introduced precise error feedback to help users understand when operations cannot proceed due to a token’s paused state.
  • Bug Fixes
    • Updated error handling to include a new error type for paused tokens, improving clarity in error reporting.

Copy link
Contributor

coderabbitai bot commented Feb 13, 2025

Walkthrough

This pull request enhances error handling for token state management by introducing a new error variant, TokenIsPausedError. The changes include updates across multiple modules: a new variant and error code in StateError, a dedicated module for the token paused error, and modifications to token transfer validation logic to check for a paused token state. Additionally, the error handling in the consensus error module for Wasm has been updated to use TokenIsPausedError in place of a previous error type.

Changes

File(s) Change Summary
packages/rs-dpp/src/errors/consensus/codes.rs, state/state_error.rs, state/token/mod.rs, state/token/token_is_paused_error.rs Introduced the new error variant TokenIsPausedError, added a dedicated module with its struct definition, constructor, getter, and conversion for wrapping into ConsensusError.
packages/rs-drive-abci/src/execution/validation/.../state_v0/mod.rs Updated token transfer validation to check the token paused state, returning TokenIsPausedError during state transition validation if the token is paused.
packages/wasm-dpp/src/errors/consensus/consensus_error.rs Replaced IdentityAssetLockTransactionOutPointAlreadyConsumedError with TokenIsPausedError in the consensus error import list.

Sequence Diagram(s)

sequenceDiagram
    actor Client
    participant Validator as TokenTransferValidation
    participant Consensus as ConsensusModule

    Client->>Validator: Submit Token Transfer Request
    Validator->>Validator: Check token state (paused?)
    alt Token Paused
        Validator->>Consensus: Return TokenIsPausedError
    else Token Active
        Validator->>Consensus: Proceed with additional validations
    end
Loading

Possibly related PRs

Suggested reviewers

  • shumkov
  • QuantumExplorer

Poem

I'm a little rabbit in a code-filled glen,
Hopping through bugs with a cheerful pen.
Paused tokens now signal a clear, bright error,
Making code paths swift and ever so fair.
Join my hop—where clean logic is our flair!


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 907971d and 4becf5f.

📒 Files selected for processing (1)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/token/token_transfer_transition_action/state_v0/mod.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/token/token_transfer_transition_action/state_v0/mod.rs
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: Rust packages (wasm-dpp) / Formatting
  • GitHub Check: Rust packages (wasm-dpp) / Detect immutable structure changes
  • GitHub Check: Rust packages (drive-abci) / Unused dependencies
  • GitHub Check: Rust packages (drive-abci) / Tests
  • GitHub Check: Rust packages (drive-abci) / Linting
  • GitHub Check: Rust packages (drive-abci) / Check each feature
  • GitHub Check: Rust packages (drive) / Unused dependencies
  • GitHub Check: Rust packages (drive) / Tests
  • GitHub Check: Rust packages (drive) / Linting
  • GitHub Check: Rust packages (dpp) / Tests
  • GitHub Check: Rust packages (dpp) / Linting
  • GitHub Check: Rust packages (dpp) / Check each feature
  • GitHub Check: Rust packages (dash-sdk) / Tests
  • GitHub Check: Rust packages (dash-sdk) / Linting
  • GitHub Check: Rust packages (dash-sdk) / Check each feature
  • GitHub Check: Build Docker images (Drive, drive, drive-abci) / Build Drive image
  • GitHub Check: Build Docker images (DAPI, dapi, dapi) / Build DAPI image
  • GitHub Check: Build Docker images (Dashmate helper, dashmate-helper, dashmate-helper) / Build Dashmate helper image
  • GitHub Check: Build JS packages / Build JS

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@pauldelucia pauldelucia marked this pull request as ready for review February 13, 2025 07:29
Base automatically changed from v2.0-tokens-dev to v2.0-dev February 13, 2025 08:55
@pauldelucia pauldelucia requested a review from shumkov February 13, 2025 11:02
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

🧹 Nitpick comments (1)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/token/token_transfer_transition_action/state_v0/mod.rs (1)

98-113: Consider optimizing check order for better performance.

The token pause check is valid but could be moved before the balance and frozen checks. This would prevent unnecessary database queries when the token is paused.

Apply this diff to optimize the check order:

-        // We need to verify that we have enough of the token
-        let balance = platform
-            .drive
-            .fetch_identity_token_balance(
-                self.token_id().to_buffer(),
-                owner_id.to_buffer(),
-                transaction,
-                platform_version,
-            )?
-            .unwrap_or_default();
-        execution_context.add_operation(ValidationOperation::RetrieveIdentityTokenBalance);
-        if balance < self.amount() {
-            return Ok(SimpleConsensusValidationResult::new_with_error(
-                ConsensusError::StateError(StateError::IdentityDoesNotHaveEnoughTokenBalanceError(
-                    IdentityDoesNotHaveEnoughTokenBalanceError::new(
-                        self.token_id(),
-                        owner_id,
-                        self.amount(),
-                        balance,
-                        "transfer".to_string(),
-                    ),
-                )),
-            ));
-        }
-
-        // We need to verify that our token account is not frozen
-        let info = platform.drive.fetch_identity_token_info(
-            self.token_id().to_buffer(),
-            owner_id.to_buffer(),
-            transaction,
-            platform_version,
-        )?;
-        if let Some(info) = info {
-            if info.frozen() == true {
-                return Ok(SimpleConsensusValidationResult::new_with_error(
-                    ConsensusError::StateError(StateError::IdentityTokenAccountFrozenError(
-                        IdentityTokenAccountFrozenError::new(
-                            self.token_id(),
-                            owner_id,
-                            "transfer".to_string(),
-                        ),
-                    )),
-                ));
-            }
-        };
-
         // We need to verify that the token is not paused
         let token_status = platform.drive.fetch_token_status(
             self.token_id().to_buffer(),
             transaction,
             platform_version,
         )?;
         if let Some(status) = token_status {
             if status.paused() {
                 return Ok(SimpleConsensusValidationResult::new_with_error(
                     ConsensusError::StateError(StateError::TokenIsPausedError(
                         TokenIsPausedError::new(self.token_id()),
                     )),
                 ));
             }
         }
+
+        // We need to verify that we have enough of the token
+        let balance = platform
+            .drive
+            .fetch_identity_token_balance(
+                self.token_id().to_buffer(),
+                owner_id.to_buffer(),
+                transaction,
+                platform_version,
+            )?
+            .unwrap_or_default();
+        execution_context.add_operation(ValidationOperation::RetrieveIdentityTokenBalance);
+        if balance < self.amount() {
+            return Ok(SimpleConsensusValidationResult::new_with_error(
+                ConsensusError::StateError(StateError::IdentityDoesNotHaveEnoughTokenBalanceError(
+                    IdentityDoesNotHaveEnoughTokenBalanceError::new(
+                        self.token_id(),
+                        owner_id,
+                        self.amount(),
+                        balance,
+                        "transfer".to_string(),
+                    ),
+                )),
+            ));
+        }
+
+        // We need to verify that our token account is not frozen
+        let info = platform.drive.fetch_identity_token_info(
+            self.token_id().to_buffer(),
+            owner_id.to_buffer(),
+            transaction,
+            platform_version,
+        )?;
+        if let Some(info) = info {
+            if info.frozen() == true {
+                return Ok(SimpleConsensusValidationResult::new_with_error(
+                    ConsensusError::StateError(StateError::IdentityTokenAccountFrozenError(
+                        IdentityTokenAccountFrozenError::new(
+                            self.token_id(),
+                            owner_id,
+                            "transfer".to_string(),
+                        ),
+                    )),
+                ));
+            }
+        };
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 125cfe7 and 907971d.

📒 Files selected for processing (6)
  • packages/rs-dpp/src/errors/consensus/codes.rs (1 hunks)
  • packages/rs-dpp/src/errors/consensus/state/state_error.rs (2 hunks)
  • packages/rs-dpp/src/errors/consensus/state/token/mod.rs (2 hunks)
  • packages/rs-dpp/src/errors/consensus/state/token/token_is_paused_error.rs (1 hunks)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/token/token_transfer_transition_action/state_v0/mod.rs (2 hunks)
  • packages/wasm-dpp/src/errors/consensus/consensus_error.rs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: Rust packages (drive-abci) / Formatting
  • GitHub Check: Rust packages (drive-abci) / Detect immutable structure changes
  • GitHub Check: Rust packages (drive-abci) / Check each feature
  • GitHub Check: Rust packages (drive) / Unused dependencies
  • GitHub Check: Rust packages (drive) / Tests
  • GitHub Check: Rust packages (drive) / Linting
  • GitHub Check: Rust packages (dpp) / Tests
  • GitHub Check: Rust packages (dpp) / Linting
  • GitHub Check: Rust packages (dpp) / Check each feature
  • GitHub Check: Rust packages (dash-sdk) / Tests
  • GitHub Check: Rust packages (dash-sdk) / Check each feature
  • GitHub Check: Rust packages (dash-sdk) / Linting
  • GitHub Check: Build Docker images (Drive, drive, drive-abci) / Build Drive image
  • GitHub Check: Build Docker images (Dashmate helper, dashmate-helper, dashmate-helper) / Build Dashmate helper image
  • GitHub Check: Build Docker images (DAPI, dapi, dapi) / Build DAPI image
  • GitHub Check: Build JS packages / Build JS
🔇 Additional comments (5)
packages/rs-dpp/src/errors/consensus/state/token/token_is_paused_error.rs (1)

1-32: Well-structured error type implementation!

The TokenIsPausedError implementation follows Rust best practices:

  • Proper error handling traits
  • Clear error message with context
  • Clean conversion to ConsensusError
packages/rs-dpp/src/errors/consensus/state/token/mod.rs (1)

9-9: Clean module integration!

The new module is properly registered and exported, maintaining alphabetical order and consistency with other modules.

Also applies to: 22-22

packages/rs-dpp/src/errors/consensus/state/state_error.rs (1)

55-55: Correctly extends StateError enum!

The new variant is properly added:

  • Placed at the end to preserve serialization order
  • Consistent use of transparent error attribute

Also applies to: 260-261

packages/rs-dpp/src/errors/consensus/codes.rs (1)

257-257: LGTM!

The new error variant TokenIsPausedError is correctly implemented with code 40161 and follows the established pattern for token-related errors.

packages/wasm-dpp/src/errors/consensus/consensus_error.rs (1)

87-87: LGTM!

The new error variant TokenIsPausedError is correctly integrated into the error handling system:

  1. Properly imported from the token module
  2. Correctly handled in the from_state_error function using the generic_consensus_error macro

Also applies to: 329-329

@pauldelucia pauldelucia changed the title feat: check if token is paused on token transfers fix: check if token is paused on token transfers Feb 13, 2025
@pauldelucia pauldelucia merged commit 82e4d4c into v2.0-dev Feb 13, 2025
42 of 48 checks passed
@pauldelucia pauldelucia deleted the feat/token-paused-validation branch February 13, 2025 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants