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: improve ZETA deposit check with max supply check #3074

Merged
merged 2 commits into from
Oct 31, 2024

Conversation

lumtis
Copy link
Member

@lumtis lumtis commented Oct 31, 2024

Description

Add a check for ZETA deposit to not surpass a supply cap as an extra security check

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a method to retrieve the supply of specific tokens, enhancing the functionality of the banking interface.
    • Added a new error message for when the maximum supply of tokens is reached.
  • Bug Fixes

    • Improved error handling in minting processes to prevent exceeding the defined maximum supply of tokens.
  • Tests

    • Expanded test coverage for minting functionality, including edge cases for maximum supply limits and error handling scenarios.

Copy link
Contributor

coderabbitai bot commented Oct 31, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces several enhancements primarily focused on the FungibleBankKeeper mock and the minting processes for ZETA tokens. A new method GetSupply is added to the FungibleBankKeeper mock, facilitating testing. Additionally, the MintZetaToEVMAccount and MintZetaToFungibleModule methods are updated to enforce a maximum supply limit for ZETA tokens. This includes the introduction of a new error type, ErrMaxSupplyReached, and modifications to various test cases to validate the new functionality and error handling.

Changes

File Path Change Summary
testutil/keeper/mocks/fungible/bank.go Added method GetSupply(ctx types.Context, denom string) types.Coin to FungibleBankKeeper.
x/fungible/keeper/deposits_test.go Enhanced test cases for TestKeeper_ZRC20DepositAndCallContract and TestKeeper_DepositCoinZeta.
x/fungible/keeper/zeta.go Introduced constant ZETAMaxSupplyStr and updated methods MintZetaToEVMAccount and MintZetaToFungibleModule to enforce max supply logic.
x/fungible/keeper/zeta_test.go Added new test cases for MintZetaToEVMAccount and MintZetaToFungibleModule focusing on max supply scenarios.
x/fungible/keeper/zevm_message_passing_test.go Updated tests for TestKeeper_ZEVMDepositAndCallContract and TestKeeper_ZEVMRevertAndCallContract to include mocks for GetSupply.
x/fungible/types/errors.go Added new error variable ErrMaxSupplyReached with error code 1135.
x/fungible/types/expected_keepers.go Added method GetSupply(ctx sdk.Context, denom string) sdk.Coin to BankKeeper interface.

Possibly related PRs

Suggested labels

m:fungible, no-changelog

Suggested reviewers

  • skosito
  • kingpinXD
  • brewmaster012
  • swift1337
  • fbac

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

@lumtis lumtis added the no-changelog Skip changelog CI check label Oct 31, 2024
Copy link

codecov bot commented Oct 31, 2024

Codecov Report

Attention: Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 63.43%. Comparing base (6ef5c57) to head (7ab29ad).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
x/fungible/keeper/zeta.go 81.25% 2 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #3074   +/-   ##
========================================
  Coverage    63.43%   63.43%           
========================================
  Files          425      425           
  Lines        30003    30019   +16     
========================================
+ Hits         19031    19044   +13     
- Misses       10137    10139    +2     
- Partials       835      836    +1     
Files with missing lines Coverage Δ
x/fungible/keeper/zeta.go 88.88% <81.25%> (-11.12%) ⬇️

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

🧹 Outside diff range and nitpick comments (4)
testutil/keeper/mocks/fungible/bank.go (1)

Line range hint 11-14: Consider adding helper methods for common testing scenarios.

Since this mock is being used for testing ZETA deposit checks with max supply validation, consider adding helper methods to simplify test setup:

type FungibleBankKeeper struct {
	mock.Mock
+
+	// Helper methods for common testing scenarios
+	defaultSupply types.Coin
}

+// WithDefaultSupply sets up the mock to return a specific supply for any denom
+func (_m *FungibleBankKeeper) WithDefaultSupply(supply types.Coin) *FungibleBankKeeper {
+	_m.defaultSupply = supply
+	_m.On("GetSupply", mock.Anything, mock.Anything).Return(supply)
+	return _m
+}

This would allow test cases to be written more concisely:

keeper := NewFungibleBankKeeper(t).WithDefaultSupply(types.NewCoin("zeta", sdkmath.NewInt(1000)))
x/fungible/keeper/zeta_test.go (1)

34-72: Enhance test readability and maintainability.

While the test logic is correct, consider these improvements:

  1. Use a named constant for the overflow amount instead of magic number "1"
  2. Make test names more descriptive of the scenarios being tested
-	t.Run("mint the token to reach max supply", func(t *testing.T) {
+	t.Run("should successfully mint tokens up to exactly max supply", func(t *testing.T) {

-	t.Run("can't mint more than max supply", func(t *testing.T) {
+	t.Run("should fail when attempting to mint beyond max supply", func(t *testing.T) {

+	const overflowAmount = 1 // Amount to exceed max supply by
-		newAmount := zetaMaxSupply.Sub(supply).Add(sdk.NewInt(1))
+		newAmount := zetaMaxSupply.Sub(supply).Add(sdk.NewInt(overflowAmount))
x/fungible/keeper/zevm_message_passing_test.go (1)

Line range hint 1-378: Enhance overall test coverage with comprehensive scenarios.

While the current test suite covers basic functionality, consider adding the following test scenarios to improve coverage:

  1. Edge Cases:

    • Test with supply exactly at max limit
    • Test with amounts that would cause overflow
    • Test with zero amounts
  2. Error Conditions:

    • Test concurrent minting scenarios
    • Test recovery from failed transactions
  3. Integration Scenarios:

    • Test interaction between supply validation and contract reverts
    • Test supply validation with various contract states

These additional test cases would help ensure the robustness of the supply validation mechanism and its interaction with other system components.

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

440-443: Enhance mock setup clarity and error handling

The mock setup could be more explicit and maintainable. Consider these improvements:

-errorMint := errors.New("", 1, "error minting coins")
+errMintFailure := errors.New("fungible", 1, "mock: mint coins operation failed")

 bankMock.On("GetSupply", ctx, mock.Anything, mock.Anything).
-    Return(sdk.NewCoin(config.BaseDenom, sdk.NewInt(0))).
+    Return(sdk.NewCoin(config.BaseDenom, sdk.NewInt(0))).
+    Run(func(args mock.Arguments) {
+        // Verify that we're checking supply for the correct denomination
+        denom := args.Get(1).(string)
+        require.Equal(t, config.BaseDenom, denom)
+    }).
     Once()
-bankMock.On("MintCoins", ctx, types.ModuleName, mock.Anything).Return(errorMint).Once()
+bankMock.On("MintCoins", ctx, types.ModuleName, mock.Anything).
+    Run(func(args mock.Arguments) {
+        // Verify the coins being minted
+        coins := args.Get(2).(sdk.Coins)
+        require.Equal(t, amount.Int64(), coins.AmountOf(config.BaseDenom).Int64())
+    }).
+    Return(errMintFailure).
+    Once()

This improves the test by:

  1. Using a more descriptive error variable name
  2. Adding domain and context to the error message
  3. Adding assertions in mock callbacks to verify input parameters
  4. Making the mock setup more explicit about what's being tested
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6ef5c57 and 193d3a8.

📒 Files selected for processing (7)
  • testutil/keeper/mocks/fungible/bank.go (1 hunks)
  • x/fungible/keeper/deposits_test.go (1 hunks)
  • x/fungible/keeper/zeta.go (3 hunks)
  • x/fungible/keeper/zeta_test.go (5 hunks)
  • x/fungible/keeper/zevm_message_passing_test.go (2 hunks)
  • x/fungible/types/errors.go (1 hunks)
  • x/fungible/types/expected_keepers.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
testutil/keeper/mocks/fungible/bank.go (1)

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

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

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

x/fungible/keeper/zeta.go (1)

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

x/fungible/keeper/zeta_test.go (1)

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

x/fungible/keeper/zevm_message_passing_test.go (1)

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

x/fungible/types/errors.go (1)

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

x/fungible/types/expected_keepers.go (1)

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

🪛 GitHub Check: codecov/patch
x/fungible/keeper/zeta.go

[warning] 22-23: x/fungible/keeper/zeta.go#L22-L23
Added lines #L22 - L23 were not covered by tests


[warning] 44-45: x/fungible/keeper/zeta.go#L44-L45
Added lines #L44 - L45 were not covered by tests

🔇 Additional comments (2)
x/fungible/types/expected_keepers.go (1)

36-36: Well-designed interface extension for supply management.

The addition of GetSupply method to the BankKeeper interface is well-structured and follows good design principles:

  • Clear and purposeful method signature
  • Appropriate return type using sdk.Coin
  • Maintains interface cohesion with existing supply management methods
  • Enables proper implementation of max supply validation for ZETA tokens
x/fungible/types/errors.go (1)

37-37: Well-structured error definition.

The new error ErrMaxSupplyReached is well-defined and follows the module's established patterns:

  • Maintains sequential error codes (1135)
  • Uses clear, concise error message
  • Properly aligned with existing declarations
  • Directly supports the PR's objective of implementing max supply validation

x/fungible/keeper/zeta.go Outdated Show resolved Hide resolved
x/fungible/keeper/zeta.go Show resolved Hide resolved
x/fungible/keeper/zeta.go Outdated Show resolved Hide resolved
testutil/keeper/mocks/fungible/bank.go Show resolved Hide resolved
x/fungible/keeper/zeta_test.go Show resolved Hide resolved
x/fungible/keeper/zeta_test.go Show resolved Hide resolved
x/fungible/keeper/zeta_test.go Show resolved Hide resolved
Copy link
Contributor

@swift1337 swift1337 left a comment

Choose a reason for hiding this comment

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

Left a comment, other than that all good

@lumtis lumtis added this pull request to the merge queue Oct 31, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 31, 2024
@lumtis lumtis added this pull request to the merge queue Oct 31, 2024
Merged via the queue into develop with commit b58046f Oct 31, 2024
35 checks passed
@lumtis lumtis deleted the max-supply-zeta-develop branch October 31, 2024 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog Skip changelog CI check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants