Skip to content

fix: token distribution timestamp in the past #2509

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

Merged
merged 5 commits into from
Mar 20, 2025

Conversation

shumkov
Copy link
Member

@shumkov shumkov commented Mar 19, 2025

Issue being fixed or feature implemented

Currently, you can pre-program a token distribution timestamp in the past

What was done?

  • Validate a pre-programed token distribution timestamp

How Has This Been Tested?

None

Breaking Changes

None

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 distribution timestamps to ensure that outdated (past) timestamps are flagged.
    • Improved error reporting during contract creation and update processes, providing clearer feedback when token distribution settings do not meet current requirements.
    • Introduced a new error type for handling pre-programmed distribution timestamps that are in the past, improving overall error management.

Copy link
Contributor

coderabbitai bot commented Mar 19, 2025

Walkthrough

This PR introduces a new error type to handle cases where a pre-programmed token distribution timestamp is in the past. A new error variant is added to the StateError enum along with a dedicated module and struct implementation. The validation logic in both data contract create and update state transitions has been enhanced to check against the current block time, returning a consensus error when a past timestamp is detected. Additionally, the WASM consensus error mapping now includes this new error variant.

Changes

File(s) Change Summary
packages/rs-dpp/src/errors/consensus/codes.rs
packages/rs-dpp/src/errors/consensus/state/state_error.rs
Added a new error variant PreProgrammedDistributionTimestampInPastError to the StateError enum and updated import statements accordingly with error code 40719.
packages/rs-dpp/src/errors/consensus/state/token/mod.rs
packages/rs-dpp/src/errors/consensus/state/token/pre_programmed_distribution_timestamp_in_past_error.rs
Introduced a new error module and struct with associated methods, trait implementations, and a From conversion to integrate the new error type into the consensus system.
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/state/v0/mod.rs
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/state/v0/mod.rs
Enhanced validation logic in data contract create and update transitions to check token distribution timestamps. If a timestamp is in the past, a consensus error with a nonce bump action is returned.
packages/wasm-dpp/src/errors/consensus/consensus_error.rs Updated the match arm in the WASM error converter to handle the new PreProgrammedDistributionTimestampInPastError variant using the generic_consensus_error! macro.
packages/rs-dpp/src/data_contract/associated_token/token_pre_programmed_distribution/mod.rs Removed the methods module declaration and added a new accessors module declaration, indicating a reorganization of the module structure.
packages/rs-drive/src/drive/tokens/distribution/add_pre_programmed_distribution/v0/mod.rs
packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_claim_transition_action/v0/transformer.rs
Updated import statements for TokenPreProgrammedDistributionV0Methods to source from accessors::v0 instead of methods::v0.

Possibly related PRs

Suggested reviewers

  • QuantumExplorer

Poem

In fields of code I hop and dance,
New error types bloom at every chance.
Past timestamps are caught with a keen eye,
Ensuring our tokens never say goodbye.
I’m a bunny, coding with flair so bright,
Hopping through bugs with delight 🐇!

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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.

# Conflicts:
#	packages/rs-dpp/src/errors/consensus/codes.rs
#	packages/rs-dpp/src/errors/consensus/state/state_error.rs
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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 03cdc36 and fe9b79d.

📒 Files selected for processing (7)
  • 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/pre_programmed_distribution_timestamp_in_past_error.rs (1 hunks)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/state/v0/mod.rs (2 hunks)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/state/v0/mod.rs (3 hunks)
  • packages/wasm-dpp/src/errors/consensus/consensus_error.rs (1 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
packages/rs-dpp/src/errors/consensus/state/token/pre_programmed_distribution_timestamp_in_past_error.rs (1)
packages/rs-dpp/src/errors/consensus/state/state_error.rs (1) (1)
  • from (285-287)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/state/v0/mod.rs (1)
packages/rs-dpp/src/errors/consensus/state/token/pre_programmed_distribution_timestamp_in_past_error.rs (1) (1)
  • new (25-37)
🔇 Additional comments (8)
packages/rs-dpp/src/errors/consensus/codes.rs (1)

307-307: LGTM: Error code assigned correctly

The error code 40719 for PreProgrammedDistributionTimestampInPastError is appropriately assigned within the token errors range (40700-40799), following the established pattern.

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

13-13: LGTM: Module addition follows established pattern

The addition of the new error module and its public export follows the established pattern in the codebase.

Also applies to: 33-33

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

45-45:

✅ Verification successful

LGTM: Error enum variant properly added

The new PreProgrammedDistributionTimestampInPastError variant is correctly integrated into the StateError enum with proper attributes and import.

Since we don't see the implementation of the PreProgrammedDistributionTimestampInPastError struct in these files, let's verify it's implemented correctly:

Also applies to: 280-281


🏁 Script executed:

#!/bin/bash
# Find the implementation of the PreProgrammedDistributionTimestampInPastError struct
echo "Searching for PreProgrammedDistributionTimestampInPastError implementation..."
rg -A 20 "struct PreProgrammedDistributionTimestampInPastError" --type rust

# Also check for any usage of this error type in validation logic
echo -e "\nSearching for validation logic using this error..."
rg -A 5 "PreProgrammedDistributionTimestampInPastError" --type rust | grep -v "use" | grep -v "enum StateError"

Length of output: 10121


Action: Confirm new error variant integration

The new PreProgrammedDistributionTimestampInPastError variant is correctly integrated into the StateError enum. Verification shows that:

  • The implementation exists in
    packages/rs-dpp/src/errors/consensus/state/token/pre_programmed_distribution_timestamp_in_past_error.rs
    with a proper constructor.
  • The error is used in validation logic and converted to a ConsensusError (as seen in both rs-drive-abci and wasm-dpp modules).
  • The import in packages/rs-dpp/src/errors/consensus/state/state_error.rs is correct and matches the implementation.

LGTM: Error enum variant properly added and integrated.

packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/state/v0/mod.rs (2)

8-11: Added imports for token distribution timestamp validation.

These imports enable the validation of token distribution timestamps to prevent them from being set in the past.


70-87: Good addition: Validates token distribution timestamps.

This new validation logic ensures that pre-programmed token distribution timestamps cannot be set in the past. The code:

  1. Iterates through token configurations
  2. Checks if any pre-programmed distribution has a timestamp earlier than the current block time
  3. Returns a specific consensus error if an invalid timestamp is found

This directly addresses the PR objective of preventing users from pre-programming token distribution timestamps in the past.

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

1-51: Well-structured new error type definition.

This new error type contains all the necessary information to properly identify and report when a token distribution timestamp is in the past:

  • Contains data contract ID, token position, the problematic timestamp, and current time
  • Has a clear error message format that explains the issue
  • Implements appropriate traits for error handling and serialization
  • Provides a constructor and necessary accessors
  • Implements conversion to ConsensusError for integration with the existing error system

The implementation is clean and follows the project's error handling patterns.

packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/state/v0/mod.rs (2)

8-14: Added imports for token validation in contract updates.

These imports support the validation of token distribution timestamps in data contract updates, consistent with the implementation in contract creation.


32-32: Added BumpIdentityNonceAction import.

This import is needed for the error handling path when a validation failure is detected.

@shumkov shumkov self-assigned this Mar 19, 2025
@shumkov shumkov moved this to In review / testing in Platform team Mar 19, 2025
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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1e6fe35 and 34775ca.

📒 Files selected for processing (6)
  • packages/rs-dpp/src/data_contract/associated_token/token_pre_programmed_distribution/mod.rs (1 hunks)
  • packages/rs-dpp/src/errors/consensus/state/token/pre_programmed_distribution_timestamp_in_past_error.rs (1 hunks)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/state/v0/mod.rs (2 hunks)
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/state/v0/mod.rs (3 hunks)
  • packages/rs-drive/src/drive/tokens/distribution/add_pre_programmed_distribution/v0/mod.rs (1 hunks)
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_claim_transition_action/v0/transformer.rs (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • packages/rs-dpp/src/data_contract/associated_token/token_pre_programmed_distribution/mod.rs
  • packages/rs-drive/src/drive/tokens/distribution/add_pre_programmed_distribution/v0/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_claim_transition_action/v0/transformer.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/rs-dpp/src/errors/consensus/state/token/pre_programmed_distribution_timestamp_in_past_error.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/state/v0/mod.rs
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: Rust packages (wasm-dpp) / Tests
  • GitHub Check: Rust packages (wasm-dpp) / Linting
  • GitHub Check: Rust packages (dash-sdk) / Linting
  • GitHub Check: Rust packages (dash-sdk) / Tests
  • GitHub Check: Rust packages (wasm-dpp) / Unused dependencies
  • GitHub Check: Rust packages (dash-sdk) / Check each feature
  • GitHub Check: Rust packages (dpp) / Check each feature
  • GitHub Check: Rust packages (dpp) / Tests
  • GitHub Check: Rust packages (dpp) / Linting
  • GitHub Check: Rust packages (drive-abci) / Check each feature
  • GitHub Check: Rust packages (drive-abci) / Tests
  • GitHub Check: Rust packages (drive) / Tests
  • GitHub Check: Rust packages (drive-abci) / Unused dependencies
  • GitHub Check: Rust packages (drive-abci) / Linting
  • GitHub Check: Rust packages (drive) / Linting
  • GitHub Check: Build Docker images (Dashmate helper, dashmate-helper, dashmate-helper) / Build Dashmate helper image
  • 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 JS packages / Build JS
🔇 Additional comments (1)
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/state/v0/mod.rs (1)

8-11: The necessary imports are correctly added for token distribution validation.

These imports provide the necessary functionality to validate token distribution timestamps and handle any validation errors that might occur during that process.

Copy link
Member

@pauldelucia pauldelucia left a comment

Choose a reason for hiding this comment

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

Tested in DET

@shumkov shumkov merged commit 1b17083 into v2.0-dev Mar 20, 2025
64 checks passed
@shumkov shumkov deleted the feat/validate-token-distribution-timestamp branch March 20, 2025 07:54
@github-project-automation github-project-automation bot moved this from In review / testing to Done in Platform team Mar 20, 2025
@thephez thephez added this to the v2.0.0 milestone Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants