-
Notifications
You must be signed in to change notification settings - Fork 898
chore(app): Refactor configurator #2914
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
Conversation
Caution Review failedThe pull request is closed. WalkthroughThe changes in this pull request primarily involve updating the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 (6)
app/config.go (1)
43-43
: Approve the type cast with a minor suggestion.The change to cast
evmtypes.EighteenDecimals
toint64
improves type safety by ensuring the argument matches the expected type forWithEVMCoinInfo
. This aligns with the PR's objective of refactoring the configurator.For improved clarity and to make the intent more explicit, consider using a named constant or a comment to explain the significance of 18 decimals in this context.
Here's a suggestion to improve readability:
- WithEVMCoinInfo(baseDenom, int64(evmtypes.EighteenDecimals)). + WithEVMCoinInfo(baseDenom, int64(evmtypes.EighteenDecimals)). // 18 decimals for EVM compatibilityAlternatively, if
EighteenDecimals
is used in multiple places, consider defining a named constant:const EVMDecimalPrecision int64 = 18 // Then use it in the function call WithEVMCoinInfo(baseDenom, EVMDecimalPrecision)This would enhance code readability and maintainability.
x/evm/keeper/setup_test.go (1)
109-109
: Approve the type casting change with a minor suggestion.The modification to cast
decimals
toint64
aligns with the PR objectives of abstracting away the Decimals type. This change improves type consistency and safety.For improved clarity, consider adding a comment explaining the reason for the type cast:
- WithEVMCoinInfo(denom, int64(decimals)). + WithEVMCoinInfo(denom, int64(decimals)). // Cast to int64 to match the updated WithEVMCoinInfo signatureThis comment would help future developers understand the reasoning behind the cast without needing to check the
WithEVMCoinInfo
method signature.app/ante/testutils/testutils.go (1)
110-111
: Investigate suppressed static checks for GetEVMCoinDenom and GetEVMCoinDecimals.The static check suppression comments for
GetEVMCoinDenom
andGetEVMCoinDecimals
suggest that these functions might be deprecated or have known issues. While they are necessary for the current test setup, it's important to investigate why they're triggering static check warnings.Consider the following actions:
- Review the reasons for these static check warnings.
- Determine if there are newer, recommended alternatives to these functions.
- If alternatives exist, update the code to use them.
- If these functions are still necessary, document the reasons for suppressing the static checks to improve code maintainability.
x/evm/types/scaling_test.go (1)
Line range hint
1-270
: Overall assessment of changesThe changes in this file consistently modify the
WithEVMCoinInfo
method calls to cast theDecimals
field toint64
. While this change likely aligns with updates in the method signature, it introduces a potential for integer overflow in multiple locations.Consider the following recommendations:
- Implement the suggested overflow checks consistently across all test functions where the casting occurs.
- Review the
WithEVMCoinInfo
method signature in the actual implementation to ensure it expectsint64
. If possible, consider usinguint64
forDecimals
to avoid the need for casting and potential overflow issues.- Add a test case that specifically checks the behavior with a large
Decimals
value to ensure the system handles such cases correctly.These changes will improve the robustness of the tests and help prevent potential issues in edge cases.
🧰 Tools
🪛 GitHub Check: Run golangci-lint
[failure] 81-81:
G115: integer overflow conversion uint64 -> int64 (gosec)x/evm/types/msg_test.go (1)
123-123
: Suggest additional test casesWhile the existing test suite is comprehensive, it would be beneficial to add specific test cases for the
cfg.Decimals
conversion. This will ensure that the type cast behaves correctly for various input values, including edge cases.Consider adding the following test cases:
- A test with
cfg.Decimals
set to the maximum allowed value.- A test with
cfg.Decimals
set to 0.- If applicable, a test with
cfg.Decimals
set to a value that would cause an overflow when cast toint64
.These additional tests will help verify the robustness of the
WithEVMCoinInfo
method with different decimal values.🧰 Tools
🪛 GitHub Check: Run golangci-lint
[failure] 123-123:
G115: integer overflow conversion uint64 -> int64 (gosec)x/evm/types/configurator.go (1)
Line range hint
95-102
: Refactor to accumulate all EIP errorsCurrently,
extendDefaultExtraEIPs
returns upon the first error, which may prevent users from seeing all issues at once. Consider accumulating all errors to provide comprehensive feedback.Here’s a refactored version that collects all errors:
func extendDefaultExtraEIPs(extraEIPs []string) error { var errs []string for _, eip := range extraEIPs { if slices.Contains(DefaultExtraEIPs, eip) { errs = append(errs, fmt.Sprintf("EIP %s is already present in the default list", eip)) continue } if err := vm.ValidateEIPName(eip); err != nil { errs = append(errs, fmt.Sprintf("invalid EIP name %s: %s", eip, err)) continue } DefaultExtraEIPs = append(DefaultExtraEIPs, eip) } if len(errs) > 0 { return fmt.Errorf("error configuring EVMConfigurator: %s", strings.Join(errs, "; ")) } return nil }Remember to import the
strings
package:import ( "fmt" "slices" "strings" geth "github.com/ethereum/go-ethereum/params" "github.com/evmos/evmos/v20/x/evm/core/vm" )
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (10)
- app/ante/testutils/testutils.go (1 hunks)
- app/config.go (1 hunks)
- app/config_testing.go (1 hunks)
- x/evm/keeper/grpc_query_test.go (1 hunks)
- x/evm/keeper/setup_test.go (1 hunks)
- x/evm/types/config_testing.go (1 hunks)
- x/evm/types/configurator.go (3 hunks)
- x/evm/types/denom_config.go (4 hunks)
- x/evm/types/msg_test.go (1 hunks)
- x/evm/types/scaling_test.go (4 hunks)
🧰 Additional context used
📓 Learnings (1)
app/ante/testutils/testutils.go (1)
Learnt from: GAtom22 PR: evmos/evmos#2898 File: app/ante/testutils/testutils.go:0-0 Timestamp: 2024-10-01T21:37:36.764Z Learning: In `app/ante/testutils/testutils.go`, the variables `denom` and `decimals` must be retained and used when resetting the chain config to prevent nil pointer errors. Removing these variables can lead to issues because they are required by `WithEVMCoinInfo(denom, decimals)` during configuration.
🪛 GitHub Check: Run golangci-lint
x/evm/types/configurator.go
[failure] 60-60:
G115: integer overflow conversion int64 -> uint64 (gosec)x/evm/types/msg_test.go
[failure] 123-123:
G115: integer overflow conversion uint64 -> int64 (gosec)x/evm/types/scaling_test.go
[failure] 81-81:
G115: integer overflow conversion uint64 -> int64 (gosec)
[failure] 147-147:
G115: integer overflow conversion uint64 -> int64 (gosec)
🔇 Additional comments (12)
app/config.go (1)
Line range hint
1-89
: Verify PR objectives in other filesWhile this change aligns with the refactoring objective, it doesn't fully address all the stated PR objectives, such as abstracting away the Decimals type and improving error handling. These changes might be implemented in other files not included in this review.
To ensure all PR objectives are met, please run the following script to check for related changes in other files:
This script will help verify if the other PR objectives have been implemented in files not included in this review.
x/evm/types/msg_test.go (1)
123-123
: Approve change with a caveatThe change from
cfg.Decimals
toint64(cfg.Decimals)
addresses a type mismatch issue. However, this cast could potentially lead to an integer overflow ifcfg.Decimals
is auint64
with a value greater thanmath.MaxInt64
.To ensure this change doesn't introduce any overflow issues, we should verify the possible values of
cfg.Decimals
. Can you provide more context on the range of valuescfg.Decimals
can take? If it's guaranteed to be within theint64
range, this change is safe. Otherwise, we might need to add a check before the cast.🧰 Tools
🪛 GitHub Check: Run golangci-lint
[failure] 123-123:
G115: integer overflow conversion uint64 -> int64 (gosec)x/evm/types/denom_config.go (5)
31-31
: LGTM: Updating 'Decimals' type touint64
Changing the
Decimals
type fromuint32
touint64
provides a wider range for future decimal values, enhancing the flexibility of the code.
33-44
: Well-implementedValidate
method forDecimals
The
Validate
method effectively checks for supported decimal values (SixDecimals
andEighteenDecimals
), enhancing input validation and robustness.
74-80
: Improved error handling insetEVMCoinDecimals
Refactoring
setEVMCoinDecimals
to return an error instead of panicking improves application stability by allowing callers to handle invalid decimal values gracefully.
84-89
: Enhanced error handling insetEVMCoinDenom
Updating
setEVMCoinDenom
to return an error when the denomination is invalid, rather than panicking, enhances robustness and allows for better error management.
104-117
: RefactoredsetEVMCoinInfo
for better error handlingBy modifying
setEVMCoinInfo
to return errors and integrating the updated helper functions (setEVMCoinDenom
andsetEVMCoinDecimals
), the code now handles configuration errors more gracefully, improving maintainability and robustness.x/evm/types/configurator.go (4)
28-28
: Field renamed toevmCoinInfo
The field
evmDenom
has been renamed toevmCoinInfo
in theEVMConfigurator
struct, which improves clarity by better representing the data it holds.
80-81
: Correct integration ofextendDefaultExtraEIPs
The
extendDefaultExtraEIPs
function is correctly invoked to handle additional EIPs, enhancing configurability.
88-91
: Configurator sealing implementedSetting
ec.sealed = true
after applying modifiers ensures that the configurator cannot be modified further, preventing unintended reconfiguration.
76-78
:⚠️ Potential issueUndefined function
setEVMCoinInfo
The function
setEVMCoinInfo
is called but not defined within this file. Ensure thatsetEVMCoinInfo
is properly defined and accessible in the codebase to prevent runtime errors.Run the following script to check if
setEVMCoinInfo
is defined elsewhere in the codebase:x/evm/keeper/grpc_query_test.go (1)
1551-1551
: LGTM: Correctly added EVMCoinInfo configurationThe inclusion of
WithEVMCoinInfo(denom, int64(decimals))
ensures that the EVM coin information is accurately set during the test configuration. This enhances the reliability of the tests by using the correct coin denomination and decimals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Good work @0xstepit
There some linter errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice refactor, thanks @0xstepit! 🙏 left some comments, especially I'm wondering why we're using uint64
for the decimals, there should not be a need for more than uint8
There was a problem hiding this 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 (8)
x/evm/types/denom_config_testing.go (2)
20-22
: LGTM: Type change from uint32 to uint64 for DecimalsThe change from
uint32
touint64
for theDecimals
type increases the range of possible values, which can be beneficial for representing a wider range of decimal places. However, be aware that this change might slightly increase memory usage forDecimals
variables and could potentially impact performance in scenarios where manyDecimals
instances are used.The updated comment accurately reflects the change in the underlying type.
Consider documenting the rationale for this change in the package or file-level comments to provide context for future maintainers.
Line range hint
89-99
: LGTM with suggestion: ConversionFactor method unchanged but could be improvedThe
ConversionFactor
method has been correctly adapted to work with the newDecimals
type (uint64). Its logic remains unchanged, which is appropriate as the functionality should remain the same despite the type change.However, with the expanded range of possible values for
Decimals
(now uint64), it might be beneficial to handle cases whereDecimals
has values other than 6 or 18.Consider adding a default case or error handling for unexpected
Decimals
values:func (d Decimals) ConversionFactor() math.Int { switch d { case SixDecimals: return math.NewInt(1e12) case EighteenDecimals: return math.NewInt(1) default: // Either return an error or a sensible default // Example: // return math.NewInt(0), fmt.Errorf("unsupported decimal places: %d", d) // Or: // log.Printf("Warning: Unexpected decimal places %d, defaulting to 1", d) return math.NewInt(1) } }This change would make the method more robust and future-proof.
x/evm/types/denom_config.go (4)
35-44
: LGTM:Validate
method added with minor suggestionThe addition of the
Validate
method forDecimals
is a good improvement, aligning with the PR objective of adding validation. It ensures that only supported decimal values (6 and 18) are accepted.However, there's a minor suggestion for improvement:
In the error message on line 42, consider using
%v
instead of%d
for formatting theDecimals
value. While%d
works foruint32
,%v
is more flexible and would continue to work if the type ofDecimals
changes in the future.- return fmt.Errorf("received unsupported decimals: %d", d) + return fmt.Errorf("received unsupported decimals: %v", d)
74-80
: LGTM: Improved error handling insetEVMCoinDecimals
The changes to
setEVMCoinDecimals
align well with the PR objectives, improving error handling by propagating errors instead of causing panics. The use of the newValidate
method is a good practice.As a minor optimization, you can remove the explicit
return nil
at the end of the function. In Go, a function with a named return value (in this case,error
) automatically returns the zero value (nil
for error) if it reaches the end of the function without a return statement.func setEVMCoinDecimals(d Decimals) error { if err := d.Validate(); err != nil { return err } evmCoinInfo.Decimals = d - return nil }
This makes the code slightly more concise without changing its behavior.
84-89
: LGTM: Improved error handling insetEVMCoinDenom
The changes to
setEVMCoinDenom
align well with the PR objectives, improving error handling by propagating errors instead of causing panics. The use ofsdk.ValidateDenom
for input validation is a good practice.As with the previous function, you can remove the explicit
return nil
at the end of the function for a minor optimization:func setEVMCoinDenom(denom string) error { if err := sdk.ValidateDenom(denom); err != nil { return err } evmCoinInfo.Denom = denom - return nil }
This makes the code slightly more concise without changing its behavior.
Line range hint
1-117
: Reminder: Update unit tests for new validations and error handlingAs mentioned in a previous review, it's crucial to update or add unit tests to cover the new validations and error handling introduced in this refactor. This ensures that the new behavior is properly tested and helps prevent future regressions.
Specifically, consider adding tests for:
- The new
Validate
method of theDecimals
type.- Error cases in
setEVMCoinDecimals
,setEVMCoinDenom
, andsetEVMCoinInfo
.- The behavior when attempting to set EVM coin info multiple times.
Would you like assistance in generating these unit tests? I can provide examples or guidelines for effective test cases if needed.
x/evm/types/configurator.go (2)
28-28
: Ensure proper documentation forevmCoinInfo
fieldThe
evmCoinInfo
field has been added to theEVMConfigurator
struct. Please ensure that this new field is adequately documented, explaining its purpose and usage to maintain code clarity and assist future developers.
Line range hint
95-104
: Consider handling duplicate EIPs without returning an errorIn the
extendDefaultExtraEIPs
function, returning an error when a duplicate EIP is encountered may prevent the addition of subsequent valid EIPs. Consider logging a warning or skipping the duplicate EIP instead of returning an error to allow the function to process all provided EIPs.Apply this diff to modify the duplicate EIP handling:
func extendDefaultExtraEIPs(extraEIPs []string) error { for _, eip := range extraEIPs { if slices.Contains(DefaultExtraEIPs, eip) { - return fmt.Errorf("error configuring EVMConfigurator: EIP %s is already present in the default list: %v", eip, DefaultExtraEIPs) + // Skip duplicate EIP and continue processing the rest + continue } if err := vm.ValidateEIPName(eip); err != nil { return fmt.Errorf("error configuring EVMConfigurator: %s", err) } DefaultExtraEIPs = append(DefaultExtraEIPs, eip) } return nil }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (11)
- app/ante/testutils/testutils.go (1 hunks)
- app/config.go (1 hunks)
- app/config_testing.go (1 hunks)
- x/evm/keeper/grpc_query_test.go (1 hunks)
- x/evm/keeper/setup_test.go (1 hunks)
- x/evm/types/config_testing.go (1 hunks)
- x/evm/types/configurator.go (3 hunks)
- x/evm/types/denom_config.go (4 hunks)
- x/evm/types/denom_config_testing.go (1 hunks)
- x/evm/types/msg_test.go (1 hunks)
- x/evm/types/scaling_test.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (8)
- app/ante/testutils/testutils.go
- app/config.go
- app/config_testing.go
- x/evm/keeper/grpc_query_test.go
- x/evm/keeper/setup_test.go
- x/evm/types/config_testing.go
- x/evm/types/msg_test.go
- x/evm/types/scaling_test.go
🧰 Additional context used
🔇 Additional comments (7)
x/evm/types/denom_config_testing.go (2)
Line range hint
24-29
: LGTM: Constants updated to new Decimals typeThe
SixDecimals
andEighteenDecimals
constants have been correctly updated to use the newDecimals
type (uint64). The values remain unchanged, maintaining the representation of 6 and 18 decimal places respectively. This change is consistent with the modification of theDecimals
type and doesn't alter the functionality of these constants.
Line range hint
1-99
: Overall assessment: Changes are well-implemented with room for minor improvementsThe modification of the
Decimals
type fromuint32
touint64
has been consistently applied throughout the file. The changes maintain existing functionality while providing an expanded range for decimal representations.Key points:
- The type change is properly reflected in the type definition, constants, and method signatures.
- Existing logic in methods like
ConversionFactor
remains intact, ensuring backward compatibility.- Comments have been updated to reflect the changes accurately.
Suggestions for improvement:
- Consider adding documentation explaining the rationale behind the type change.
- The
ConversionFactor
method could be enhanced to handle a wider range ofDecimals
values, given the expanded range provided byuint64
.Overall, the changes are well-implemented and improve the flexibility of the
Decimals
type. With the suggested minor improvements, the code would be more robust and future-proof.x/evm/types/denom_config.go (2)
13-13
: LGTM: Import added for improved error handlingThe addition of the "errors" import aligns with the PR objective of improving error handling. This is a positive change that will allow for more standardized error creation and handling throughout the file.
31-31
: Clarification needed:Decimals
type remainsuint32
The AI summary incorrectly states that the
Decimals
type has been changed touint64
, but the code shows it's stilluint32
. Could you clarify if there was a decision to keep it asuint32
instead of changing it touint64
? If so, what were the considerations behind this decision?Additionally, given that only two specific decimal values (6 and 18) are supported as per the
Validate
method, would it be more appropriate to use an enum or a more restrictive type?x/evm/types/configurator.go (3)
59-60
: Parameter type change touint32
addresses previous overflow concernThe
decimals
parameter inWithEVMCoinInfo
has been changed touint32
, which prevents negative values and aligns with the expected range for token decimals. This change addresses the previous concern about integer underflow when converting from a signed to an unsigned integer type.
88-93
: Good practice sealing the configurator after modificationsSealing the
EVMConfigurator
after applying modifiers ensures that it cannot be reconfigured, preventing unintended changes after initialization. This is a best practice for maintaining the integrity of the configurator's state.
76-78
: Verify error handling insetEVMCoinInfo
Ensure that the
setEVMCoinInfo
function properly validates theevmCoinInfo
data and handles potential errors effectively. Robust validation and error handling are crucial for maintaining the stability and reliability of the configurator.Run the following script to check for proper error handling in
setEVMCoinInfo
:✅ Verification successful
Verification of error handling in
setEVMCoinInfo
confirmedThe
setEVMCoinInfo
function properly validates theevmCoinInfo
data and effectively handles potential errors by:
- Preventing reassignment if
evmCoinInfo
is already set.- Handling errors from
setEVMCoinDenom
andsetEVMCoinDecimals
appropriately.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Search for implementations of setEVMCoinInfo to verify error handling. # Expectation: Functions should validate `evmCoinInfo` and handle errors appropriately. rg --type go --func 'setEVMCoinInfo' -A 10Length of output: 451
Script:
#!/bin/bash # Description: Search for implementations of setEVMCoinInfo to verify error handling. # Using rg to find function definitions of setEVMCoinInfo rg 'func\s+setEVMCoinInfo' --type go -A 10Length of output: 634
Script:
#!/bin/bash # Description: Retrieve the full implementation of setEVMCoinInfo to verify validation and error handling. # Using rg with context lines to capture the entire function rg -A 50 'func\s+setEVMCoinInfo' --type goLength of output: 776
Small refactor to the configurator:
Summary by CodeRabbit
New Features
Bug Fixes
Improvements
Tests