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: deprecate finalize_arb and make new version of it #172

Merged
merged 6 commits into from
Dec 23, 2024

Conversation

ALPAC-4
Copy link
Contributor

@ALPAC-4 ALPAC-4 commented Dec 19, 2024

Description

Fix

As MsgFinalizeWithdrawal interface changed deprecated finalize_arb and create finalize_arb_v2

Difference between finalize_arb and finalize_arb_v2

  • In finalize_arb, the pool directly executed MsgFinalizeWithdrawal.
  • In finalize_arb_v2, the executor must execute MsgFinalizeWithdrawal themselves before calling finalize_arb_v2.

Although a mismatch between MsgFinalizeWithdrawal and arb_info can occur, it doesn't affect the final result since execution only proceeds if the current balance is greater than or equal to arb_info.ibc_op_init_sent.

Closes: #XXXX


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
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • 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.

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

  • New Features

    • Introduced a new entry function for finalizing arbitrage operations, simplifying the process for users.
  • Deprecations

    • Deprecated the previous finalization function, providing a clearer error message for users who attempt to use it.

@ALPAC-4 ALPAC-4 requested a review from a team as a code owner December 19, 2024 07:01
Copy link

coderabbitai bot commented Dec 19, 2024

Walkthrough

The pull request introduces changes to the initia_std::minitswap module, focusing on simplifying the arbitrage finalization process. A new entry function finalize_arb_v2 is added with a streamlined signature, replacing the more complex finalize_arb function. The existing function is marked as deprecated and will abort if called, reducing the number of parameters required for arbitrage finalization and improving the module's interaction model.

Changes

File Change Summary
precompile/modules/initia_stdlib/sources/minitswap.move - Added new entry function finalize_arb_v2 with simplified parameters
- Deprecated finalize_arb function with error handling
- Introduced new error constant EDEPRECATED

Sequence Diagram

sequenceDiagram
    participant User
    participant MiniswapModule
    participant PoolState

    User->>MiniswapModule: finalize_arb_v2(account, arb_index)
    MiniswapModule->>PoolState: Check pool balance
    MiniswapModule->>MiniswapModule: Execute finalize_arb_hook
    MiniswapModule-->>User: Arbitrage finalization complete
Loading

Possibly related PRs

Suggested reviewers

  • beer-1

Poem

🐰 Hop, hop, arbitrage dance!
A new function takes its stance
Parameters trimmed with care
Deprecated code beware
Minitswap's magic, swift and bright
Simplicity now shines so light! 🌟


🪧 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: 0

🧹 Nitpick comments (1)
precompile/modules/initia_stdlib/sources/minitswap.move (1)

1455-1459: New entry function aligns with the updated withdrawal flow.

The comments on lines 1455-1457 clarify the rationale behind having the executor manually trigger MsgFinalizeWithdrawal before calling “finalize_arb_v2”. This approach reduces the complexity of the pool’s responsibilities. Ensure that all external references are updated to call this new function instead of the deprecated one.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e5294e7 and 7e844a3.

📒 Files selected for processing (1)
  • precompile/modules/initia_stdlib/sources/minitswap.move (3 hunks)
🔇 Additional comments (5)
precompile/modules/initia_stdlib/sources/minitswap.move (5)

45-45: Good use of a dedicated deprecation error code.

The addition of "EDEPRECATED" is a clear, standardized way to abort deprecated functions and communicate this status to developers.


1468-1469: Consistent retrieval of pool address and global state.

No issues here—we see that you address the pool object properly. It's a safe approach to first fetch the pool's address, then borrow the global resource.


1476-1487: Verification of pool balance is correct but consider further checks.

Verifying that 'uinit_pool_balance' covers 'arb_info.ibc_op_init_sent' prevents underflow. However, consider also checking whether the pool is active before finalizing the arbitrage. If the pool is deactivated after the arbitrage is set up but before this call, it might lead to confusion.


1488-1488: Empty line.

No changes needed here.


1489-1502: Properly deprecates the old finalize_arb function.

Aborting with EDEPRECATED is appropriate to avoid unintended usage. This matches the PR objective of manually calling MsgFinalizeWithdrawal first, then switching to “finalize_arb_v2.”

Copy link
Collaborator

@beer-1 beer-1 left a comment

Choose a reason for hiding this comment

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

I think we can remove generate_finalize_token_withdrawal_msg function (private function can be removed)

also can you build the binary?

Copy link
Collaborator

@beer-1 beer-1 left a comment

Choose a reason for hiding this comment

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

LGTM

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

🧹 Nitpick comments (2)
precompile/modules/initia_stdlib/sources/minitswap.move (2)

1477-1480: Consider adding more context to the error message

The error message for insufficient balance could be more descriptive to help users understand the specific requirement.

Consider updating the error message to include the expected and actual values:

-            error::invalid_state(ENOT_ENOUGH_BALANCE)
+            error::invalid_state_with_data(ENOT_ENOUGH_BALANCE, arb_info.ibc_op_init_sent - uinit_pool_balance)

1489-1502: Enhance deprecation notice with migration guidance

While the function is correctly marked as deprecated, it would be helpful to provide migration guidance in the documentation.

Add a more detailed deprecation notice:

-    // deprecated
+    /// @deprecated Use `finalize_arb_v2` instead.
+    /// This function is deprecated as the execution flow has changed to require
+    /// executors to trigger MsgFinalizeWithdrawal independently before calling
+    /// finalize_arb_v2.
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7e844a3 and 871ac52.

📒 Files selected for processing (1)
  • precompile/modules/initia_stdlib/sources/minitswap.move (3 hunks)
🔇 Additional comments (2)
precompile/modules/initia_stdlib/sources/minitswap.move (2)

45-45: LGTM: Error constant for deprecated function

The error constant follows the module's naming convention and is appropriately used for the deprecated function.


1455-1458: LGTM: Well-documented function signature change

The documentation clearly explains the change in execution flow where the executor must now trigger MsgFinalizeWithdrawal independently before calling this function.

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

🧹 Nitpick comments (2)
precompile/modules/initia_stdlib/sources/minitswap.move (2)

1455-1458: Add documentation for the new function.

The function lacks documentation explaining its purpose, parameters, and return values. Consider adding a doc comment that explains:

  • The purpose of the function
  • The parameters it accepts
  • Any preconditions or requirements
  • The expected behavior
+    /// Finalizes an arbitrage operation after MsgFinalizeWithdrawal has been executed.
+    /// * `account` - The account executing the finalization
+    /// * `arb_index` - The index of the arbitrage operation to finalize
+    ///
+    /// This function verifies that the pool has sufficient balance and triggers the finalization hook.
     public entry fun finalize_arb_v2(

1489-1502: Enhance deprecation notice with migration guide.

While the function is correctly marked as deprecated and aborts with the appropriate error, it would be helpful to provide more context about the deprecation and migration path in the documentation.

-    // deprecated
+    /// @deprecated This function is deprecated and will abort if called.
+    /// Migration: Use `finalize_arb_v2` instead. The new version requires executing
+    /// MsgFinalizeWithdrawal independently before calling finalize_arb_v2.
+    /// See documentation for `finalize_arb_v2` for more details.
     public entry fun finalize_arb(
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 871ac52 and cee8c2f.

📒 Files selected for processing (1)
  • precompile/modules/initia_stdlib/sources/minitswap.move (3 hunks)
🔇 Additional comments (2)
precompile/modules/initia_stdlib/sources/minitswap.move (2)

1476-1480: Verify potential race conditions in balance check.

The balance check between lines 1476-1480 could potentially be subject to race conditions in a concurrent environment. While Move's linear type system provides some protection, it would be good to verify that there are no scenarios where the balance could change between the check and the actual usage.

✅ Verification successful

Let me analyze the code further to verify the potential race conditions.


Based on the code analysis, I can now provide a final response regarding the potential race conditions in the balance check.

Race conditions in balance check are prevented by Move's resource model and acquires annotation.

The balance check at lines 1476-1480 is protected from race conditions by Move's type system and resource model:

  1. The function containing this code acquires the ModuleStore resource, as evidenced by the acquires annotations throughout the codebase
  2. Move's linear type system ensures that resources can only be accessed by one function at a time
  3. The balance check is performed within a function that has exclusive access to the pool's resources through the acquires mechanism

This means that no other transaction can modify the pool's balance while this check is being performed, effectively preventing race conditions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for other functions that might modify the pool balance
# to verify potential race conditions

# Check for functions that modify pool balance
ast-grep --pattern 'fun $name($params) {
  $$$
  primary_fungible_store::$action($_, $pool_addr, $_, $_)
  $$$
}'

# Check for functions that might be called concurrently
rg -A 5 "public entry fun" 

Length of output: 106121


Script:

#!/bin/bash
# Search for functions that might modify the pool balance or be called concurrently with the balance check

# Check for functions that read or modify pool balance
ast-grep --pattern 'fun $name($params) {
  $$$
  coin::balance($pool_addr, $_)
  $$$
}'

# Check for functions that might modify pool balance
ast-grep --pattern 'fun $name($params) {
  $$$
  primary_fungible_store::withdraw($_, $pool_addr, $_)
  $$$
}'

# Check for functions that might modify pool balance
ast-grep --pattern 'fun $name($params) {
  $$$
  primary_fungible_store::deposit($pool_addr, $_)
  $$$
}'

# Look for any acquire statements around the balance check
rg -B 5 -A 5 "acquires" precompile/modules/initia_stdlib/sources/minitswap.move

Length of output: 18496


Line range hint 1455-1502: Verify impact of breaking changes on existing integrations.

The change from finalize_arb to finalize_arb_v2 represents a significant workflow change where MsgFinalizeWithdrawal must now be executed independently. This is a breaking change that could affect existing integrations.

✅ Verification successful

The initial search showed only the function definition but we need to verify:

  1. If there are any other files importing or using this module
  2. If there are test files that might be affected
  3. If there are any other references to this functionality

This will help us determine if this is truly a breaking change that affects other parts of the codebase.


The previous search revealed that there's documentation that mentions finalize_arb. We should check this documentation and also look for related arbitrage functions to fully understand the scope of this change and its impact on the ecosystem.


Based on the search results and documentation, I can now generate the final response:

Breaking changes are properly handled with deprecation notice

The changes follow good deprecation practices:

  1. The old finalize_arb function is marked as deprecated and aborts with EDEPRECATED error
  2. The new finalize_arb_v2 function is introduced with clear documentation about the workflow change
  3. The only references to these functions are in the module itself, with no external dependencies found

The code changes represent a clean transition that maintains backward compatibility by keeping the old function while clearly marking it as deprecated. This allows existing integrations to gracefully migrate to the new workflow.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for existing usages of finalize_arb to identify affected code

# Search for direct function calls
rg -A 5 "finalize_arb\(" 

# Search for any tests that might need updating
rg -A 5 "test.*finalize.*arb" 

Length of output: 579


Script:

#!/bin/bash
# Let's search for any references to finalize_arb in the entire codebase
rg -l "finalize_arb"

# Also search for any potential imports of the minitswap module
rg "use.*minitswap"

# Look for any potential test files related to arbitrage
fd -e move test.*arb

Length of output: 173


Script:

#!/bin/bash
# Let's check the documentation to understand the impact
cat precompile/modules/initia_stdlib/doc/minitswap.md

# Let's also check if there are any other functions that might be related to arbitrage
rg -A 5 "arb" precompile/modules/initia_stdlib/sources/minitswap.move

Length of output: 65661

@ALPAC-4 ALPAC-4 merged commit bce40b3 into main Dec 23, 2024
3 checks passed
@ALPAC-4 ALPAC-4 deleted the fix/finalize-token-withdrawal branch December 23, 2024 04:17
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