Skip to content

fix: prevent cctx being set as abortRefunded if the abort processing failed before the refund#3901

Merged
lumtis merged 3 commits intodevelopfrom
fix/abort-refund
May 22, 2025
Merged

fix: prevent cctx being set as abortRefunded if the abort processing failed before the refund#3901
lumtis merged 3 commits intodevelopfrom
fix/abort-refund

Conversation

@lumtis
Copy link
Contributor

@lumtis lumtis commented May 22, 2025

Description

Fixes https://github.com/zeta-chain/protocol-private/issues/244

Add also some tests that were missing for processAbort

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of cross-chain transaction aborts to ensure transactions are not incorrectly marked as refunded if the abort processing fails before the refund is completed.
  • Tests

    • Enhanced test coverage for abort processing, including new tests for different error scenarios during aborts.
  • Documentation

    • Updated changelog to reflect the fix in cross-chain transaction abort handling.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 22, 2025

📝 Walkthrough

Walkthrough

This update refines the abort refund logic in cross-chain transaction handling. It introduces nuanced error handling to distinguish between generic abort failures and specific onAbort handler failures, ensuring the refund status accurately reflects the underlying outcome. Associated tests and error definitions are updated to validate and support this improved behavior.

Changes

File(s) Change Summary
changelog.md Added a new fix entry describing the improved handling of the abort refund flag in cross-chain transactions, preventing it from being set when abort processing fails before the refund is completed.
x/crosschain/keeper/abort.go Refined logic for setting the IsAbortRefunded flag in ProcessAbort. The flag is now set only if there is no error or if the error is specifically ErrOnAbortFailed, ensuring accurate reflection of refund status. Assignment occurs before commit; error message handling remains unchanged.
x/crosschain/keeper/abort_test.go Split the abort failure test into two: one for generic errors (refund flag not set) and one for ErrOnAbortFailed (refund flag set). This validates the new nuanced error handling for abort refunds.
x/fungible/keeper/deposits.go Updated imports to resolve naming conflicts, and in ProcessAbort, now wraps onAbort call errors using errors.Join with ErrOnAbortFailed. Updated error wrapping in getAndCheckZRC20 to use the correct alias. No changes to exported APIs.
x/fungible/keeper/deposits_test.go Added deployTestAbort helper and a comprehensive TestKeeper_ProcessAbort suite, covering success, onAbort failure, rejection for ZETA coin, and invalid chain ID. Tests validate correct error propagation and state changes.
x/fungible/types/evm.go Introduced a new exported error variable ErrOnAbortFailed to represent abort handler failures. No other logic changes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CrossChainKeeper
    participant FungibleKeeper
    participant EVMContract

    User->>CrossChainKeeper: Initiate abort (ProcessAbort)
    CrossChainKeeper->>FungibleKeeper: ProcessAbort
    FungibleKeeper->>EVMContract: Call onAbort
    alt onAbort succeeds
        FungibleKeeper-->>CrossChainKeeper: Success, no error
        CrossChainKeeper-->>User: Mark abort as refunded (IsAbortRefunded = true)
    else onAbort fails with ErrOnAbortFailed
        FungibleKeeper-->>CrossChainKeeper: Error (ErrOnAbortFailed)
        CrossChainKeeper-->>User: Mark abort as refunded (IsAbortRefunded = true)
    else onAbort fails with other error
        FungibleKeeper-->>CrossChainKeeper: Generic error
        CrossChainKeeper-->>User: Do not mark abort as refunded (IsAbortRefunded = false)
    end
Loading

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 30th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

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.

@codecov
Copy link

codecov bot commented May 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.85%. Comparing base (0da8f89) to head (7962c75).
Report is 2 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3901      +/-   ##
===========================================
+ Coverage    64.69%   64.85%   +0.15%     
===========================================
  Files          469      469              
  Lines        34352    34357       +5     
===========================================
+ Hits         22224    22281      +57     
+ Misses       11100    11044      -56     
- Partials      1028     1032       +4     
Files with missing lines Coverage Δ
x/crosschain/keeper/abort.go 82.11% <100.00%> (+0.11%) ⬆️
x/fungible/keeper/deposits.go 68.53% <100.00%> (+16.81%) ⬆️
x/fungible/types/evm.go 100.00% <ø> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lumtis lumtis marked this pull request as ready for review May 22, 2025 12:51
@lumtis lumtis requested a review from a team as a code owner May 22, 2025 12:51
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

🧹 Nitpick comments (1)
x/fungible/keeper/deposits_test.go (1)

793-822: Consider improving test name for clarity

While the test correctly verifies that ZETA token aborts are rejected with an error different from ErrOnAbortFailed, the test name could be more explicit about testing the error type distinction.

-	t.Run("can't process abort for ZETA token", func(t *testing.T) {
+	t.Run("should reject ZETA token aborts with error distinct from ErrOnAbortFailed", func(t *testing.T) {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 863a1cc and 7962c75.

📒 Files selected for processing (6)
  • changelog.md (1 hunks)
  • x/crosschain/keeper/abort.go (1 hunks)
  • x/crosschain/keeper/abort_test.go (1 hunks)
  • x/fungible/keeper/deposits.go (4 hunks)
  • x/fungible/keeper/deposits_test.go (3 hunks)
  • x/fungible/types/evm.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.go`: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

**/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

  • x/fungible/types/evm.go
  • x/crosschain/keeper/abort.go
  • x/fungible/keeper/deposits.go
  • x/fungible/keeper/deposits_test.go
  • x/crosschain/keeper/abort_test.go
🧬 Code Graph Analysis (3)
x/crosschain/keeper/abort.go (1)
x/fungible/types/evm.go (1)
  • ErrOnAbortFailed (14-14)
x/fungible/keeper/deposits_test.go (5)
e2e/contracts/testabort/TestAbort.go (1)
  • TestAbortMetaData (43-46)
x/crosschain/keeper/abort_test.go (1)
  • TestKeeper_ProcessAbort (24-189)
pkg/chains/chains.go (1)
  • DefaultChainsList (511-551)
testutil/sample/crypto.go (1)
  • EthAddress (89-91)
x/fungible/types/evm.go (1)
  • ErrOnAbortFailed (14-14)
x/crosschain/keeper/abort_test.go (5)
testutil/keeper/crosschain.go (3)
  • CrosschainKeeperWithMocks (95-240)
  • CrosschainMockOptions (38-47)
  • GetCrosschainFungibleMock (291-295)
testutil/sample/crosschain.go (1)
  • CrossChainTx (266-280)
testutil/sample/crypto.go (1)
  • EthAddress (89-91)
x/crosschain/types/status.go (1)
  • StatusMessages (8-13)
x/fungible/types/evm.go (1)
  • ErrOnAbortFailed (14-14)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: e2e-ok
🔇 Additional comments (13)
x/fungible/types/evm.go (1)

11-15: Appropriate error sentinel introduction for improved error handling.

The new ErrOnAbortFailed sentinel error effectively distinguishes between generic abort failures and the specific case where funds are refunded but the onAbort handler fails.

x/crosschain/keeper/abort.go (1)

97-100: Improved conditional setting of IsAbortRefunded flag.

This change ensures the refunded status accurately reflects reality - only marking a transaction as refunded when either:

  1. The abort process completes successfully (err == nil), or
  2. The funds were refunded but the onAbort handler failed (ErrOnAbortFailed)

This prevents incorrect status reporting when actual refund processing fails.

x/fungible/keeper/deposits.go (3)

3-11: Proper error package imports organization.

The addition of the standard "errors" package alongside renaming the existing errors import to "errorspkg" resolves potential naming conflicts while maintaining backward compatibility.


225-240: Enhanced error handling for abort processing.

The modified error handling properly wraps errors from CallExecuteAbort with the sentinel error ErrOnAbortFailed. This creates a clear distinction between generic failures and the specific case where funds are refunded but the contract's onAbort handler fails.

This change works in conjunction with the updated logic in the crosschain module to ensure accurate tracking of refund status.


269-273: Consistent error handling with renamed package.

The error wrapping was updated to use the renamed errorspkg package alias, maintaining consistent error handling patterns.

x/crosschain/keeper/abort_test.go (2)

118-152: Well-structured test for handling non-onAbort errors

This test correctly verifies that when ProcessAbort fails with an error other than ErrOnAbortFailed, the CCTX status remains Aborted but IsAbortRefunded is not set to true. This distinction is crucial for the PR objective of preventing incorrect abortRefunded status.


154-188: Excellent test for onAbort failure handling

This test properly validates that when ProcessAbort fails with ErrOnAbortFailed (joined with another error), the CCTX is still marked as abortRefunded despite the error. The use of errors.Join is appropriate for testing error wrapping behavior.

x/fungible/keeper/deposits_test.go (5)

4-4: Appropriate import for test abort contract

The import for the testabort contract is correctly added to support the new abort tests.


72-80: Well-implemented helper function for abort tests

This helper function follows the established pattern for contract deployment helpers in the codebase, making it easy to use in the new abort tests.


721-754: Good test for successful abort processing

This test correctly verifies that ProcessAbort works as expected when the onAbort call succeeds. It appropriately checks that the balance is updated after a successful abort operation.


756-791: Effective test for onAbort failure case

This test properly validates that when onAbort fails (due to an invalid contract address), the error returned is ErrOnAbortFailed. It also correctly verifies that the balance is still updated despite the onAbort function failure, which is an important part of the behavior being fixed in this PR.


824-851: Complete test for invalid chain ID case

This test correctly verifies that an abort for an invalid chain ID is rejected with an error that is not ErrOnAbortFailed, which helps validate the error handling patterns implemented in the PR.

changelog.md (1)

51-51: Changelog entry looks accurate and well-formatted.

The new fix entry correctly documents PR #3901 under the "Fixes" section, matches the PR summary, and follows existing formatting conventions.

@lumtis lumtis added this pull request to the merge queue May 22, 2025
@github-actions github-actions bot added the MERGE_FAILED Merge Queue CI failed label May 22, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 22, 2025
@lumtis lumtis added the CONSENSUS_BREAKING_ACK Acknowledge a consensus breaking change label May 22, 2025
@lumtis lumtis added this pull request to the merge queue May 22, 2025
Merged via the queue into develop with commit 2b06813 May 22, 2025
59 checks passed
@lumtis lumtis deleted the fix/abort-refund branch May 22, 2025 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CONSENSUS_BREAKING_ACK Acknowledge a consensus breaking change MERGE_FAILED Merge Queue CI failed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants