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

imp(all): Add general types and utils #15

Merged
merged 5 commits into from
Aug 2, 2024
Merged

imp(all): Add general types and utils #15

merged 5 commits into from
Aug 2, 2024

Conversation

MalteHerrmann
Copy link
Collaborator

@MalteHerrmann MalteHerrmann commented Aug 2, 2024

This PR adds the types and utils from the Evmos repository. Note, that the prior utils.BaseDenom and chain specific IDs have been changed to be only within the testutils directory, to make it clear for evmOS partners which assumptions are test helpers and which are actually implementation related.

Summary by CodeRabbit

  • New Features

    • Introduced robust utilities for Ethereum and Cosmos SDK address conversions.
    • Added functions for validating Ethereum addresses and hashes.
    • Implemented Hierarchical Deterministic (HD) path support for Ethereum accounts.
  • Improvements

    • Enhanced gas management capabilities with an infinite gas meter.
    • Established structured error handling for chain ID validation.
    • Implemented an Ethereum transaction indexer interface to manage transaction data.
  • Tests

    • Comprehensive unit tests added for address validation, key support, and utility functions.

@MalteHerrmann MalteHerrmann requested a review from a team as a code owner August 2, 2024 10:56
@MalteHerrmann MalteHerrmann requested review from Vvaradinov and GAtom22 and removed request for a team August 2, 2024 10:56
Copy link

coderabbitai bot commented Aug 2, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

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

The recent changes enhance the Evmos blockchain framework by introducing new utilities, constants, and validation functions. This update focuses on improving address management, gas handling, and transaction indexing, while also refining test coverage for these features. Additionally, the migration to a more structured import system facilitates better code organization and clarity.

Changes

Files Change Summary
CHANGELOG.md Added entry for general types and utilities, emphasizing improved functionality.
ethereum/eip712/*.go Updated import paths to use testutil for test values, ensuring consistency. Changed dependencies for evmostypes imports to reflect new structure.
testutil/constants.go Introduced constants for example chain ID and denomination for standardized testing.
types/*.go New files created for gas management, HD path handling, protocol versioning, validation utilities, and transaction indexing, enhancing overall functionality.
utils/utils.go Added utility functions for Ethereum and Cosmos address conversions, IBC denom management, and public key validation.
utils/utils_test.go Comprehensive tests for address handling, key validation, and denomination checks to ensure functionality and reliability.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant Chain
    participant IBC

    User->>App: Initiates transaction
    App->>Chain: Processes transaction
    Chain->>IBC: Validates cross-chain denom
    Chain-->>App: Returns transaction result
    App-->>User: Displays result
Loading

🐇 In a world where the code does dance,
New functions and tests, we take a chance!
With addresses bright and gas limits high,
Our blockchain hops, oh my, oh my!
So celebrate with joy, let out a cheer,
For Evmos is better, the future is clear! 🌟


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

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@MalteHerrmann MalteHerrmann changed the title improvements(all): Add general types and utils imp(all): Add general types and utils Aug 2, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Outside diff range, codebase verification and nitpick comments (2)
types/indexer.go (1)

11-21: Consider adding more context to the method comments.

The comments for the methods are brief. Adding more details about the expected behavior and parameters could improve clarity.

-	// LastIndexedBlock returns -1 if indexer db is empty
+	// LastIndexedBlock returns the last indexed block number.
+	// Returns -1 if the indexer database is empty.
-	// GetByTxHash returns nil if tx not found.
+	// GetByTxHash retrieves a transaction result by its hash.
+	// Returns nil if the transaction is not found.
-	// GetByBlockAndIndex returns nil if tx not found.
+	// GetByBlockAndIndex retrieves a transaction result by the block number and transaction index.
+	// Returns nil if the transaction is not found.
types/hdpath.go (1)

9-15: Consider adding more context to the constant comments.

The comments for the constants are brief. Adding more details about their usage and significance could improve clarity.

-	// Bip44CoinType satisfies EIP84. See https://github.com/ethereum/EIPs/issues/84 for more info.
+	// Bip44CoinType is the coin type for Ethereum as per BIP44.
+	// It satisfies EIP84. See https://github.com/ethereum/EIPs/issues/84 for more info.
-	// BIP44HDPath is the default BIP44 HD path used on Ethereum.
+	// BIP44HDPath is the default BIP44 HD path used for Ethereum accounts.
+	// It follows the standard defined in BIP44.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c7269ec and eeb79cf.

Files ignored due to path filters (3)
  • types/dynamic_fee.pb.go is excluded by !**/*.pb.go
  • types/indexer.pb.go is excluded by !**/*.pb.go
  • types/web3.pb.go is excluded by !**/*.pb.go
Files selected for processing (22)
  • CHANGELOG.md (1 hunks)
  • ethereum/eip712/eip712_test.go (3 hunks)
  • ethereum/eip712/encoding.go (1 hunks)
  • ethereum/eip712/encoding_legacy.go (1 hunks)
  • ethereum/eip712/preprocess.go (1 hunks)
  • ethereum/eip712/preprocess_test.go (4 hunks)
  • testutil/constants.go (1 hunks)
  • types/block.go (1 hunks)
  • types/chain_id.go (1 hunks)
  • types/chain_id_test.go (1 hunks)
  • types/codec.go (1 hunks)
  • types/dynamic_fee.go (1 hunks)
  • types/errors.go (1 hunks)
  • types/gasmeter.go (1 hunks)
  • types/hdpath.go (1 hunks)
  • types/indexer.go (1 hunks)
  • types/int.go (1 hunks)
  • types/protocol.go (1 hunks)
  • types/validation.go (1 hunks)
  • types/validation_test.go (1 hunks)
  • utils/utils.go (1 hunks)
  • utils/utils_test.go (1 hunks)
Files skipped from review due to trivial changes (4)
  • ethereum/eip712/encoding_legacy.go
  • ethereum/eip712/preprocess.go
  • testutil/constants.go
  • types/protocol.go
Additional comments not posted (61)
types/errors.go (2)

1-3: LGTM!

The copyright and license information is correctly formatted.


10-14: LGTM!

The error handling code is correctly defining a codespace and registering an error.

types/dynamic_fee.go (2)

1-3: LGTM!

The copyright and license information is correctly formatted.


9-13: LGTM!

The function correctly checks for the dynamic fee extension option and returns a boolean value.

CHANGELOG.md (1)

11-11: LGTM!

The changelog entry is correctly formatted and provides a clear description of the changes made in the PR.

types/hdpath.go (1)

21-34: LGTM!

The function correctly handles the input parameters and returns the appropriate iterator. Error handling is also properly implemented.

types/int.go (3)

17-24: LGTM! The function is well-implemented.

The function correctly checks for overflows and returns appropriate errors.


26-32: LGTM! The function is well-implemented.

The function correctly checks for 256-bit overflow and returns appropriate errors.


34-37: LGTM! The function is well-implemented.

The function correctly checks if a big.Int is within 256-bit bounds.

types/block.go (1)

11-41: LGTM! The function is well-implemented.

The function correctly retrieves the block gas limit from the context and handles different scenarios appropriately.

types/validation.go (4)

13-16: LGTM! The function is well-implemented.

The function correctly checks if a hash corresponds to an empty Ethereum hex hash.


18-21: LGTM! The function is well-implemented.

The function correctly checks if an address corresponds to an empty Ethereum hex address.


23-32: LGTM! The function is well-implemented.

The function correctly validates if a string is a hex formatted Ethereum address and returns appropriate errors.


34-44: LGTM! The function is well-implemented.

The function correctly validates if a string is a non-zero hex formatted Ethereum address and returns appropriate errors.

types/chain_id.go (3)

28-35: LGTM! The function implementation is correct.

The function correctly validates the chain ID format using the compiled regular expression.


37-57: LGTM! The function implementation is correct.

The function correctly parses the chain ID string, handles errors, and converts the epoch to a big integer.


14-26: Verify the regular expression components.

The regular expression components seem to be well-defined and combined correctly. Ensure that these components cover all valid chain ID formats.

Verification successful

Verify the regular expression components.

The regular expression components are correctly defined and combined to form the evmosChainID regular expression. The IsValidChainID function appropriately uses this regular expression to validate chain IDs.

  • types/chain_id.go: Lines 14-26
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the regular expression components cover all valid chain ID formats.

# Test: Search for the usage of the regular expressions. Expect: Only valid chain ID formats are matched.
rg --type go -A 5 $'regexChainID|regexEIP155Separator|regexEIP155|regexEpochSeparator|regexEpoch'

Length of output: 888


Script:

#!/bin/bash
# Description: Verify the usage of the `evmosChainID` variable and the `IsValidChainID` function.

# Test: Search for the usage of the `evmosChainID` variable. Expect: Appropriate usage in validating chain IDs.
rg --type go -A 5 'evmosChainID'

# Test: Search for the usage of the `IsValidChainID` function. Expect: Appropriate usage in validating chain IDs.
rg --type go -A 5 'IsValidChainID'

Length of output: 1846

types/chain_id_test.go (1)

11-87: LGTM! The test cases are well-defined and cover a wide range of scenarios.

The test cases correctly test various valid and invalid chain ID formats, edge cases, and error handling.

Verification successful

To verify the coverage of the ParseChainID function without using the go command, we can search for the function definition and its usage across the test files. This will help us determine if the function is adequately tested.


LGTM! The test cases are well-defined and cover a wide range of scenarios.

The test cases correctly test various valid and invalid chain ID formats, edge cases, and error handling. The ParseChainID function is adequately tested in types/chain_id_test.go.

  • types/chain_id_test.go: Test cases for ParseChainID.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the coverage of the test cases.

# Test: Run the tests and check the coverage. Expect: High coverage for the `ParseChainID` function.
go test -cover ./types

Length of output: 94


Script:

#!/bin/bash
# Find the definition of the `ParseChainID` function.
ast-grep --lang go --pattern 'func ParseChainID($_) $_' 

# Find all test files that call the `ParseChainID` function.
rg 'ParseChainID' --glob '*_test.go' -A 5

Length of output: 1777

types/validation_test.go (5)

12-33: LGTM! The test cases are well-defined and cover multiple scenarios.

The test cases correctly test various scenarios for the IsEmptyHash function.


35-56: LGTM! The test cases are well-defined and cover multiple scenarios.

The test cases correctly test various scenarios for the IsZeroAddress function.


58-87: LGTM! The test cases are well-defined and cover multiple scenarios.

The test cases correctly test various scenarios for the ValidateAddress function.


89-118: LGTM! The test cases are well-defined and cover multiple scenarios.

The test cases correctly test various scenarios for the ValidateNonZeroAddress function.


120-144: LGTM! The test cases are well-defined and cover multiple scenarios.

The test cases correctly test various scenarios for the SafeInt64 function.

types/gasmeter.go (12)

12-17: LGTM!

The ErrorNegativeGasConsumed struct is well-defined and straightforward.


19-23: LGTM!

The ErrorGasOverflow struct is well-defined and straightforward.


30-36: LGTM!

The NewInfiniteGasMeterWithLimit function is well-defined and initializes the infiniteGasMeterWithLimit correctly.


38-41: LGTM!

The GasConsumed function correctly returns the consumed gas.


43-49: LGTM!

The GasConsumedToLimit function correctly returns the consumed gas.


51-54: LGTM!

The Limit function correctly returns the gas limit.


56-64: LGTM!

The addUint64Overflow function correctly handles overflow checking.


76-88: LGTM!

The RefundGas function correctly handles gas refunding and negative gas consumption checking.


90-93: LGTM!

The IsPastLimit function always returns false, which might be intentional for this type of gas meter.


95-98: LGTM!

The IsOutOfGas function always returns false, which might be intentional for this type of gas meter.


100-103: LGTM!

The String function correctly formats and returns the string representation of the gas meter.


105-108: LGTM!

The GasRemaining function correctly returns the maximum uint64 value, indicating an infinite gas meter.

ethereum/eip712/preprocess_test.go (5)

22-23: LGTM!

The new import statements for testutil and types are necessary for the changes made in the test cases.


29-29: LGTM!

The chainID variable is correctly updated to use testutil.ExampleChainID.


98-98: LGTM!

The fee denomination is correctly updated to use testutil.ExampleAttoDenom.


196-198: LGTM!

The fee denomination is correctly updated to use testutil.ExampleAttoDenom.


208-210: LGTM!

The fee denomination is correctly updated to use testutil.ExampleAttoDenom.

utils/utils.go (11)

23-27: LGTM!

The EthHexToCosmosAddr function correctly converts a Hex string to a Cosmos SDK account address.


29-32: LGTM!

The EthToCosmosAddr function correctly converts an Ethereum style address to an SDK address.


34-43: LGTM!

The Bech32ToHexAddr function correctly converts a Bech32 address string to an Ethereum address.


45-49: LGTM!

The CosmosToEthAddr function correctly converts an SDK account address to an Ethereum address.


51-77: LGTM!

The IsSupportedKey function correctly checks the supported public key types and handles nested multisigs.


79-101: LGTM!

The GetAccAddressFromBech32 function correctly converts the Bech32 address to an SDK account address and handles invalid addresses.


103-120: LGTM!

The CreateAccAddressFromBech32 function correctly creates the SDK account address from the Bech32 string and handles empty and invalid addresses.


122-139: LGTM!

The GetIBCDenomAddress function correctly returns the address from the hash of the ICS20's DenomTrace Path and handles invalid IBC denominations.


141-153: LGTM!

The ComputeIBCDenomTrace function correctly computes the IBC voucher denom trace.


155-162: LGTM!

The ComputeIBCDenom function correctly computes the IBC voucher denom.


164-169: LGTM!

The SortSlice function correctly sorts the slice using the generic type constraint.

ethereum/eip712/encoding.go (1)

15-15: Verify the impact of the changed import path.

The import path for evmostypes has been updated from github.com/evmos/evmos/v19/types to github.com/evmos/os/types. Ensure that the new import path provides the necessary types and functionalities required by this file.

Verification successful

The ParseChainID function is correctly defined in the new import path.

The ParseChainID function is found in the types/chain_id.go file, which aligns with the new import path github.com/evmos/os/types. This confirms that the new import path provides the necessary types and functionalities required by the file.

  • Location of ParseChainID function: types/chain_id.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the new import path provides the necessary types and functionalities.

# Test: Search for the usage of `evmostypes` in this file. Expect: No unresolved references.
rg --type go 'evmostypes' ethereum/eip712/encoding.go

Length of output: 210


Script:

#!/bin/bash
# Search for the definition of the ParseChainID function in the new import path
ast-grep --lang go --pattern 'func ParseChainID($_) $_' -- github.com/evmos/os/types

Length of output: 159


Script:

#!/bin/bash
# Search for the definition of the ParseChainID function across all Go files in the repository
ast-grep --lang go --pattern 'func ParseChainID($_) $_'

Length of output: 1130

utils/utils_test.go (6)

17-67: LGTM!

The test cases for TestIsSupportedKeys cover a wide range of scenarios and the function is well-structured.


70-126: LGTM!

The test cases for TestGetAccAddressFromBech32 cover a wide range of scenarios and the function is well-structured.


129-181: LGTM!

The test cases for TestEvmosCoinDenom cover a wide range of scenarios and the function is well-structured.


184-231: LGTM!

The test cases for TestAccAddressFromBech32 cover a wide range of scenarios and the function is well-structured.


234-251: LGTM!

The test cases for TestAddressConversion cover both directions of conversion and ensure correctness. The function is well-structured.


253-297: LGTM!

The test cases for TestGetIBCDenomAddress cover a wide range of scenarios and the function is well-structured.

ethereum/eip712/eip712_test.go (3)

31-31: LGTM!

The import statement for the testutil package is appropriate for using example values in the test suite.


73-73: LGTM!

The assignment of suite.denom to testutil.ExampleAttoDenom improves clarity by using example values from the testutil package.


353-353: LGTM!

The assignment of chainID to testutil.ExampleChainID improves clarity by using example values from the testutil package.

Comment on lines +11 to +29
// RegisterInterfaces registers the tendermint concrete client-related
// implementations and interfaces.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*authtypes.AccountI)(nil),
// TODO: uncomment after moving into migrations for EVM version
// &EthAccount{},
)
registry.RegisterImplementations(
(*authtypes.GenesisAccount)(nil),
// TODO: uncomment after moving into migrations for EVM version
// &EthAccount{},
)
registry.RegisterImplementations(
(*tx.TxExtensionOptionI)(nil),
&ExtensionOptionsWeb3Tx{},
&ExtensionOptionDynamicFeeTx{},
)
}
Copy link

@coderabbitai coderabbitai bot Aug 2, 2024

Choose a reason for hiding this comment

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

Reminder: Address TODO comments.

The TODO comments indicate future changes. Ensure these are tracked and addressed in a timely manner.

Do you want me to open a GitHub issue to track these TODO comments?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure that would be good, please open one combined for both of the TODOs in this file

Copy link

Choose a reason for hiding this comment

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

@MalteHerrmann, I've created the GitHub issue to track the TODO comments in the types/codec.go file. You can find the issue here: Track TODO comments in types/codec.go.

Analysis chain

@MalteHerrmann, I'll open a GitHub issue to track these TODO comments.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
gh issue create --title "Track TODO comments in types/codec.go" --body "The file \`types/codec.go\` contains TODO comments that need to be addressed:
- Uncomment the lines related to \`EthAccount\` after moving into migrations for the EVM version.

Relevant TODO comments:
\`\`\`go
// TODO: uncomment after moving into migrations for EVM version
// &EthAccount{},
\`\`\`

File: [types/codec.go](https://github.com/evmos/os/blob/main/types/codec.go)
" --assignee MalteHerrmann --label "enhancement"

Length of output: 525

types/gasmeter.go Show resolved Hide resolved
Copy link
Contributor

@GAtom22 GAtom22 left a comment

Choose a reason for hiding this comment

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

LGTM! Great work @MalteHerrmann!!!

@MalteHerrmann MalteHerrmann merged commit 9c637f2 into main Aug 2, 2024
19 of 25 checks passed
@MalteHerrmann MalteHerrmann deleted the add-os-types branch August 2, 2024 12:48
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.

2 participants