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: rename OriginalFXDenom to LegacyFXDenom #926

Merged
merged 1 commit into from
Jan 20, 2025
Merged

Conversation

zakir-code
Copy link
Contributor

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

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new legacy denomination constant LegacyFXDenom
  • Refactor

    • Updated token denomination references across multiple components
    • Replaced IBCFXDenom and OriginalFXDenom() with LegacyFXDenom
    • Modified migration and token handling logic to use new legacy denomination
  • Tests

    • Updated test cases to reflect new denomination constant
    • Ensured consistent testing with legacy denomination

The changes primarily focus on standardizing token denomination handling across the system.

Copy link

coderabbitai bot commented Jan 20, 2025

Walkthrough

The pull request introduces a comprehensive change in handling token denominations across multiple files. The primary modification involves replacing fxtypes.IBCFXDenom and fxtypes.OriginalFXDenom() with a new constant fxtypes.LegacyFXDenom. This change impacts various components including upgrade processes, token migrations, IBC middleware, and testing scenarios. The modifications ensure consistent use of the legacy denomination across different modules and functions, reflecting a strategic update in how token denominations are managed and referenced.

Changes

File Change Summary
types/constant.go Added LegacyFXDenom constant, removed OriginalFXDenom() function
types/denom_wrap.go Replaced IBCFXDenom with LegacyFXDenom in maps and references
app/upgrade_test.go Updated token denomination checks in test functions
app/upgrades/v8/upgrade.go Replaced denomination references in migration functions
x/erc20/migrations/v8/keys.go, migrate.go Updated token migration logic with new denomination
x/ibc/middleware/keeper/relay.go, relay_test.go Modified denomination checks in IBC packet handling

Sequence Diagram

sequenceDiagram
    participant Types as Token Types
    participant Migrator as Token Migrator
    participant Middleware as IBC Middleware
    
    Types->>Migrator: Provide LegacyFXDenom
    Migrator->>Migrator: Process token migration
    Migrator->>Middleware: Transfer tokens
    Middleware->>Middleware: Validate denomination
    Middleware-->>Migrator: Confirm transfer
Loading

Possibly related PRs

Poem

🐰 A Rabbit's Denomination Tale 🌈

From IBCFXDenom to Legacy's might,
Our tokens dance in denominational light!
Migrations flow, tests gleam so bright,
A transformation of tokens, pure delight!
Hop, hop, hurray for code so right! 🚀

✨ 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.

Copy link

@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

🔭 Outside diff range comments (2)
app/upgrades/v8/upgrade.go (2)

Line range hint 365-371: Consider adding validation for metadata updates.

The metadata update logic skips LegacyFXDenom but doesn't validate that the new base denomination doesn't already exist. This could potentially lead to conflicts.

 if md.Base == fxtypes.LegacyFXDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != pundixSymbol {
     continue
 }
+// Validate that the new base denom doesn't exist
+newBase := strings.ToLower(md.Symbol)
+if bankKeeper.HasDenomMetaData(ctx, newBase) && newBase != md.Base {
+    return fmt.Errorf("metadata for denom %s already exists", newBase)
+}

Line range hint 704-744: Add transaction boundary for bank module migration.

The bank module migration performs multiple state changes without a clear transaction boundary. If any operation fails mid-way, it could leave the system in an inconsistent state.

Consider wrapping the entire migration in a transaction:

+func migrateBankModule(ctx sdk.Context, bankKeeper bankkeeper.Keeper) error {
+    // Create a cached context
+    cacheCtx, commit := ctx.CacheContext()
+
     sendEnabledEntry, found := bankKeeper.GetSendEnabledEntry(ctx, fxtypes.LegacyFXDenom)
     if found {
         bankKeeper.DeleteSendEnabled(ctx, fxtypes.LegacyFXDenom)
         bankKeeper.SetSendEnabled(ctx, fxtypes.DefaultDenom, sendEnabledEntry.Enabled)
     }
     // ... rest of the migration code ...
+    // If we get here, commit all changes
+    commit()
+    return nil
+}
🧹 Nitpick comments (3)
types/constant.go (1)

23-24: LGTM! Consider adding documentation.

The addition of LegacyFXDenom and its relationship with FXDenom is clear. Consider adding comments to document the purpose and distinction between these constants.

+// LegacyFXDenom represents the legacy FX token denomination in uppercase
 LegacyFXDenom = "FX"
+// FXDenom represents the current FX token denomination in lowercase
 FXDenom       = "fx"
x/erc20/migrations/v8/keys.go (1)

76-76: LGTM! Consider extracting the condition for better readability.

The migration logic correctly handles both LegacyFXDenom and lowercase symbol cases. Consider extracting the condition into a helper function for better readability.

+func isSpecialDenom(md banktypes.Metadata) bool {
+    return md.Base == fxtypes.LegacyFXDenom || md.Base == strings.ToLower(md.Symbol)
+}
+
 func (m Migrator) migrateTokenPair(ctx sdk.Context, store storetypes.KVStore) error {
     // ...
-    if md.Base == fxtypes.LegacyFXDenom || md.Base == strings.ToLower(md.Symbol) {
+    if isSpecialDenom(md) {
         // ...
     }
     // ...
 }
app/upgrades/v8/upgrade.go (1)

Line range hint 749-757: Add validation for chain ID in escrow denoms migration.

The function assumes the chain ID will be either mainnet or testnet but doesn't handle invalid chain IDs.

 func GetMigrateEscrowDenoms(chainID string) map[string]string {
+    if chainID != fxtypes.MainnetChainId && chainID != fxtypes.TestnetChainId {
+        panic(fmt.Sprintf("unsupported chain ID: %s", chainID))
+    }
     result := make(map[string]string, 2)
     result[fxtypes.LegacyFXDenom] = fxtypes.DefaultDenom
     // ... rest of the function
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7268f86 and 7793f6d.

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

28-29: LGTM! Consistent denomination updates.

The replacement of IBCFXDenom with LegacyFXDenom in the denomination mapping is consistent with the PR's objective.

Also applies to: 40-41

types/denom_wrap_test.go (1)

32-32: LGTM! Test cases updated consistently.

The test cases have been properly updated to use LegacyFXDenom, maintaining test coverage for the denomination changes.

Also applies to: 40-40, 56-56, 64-64

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

20-20: LGTM! Consistent replacement of denomination source.

The changes correctly replace the function call OriginalFXDenom() with the constant LegacyFXDenom, maintaining the same logic while improving code consistency.

Also applies to: 27-27

x/ibc/middleware/keeper/relay.go (1)

142-142: LGTM! Consistent denomination handling in IBC packet processing.

The change correctly updates the denomination check from IBCFXDenom to LegacyFXDenom, maintaining consistency with the broader refactoring effort.

x/ibc/middleware/keeper/relay_test.go (1)

151-151: LGTM! Test data updated to match implementation changes.

The test case correctly uses LegacyFXDenom to align with the changes in the implementation code.

app/upgrade_test.go (1)

645-647: LGTM! Test validations updated consistently.

The changes correctly update all test validations to use LegacyFXDenom, maintaining consistency across:

  • ERC20 token existence checks
  • Balance denomination validations
  • Send-enabled entry validations

Also applies to: 672-672, 683-683

app/upgrades/v8/upgrade.go (1)

Line range hint 227-233: Verify the token amount conversion during escrow migration.

The code swaps the amount for LegacyFXDenom tokens in escrow. Ensure that:

  1. The swap ratio is correctly applied
  2. No precision is lost during the conversion
  3. All escrow balances are properly accounted for
✅ Verification successful

Token amount conversion during escrow migration is correctly implemented

The conversion implementation is safe and precise:

  • Uses integer division (QuoRaw) for 100:1 ratio
  • Properly zeroes out old denomination before setting new one
  • Maintains precision without floating-point operations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify escrow balances and conversion ratios
# Check for any remaining LegacyFXDenom in escrow
rg -A 5 "SetTotalEscrowForDenom.*LegacyFXDenom"

# Check for SwapAmount implementation
ast-grep --pattern 'func SwapAmount($_) $_'

Length of output: 237

Comment on lines +679 to +687
erc20Token, err := keeper.GetERC20Token(ctx, fxtypes.LegacyFXDenom)
if err != nil {
return err
}
erc20Token.Denom = fxtypes.DefaultDenom
if err = keeper.ERC20Token.Set(ctx, erc20Token.Denom, erc20Token); err != nil {
return err
}
return keeper.ERC20Token.Remove(ctx, fxDenom)
return keeper.ERC20Token.Remove(ctx, fxtypes.LegacyFXDenom)
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for token removal.

The function removes the old ERC20 token after setting the new one, but there's no rollback mechanism if the removal fails.

 if err = keeper.ERC20Token.Set(ctx, erc20Token.Denom, erc20Token); err != nil {
     return err
 }
-return keeper.ERC20Token.Remove(ctx, fxtypes.LegacyFXDenom)
+if err = keeper.ERC20Token.Remove(ctx, fxtypes.LegacyFXDenom); err != nil {
+    // Rollback the previous set operation
+    keeper.ERC20Token.Remove(ctx, erc20Token.Denom)
+    return fmt.Errorf("failed to remove legacy token: %w", err)
+}
+return nil
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
erc20Token, err := keeper.GetERC20Token(ctx, fxtypes.LegacyFXDenom)
if err != nil {
return err
}
erc20Token.Denom = fxtypes.DefaultDenom
if err = keeper.ERC20Token.Set(ctx, erc20Token.Denom, erc20Token); err != nil {
return err
}
return keeper.ERC20Token.Remove(ctx, fxDenom)
return keeper.ERC20Token.Remove(ctx, fxtypes.LegacyFXDenom)
erc20Token, err := keeper.GetERC20Token(ctx, fxtypes.LegacyFXDenom)
if err != nil {
return err
}
erc20Token.Denom = fxtypes.DefaultDenom
if err = keeper.ERC20Token.Set(ctx, erc20Token.Denom, erc20Token); err != nil {
return err
}
if err = keeper.ERC20Token.Remove(ctx, fxtypes.LegacyFXDenom); err != nil {
// Rollback the previous set operation
keeper.ERC20Token.Remove(ctx, erc20Token.Denom)
return fmt.Errorf("failed to remove legacy token: %w", err)
}
return nil

@fx0x55 fx0x55 merged commit 3b8384b into main Jan 20, 2025
13 checks passed
@fx0x55 fx0x55 deleted the zakir/old-fx-denom branch January 20, 2025 09:24
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.

2 participants