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

refactor(x/mint)!: avoid writing same minter and use millisecond precision for minting #20747

Merged
merged 3 commits into from
Jun 26, 2024

Conversation

facundomedica
Copy link
Member

@facundomedica facundomedica commented Jun 21, 2024

Description

This PR is meant to be the last refactor before calling complete the rework on x/mint.

  • Avoid writing the minter if there were no modifications on it during mint.
  • Use milliseconds for minting in the example mintFn, as more and more chains are going for subsecond blocks.

Next steps:

  • Eventually we should deprecate DefaultMintFn (block based minting, kept for compatibility) and move to something like ExampleMintFn (time based minting).

Closes: #19761


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Improvements

    • Enhanced precision in token minting calculations by switching from seconds to milliseconds.
  • New Features

    • Added a method to compare two minters based on their attributes.
    • Introduced logic to compare marshaled data before setting the minter.
  • Bug Fixes

    • Prevent unnecessary updates by comparing state before and after minting operations.

@facundomedica facundomedica requested a review from a team as a code owner June 21, 2024 16:13
Copy link
Contributor

coderabbitai bot commented Jun 21, 2024

Walkthrough

Walkthrough

The updates across various files primarily focus on enhancing the precision of time calculations in the token minting process by shifting from seconds to milliseconds. Additionally, there are optimizations in data comparison logic for minter data to minimize unnecessary updates. The changes aim to improve the overall efficiency and accuracy of the minting module within the application.

Changes

Files Change Summary
simapp/mint_fn.go Adjusted time calculations from seconds to milliseconds to increase precision in token minting.
x/mint/epoch_hooks.go, x/mint/keeper/abci.go Added imports for bytes module. Introduced logic to compare marshaled data before updating minters.
x/mint/keeper/keeper_test.go Imported context and cosmossdk.io/core/appmodule. Enhanced test to check the state of minters before and after a mint operation.
x/mint/types/minter.go Added IsEqual method to the Minter struct for field-wise comparison of minters.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant MintModule
    participant HeaderService

    User ->> App: Initiate Minting Operation
    App ->> HeaderService: Get Current Time
    HeaderService -->> App: Current UnixMilli Time
    App ->> MintModule: Calculate Tokens to Mint
    MintModule ->> App: Mint Tokens
    App -->> User: Minting Successfully Completed
Loading

Assessment against linked issues

Objective Addressed Explanation
Refactor mint to use time instead of blocks (Issue #19761)
Evaluate other changes needed for mint (Issue #19761) The changes improve precision and efficiency, which are beneficial, but further evaluation is required to confirm if all requested features are covered.
Cleanup and amend the code in the mint module for better UX (Issue #19761)
Update Docs (Issue #19761) The provided summary does not include any documentation updates.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b03a2c6 and b4ddfb8.

Files selected for processing (3)
  • simapp/mint_fn.go (1 hunks)
  • x/mint/epoch_hooks.go (2 hunks)
  • x/mint/keeper/abci.go (2 hunks)
Additional context used
Path-based instructions (3)
x/mint/keeper/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/mint/epoch_hooks.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simapp/mint_fn.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (6)
x/mint/keeper/abci.go (2)

4-4: Approved new import.

The addition of the bytes package is justified as it is used for comparing byte slices to optimize the minting process.


22-22: Optimization of minting logic.

The use of marshaling and byte comparison before setting the minter is a good optimization, ensuring that updates to the minter are only made when necessary. This should enhance the efficiency of the BeginBlocker function.

Also applies to: 31-34

x/mint/epoch_hooks.go (2)

4-4: Approved new import.

The addition of the bytes package is essential for the new logic that compares byte slices in the BeforeEpochStart function.


24-24: Optimization of epoch start logic.

Adding logic to marshal the minter object and compare its state before and after potential modifications optimizes the process, preventing unnecessary updates. This is a thoughtful addition that aligns with the PR's goal to enhance efficiency.

Also applies to: 31-34

simapp/mint_fn.go (2)

67-67: Precision adjustment in time calculations.

Switching from second-based to millisecond-based time calculations (Unix() to UnixMilli()) is a critical update that supports the PR's objective of enhancing the minting process to accommodate faster block times.

Also applies to: 71-71


74-75: Updated minting calculation using millisecond precision.

The update to use milliseconds in the minting calculation (31536000000 instead of 31536000) aligns with the change to millisecond precision and is correctly implemented to ensure the minting amount is accurate.

x/mint/keeper/abci.go Fixed Show fixed Hide fixed
x/mint/keeper/abci.go Fixed Show fixed Hide fixed
x/mint/keeper/abci.go Outdated Show resolved Hide resolved
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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b4ddfb8 and ec7a4e2.

Files selected for processing (4)
  • x/mint/epoch_hooks.go (1 hunks)
  • x/mint/keeper/abci.go (1 hunks)
  • x/mint/keeper/keeper_test.go (2 hunks)
  • x/mint/types/minter.go (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • x/mint/epoch_hooks.go
  • x/mint/keeper/abci.go
Additional context used
Path-based instructions (2)
x/mint/types/minter.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/mint/keeper/keeper_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

Additional comments not posted (2)
x/mint/types/minter.go (1)

85-99: New IsEqual method for Minter struct

The addition of the IsEqual method in the Minter struct is a crucial enhancement for comparing minter instances. This method checks all relevant fields (Inflation, AnnualProvisions, and Data) to ensure that two Minter instances are identical. This is aligned with the PR's aim to enhance the efficiency of data comparison in the minting process, potentially reducing unnecessary writes to the state.

The implementation uses direct comparisons for Inflation and AnnualProvisions and a byte-wise comparison for Data. This approach is efficient and appropriate for the types of data involved. The method is concise and adheres to the Uber Golang style guide.

x/mint/keeper/keeper_test.go (1)

158-167: Enhanced testing in TestBeginBlocker

The new test block in TestBeginBlocker is a significant addition. It tests the scenario where a mint function that performs no operations is used, and it verifies that the minter state remains unchanged after the operation. This is crucial for validating the new logic that prevents unnecessary state writes when no changes occur in the minter data.

This test effectively uses a no-op mint function and asserts that the minter state before and after the function call is the same, ensuring that the minting logic is correctly optimized to avoid unnecessary writes. The test is well-structured and follows best practices in unit testing.

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

makes sense!

@facundomedica facundomedica added this pull request to the merge queue Jun 26, 2024
Merged via the queue into main with commit 4bda5d6 Jun 26, 2024
70 checks passed
@facundomedica facundomedica deleted the facu/avoidwritingminter branch June 26, 2024 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Epic]: Mint
6 participants