Skip to content
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

feat: adding OnChanUpgradeInit handler implementation to 29-fee #4019

Merged
merged 14 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion modules/apps/29-fee/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,32 @@ func (im IBCMiddleware) OnTimeoutPacket(

// OnChanUpgradeInit implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeInit(ctx sdk.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, sequence uint64, version, previousVersion string) (string, error) {
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
return im.app.OnChanUpgradeInit(ctx, portID, channelID, order, connectionHops, sequence, version, previousVersion)
var versionMetadata types.Metadata
if err := types.ModuleCdc.UnmarshalJSON([]byte(version), &versionMetadata); err != nil {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
// Since it is valid for fee version to not be specified, the above middleware version may be for a middleware
// lower down in the stack. Thus, if it is not a fee version we pass the entire version string onto the underlying
// application.
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
return im.app.OnChanUpgradeInit(ctx, portID, channelID, order, connectionHops, sequence, version, previousVersion)
}

if versionMetadata.FeeVersion != types.Version {
return "", errorsmod.Wrapf(types.ErrInvalidVersion, "expected %s, got %s", types.Version, versionMetadata.FeeVersion)
}

appVersion, err := im.app.OnChanUpgradeInit(ctx, portID, channelID, order, connectionHops, sequence, versionMetadata.AppVersion, previousVersion)
if err != nil {
return "", err
}

versionMetadata.AppVersion = appVersion
versionBz, err := types.ModuleCdc.MarshalJSON(&versionMetadata)
if err != nil {
return "", err
}

im.keeper.SetFeeEnabled(ctx, portID, channelID)
charleenfei marked this conversation as resolved.
Show resolved Hide resolved

return string(versionBz), nil
}

// OnChanUpgradeTry implement s the IBCModule interface
Expand Down
84 changes: 84 additions & 0 deletions modules/apps/29-fee/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,90 @@ func (suite *FeeTestSuite) TestOnTimeoutPacket() {
}
}

func (suite *FeeTestSuite) TestOnChanUpgradeInit() {
var path *ibctesting.Path

testCases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
{
"invalid upgrade version",
func() {
path.EndpointA.ChannelConfig.ProposedUpgrade.Fields.Version = "invalid-version"
path.EndpointB.ChannelConfig.ProposedUpgrade.Fields.Version = "invalid-version"

suite.chainA.GetSimApp().FeeMockModule.IBCApp.OnChanUpgradeInit = func(_ sdk.Context, _, _ string, _ channeltypes.Order, _ []string, _ uint64, _, _ string) (string, error) {
return "", ibcmock.MockApplicationCallbackError
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}
},
ibcmock.MockApplicationCallbackError,
},
{
"invalid fee version",
func() {
upgradeVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: "invalid-version", AppVersion: ibcmock.Version}))
path.EndpointA.ChannelConfig.ProposedUpgrade.Fields.Version = upgradeVersion
path.EndpointB.ChannelConfig.ProposedUpgrade.Fields.Version = upgradeVersion
},
types.ErrInvalidVersion,
},
{
"underlying app callback returns error",
func() {
suite.chainA.GetSimApp().FeeMockModule.IBCApp.OnChanUpgradeInit = func(_ sdk.Context, _, _ string, _ channeltypes.Order, _ []string, _ uint64, _, _ string) (string, error) {
return "", ibcmock.MockApplicationCallbackError
}
},
ibcmock.MockApplicationCallbackError,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()

path = ibctesting.NewPath(suite.chainA, suite.chainB)

// configure the initial path to create an unincentivized mock channel
path.EndpointA.ChannelConfig.PortID = ibctesting.MockFeePort
path.EndpointB.ChannelConfig.PortID = ibctesting.MockFeePort
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
path.EndpointA.ChannelConfig.Version = ibcmock.Version
path.EndpointB.ChannelConfig.Version = ibcmock.Version

suite.coordinator.Setup(path)

// configure the channel upgrade version to enabled ics29 fee middleware
upgradeVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version}))
path.EndpointA.ChannelConfig.ProposedUpgrade.Fields.Version = upgradeVersion
path.EndpointB.ChannelConfig.ProposedUpgrade.Fields.Version = upgradeVersion

tc.malleate()

err := path.EndpointA.ChanUpgradeInit()

isFeeEnabled := suite.chainA.GetSimApp().IBCFeeKeeper.IsFeeEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

expPass := tc.expError == nil
if expPass {
suite.Require().True(isFeeEnabled)
suite.Require().NoError(err)
} else {
suite.Require().False(isFeeEnabled)
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expError)
}
})
}
}

func (suite *FeeTestSuite) TestGetAppVersion() {
var (
portID string
Expand Down
3 changes: 1 addition & 2 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,13 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
chain.Coordinator.UpdateTimeForChain(chain)

_, r, err := simapp.SignAndDeliver(
chain.TB,
chain.TxConfig,
chain.App.GetBaseApp(),
msgs,
chain.ChainID,
[]uint64{chain.SenderAccount.GetAccountNumber()},
[]uint64{chain.SenderAccount.GetSequence()},
true, chain.SenderPrivKey,
chain.SenderPrivKey,
)
if err != nil {
return nil, err
Expand Down
21 changes: 5 additions & 16 deletions testing/simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package simapp
import (
"encoding/json"
"math/rand"
"testing"
"time"

dbm "github.com/cometbft/cometbft-db"
Expand All @@ -22,7 +21,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v7/testing/mock"
)
Expand Down Expand Up @@ -209,8 +207,8 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba
//
// CONTRACT: BeginBlock must be called before this function.
func SignAndDeliver(
tb testing.TB, txCfg client.TxConfig, app *bam.BaseApp, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, expPass bool, priv ...cryptotypes.PrivKey,
txCfg client.TxConfig, app *bam.BaseApp, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
Expand All @@ -223,18 +221,9 @@ func SignAndDeliver(
accSeqs,
priv...,
)
require.NoError(tb, err)

// Simulate a sending a transaction
gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx)

if expPass {
require.NoError(tb, err)
require.NotNil(tb, res)
} else {
require.Error(tb, err)
require.Nil(tb, res)
if err != nil {
return sdk.GasInfo{}, nil, err
}

return gInfo, res, err
return app.SimDeliver(txCfg.TxEncoder(), tx)
}
Loading