Skip to content

Commit

Permalink
do handshake logic, create test file
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Aug 3, 2021
1 parent 158a251 commit 7b51ebd
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 12 deletions.
45 changes: 35 additions & 10 deletions modules/apps/29-fee/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fee

/*
import (
"context"
"encoding/json"
Expand Down Expand Up @@ -90,12 +89,14 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
type AppModule struct {
AppModuleBasic
keeper keeper.Keeper
app porttypes.IBCModule
}

// NewAppModule creates a new 29-fee module
func NewAppModule(k keeper.Keeper) AppModule {
func NewAppModule(k keeper.Keeper, app porttypes.IBCModule) AppModule {
return AppModule{
keeper: k,
app: app,
}
}

Expand Down Expand Up @@ -191,7 +192,13 @@ func (am AppModule) OnChanOpenInit(
counterparty channeltypes.Counterparty,
version string,
) error {
return nil
feeVersion, appVersion := channeltypes.SplitChannelVersion(version)
if feeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected: %s, got: %s", types.Version, feeVersion)
}
// call underlying app's OnChanOpenInit callback with the appVersion
return am.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID,
chanCap, counterparty, appVersion)
}

// OnChanOpenTry implements the IBCModule interface
Expand All @@ -206,7 +213,18 @@ func (am AppModule) OnChanOpenTry(
version,
counterpartyVersion string,
) error {
return nil
feeVersion, appVersion := channeltypes.SplitChannelVersion(version)
cpFeeVersion, cpAppVersion := channeltypes.SplitChannelVersion(counterpartyVersion)

if feeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected: %s, got: %s", types.Version, feeVersion)
}
if cpFeeVersion != feeVersion {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected counterparty version: %s, got: %s", types.Version, cpFeeVersion)
}
// call underlying app's OnChanOpenTry callback with the app versions
return am.app.OnChanOpenTry(ctx, order, connectionHops, portID, channelID,
chanCap, counterparty, appVersion, cpAppVersion)
}

// OnChanOpenAck implements the IBCModule interface
Expand All @@ -216,7 +234,13 @@ func (am AppModule) OnChanOpenAck(
channelID string,
counterpartyVersion string,
) error {
return nil
cpFeeVersion, cpAppVersion := channeltypes.SplitChannelVersion(counterpartyVersion)

if cpFeeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected counterparty version: %s, got: %s", types.Version, cpFeeVersion)
}
// call underlying app's OnChanOpenAck callback with the counterparty app version.
return am.app.OnChanOpenAck(ctx, portID, channelID, cpAppVersion)
}

// OnChanOpenConfirm implements the IBCModule interface
Expand All @@ -225,7 +249,8 @@ func (am AppModule) OnChanOpenConfirm(
portID,
channelID string,
) error {
return nil
// call underlying app's OnChanOpenConfirm callback.
return am.app.OnChanOpenConfirm(ctx, portID, channelID)
}

// OnChanCloseInit implements the IBCModule interface
Expand All @@ -234,8 +259,8 @@ func (am AppModule) OnChanCloseInit(
portID,
channelID string,
) error {
// Disallow user-initiated channel closing for 29-fee channels
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
// TODO: Unescrow all remaining funds for unprocessed packets
return am.app.OnChanCloseInit(ctx, portID, channelID)
}

// OnChanCloseConfirm implements the IBCModule interface
Expand All @@ -244,7 +269,8 @@ func (am AppModule) OnChanCloseConfirm(
portID,
channelID string,
) error {
return nil
// TODO: Unescrow all remaining funds for unprocessed packets
return am.app.OnChanCloseConfirm(ctx, portID, channelID)
}

// OnRecvPacket implements the IBCModule interface.
Expand Down Expand Up @@ -274,4 +300,3 @@ func (am AppModule) OnTimeoutPacket(
) error {
return nil
}
*/
Empty file.
6 changes: 4 additions & 2 deletions modules/apps/29-fee/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package types

import (
// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// 29-fee sentinel errors
var ()
var (
ErrInvalidVersion = sdkerrors.Register(ModuleName, 2, "invalid ICS29 middleware version")
)
2 changes: 2 additions & 0 deletions modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ const (

// QuerierRoute is the querier route for IBC transfer
QuerierRoute = ModuleName

Version = "fee29-1"
)
19 changes: 19 additions & 0 deletions modules/core/04-channel/types/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types

import "strings"

// SplitChannelVersion middleware version will split the channel version string
// into the outermost middleware version and the underlying app version.
// It will use the default delimiter `:` for middleware versions.
// In case there's no delimeter, this function returns an empty string for the middleware version (first return argument),
// and the full input as the second underlying app version.
func SplitChannelVersion(version string) (middlewareVersion, appVersion string) {
// only split out the first middleware version
splitVersions := strings.Split(version, ":")
if len(splitVersions) == 1 {
return "", version
}
middlewareVersion = splitVersions[0]
appVersion = strings.Join(splitVersions[1:], ":")
return
}
43 changes: 43 additions & 0 deletions modules/core/04-channel/types/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/modules/core/04-channel/types"
)

func TestSplitVersions(t *testing.T) {
testCases := []struct {
name string
version string
mwVersion string
appVersion string
}{
{
"single wrapped middleware",
"fee29-1:ics20-1",
"fee29-1",
"ics20-1",
},
{
"multiple wrapped middleware",
"fee29-1:whitelist:ics20-1",
"fee29-1",
"whitelist:ics20-1",
},
{
"no middleware",
"ics20-1",
"",
"ics20-1",
},
}

for _, tc := range testCases {
mwVersion, appVersion := types.SplitChannelVersion(tc.version)
require.Equal(t, tc.mwVersion, mwVersion, "middleware version is unexpected for case: %s", tc.name)
require.Equal(t, tc.appVersion, appVersion, "app version is unexpected for case: %s", tc.name)
}
}

0 comments on commit 7b51ebd

Please sign in to comment.