-
Notifications
You must be signed in to change notification settings - Fork 586
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
(chore) Refactor code around forwarding validation #6706
Merged
bznein
merged 12 commits into
feat/ics20-v2-path-forwarding
from
bznein/6696/refactorvalidation
Jun 26, 2024
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
baa0e8b
Refactor validation
bznein 2121f42
Fixed verification logic, added two tests
bznein 05b64e2
Fix check for unwind
bznein 2944cae
removed unneeded indirection
bznein e953cf0
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6696/refacto…
bznein 99af151
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6696/refacto…
gjermundgaraba da8fddb
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6696/refacto…
gjermundgaraba 72dfc06
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6696/refacto…
gjermundgaraba d89d447
Update modules/apps/transfer/types/msgs.go
bznein 6f4bc87
Add docstring.
bznein 4f6033a
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6696/refacto…
bznein d297771
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6696/refacto…
bznein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,10 +69,22 @@ func NewMsgTransfer( | |
// NOTE: The recipient addresses format is not validated as the format defined by | ||
// the chain is not known to IBC. | ||
func (msg MsgTransfer) ValidateBasic() error { | ||
if err := validateSourcePortAndChannel(msg); err != nil { | ||
return err // The actual error and its message are already wrapped in the called function. | ||
if err := msg.validateForwarding(); err != nil { | ||
return err | ||
} | ||
if !msg.Forwarding.Unwind { | ||
// We verify that portID and channelID are valid IDs only if | ||
// we are not setting unwind to true. | ||
// In that case, validation that they are empty is performed in | ||
// validateForwarding(). | ||
if err := host.PortIdentifierValidator(msg.SourcePort); err != nil { | ||
return errorsmod.Wrap(err, "invalid source port ID") | ||
} | ||
|
||
if err := host.ChannelIdentifierValidator(msg.SourceChannel); err != nil { | ||
return errorsmod.Wrap(err, "invalid source channel ID") | ||
} | ||
} | ||
if len(msg.Tokens) == 0 && !isValidIBCCoin(msg.Token) { | ||
return errorsmod.Wrap(ibcerrors.ErrInvalidCoins, "either token or token array must be filled") | ||
} | ||
|
@@ -99,30 +111,41 @@ func (msg MsgTransfer) ValidateBasic() error { | |
return errorsmod.Wrapf(ErrInvalidMemo, "memo must not exceed %d bytes", MaximumMemoLength) | ||
} | ||
|
||
for _, coin := range msg.GetCoins() { | ||
if err := validateIBCCoin(coin); err != nil { | ||
return errorsmod.Wrapf(ibcerrors.ErrInvalidCoins, "%s: %s", err.Error(), coin.String()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (msg MsgTransfer) validateForwarding() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets slap a docusting on this here method |
||
if !msg.ShouldBeForwarded() { | ||
return nil | ||
} | ||
if err := msg.Forwarding.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
if msg.ShouldBeForwarded() { | ||
if !msg.TimeoutHeight.IsZero() { | ||
// when forwarding, the timeout height must not be set | ||
if !msg.TimeoutHeight.IsZero() { | ||
return errorsmod.Wrapf(ErrInvalidPacketTimeout, "timeout height must not be set if forwarding path hops is not empty: %s, %s", msg.TimeoutHeight, msg.Forwarding.Hops) | ||
} | ||
return errorsmod.Wrapf(ErrInvalidPacketTimeout, "timeout height must not be set if forwarding path hops is not empty: %s, %s", msg.TimeoutHeight, msg.Forwarding.Hops) | ||
bznein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if msg.Forwarding.Unwind { | ||
// When unwinding, we must have at most one token. | ||
if msg.SourcePort != "" { | ||
return errorsmod.Wrapf(ErrInvalidForwarding, "source port must be empty when unwind is set, got %s instead", msg.SourcePort) | ||
} | ||
if msg.SourceChannel != "" { | ||
return errorsmod.Wrapf(ErrInvalidForwarding, "source channel must be empty when unwind is set, got %s instead", msg.SourceChannel) | ||
} | ||
if len(msg.GetCoins()) > 1 { | ||
// When unwinding, we must have at most one token. | ||
return errorsmod.Wrap(ibcerrors.ErrInvalidCoins, "cannot unwind more than one token") | ||
} | ||
} | ||
|
||
for _, coin := range msg.GetCoins() { | ||
if err := validateIBCCoin(coin); err != nil { | ||
return errorsmod.Wrapf(ibcerrors.ErrInvalidCoins, "%s: %s", err.Error(), coin.String()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -164,25 +187,3 @@ func validateIBCCoin(coin sdk.Coin) error { | |
|
||
return nil | ||
} | ||
|
||
func validateSourcePortAndChannel(msg MsgTransfer) error { | ||
// If unwind is set, we want to ensure that port and channel are empty. | ||
if msg.Forwarding.Unwind { | ||
if msg.SourcePort != "" { | ||
return errorsmod.Wrapf(ErrInvalidForwarding, "source port must be empty when unwind is set, got %s instead", msg.SourcePort) | ||
} | ||
if msg.SourceChannel != "" { | ||
return errorsmod.Wrapf(ErrInvalidForwarding, "source channel must be empty when unwind is set, got %s instead", msg.SourceChannel) | ||
} | ||
return nil | ||
} | ||
|
||
// Otherwise, we just do the usual validation of the port and channel identifiers. | ||
if err := host.PortIdentifierValidator(msg.SourcePort); err != nil { | ||
return errorsmod.Wrap(err, "invalid source port ID") | ||
} | ||
if err := host.ChannelIdentifierValidator(msg.SourceChannel); err != nil { | ||
return errorsmod.Wrap(err, "invalid source channel ID") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ate my other comment 😅
this seems odd outside of the
validateForwarding
, seems like it could be mvoed there if not for early return ofif !msg.ShouldBeForwarded
. Non blocker for me though, should probably check the walkthrough call first to see concern raised!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.
I wanted to achieve the same, but couldn't find a way that's actually nice.
The problem is that, if we move this inside
validateForwarding
, we are actually validating something that is not forwarding-related (since we havelen(hops)==0 && !msg.Forwarind.Unwind
) hidden there.I thought of the current situation as a valid compromise but I'm open to suggestion