-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/ics20-v2-path-forwarding' into serdar/6558-refacto…
…r-revert-forward
- Loading branch information
Showing
10 changed files
with
380 additions
and
92 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package types | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
|
||
host "github.com/cosmos/ibc-go/v8/modules/core/24-host" | ||
) | ||
|
||
const MaximumNumberOfForwardingHops = 64 | ||
|
||
// NewForwardingInfo creates a new ForwardingInfo instance given a memo and a variable number of hops. | ||
func NewForwardingInfo(memo string, hops ...Hop) *ForwardingInfo { | ||
return &ForwardingInfo{ | ||
Memo: memo, | ||
Hops: hops, | ||
} | ||
} | ||
|
||
// Validate performs a basic validation of the ForwardingInfo fields. | ||
func (fi ForwardingInfo) Validate() error { | ||
if len(fi.Hops) > MaximumNumberOfForwardingHops { | ||
return errorsmod.Wrapf(ErrInvalidForwardingInfo, "number of hops in forwarding path cannot exceed %d", MaximumNumberOfForwardingHops) | ||
} | ||
|
||
for _, hop := range fi.Hops { | ||
if err := host.PortIdentifierValidator(hop.PortId); err != nil { | ||
return errorsmod.Wrapf(err, "invalid source port ID %s", hop.PortId) | ||
} | ||
if err := host.ChannelIdentifierValidator(hop.ChannelId); err != nil { | ||
return errorsmod.Wrapf(err, "invalid source channel ID %s", hop.ChannelId) | ||
} | ||
} | ||
|
||
if len(fi.Memo) > MaximumMemoLength { | ||
return errorsmod.Wrapf(ErrInvalidMemo, "memo length cannot exceed %d", MaximumMemoLength) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" | ||
host "github.com/cosmos/ibc-go/v8/modules/core/24-host" | ||
ibctesting "github.com/cosmos/ibc-go/v8/testing" | ||
) | ||
|
||
var validHop = types.Hop{ | ||
PortId: types.PortID, | ||
ChannelId: ibctesting.FirstChannelID, | ||
} | ||
|
||
func TestForwardingInfo_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
forwardingInfo *types.ForwardingInfo | ||
expError error | ||
}{ | ||
{ | ||
"valid forwarding info with no hops", | ||
types.NewForwardingInfo(""), | ||
nil, | ||
}, | ||
{ | ||
"valid forwarding info with hops", | ||
types.NewForwardingInfo("", validHop), | ||
nil, | ||
}, | ||
{ | ||
"valid forwarding info with memo", | ||
types.NewForwardingInfo(testMemo1, validHop, validHop), | ||
nil, | ||
}, | ||
{ | ||
"valid forwarding info with max hops", | ||
types.NewForwardingInfo("", generateHops(types.MaximumNumberOfForwardingHops)...), | ||
nil, | ||
}, | ||
{ | ||
"valid forwarding info with max memo length", | ||
types.NewForwardingInfo(ibctesting.GenerateString(types.MaximumMemoLength), validHop), | ||
nil, | ||
}, | ||
{ | ||
"invalid forwarding info with too many hops", | ||
types.NewForwardingInfo("", generateHops(types.MaximumNumberOfForwardingHops+1)...), | ||
types.ErrInvalidForwardingInfo, | ||
}, | ||
{ | ||
"invalid forwarding info with too long memo", | ||
types.NewForwardingInfo(ibctesting.GenerateString(types.MaximumMemoLength+1), validHop), | ||
types.ErrInvalidMemo, | ||
}, | ||
{ | ||
"invalid forwarding info with too short hop port ID", | ||
types.NewForwardingInfo( | ||
"", | ||
types.Hop{ | ||
PortId: invalidShortPort, | ||
ChannelId: ibctesting.FirstChannelID, | ||
}, | ||
), | ||
host.ErrInvalidID, | ||
}, | ||
{ | ||
"invalid forwarding info with too long hop port ID", | ||
types.NewForwardingInfo( | ||
"", | ||
types.Hop{ | ||
PortId: invalidLongPort, | ||
ChannelId: ibctesting.FirstChannelID, | ||
}, | ||
), | ||
host.ErrInvalidID, | ||
}, | ||
{ | ||
"invalid forwarding info with non-alpha hop port ID", | ||
types.NewForwardingInfo( | ||
"", | ||
types.Hop{ | ||
PortId: invalidPort, | ||
ChannelId: ibctesting.FirstChannelID, | ||
}, | ||
), | ||
host.ErrInvalidID, | ||
}, | ||
{ | ||
"invalid forwarding info with too long hop channel ID", | ||
types.NewForwardingInfo( | ||
"", | ||
types.Hop{ | ||
PortId: types.PortID, | ||
ChannelId: invalidLongChannel, | ||
}, | ||
), | ||
host.ErrInvalidID, | ||
}, | ||
{ | ||
"invalid forwarding info with too short hop channel ID", | ||
types.NewForwardingInfo( | ||
"", | ||
types.Hop{ | ||
PortId: types.PortID, | ||
ChannelId: invalidShortChannel, | ||
}, | ||
), | ||
host.ErrInvalidID, | ||
}, | ||
{ | ||
"invalid forwarding info with non-alpha hop channel ID", | ||
types.NewForwardingInfo( | ||
"", | ||
types.Hop{ | ||
PortId: types.PortID, | ||
ChannelId: invalidChannel, | ||
}, | ||
), | ||
host.ErrInvalidID, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc := tc | ||
|
||
err := tc.forwardingInfo.Validate() | ||
|
||
expPass := tc.expError == nil | ||
if expPass { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorIs(t, err, tc.expError) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func generateHops(n int) []types.Hop { | ||
hops := make([]types.Hop, n) | ||
for i := 0; i < n; i++ { | ||
hops[i] = types.Hop{ | ||
PortId: types.PortID, | ||
ChannelId: ibctesting.FirstChannelID, | ||
} | ||
} | ||
return hops | ||
} |
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
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
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.