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: add validations for evm address #2707

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

kingpinXD
Copy link
Contributor

@kingpinXD kingpinXD commented Aug 14, 2024

Description

Closes #2696

How Has This Been Tested?

  • Tested CCTX in localnet
  • Tested in development environment
  • Go unit tests
  • Go integration tests
  • Tested via GitHub Actions

Summary by CodeRabbit

  • New Features

    • Added a new test case for handling invalid address deposits in the Zeta deposit functionality.
    • Introduced a new function for validating cryptocurrency addresses across multiple blockchain networks.
    • Added comprehensive unit tests for address validation across various blockchain networks.
    • Enhanced error handling for invalid receiver addresses in cross-chain transactions.
  • Bug Fixes

    • Improved error handling for invalid receiver addresses in the EVM deposit process.
  • Documentation

    • Updated error messages for clarity in address validation processes.
  • Chores

    • Removed obsolete address validation functions and tests to streamline codebase.

Copy link
Contributor

coderabbitai bot commented Aug 14, 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

The changes introduce enhancements to the address validation processes across various components of the Zeta deposit functionality. New test cases have been added to ensure invalid addresses are correctly handled, and existing methods have been updated to utilize a more robust validation approach. Additionally, error handling has been improved to ensure that only valid addresses are processed, thereby minimizing the risk of truncation or erroneous processing of invalid address fields.

Changes

Files Change Summary
cmd/zetae2e/local/local.go, e2e/e2etests/e2etests.go, e2e/e2etests/test_zeta_deposit.go, e2e/e2etests/test_zeta_deposit_new_address.go, e2e/e2etests/test_zeta_deposit_restricted_address.go Added new test cases for invalid address handling and modified existing tests to ensure addresses are processed as byte slices.
e2e/runner/zeta.go Updated DepositZetaWithAmount method to accept addresses as byte slices instead of ethcommon.Address.
pkg/address/validate_address.go, pkg/address/validate_address_test.go Introduced address validation functionality for multiple blockchain networks and added corresponding unit tests to ensure robustness.
x/crosschain/types/cctx.go, x/crosschain/types/errors.go, x/crosschain/types/message_whitelist_erc20.go, x/crosschain/types/validate.go, x/crosschain/types/validate_test.go, x/fungible/types/message_update_system_contract.go Enhanced address validation logic and error handling across various components, replacing old validation methods with new, more robust approaches.
x/crosschain/keeper/evm_deposit.go, x/crosschain/keeper/evm_deposit_test.go Modified the HandleEVMDeposit function to include error handling for invalid receiver addresses, along with corresponding test cases to validate this logic.

Assessment against linked issues

Objective Addressed Explanation
Invalid address fields may be truncated and processed in error (#2696)
Improve error handling for invalid addresses (#2696)
Ensure comprehensive testing for address validation (#2696)

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

Copy link

!!!WARNING!!!
nosec detected in the following files: e2e/e2etests/e2etests.go

Be very careful about using #nosec in code. It can be a quick way to suppress security warnings and move forward with development, it should be employed with caution. Suppressing warnings with #nosec can hide potentially serious vulnerabilities. Only use #nosec when you're absolutely certain that the security issue is either a false positive or has been mitigated in another way.

Only suppress a single rule (or a specific set of rules) within a section of code, while continuing to scan for other problems. To do this, you can list the rule(s) to be suppressed within the #nosec annotation, e.g: /* #nosec G401 */ or //#nosec G201 G202 G203
Broad #nosec annotations should be avoided, as they can hide other vulnerabilities. The CI will block you from merging this PR until you remove #nosec annotations that do not target specific rules.

Pay extra attention to the way #nosec is being used in the files listed above.

@github-actions github-actions bot added the nosec label Aug 14, 2024
Copy link

codecov bot commented Aug 14, 2024

Codecov Report

Attention: Patch coverage is 55.22388% with 30 lines in your changes missing coverage. Please review.

Project coverage is 66.54%. Comparing base (91c323d) to head (b0c2455).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
x/crosschain/types/cctx.go 0.00% 26 Missing ⚠️
pkg/address/validate_address.go 87.50% 3 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2707      +/-   ##
===========================================
- Coverage    66.79%   66.54%   -0.25%     
===========================================
  Files          370      371       +1     
  Lines        20677    20775      +98     
===========================================
+ Hits         13811    13825      +14     
- Misses        6230     6311      +81     
- Partials       636      639       +3     
Files with missing lines Coverage Δ
x/crosschain/keeper/cctx_gateway_zevm.go 100.00% <ø> (ø)
x/crosschain/keeper/evm_deposit.go 85.10% <100.00%> (-1.86%) ⬇️
x/crosschain/types/message_whitelist_erc20.go 100.00% <100.00%> (ø)
x/crosschain/types/validate.go 83.33% <ø> (+1.51%) ⬆️
x/fungible/types/message_update_system_contract.go 100.00% <100.00%> (ø)
pkg/address/validate_address.go 87.50% <87.50%> (ø)
x/crosschain/types/cctx.go 78.53% <0.00%> (-13.53%) ⬇️

... and 9 files with indirect coverage changes

@lumtis lumtis added UPGRADE_LIGHT_TESTS Run make start-upgrade-test-light and removed UPGRADE_LIGHT_TESTS Run make start-upgrade-test-light labels Aug 15, 2024
@kingpinXD kingpinXD changed the title refactor: add valiations for evm address refactor: add validations for evm address Aug 16, 2024
@@ -203,6 +205,51 @@ func (m CrossChainTx) SetOutboundBallotIndex(index string) {
}

// GetCctxIndexFromBytes returns the CCTX index from a byte array.
func (m CrossChainTx) GetValidReceiverAddress() (ethcommon.Address, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will be adding more unit tests , and wanted to open up this pr for discussions wether this sort of validation based on coin type is the direction we want to go with


// TODO : Use this function to validate Sender and Receiver address for CCTX
// https://github.com/zeta-chain/node/issues/2697
func ValidateAddressForChain(address string, chainID int64, additionalChains []chains.Chain) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same for this , Not using this function right now , but if used eventually it would make make the address validation a bit more strict . Wanted to get some thoughts before adding more unit tests and using this function

@kingpinXD kingpinXD marked this pull request as ready for review August 30, 2024 01:22
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, codebase verification and nitpick comments (3)
x/fungible/types/message_update_system_contract.go (1)

48-50: Add Unit Tests for ValidateEthereumAddress Usage

The recent changes to the address validation logic in message_update_system_contract.go do not appear to be covered by existing unit tests. It is essential to add tests to ensure the robustness of the ValidateEthereumAddress function, particularly after modifications to its usage.

  • Consider creating unit tests that specifically validate the MsgUpdateSystemContract struct and its address validation logic.
Analysis chain

LGTM! Ensure unit tests cover the new validation logic.

The changes improve the robustness of the address validation process. Ensure that unit tests are added to cover the new validation logic.

Run the following script to verify the test coverage:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the new validation logic is covered by tests.

# Test: Search for test functions that cover the new validation logic.
rg --type go -A 5 $'ValidateEthereumAddress'

Length of output: 3660

pkg/address/validate_address.go (1)

48-52: Add Unit Tests for ValidateEthereumAddress Function

The ValidateEthereumAddress function does not appear to be covered by existing unit tests. It is crucial to add dedicated unit tests to ensure the function behaves as expected and to maintain the robustness of the codebase.

  • Consider creating a new test file specifically for testing address validation functions.
  • Ensure edge cases and invalid inputs are thoroughly tested.
Analysis chain

LGTM! Ensure unit tests cover the new function.

The function provides robust validation for Ethereum addresses. Ensure that unit tests are added to cover the new function.

Run the following script to verify the test coverage:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the new function is covered by tests.

# Test: Search for test functions that cover the new function.
rg --type go -A 5 $'ValidateEthereumAddress'

Length of output: 3660


Script:

#!/bin/bash
# Search for test files that might indirectly test the ValidateEthereumAddress function.
rg --type go -l 'ValidateEthereumAddress' | grep '_test.go'

Length of output: 57

x/crosschain/types/message_whitelist_erc20.go (1)

57-59: Update all instances to use the new validation method.

The old method ethcommon.HexToAddress is still prevalent in the codebase, which may lead to inconsistent address validation. Ensure that all occurrences are updated to use address.ValidateEthereumAddress for uniform and robust validation.

  • Files where ethcommon.HexToAddress is used:
    • zetaclient/testutils/constant.go
    • zetaclient/testutils/mocks/tss_signer.go
    • zetaclient/orchestrator/bootstrap.go
    • x/fungible/keeper/system_contract.go
    • And many others as listed in the output.
Analysis chain

LGTM! But verify the usage of the new validation method.

The new validation method and enhanced error handling improve the robustness of the code. Ensure that all usages of the old validation method are updated to use address.ValidateEthereumAddress.

Run the following script to verify the usage of the new validation method:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of the new validation method.

# Test: Search for the old validation method. Expect: No occurrences of the old method.
rg --type go -A 5 $'ethcommon.HexToAddress'

# Test: Search for the new validation method. Expect: Occurrences of the new method.
rg --type go -A 5 $'address.ValidateEthereumAddress'

Length of output: 63833

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 91c323d and 564d747.

Files selected for processing (19)
  • cmd/zetae2e/local/local.go (1 hunks)
  • e2e/e2etests/e2etests.go (2 hunks)
  • e2e/e2etests/test_migrate_chain_support.go (1 hunks)
  • e2e/e2etests/test_zeta_deposit.go (2 hunks)
  • e2e/e2etests/test_zeta_deposit_new_address.go (1 hunks)
  • e2e/e2etests/test_zeta_deposit_restricted_address.go (1 hunks)
  • e2e/runner/zeta.go (2 hunks)
  • e2e/utils/require.go (1 hunks)
  • pkg/address/validate_address.go (1 hunks)
  • pkg/address/validate_address_test.go (1 hunks)
  • x/crosschain/keeper/cctx_gateway_zevm.go (1 hunks)
  • x/crosschain/keeper/evm_deposit.go (1 hunks)
  • x/crosschain/keeper/evm_deposit_test.go (2 hunks)
  • x/crosschain/types/cctx.go (2 hunks)
  • x/crosschain/types/errors.go (1 hunks)
  • x/crosschain/types/message_whitelist_erc20.go (2 hunks)
  • x/crosschain/types/validate.go (2 hunks)
  • x/crosschain/types/validate_test.go (1 hunks)
  • x/fungible/types/message_update_system_contract.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • x/crosschain/keeper/cctx_gateway_zevm.go
Additional context used
Path-based instructions (18)
e2e/e2etests/test_zeta_deposit_restricted_address.go (1)

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

e2e/e2etests/test_zeta_deposit_new_address.go (1)

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

e2e/e2etests/test_zeta_deposit.go (1)

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

x/crosschain/types/validate_test.go (1)

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

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

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

pkg/address/validate_address.go (1)

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

x/crosschain/types/message_whitelist_erc20.go (1)

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

e2e/utils/require.go (1)

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

x/crosschain/types/validate.go (1)

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

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

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

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

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

pkg/address/validate_address_test.go (1)

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

e2e/runner/zeta.go (1)

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

e2e/e2etests/test_migrate_chain_support.go (1)

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

x/crosschain/types/cctx.go (1)

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

cmd/zetae2e/local/local.go (1)

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

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

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

e2e/e2etests/e2etests.go (1)

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

golangci-lint
pkg/address/validate_address.go

6-6: File is not gci-ed with --skip-generated -s standard -s default -s prefix(github.com/zeta-chain/zetacore)

(gci)

GitHub Check: codecov/patch
pkg/address/validate_address.go

[warning] 15-15: pkg/address/validate_address.go#L15
Added line #L15 was not covered by tests


[warning] 29-29: pkg/address/validate_address.go#L29
Added line #L29 was not covered by tests


[warning] 43-44: pkg/address/validate_address.go#L43-L44
Added lines #L43 - L44 were not covered by tests

x/crosschain/types/cctx.go

[warning] 208-210: x/crosschain/types/cctx.go#L208-L210
Added lines #L208 - L210 were not covered by tests


[warning] 212-212: x/crosschain/types/cctx.go#L212
Added line #L212 was not covered by tests


[warning] 214-216: x/crosschain/types/cctx.go#L214-L216
Added lines #L214 - L216 were not covered by tests


[warning] 218-218: x/crosschain/types/cctx.go#L218
Added line #L218 was not covered by tests


[warning] 223-223: x/crosschain/types/cctx.go#L223
Added line #L223 was not covered by tests


[warning] 225-228: x/crosschain/types/cctx.go#L225-L228
Added lines #L225 - L228 were not covered by tests


[warning] 230-231: x/crosschain/types/cctx.go#L230-L231
Added lines #L230 - L231 were not covered by tests


[warning] 233-235: x/crosschain/types/cctx.go#L233-L235
Added lines #L233 - L235 were not covered by tests


[warning] 237-237: x/crosschain/types/cctx.go#L237
Added line #L237 was not covered by tests


[warning] 240-240: x/crosschain/types/cctx.go#L240
Added line #L240 was not covered by tests


[warning] 242-244: x/crosschain/types/cctx.go#L242-L244
Added lines #L242 - L244 were not covered by tests


[warning] 246-246: x/crosschain/types/cctx.go#L246
Added line #L246 was not covered by tests


[warning] 248-249: x/crosschain/types/cctx.go#L248-L249
Added lines #L248 - L249 were not covered by tests

Additional comments not posted (24)
e2e/e2etests/test_zeta_deposit_restricted_address.go (1)

18-18: LGTM! But verify the method signature.

The change ensures that the address is correctly formatted as a byte slice before being processed. However, ensure that the DepositZetaWithAmount method expects a byte slice and handles it correctly.

The code changes are approved.

Run the following script to verify the method signature:

e2e/e2etests/test_zeta_deposit_new_address.go (1)

18-18: LGTM! But verify the method signature.

The change ensures that the address is correctly formatted as a byte slice before being processed. However, ensure that the DepositZetaWithAmount method expects a byte slice and handles it correctly.

The code changes are approved.

Run the following script to verify the method signature:

e2e/e2etests/test_zeta_deposit.go (2)

27-42: LGTM!

The new test function is well-implemented and covers the scenario of depositing to an invalid address. The logging and validation are appropriate.

The code changes are approved.


20-20: LGTM! But verify the method signature.

The change ensures that the address is correctly formatted as a byte slice before being processed. However, ensure that the DepositZetaWithAmount method expects a byte slice and handles it correctly.

The code changes are approved.

Run the following script to verify the method signature:

pkg/address/validate_address.go (1)

12-46: LGTM! Ensure unit tests cover the new function.

The function provides robust validation for addresses across different blockchain networks. Ensure that unit tests are added to cover the new function.

Run the following script to verify the test coverage:

Verification successful

The ValidateAddressForChain function is well-covered by unit tests.

The function has comprehensive test coverage, ensuring its reliability across different blockchain networks. The tests include both valid and invalid scenarios, which is essential for robust validation. No further action is required.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the new function is covered by tests.

# Test: Search for test functions that cover the new function.
rg --type go -A 5 $'ValidateAddressForChain'

Length of output: 9835

Tools
GitHub Check: codecov/patch

[warning] 15-15: pkg/address/validate_address.go#L15
Added line #L15 was not covered by tests


[warning] 29-29: pkg/address/validate_address.go#L29
Added line #L29 was not covered by tests


[warning] 43-44: pkg/address/validate_address.go#L43-L44
Added lines #L43 - L44 were not covered by tests

x/crosschain/types/message_whitelist_erc20.go (1)

7-7: LGTM!

The import statement for the address package is necessary for the new validation method.

e2e/utils/require.go (1)

25-34: LGTM!

The new function ContainsStringInCCTXStatusMessage is well-implemented and enhances the testing capabilities.

x/crosschain/types/validate.go (2)

5-5: LGTM!

The removal of the import statement for ethcommon is necessary due to the removal of the ValidateAddressForChain function.


Line range hint 33-33: LGTM! But verify the usage of the new validation method.

The removal of the ValidateAddressForChain function indicates a shift in how address validation is handled. Ensure that the new validation method is used consistently across the codebase.

Run the following script to verify the usage of the new validation method:

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

59-59: LGTM!

The new error variable ErrInvalidReceiverAddress is correctly registered and follows the existing pattern for error registration.

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

27-30: LGTM!

The new method call GetValidReceiverAddress enhances the robustness of the function by ensuring that only valid receiver addresses are processed. The error handling is correctly implemented.

pkg/address/validate_address_test.go (1)

1-149: LGTM!

The test cases are comprehensive and cover a wide range of scenarios. The use of require from the testify package ensures that the tests are expressive and fail fast.

e2e/runner/zeta.go (2)

98-98: LGTM!

The change is consistent with the updated DepositZetaWithAmount method signature.


Line range hint 102-120: LGTM!

The change is consistent with the updated method signature and ensures that the DestinationAddress is correctly passed as a byte slice.

e2e/e2etests/test_migrate_chain_support.go (1)

41-41: LGTM!

The change is consistent with the updated DepositZetaWithAmount method signature.

cmd/zetae2e/local/local.go (1)

253-253: LGTM!

The addition of the new test case TestZetaDepositInvalidAddressName enhances the robustness of the code by ensuring that edge cases related to address validation are covered.

x/crosschain/keeper/evm_deposit_test.go (6)

54-77: LGTM!

The test case correctly ensures that invalid receiver addresses are identified and handled for coin type Zeta.


79-103: LGTM!

The test case correctly ensures that invalid receiver addresses are identified and handled for coin type Gas.


105-128: LGTM!

The test case correctly ensures that invalid receiver addresses are identified and handled for coin type ERC20.


601-601: LGTM!

The modification correctly aligns the error handling with the new logic introduced in the added test cases.


129-129: LGTM!

The test case correctly ensures that invalid sender addresses are identified and handled.


601-601: LGTM!

The test case correctly ensures that failed deposits with non-failed transactions are handled.

e2e/e2etests/e2etests.go (2)

14-14: LGTM!

The addition of the constant TestZetaDepositInvalidAddressName follows the existing naming pattern and is correctly implemented.


167-174: LGTM!

The new test case runner.NewE2ETest(TestZetaDepositInvalidAddressName, ...) is correctly added to the AllE2ETests slice. The test case description and argument definitions are clear and follow the existing pattern.

Comment on lines 208 to 251
func (m CrossChainTx) GetValidReceiverAddress() (ethcommon.Address, error) {
inboundCoinType := m.InboundParams.CoinType
switch inboundCoinType {
// For coin type ZETA the receiver is added to the receiver field of the inbound vote so we can use it directly
case coin.CoinType_Zeta:
{
err := address.ValidateEthereumAddress(m.GetCurrentOutboundParam().Receiver)
if err != nil {
return ethcommon.Address{}, cosmoserrors.Wrap(ErrInvalidReceiverAddress, err.Error())
}
return ethcommon.HexToAddress(m.GetCurrentOutboundParam().Receiver), nil
}
// For coin type gas we create the message by composing the receiver and the call data so we need to parse the address from the RelayedMessage
// The receiver in this case is set to me the sender of the message
// For cases where this is not the case we should try and use the receiver field of the inbound vote
case coin.CoinType_Gas:
{
to := m.GetCurrentOutboundParam().Receiver
parsedAddress, _, err := chains.ParseAddressAndData(m.RelayedMessage)
if err != nil {
return ethcommon.Address{}, cosmoserrors.Wrap(ErrInvalidReceiverAddress, err.Error())
}
if parsedAddress != (ethcommon.Address{}) {
to = parsedAddress.String()
}
err = address.ValidateEthereumAddress(to)
if err != nil {
return ethcommon.Address{}, cosmoserrors.Wrap(ErrInvalidReceiverAddress, err.Error())
}
return ethcommon.HexToAddress(m.GetCurrentOutboundParam().Receiver), nil
}
// For coin type ERC20 we can use the receiver field of the inbound vote
case coin.CoinType_ERC20:
{
err := address.ValidateEthereumAddress(m.GetCurrentOutboundParam().Receiver)
if err != nil {
return ethcommon.Address{}, cosmoserrors.Wrap(ErrInvalidReceiverAddress, err.Error())
}
return ethcommon.HexToAddress(m.GetCurrentOutboundParam().Receiver), nil
}
default:
return ethcommon.Address{}, fmt.Errorf("invalid coin type %s", inboundCoinType)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! But add unit tests for the new method.

The method is well-structured and handles different coin types appropriately. However, the lack of test coverage is a concern.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 208-210: x/crosschain/types/cctx.go#L208-L210
Added lines #L208 - L210 were not covered by tests


[warning] 212-212: x/crosschain/types/cctx.go#L212
Added line #L212 was not covered by tests


[warning] 214-216: x/crosschain/types/cctx.go#L214-L216
Added lines #L214 - L216 were not covered by tests


[warning] 218-218: x/crosschain/types/cctx.go#L218
Added line #L218 was not covered by tests


[warning] 223-223: x/crosschain/types/cctx.go#L223
Added line #L223 was not covered by tests


[warning] 225-228: x/crosschain/types/cctx.go#L225-L228
Added lines #L225 - L228 were not covered by tests


[warning] 230-231: x/crosschain/types/cctx.go#L230-L231
Added lines #L230 - L231 were not covered by tests


[warning] 233-235: x/crosschain/types/cctx.go#L233-L235
Added lines #L233 - L235 were not covered by tests


[warning] 237-237: x/crosschain/types/cctx.go#L237
Added line #L237 was not covered by tests


[warning] 240-240: x/crosschain/types/cctx.go#L240
Added line #L240 was not covered by tests


[warning] 242-244: x/crosschain/types/cctx.go#L242-L244
Added lines #L242 - L244 were not covered by tests


[warning] 246-246: x/crosschain/types/cctx.go#L246
Added line #L246 was not covered by tests


[warning] 248-249: x/crosschain/types/cctx.go#L248-L249
Added lines #L248 - L249 were not covered by tests

return combined
}

func TestValidateAddressForChain(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: We should document somewhere that this test has to be extended whenever we include a chain with a different address format.

}

// DepositZetaWithAmount deposits ZETA on ZetaChain from the ZETA smart contract on EVM with the specified amount
func (r *E2ERunner) DepositZetaWithAmount(to ethcommon.Address, amount *big.Int) ethcommon.Hash {
func (r *E2ERunner) DepositZetaWithAmount(to []byte, amount *big.Int) ethcommon.Hash {
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK this function is called when the deposit comes from the EVM, it would be a good idea to change the to parameter to to [20]byte.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let’s keep the E2E with ethcommon.Address, rather than refactoring to []byte solely to test an invalid EVM address. I suggest we handle this case change separately.

Copy link
Contributor Author

@kingpinXD kingpinXD Sep 5, 2024

Choose a reason for hiding this comment

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

I think using []byte is fine for now , it keeps the function generic .
We could move to [20]byte , but since this is a helper function for tests , I don't think its needed for now

pkg/address/validate_address.go Show resolved Hide resolved
x/crosschain/types/cctx.go Outdated Show resolved Hide resolved
x/crosschain/types/cctx.go Outdated Show resolved Hide resolved
x/crosschain/types/cctx.go Outdated Show resolved Hide resolved
pkg/address/validate_address.go Outdated Show resolved Hide resolved
pkg/address/validate_address.go Outdated Show resolved Hide resolved
pkg/address/validate_address.go Outdated Show resolved Hide resolved
}

// DepositZetaWithAmount deposits ZETA on ZetaChain from the ZETA smart contract on EVM with the specified amount
func (r *E2ERunner) DepositZetaWithAmount(to ethcommon.Address, amount *big.Int) ethcommon.Hash {
func (r *E2ERunner) DepositZetaWithAmount(to []byte, amount *big.Int) ethcommon.Hash {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let’s keep the E2E with ethcommon.Address, rather than refactoring to []byte solely to test an invalid EVM address. I suggest we handle this case change separately.

case chains.Network_eth:
return ValidateEthereumAddress(address)
case chains.Network_zeta:
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also validate for evm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is not used right now
It would depend on the v2 protocol refactor on how this value is utilised .
Presently for btc inbound ,the receiver is set as FromAddress which would cause this check to fail and disable btc deposits .
Overall we should test this via end to end tests when using this function

pkg/address/validate_address.go Outdated Show resolved Hide resolved
case chains.Network_bsc:
return ValidateEthereumAddress(address)
case chains.Network_optimism:
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to validate by VM type, not by chain.network, all EVM's have the same addr

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I agree, to add all new check here we would need adding a new VM for Bitcoin (which can make sense as Bitcoin has some scripting functionality) but this would be higher scope than the PR.
I suggest to use VM for check with an exception for Bitcoin networks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with the fact the function should check based on the VM, which is in fact what this function does internally .
All EVM based chains can use the same ValidateEthereumAddress function.

Adding individual cases for each chain lets us provide a more granular check for each chain , and choose to ignore checks for some chains if we want to.
IMO for checks like these it would be good be more cautious , and keep it at chain level

@@ -203,6 +205,51 @@ func (m CrossChainTx) SetOutboundBallotIndex(index string) {
}

// GetCctxIndexFromBytes returns the CCTX index from a byte array.
func (m CrossChainTx) GetValidReceiverAddress() (ethcommon.Address, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (m CrossChainTx) GetValidReceiverAddress() (ethcommon.Address, error) {
func (m CrossChainTx) GetReceiver() (ethcommon.Address, error) {

case chains.Network_bsc:
return ValidateEthereumAddress(address)
case chains.Network_optimism:
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Yes, I agree, to add all new check here we would need adding a new VM for Bitcoin (which can make sense as Bitcoin has some scripting functionality) but this would be higher scope than the PR.
I suggest to use VM for check with an exception for Bitcoin networks

Comment on lines 48 to 49
err = address.ValidateEthereumAddress(msg.NewSystemContractAddress)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
err = address.ValidateEthereumAddress(msg.NewSystemContractAddress)
if err != nil {
if err := address.ValidateEthereumAddress(msg.NewSystemContractAddress); err != nil {

Comment on lines 57 to 58
err = address.ValidateEthereumAddress(msg.Erc20Address)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
err = address.ValidateEthereumAddress(msg.Erc20Address)
if err != nil {
if err := address.ValidateEthereumAddress(msg.Erc20Address); err != nil {

Comment on lines +220 to +222
// For coin type gas we create the message by composing the receiver and the call data so we need to parse the address from the RelayedMessage
// The receiver in this case is set to me the sender of the message
// For cases where this is not the case we should try and use the receiver field of the inbound vote
Copy link
Member

Choose a reason for hiding this comment

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

This logic will be removed for V2 contracts, receiver will always contain the receiver address (which should have always been the case)

@@ -24,7 +24,10 @@ const InCCTXIndexKey = "inCctxIndex"
// returns (isContractReverted, err)
// (true, non-nil) means CallEVM() reverted
func (k Keeper) HandleEVMDeposit(ctx sdk.Context, cctx *types.CrossChainTx) (bool, error) {
to := ethcommon.HexToAddress(cctx.GetCurrentOutboundParam().Receiver)
to, err := cctx.GetValidReceiverAddress()
Copy link
Member

Choose a reason for hiding this comment

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

ctx.GetCurrentOutboundParam().Receiver will always contains the receiver moving forward, and since it is on ZetaChain, the address should always be EVM format

I think we could implement a function such as #2789 and use it here with receiver address

Copy link
Contributor Author

Choose a reason for hiding this comment

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

THe function is a good idea .
This is really the only change in logic that the PR implements, IMO if we want to change this , it would make sense to discard this pr for now , and revisit this once we have everything related to v2 functional

@kingpinXD kingpinXD closed this Sep 16, 2024
@kingpinXD kingpinXD reopened this Sep 16, 2024
@kingpinXD kingpinXD marked this pull request as draft September 16, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

zetacore : Invalid address fields may be truncated and processed in error
4 participants