-
Notifications
You must be signed in to change notification settings - Fork 14
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(ibc): refine the conversion logic between ibc coin and base coin during ibc transfer #723
Conversation
…coin during ibc transfer
WalkthroughThe pull request encompasses modifications across multiple files, primarily focusing on enhancing cross-chain functionality within the application. Key changes include updates to the Changes
Possibly related PRs
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: 14
🧹 Outside diff range and nitpick comments (11)
x/ibc/middleware/keeper/keeper.go (1)
18-23
: LGTM! Consider updating function documentation.The changes to the
NewKeeper
function correctly reflect the modifications made to theKeeper
struct. The function signature and body have been updated appropriately to handle the newcrossChainKeeper
parameter and remove therefundHook
anderc20Keeper
parameters.If there's any existing documentation for this function (e.g., godoc comments), please ensure it's updated to reflect these changes.
x/crosschain/precompile/contract.go (2)
36-36
: LGTM. Consider updating documentation.The addition of the
crossChainKeeper
field to theKeeper
struct initialization is consistent with the new parameter and integrates the cross-chain functionality into the keeper.Consider updating the documentation for the
Keeper
struct (if it exists) to reflect this new field and its purpose in the cross-chain functionality.
Line range hint
1-114
: Summary: Cross-chain integration looks good.The changes in this file are minimal and focused on integrating cross-chain functionality:
- Added
crossChainKeeper
parameter toNewPrecompiledContract
.- Included
crossChainKeeper
in theKeeper
struct initialization.These modifications align with the PR objectives and don't introduce any apparent issues. The overall structure and functionality of the contract remain intact, maintaining backwards compatibility.
As the cross-chain functionality is being integrated, consider reviewing the error handling and logging mechanisms throughout the codebase to ensure they adequately capture and report any cross-chain related issues or events.
types/target.go (1)
90-95
: LGTM! Consider adding error handling for robustness.The
String()
method implementation looks good and provides a consistent string representation of theFxTarget
. It aligns well with the existingGetTarget()
method while offering a different format for IBC targets.For added robustness, consider handling potential error cases:
func (i FxTarget) String() string { if i.isIBC { + if !strings.HasPrefix(i.SourceChannel, channeltypes.ChannelPrefix) { + return fmt.Sprintf("invalid-ibc-target:%s", i.SourceChannel) + } return fmt.Sprintf("ibc/%s/%s", strings.TrimPrefix(i.SourceChannel, channeltypes.ChannelPrefix), i.Prefix) } return i.target }This change ensures that the method handles cases where
SourceChannel
might not have the expected prefix, improving the robustness of the code.x/crosschain/types/expected_keepers.go (2)
63-64
: LGTM: New methods for IBC functionality in Erc20KeeperThe new methods
IbcRefund
andDeleteIBCTransferRelation
are well-defined and consistent with the existing codebase style. They enhance the IBC transfer handling capabilities of theErc20Keeper
interface.Consider adding inline documentation for these new methods to explain their purpose and expected behavior.
Line range hint
1-93
: Overall assessment: Changes enhance IBC functionalityThe modifications to this file align well with the PR objectives of refining the conversion logic between IBC coin and base coin during IBC transfer. The new methods in the
Erc20Keeper
andIBCTransferKeeper
interfaces provide enhanced capabilities for handling IBC transfers and denom tracing.Key points:
- New import for
tmbytes
is correctly added.- Two new methods in
Erc20Keeper
improve IBC refund and transfer relation management.- New
GetDenomTrace
method inIBCTransferKeeper
enhances denom tracing capabilities.These changes should improve the cross-chain functionality of the application. Please address the typo mentioned earlier and consider adding inline documentation for the new methods to improve code maintainability.
x/crosschain/keeper/keeper_v1_test.go (1)
202-211
: LGTM: Well-structured helper method for IBC denom traces.The
SetIBCDenom
method is a well-implemented helper for setting up IBC denomination traces in tests. It correctly constructs the prefixed denomination, creates theDenomTrace
, and handles the case where the trace doesn't exist in the keeper.Consider adding error handling for the
ParseDenomTrace
call, as it might return an error in some cases:func (suite *KeeperTestSuite) SetIBCDenom(portID, channelID, denom string) ibctransfertypes.DenomTrace { sourcePrefix := ibctransfertypes.GetDenomPrefix(portID, channelID) prefixedDenom := sourcePrefix + denom - denomTrace := ibctransfertypes.ParseDenomTrace(prefixedDenom) + denomTrace, err := ibctransfertypes.ParseDenomTrace(prefixedDenom) + suite.Require().NoError(err) traceHash := denomTrace.Hash() if !suite.App.IBCTransferKeeper.HasDenomTrace(suite.Ctx, traceHash) { suite.App.IBCTransferKeeper.SetDenomTrace(suite.Ctx, denomTrace) } return denomTrace }This change ensures that any parsing errors are caught and reported during testing.
x/ibc/middleware/types/expected_keepers.go (1)
17-19
: Consider adding documentation comments to interface methodsAdding GoDoc comments to the methods in the
CrossChainKeeper
interface would improve readability and maintainability by clearly explaining the purpose, expected behavior, and usage of each method. This is especially helpful for interfaces, as it guides implementers on how to properly fulfill the contract.Example:
type CrossChainKeeper interface { // IBCCoinToEvm handles the conversion of an IBC coin to its EVM equivalent. IBCCoinToEvm(ctx sdk.Context, coin sdk.Coin, holder sdk.AccAddress) error // IBCCoinRefund processes the refund of an IBC coin to the holder. IBCCoinRefund(ctx sdk.Context, coin sdk.Coin, holder sdk.AccAddress, ibcChannel string, ibcSequence uint64) error // AfterIBCAckSuccess performs post-processing after a successful IBC acknowledgment. AfterIBCAckSuccess(ctx sdk.Context, sourceChannel string, sequence uint64) }x/crosschain/keeper/send_to_fx.go (1)
Line range hint
72-74
: Remove outdated TODO comment about converting to IBC tokenIn the
RelayTransferHandler
method, the comment// todo convert to ibc token
is no longer necessary since the conversion is now handled in thetransferIBCHandler
function with the introduction ofibcCoin
. Removing this comment will help keep the code clean and prevent confusion.x/crosschain/keeper/many_to_one.go (1)
242-253
: Handle potential errors fromConvertCoin
more thoroughly.After calling
k.erc20Keeper.ConvertCoin
, consider inspecting the response for additional information or warnings that might need handling, even if an error isn't returned.Enhance error handling by checking the response:
_, err = k.erc20Keeper.ConvertCoin(ctx, &erc20types.MsgConvertCoin{ Coin: baseCoin, Receiver: common.BytesToAddress(holder).String(), Sender: holder.String(), }) +if err != nil { + return err +} // Handle any additional response data if necessary return nilx/ibc/middleware/keeper/relay_test.go (1)
Line range hint
17-231
: Reconsider Commenting Out theTestOnRecvPacket
FunctionThe entire
TestOnRecvPacket
function has been commented out (lines 17-231). Commenting out test code reduces test coverage and might allow bugs to go unnoticed. If this test is outdated due to recent refactoring, consider updating it to align with the new logic. Alternatively, if it's no longer needed, it might be cleaner to remove it entirely from the codebase.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (18)
- Makefile (1 hunks)
- app/keepers/keepers.go (2 hunks)
- types/target.go (1 hunks)
- x/crosschain/keeper/abci_test.go (3 hunks)
- x/crosschain/keeper/keeper_v1_test.go (2 hunks)
- x/crosschain/keeper/many_to_one.go (3 hunks)
- x/crosschain/keeper/send_to_fx.go (2 hunks)
- x/crosschain/mock/expected_keepers_mocks.go (4 hunks)
- x/crosschain/precompile/contract.go (1 hunks)
- x/crosschain/precompile/crosschain_test.go (0 hunks)
- x/crosschain/precompile/expected_keepers.go (1 hunks)
- x/crosschain/precompile/keeper.go (2 hunks)
- x/crosschain/types/expected_keepers.go (3 hunks)
- x/erc20/keeper/transfer_relation.go (1 hunks)
- x/ibc/middleware/keeper/keeper.go (1 hunks)
- x/ibc/middleware/keeper/relay.go (4 hunks)
- x/ibc/middleware/keeper/relay_test.go (8 hunks)
- x/ibc/middleware/types/expected_keepers.go (1 hunks)
💤 Files with no reviewable changes (1)
- x/crosschain/precompile/crosschain_test.go
🧰 Additional context used
🔇 Additional comments (27)
x/crosschain/precompile/expected_keepers.go (2)
Line range hint
1-65
: Summary: New CrossChainKeeper interface enhances cross-chain functionalityThe addition of the
CrossChainKeeper
interface with theBaseCoinToIBCCoin
method is a positive change that aligns with the PR objectives. It extends the cross-chain functionality, specifically for converting base coins to IBC coins during transfers. This change is well-integrated with the existing keeper interfaces in the file and follows good coding practices.
63-65
: LGTM! New CrossChainKeeper interface looks good.The new
CrossChainKeeper
interface with theBaseCoinToIBCCoin
method is well-defined and aligns with the PR objectives. The method signature is correct and follows Go conventions.Let's verify if this interface is implemented correctly in the codebase:
x/crosschain/types/expected_keepers.go (1)
7-7
: LGTM: New import for tmbytesThe new import for
tmbytes
from the CometBFT library is correctly added and necessary for the new method signature in theIBCTransferKeeper
interface.x/crosschain/keeper/keeper_v1_test.go (1)
14-14
: LGTM: Import statement for IBC transfer types.The import of
ibctransfertypes
is correctly added and necessary for the newSetIBCDenom
method.Makefile (1)
145-145
: Verify the reduction in lint suppressions.The change from 32 to 31 in the
check-no-lint
target suggests that a lint suppression has been removed from the codebase. This is a positive change as it indicates a reduction in the number of lint suppressions.Please confirm that this change corresponds to an actual removal of a 'nolint' or '#nosec' comment in the codebase. You can use the following command to verify:
✅ Verification successful
Lint suppression count verified.
The number of 'nolint' or '#nosec' occurrences has been successfully reduced to 31, as reflected in the
check-no-lint
target.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Count the number of 'nolint' or '#nosec' comments in Go files # Test: Count 'nolint' or '#nosec' occurrences count=$(find . -name '*.go' -type f | xargs grep -E 'nolint|#nosec' | wc -l) echo "Number of 'nolint' or '#nosec' occurrences: $count" # Verify if the count matches the new expected value if [ "$count" -eq 31 ]; then echo "The count matches the new expected value (31)." else echo "The count does not match the new expected value. Please investigate." fiLength of output: 321
x/crosschain/mock/expected_keepers_mocks.go (5)
17-17
: LGTM: New import added correctly.The new import for the
bytes
package is correctly added and is necessary for the newGetDenomTrace
method.
431-443
: LGTM: New mock methodDeleteIBCTransferRelation
added correctly.The
DeleteIBCTransferRelation
method is correctly implemented in theMockErc20Keeper
. It follows the GoMock conventions and has the appropriate signature.
500-512
: LGTM: New mock methodIbcRefund
added correctly.The
IbcRefund
method is correctly implemented in theMockErc20Keeper
. It follows the GoMock conventions and has the appropriate signature.
643-656
: LGTM: New mock methodGetDenomTrace
added correctly.The
GetDenomTrace
method is correctly implemented in theMockIBCTransferKeeper
. It follows the GoMock conventions, has the appropriate signature, and correctly uses thebytes.HexBytes
type fordenomTraceHash
.
Line range hint
1-857
: Summary: Mock implementations enhanced for IBC and denomination tracing.The changes to this mock file introduce new methods that enhance the ability to test IBC transfer and denomination tracing functionality. These additions include:
- A new import for the
bytes
package.DeleteIBCTransferRelation
method inMockErc20Keeper
.IbcRefund
method inMockErc20Keeper
.GetDenomTrace
method inMockIBCTransferKeeper
.These changes will allow for more comprehensive unit testing of systems that depend on these interfaces, particularly in scenarios involving IBC transfers and denomination tracing.
x/erc20/keeper/transfer_relation.go (2)
15-20
: Ensure proper error handling fromConvertCoin
The error returned by
k.ConvertCoin
is being returned directly. IfConvertCoin
fails, additional context or actions might be necessary to handle the failure adequately.Consider logging the error or adding additional context to the error before returning it.
_, err := k.ConvertCoin(ctx, &types.MsgConvertCoin{ Coin: amount, Receiver: common.BytesToAddress(sender.Bytes()).String(), Sender: sender.String(), }) -return err +if err != nil { + return fmt.Errorf("failed to convert coin: %w", err) +} +return nil
10-20
: Verify all references to the renamed function are updatedThe method
RefundAfter
has been renamed toIbcRefund
. Ensure that all other references toRefundAfter
in the codebase have been updated accordingly to prevent any broken references.Run the following script to check for any remaining references to
RefundAfter
:x/ibc/middleware/keeper/relay.go (3)
6-6
: Importingsdkerrors
for error handlingThe addition of
sdkerrors
is appropriate, as it is used for wrapping errors in the updated code.
58-58
: Proper handling of successful acknowledgementsThe call to
k.crossChainKeeper.AfterIBCAckSuccess
correctly handles the logic after a successful packet acknowledgment.
85-85
: Refactor refund logic to utilizecrossChainKeeper
Refactoring to use
k.crossChainKeeper.IBCCoinRefund
centralizes the refund logic, enhancing code maintainability and readability.x/crosschain/keeper/send_to_fx.go (2)
92-96
: Properly converting base coin to IBC coin before transferThe addition of
ibcCoin, err := k.BaseCoinToIBCCoin(ctx, coin, receive, target.String())
ensures that the base coin is correctly converted to an IBC coin before initiating the transfer. Error handling is appropriately managed by checking and returning the error if it occurs.
105-105
: Correct usage of the converted IBC coin in IBC transferBy passing
ibcCoin
totransfertypes.NewMsgTransfer
, you ensure that the transfer uses the correctly converted IBC coin. This change is crucial for maintaining the integrity of cross-chain transactions.x/crosschain/precompile/keeper.go (1)
160-166
: Confirm proper handling of theBaseCoinToIBCCoin
conversionThe addition of the
BaseCoinToIBCCoin
conversion whenoriginToken
isfalse
ensures that base coins are correctly converted to IBC coins before the transfer. This change is essential for refining the conversion logic during IBC transfers.Consider running integration tests to ensure that:
- The conversion logic handles various edge cases, such as zero amounts or maximum coin values.
- The function behaves correctly with different
fxTarget
values.- Errors are appropriately returned and handled.
x/crosschain/keeper/many_to_one.go (5)
5-5
: Import of "fmt" is appropriate and necessary.The addition of the
fmt
package is required for string formatting used in the code, such as infmt.Sprintf
.
13-13
: Inclusion of Ethereum common package is justified.Importing
github.com/ethereum/go-ethereum/common
is necessary for Ethereum address conversions, specificallycommon.BytesToAddress
.
16-16
: Addition of erc20types import is appropriate.The
erc20types
package is utilized in the code for operations involving the ERC20 module, such as invokingConvertCoin
.
255-261
: Ensure correctness of the refund process inIBCCoinRefund
.Confirm that converting the IBC coin to base coin and then calling
k.erc20Keeper.IbcRefund
accurately processes the refund without side effects. Pay special attention to state changes and potential edge cases.Consider adding tests to cover various refund scenarios, ensuring that the refund logic behaves as expected under different conditions.
263-265
: Validate cleanup operation inAfterIBCAckSuccess
.Deleting the outgoing transfer relation after a successful IBC acknowledgment is critical. Ensure that this operation doesn't inadvertently affect other processes or data dependencies within the module.
Add logging or monitoring to track the deletion and assess its impact, and consider writing tests to verify that related functionalities remain unaffected.
x/crosschain/keeper/abci_test.go (4)
6-6
: Importing "strings" package is necessaryThe addition of the
"strings"
package is appropriate sincestrings.ToUpper
is used in the code.
12-12
: Importingibctransfertypes
is appropriateThe import of
ibctransfertypes
is necessary for accessingibctransfertypes.ModuleName
used later in the code.
72-72
: Ensure the amount uses the correct denomination unitsConfirm that the amount
sendToFxClaim.Amount
used insdk.NewCoin(ibcDenom, sendToFxClaim.Amount)
is in the smallest unit of the token (typically1e-18
for tokens with 18 decimals). This ensures accurate token balances and prevents potential discrepancies.
72-72
: Verify the correctness of minting tokens toibctransfertypes.ModuleName
At line 72, tokens are minted to the
ibctransfertypes.ModuleName
module account. Please verify if this is the intended behavior. Typically, tokens are minted to thecrosschain
module or sent directly to user accounts. Minting to the IBC transfer module may have unintended effects if not handled properly.Run the following script to review all instances where
MintTokenToModule
is used and confirm that the correct module names are being used:
Summary by CodeRabbit
New Features
CrossChainKeeper
interface to support cross-chain operations.FxTarget
instances.Bug Fixes
Refactor
Tests