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

Include prefix in address conversion #271

Merged
merged 2 commits into from
Oct 25, 2024
Merged

Conversation

joshklop
Copy link
Collaborator

@joshklop joshklop commented Oct 25, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for Ethereum to Cosmos address conversions.
    • Updated address handling in transaction processing and minting operations.
  • Bug Fixes

    • Improved robustness of address conversion processes to prevent silent failures.
  • Documentation

    • Updated function signatures to reflect changes in address handling and error management.
  • Tests

    • Revised test cases to ensure proper handling of converted addresses and improved error checks.

Copy link
Contributor

coderabbitai bot commented Oct 25, 2024

Walkthrough

The changes in this pull request primarily focus on enhancing error handling and address conversion processes across several files. Modifications include updates to the EvmToCosmosAddress function to include error handling and a new parameter, "cosmos". Various test files have been adjusted to reflect these changes, ensuring that address conversions are validated and that the correct address types are used throughout. The overall goal is to improve the robustness and maintainability of transaction handling related to Ethereum and Cosmos addresses.

Changes

File Path Change Summary
builder/builder_test.go Updated TestBuildRollupTxs for error handling in address conversion and simplified address usage.
e2e/stack_test.go Enhanced address conversion with error checks and updated function calls to include new parameters.
utils/utils.go Changed import statement and updated EvmToCosmosAddress to include error handling and a prefix.
x/rollup/keeper/deposits.go Modified address handling in processL1UserDepositTxs, mintETH, and mintERC20 with error checks.
x/rollup/tests/integration/rollup_test.go Updated address handling in balance queries and message structures, ensuring type consistency.
scripts/go-wrapper.sh Adjusted linker flags management by introducing an LDFLAGS variable for Go command execution.

Possibly related PRs

Suggested reviewers

  • NiloCK

🐇 In the land of code where the rabbits play,
We fix the bugs and make them sway.
With addresses clear and errors caught,
Our tests now shine, and troubles are naught!
Hopping through changes, we leap with glee,
For a robust future, just wait and see! 🌟


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.

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.

@joshklop joshklop force-pushed the joshklop.addr-conversion-prefix branch from 6027e1c to 6858e7b Compare October 25, 2024 13:20
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: 1

🧹 Outside diff range and nitpick comments (7)
utils/utils.go (1)

27-27: Enhance function documentation.

The current comment is too brief. Consider adding details about:

  • The purpose of the prefix parameter
  • The format of the returned string
  • Possible error conditions

Example:

// EvmToCosmosAddress converts an Ethereum address to a bech32-encoded string using the specified prefix.
// The prefix determines the address type (e.g., "cosmos" for standard accounts).
// Returns the bech32-encoded address string or an error if the conversion fails.
x/rollup/keeper/deposits.go (4)

124-135: Enhance error messages for address conversion failures.

The error messages could be more specific about which address (sender or recipient) failed conversion to help with debugging.

Apply this diff to improve error messages:

-			return nil, fmt.Errorf("evm to cosmos address: %v", err)
+			return nil, fmt.Errorf("failed to convert sender EVM address %s to cosmos address: %v", from.String(), err)
-			return nil, fmt.Errorf("evm to cosmos address: %v", err)
+			return nil, fmt.Errorf("failed to convert recipient EVM address %s to cosmos address: %v", tx.To().String(), err)

243-243: Use Bech32 address string in error message for better readability.

The error message uses the SDK address which may not be human-readable. Consider using the original Bech32 address string.

Apply this diff:

-			return nil, fmt.Errorf("failed to send ETH deposit coins from rollup module to user account %v: %v", recipientAddrSDK, err)
+			return nil, fmt.Errorf("failed to send ETH deposit coins from rollup module to user account %s: %v", recipientAddr, err)

292-293: Enhance error message for failed coin transfer.

The error message could be more specific about the ERC20 token being transferred.

Apply this diff:

-		return nil, fmt.Errorf("failed to send ERC-20 deposit coins from rollup module to user account %v: %v", userAddr, err)
+		return nil, fmt.Errorf("failed to send ERC-20 token %s deposit coins from rollup module to user account %s: %v", erc20addr, userAddr, err)

195-198: Enhance error message for cross-domain address conversion.

The error message could provide more context about the cross-domain message processing.

Apply this diff:

-			return nil, fmt.Errorf("evm to cosmos address: %v", err)
+			return nil, fmt.Errorf("failed to convert cross-domain recipient EVM address %s to cosmos address: %v", finalizeBridgeERC20.To.String(), err)
builder/builder_test.go (1)

317-323: Consider using a constant for the "cosmos" prefix.

While the implementation correctly uses the new prefix parameter, having "cosmos" as a magic string could be improved by defining it as a constant. This would make it easier to maintain and modify the prefix if needed.

Consider defining a constant at the package level:

const CosmosAddressPrefix = "cosmos"

Then use it in the conversions:

-recipientAddr, err := utils.EvmToCosmosAddress("cosmos", *depositTxETH.To())
+recipientAddr, err := utils.EvmToCosmosAddress(CosmosAddressPrefix, *depositTxETH.To())

-mintAddr, err := utils.EvmToCosmosAddress("cosmos", from)
+mintAddr, err := utils.EvmToCosmosAddress(CosmosAddressPrefix, from)
e2e/stack_test.go (1)

334-335: Consider extracting the "cosmos" prefix as a constant.

While the implementation is correct, consider extracting the "cosmos" string as a package-level constant to improve maintainability and ensure consistency across the codebase.

+const (
+    cosmosAddressPrefix = "cosmos"
+)

-senderAddr, err := utils.EvmToCosmosAddress("cosmos", *withdrawalTx.Sender)
+senderAddr, err := utils.EvmToCosmosAddress(cosmosAddressPrefix, *withdrawalTx.Sender)

Also applies to: 339-339

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2f8bb43 and 6858e7b.

📒 Files selected for processing (5)
  • builder/builder_test.go (2 hunks)
  • e2e/stack_test.go (3 hunks)
  • utils/utils.go (2 hunks)
  • x/rollup/keeper/deposits.go (7 hunks)
  • x/rollup/tests/integration/rollup_test.go (5 hunks)
🔇 Additional comments (9)
utils/utils.go (2)

7-7: LGTM! Appropriate import for bech32 encoding.

The switch to using the bech32 package directly is a good choice as it provides more explicit control over address encoding.


28-33: LGTM! Clean implementation with proper error handling.

The implementation is concise and handles errors appropriately. The use of bech32.ConvertAndEncode is the correct approach for this conversion.

Let's verify that all callers have been updated to handle the new signature:

✅ Verification successful

All callers of EvmToCosmosAddress are properly updated with the new signature and error handling

The verification shows that all callers:

  • Pass the required prefix parameter (either "cosmos" or using sdk.GetConfig().GetBech32AccountAddrPrefix())
  • Handle the returned error appropriately through either:
    • require.NoError(t, err) in test files
    • Error logging with context in keeper files
    • Error propagation with wrapping in business logic
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all callers of EvmToCosmosAddress are updated to handle the new signature

# Search for function calls to ensure they include the prefix parameter and handle errors
rg -A 2 "EvmToCosmosAddress\(" --type go

Length of output: 2909

x/rollup/tests/integration/rollup_test.go (3)

53-57: LGTM: Proper error handling for address conversions.

The error handling for EVM to Cosmos address conversions is well implemented, with appropriate error checks using require.NoError.


67-69: LGTM: Consistent error handling for ERC20 address conversion.

The error handling pattern is consistently applied to the ERC20 user address conversion.


94-97: Verify the withdrawal target address format.

While the Sender field correctly uses the Cosmos address, consider validating the Target address format to ensure it's a valid Ethereum address. This would prevent potential issues with invalid withdrawal addresses.

Also applies to: 104-107

builder/builder_test.go (2)

Line range hint 326-356: LGTM! Changes look good.

The withdrawal message construction and deposit check are properly implemented with the converted addresses.


Line range hint 317-356: Verify all usages of EvmToCosmosAddress are updated.

Let's ensure all calls to EvmToCosmosAddress across the codebase have been updated to include the prefix parameter.

✅ Verification successful

All usages of EvmToCosmosAddress have been properly updated with prefix parameter

Based on the search results, all calls to EvmToCosmosAddress across the codebase are correctly using two parameters:

  • Production code in x/rollup/keeper/deposits.go uses sdk.GetConfig().GetBech32AccountAddrPrefix()
  • Test files consistently use either "cosmos" or sdk.GetConfig().GetBech32AccountAddrPrefix()
  • The function definition in utils/utils.go correctly accepts both parameters
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all usages of EvmToCosmosAddress to verify they include the prefix parameter
# Expected: All calls should have two arguments (prefix and address)

echo "Searching for EvmToCosmosAddress usage patterns..."
rg "EvmToCosmosAddress\([^,)]+\)" --type go

echo -e "\nSearching for updated usage patterns..."
rg "EvmToCosmosAddress\([^,]+,[^)]+\)" --type go

Length of output: 2215

e2e/stack_test.go (2)

319-320: LGTM! Proper error handling for address conversion.

The addition of error handling for the address conversion is a good practice, and the consistent use of the "cosmos" prefix parameter aligns with the changes across the codebase.


536-538: LGTM! Consistent address conversion implementation.

The implementation maintains consistency with other address conversions in the file and includes proper error handling.

x/rollup/tests/integration/rollup_test.go Show resolved Hide resolved
@joshklop joshklop force-pushed the joshklop.addr-conversion-prefix branch from 9e589c0 to 9e8be02 Compare October 25, 2024 13:53
x/rollup/keeper/deposits.go Outdated Show resolved Hide resolved
x/rollup/tests/integration/rollup_test.go Outdated Show resolved Hide resolved
The previous method of converting EVM to Cosmos addresses used a global variable in the Cosmos SDK that may not be set in e2e tests.
This led to difficult-to-debug errors.
@joshklop joshklop force-pushed the joshklop.addr-conversion-prefix branch from 9e8be02 to 64b7a4c Compare October 25, 2024 15: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: 5

🧹 Outside diff range and nitpick comments (2)
utils/utils.go (1)

27-33: Enhance function documentation for clarity

While the implementation is solid, the documentation could be more descriptive about:

  • The expected prefix format
  • The returned address format
  • Possible error conditions

Consider updating the comment to:

-// EvmToCosmosAddress converts an EVM address to a string
+// EvmToCosmosAddress converts an Ethereum address to a bech32 encoded Cosmos address string.
+// The prefix parameter determines the bech32 address prefix (e.g., "cosmos", "osmo").
+// Returns the bech32 encoded address string or an error if the conversion fails.
e2e/stack_test.go (1)

536-538: Improve error handling in ERC20 test flow.

The error handling for address conversion is consistent with other test cases. However, consider adding test cases for invalid addresses to ensure the error handling works as expected.

Add a test case with an invalid address:

_, err = utils.EvmToCosmosAddress("cosmos", common.Address{})
require.Error(t, err, "should fail for zero address")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6858e7b and 64b7a4c.

📒 Files selected for processing (6)
  • builder/builder_test.go (2 hunks)
  • e2e/stack_test.go (3 hunks)
  • scripts/go-wrapper.sh (1 hunks)
  • utils/utils.go (2 hunks)
  • x/rollup/keeper/deposits.go (7 hunks)
  • x/rollup/tests/integration/rollup_test.go (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • builder/builder_test.go
  • x/rollup/tests/integration/rollup_test.go
🔇 Additional comments (6)
scripts/go-wrapper.sh (1)

9-10: LGTM! Proper environment variable handling

The export of LDFLAGS before go command execution is correct and preserves any existing flags from the environment.

utils/utils.go (2)

7-7: LGTM: Import change aligns with new implementation

The addition of the bech32 package is appropriate for the new address encoding approach.


28-33: Verify the impact of breaking signature change

The implementation looks good with proper error handling and standard bech32 encoding. However, since this is a breaking change, we should verify all callers have been updated.

✅ Verification successful

Let me analyze the results from the executed script. I can see all the usages of EvmToCosmosAddress in the codebase, and they all follow the same signature pattern:

  1. All calls pass a prefix string as the first argument (either "cosmos" or using sdk.GetConfig().GetBech32AccountAddrPrefix())
  2. All calls pass an EVM address as the second argument
  3. All calls properly handle the error return value

Since all callers are already using the correct signature and handling errors appropriately, I can generate the final response:

Breaking signature change verification completed - all callers are compatible

All usages of EvmToCosmosAddress across the codebase are already using the correct signature with proper error handling, including:

  • x/rollup/keeper/deposits.go
  • x/rollup/tests/integration/rollup_test.go
  • e2e/stack_test.go
  • builder/builder_test.go
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all usages of EvmToCosmosAddress to ensure they've been updated with the new signature
rg -l "EvmToCosmosAddress" | while read -r file; do
  echo "=== $file ==="
  rg -A 2 "EvmToCosmosAddress" "$file"
done

Length of output: 2741

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

124-129: LGTM! Improved address conversion with proper error handling.

The changes correctly retrieve the Bech32 prefix from SDK config and add comprehensive error handling for address conversion.

Also applies to: 131-135


195-198: LGTM! Consistent address conversion implementation.

The changes maintain consistency with the address conversion pattern used elsewhere in the codebase.

Also applies to: 202-202


231-234: Rename recipientSDKAddr to recipientAddr for consistency.

As per previous review comment, consider renaming recipientSDKAddr to match the naming convention used with mintSDKAddr.

Also applies to: 240-240

scripts/go-wrapper.sh Show resolved Hide resolved
x/rollup/keeper/deposits.go Show resolved Hide resolved
x/rollup/keeper/deposits.go Show resolved Hide resolved
e2e/stack_test.go Show resolved Hide resolved
e2e/stack_test.go Show resolved Hide resolved
@joshklop joshklop merged commit 63bbf2c into main Oct 25, 2024
16 checks passed
@joshklop joshklop deleted the joshklop.addr-conversion-prefix branch October 25, 2024 19:05
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