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: update FX denom in bank module #923

Merged
merged 1 commit into from
Jan 20, 2025
Merged

refactor: update FX denom in bank module #923

merged 1 commit into from
Jan 20, 2025

Conversation

zakir-code
Copy link
Contributor

@zakir-code zakir-code commented Jan 20, 2025

Summary by CodeRabbit

  • New Features

    • Added a new utility function OriginalFXDenom() to retrieve the original FX denomination
    • Introduced a new bank module migration function to handle denomination transitions
  • Refactor

    • Updated token migration and upgrade processes to use the new OriginalFXDenom() method
    • Modified how original FX denomination is referenced across multiple components
  • Tests

    • Enhanced upgrade test suite with additional checks for bank module and token migrations

Copy link

coderabbitai bot commented Jan 20, 2025

Walkthrough

The pull request introduces modifications to several files related to upgrade processes, token migrations, and denomination handling. The changes primarily focus on refactoring how the original FX denomination is referenced and processed across different modules. A new utility function OriginalFXDenom() is added to consistently retrieve the uppercase version of the FX denomination. The modifications impact upgrade testing, bank module migration, and ERC20 token handling, ensuring more consistent and precise denomination management during system upgrades.

Changes

File Change Summary
types/constant.go Added OriginalFXDenom() method to return uppercase FX denomination
app/upgrade_test.go Added checkBankModule function for bank module validation during upgrades
app/upgrades/v8/upgrade.go Introduced migrateBankModule function for handling denomination migrations
x/erc20/migrations/v8/keys.go, x/erc20/migrations/v8/migrate.go Replaced strings.ToUpper(fxtypes.FXDenom) with fxtypes.OriginalFXDenom()

Sequence Diagram

sequenceDiagram
    participant App as Blockchain App
    participant BankModule as Bank Module
    participant Migrator as Token Migrator

    App->>BankModule: Initiate Upgrade
    BankModule->>Migrator: Migrate Original Denomination
    Migrator-->>BankModule: Update Token Balances
    BankModule-->>App: Confirm Migration Complete
Loading

Possibly related PRs

Poem

🐰 Hopping through code with glee,
Migrations dance, denominations free!
FX tokens transform with grace,
Upgrades leap at lightning pace,
A rabbit's code-fu sets them free! 🚀


📜 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 0d36177 and eb28537.

📒 Files selected for processing (5)
  • app/upgrade_test.go (3 hunks)
  • app/upgrades/v8/upgrade.go (6 hunks)
  • types/constant.go (1 hunks)
  • x/erc20/migrations/v8/keys.go (1 hunks)
  • x/erc20/migrations/v8/migrate.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Summary
🔇 Additional comments (11)
types/constant.go (1)

108-110: LGTM! The utility function provides a consistent way to get the uppercase FX denomination.

The function is well-implemented and helps centralize the logic for getting the uppercase version of FXDenom.

x/erc20/migrations/v8/keys.go (1)

67-67: LGTM! The change correctly uses the new utility function.

The update to use OriginalFXDenom() aligns with the refactoring to centralize FX denomination handling.

x/erc20/migrations/v8/migrate.go (2)

19-19: LGTM! The change correctly uses the new utility function.

The update to use OriginalFXDenom() aligns with the refactoring to centralize FX denomination handling.


77-77: LGTM! The change correctly uses the new utility function.

The update to use OriginalFXDenom() aligns with the refactoring to centralize FX denomination handling.

app/upgrade_test.go (2)

631-632: LGTM! The change correctly uses the new utility function.

The update to use OriginalFXDenom() aligns with the refactoring to centralize FX denomination handling.


652-672: LGTM! The new test function thoroughly validates the bank module state.

The function performs comprehensive checks:

  1. Verifies that the original FX denomination is not present in any balances
  2. Validates the total supply calculation for the default denomination
  3. Ensures the original FX denomination is not present in send-enabled entries
app/upgrades/v8/upgrade.go (5)

10-10: LGTM!

The collections package import is required for the new migrateBankModule function.


267-270: LGTM!

The code correctly uses fxtypes.OriginalFXDenom() for consistent denomination handling.


343-348: LGTM!

The code correctly uses fxtypes.OriginalFXDenom() for metadata updates.


Line range hint 662-685: LGTM!

The migration of ERC20 FX token and metadata to PundiAI is implemented correctly:

  1. Updates the ERC20 token denomination
  2. Removes the old FX metadata

687-730: Verify error handling in migrateBankModule.

The function handles the migration of bank module balances from FX to the new denomination. While the implementation looks correct, there are a few concerns:

  1. The function modifies critical state (balances and supply) without transaction safety.
  2. The error handling in the iterator could leave the system in an inconsistent state if an error occurs mid-migration.

Consider wrapping the entire operation in a cache context to ensure atomicity:

 func migrateBankModule(ctx sdk.Context, bankKeeper bankkeeper.Keeper) error {
+    cacheCtx, commit := ctx.CacheContext()
     fxDenom := fxtypes.OriginalFXDenom()
     
     sendEnabledEntry, found := bankKeeper.GetSendEnabledEntry(ctx, fxDenom)
     if found {
-        bankKeeper.DeleteSendEnabled(ctx, fxDenom)
-        bankKeeper.SetSendEnabled(ctx, fxtypes.DefaultDenom, sendEnabledEntry.Enabled)
+        bankKeeper.DeleteSendEnabled(cacheCtx, fxDenom)
+        bankKeeper.SetSendEnabled(cacheCtx, fxtypes.DefaultDenom, sendEnabledEntry.Enabled)
     }
     
     var err error
-    fxSupply := bankKeeper.GetSupply(ctx, fxDenom)
+    fxSupply := bankKeeper.GetSupply(cacheCtx, fxDenom)
     apundiaiSupply := sdkmath.ZeroInt()
     
     bk, ok := bankKeeper.(bankkeeper.BaseKeeper)
     if !ok {
         return errors.New("bank keeper not implement bank.BaseKeeper")
     }
-    bk.IterateAllBalances(ctx, func(address sdk.AccAddress, coin sdk.Coin) (stop bool) {
+    bk.IterateAllBalances(cacheCtx, func(address sdk.AccAddress, coin sdk.Coin) (stop bool) {
         if coin.Denom != fxDenom {
             return false
         }
-        if err = bk.Balances.Remove(ctx, collections.Join(address, coin.Denom)); err != nil {
+        if err = bk.Balances.Remove(cacheCtx, collections.Join(address, coin.Denom)); err != nil {
             return true
         }
         coin.Denom = fxtypes.DefaultDenom
         coin.Amount = fxtypes.SwapAmount(coin.Amount)
         if !coin.IsPositive() {
             return false
         }
         apundiaiSupply = apundiaiSupply.Add(coin.Amount)
-        if err = bk.Balances.Set(ctx, collections.Join(address, coin.Denom), coin.Amount); err != nil {
+        if err = bk.Balances.Set(cacheCtx, collections.Join(address, coin.Denom), coin.Amount); err != nil {
             return true
         }
         return false
     })
     if err != nil {
         return err
     }
     
-    ctx.Logger().Info("migrate fx to apundiai", "FX supply", fxSupply.Amount.String(), "apundiai supply", apundiaiSupply.String())
-    if err = bk.Supply.Remove(ctx, fxDenom); err != nil {
+    cacheCtx.Logger().Info("migrate fx to apundiai", "FX supply", fxSupply.Amount.String(), "apundiai supply", apundiaiSupply.String())
+    if err = bk.Supply.Remove(cacheCtx, fxDenom); err != nil {
         return err
     }
-    return bk.Supply.Set(ctx, fxtypes.DefaultDenom, apundiaiSupply)
+    if err = bk.Supply.Set(cacheCtx, fxtypes.DefaultDenom, apundiaiSupply); err != nil {
+        return err
+    }
+    commit()
+    return nil
 }

Also, verify that all balances are migrated correctly by running this script:

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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.

@fx0x55 fx0x55 merged commit 73af7fa into main Jan 20, 2025
13 checks passed
@fx0x55 fx0x55 deleted the nulnut/bank-module branch January 20, 2025 03:18
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