Skip to content

Commit

Permalink
Adding ICA Host module (backport #1564) (#1576)
Browse files Browse the repository at this point in the history
* Adding ICA Host module (#1564)

(cherry picked from commit 09f3184)

# Conflicts:
#	app/upgrades/v9/constants.go
#	app/upgrades/v9/upgrades.go

* fix conflicts

Co-authored-by: Dev Ojha <[email protected]>
Co-authored-by: Roman <[email protected]>
  • Loading branch information
3 people authored May 25, 2022
1 parent d3c094e commit 3b89365
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 4 deletions.
26 changes: 25 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

icahost "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host"
icahostkeeper "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/keeper"
icahosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
ibctransferkeeper "github.com/cosmos/ibc-go/v3/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
ibcclient "github.com/cosmos/ibc-go/v3/modules/core/02-client"
Expand Down Expand Up @@ -82,6 +86,7 @@ type AppKeepers struct {

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

Expand All @@ -93,6 +98,7 @@ type AppKeepers struct {
DistrKeeper *distrkeeper.Keeper
SlashingKeeper *slashingkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
ICAHostKeeper *icahostkeeper.Keeper
TransferKeeper *ibctransferkeeper.Keeper
Bech32IBCKeeper *bech32ibckeeper.Keeper
Bech32ICS20Keeper *bech32ics20keeper.Keeper
Expand All @@ -108,6 +114,7 @@ type AppKeepers struct {
GovKeeper *govkeeper.Keeper
WasmKeeper *wasm.Keeper
TokenFactoryKeeper *tokenfactorykeeper.Keeper
// IBC modules
// transfer module
TransferModule transfer.AppModule

Expand Down Expand Up @@ -206,9 +213,22 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.TransferModule = transfer.NewAppModule(*appKeepers.TransferKeeper)
transferIBCModule := transfer.NewIBCModule(*appKeepers.TransferKeeper)

icaHostKeeper := icahostkeeper.NewKeeper(
appCodec, appKeepers.keys[icahosttypes.StoreKey],
appKeepers.GetSubspace(icahosttypes.SubModuleName),
appKeepers.IBCKeeper.ChannelKeeper,
&appKeepers.IBCKeeper.PortKeeper,
appKeepers.AccountKeeper,
appKeepers.ScopedICAHostKeeper,
bApp.MsgServiceRouter(),
)
appKeepers.ICAHostKeeper = &icaHostKeeper

icaHostIBCModule := icahost.NewIBCModule(*appKeepers.ICAHostKeeper)
// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
ibcRouter.AddRoute(icahosttypes.SubModuleName, icaHostIBCModule).
AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
// Note: the sealing is done after creating wasmd and wiring that up

appKeepers.Bech32IBCKeeper = bech32ibckeeper.NewKeeper(
Expand Down Expand Up @@ -389,6 +409,7 @@ func (appKeepers *AppKeepers) InitSpecialKeepers(
// add capability keeper and ScopeToModule for ibc module
appKeepers.CapabilityKeeper = capabilitykeeper.NewKeeper(appCodec, appKeepers.keys[capabilitytypes.StoreKey], appKeepers.memKeys[capabilitytypes.MemStoreKey])
appKeepers.ScopedIBCKeeper = appKeepers.CapabilityKeeper.ScopeToModule(ibchost.ModuleName)
appKeepers.ScopedICAHostKeeper = appKeepers.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName)
appKeepers.ScopedTransferKeeper = appKeepers.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
appKeepers.ScopedWasmKeeper = appKeepers.CapabilityKeeper.ScopeToModule(wasm.ModuleName)
appKeepers.CapabilityKeeper.Seal()
Expand Down Expand Up @@ -424,6 +445,7 @@ func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legac
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(incentivestypes.ModuleName)
paramsKeeper.Subspace(poolincentivestypes.ModuleName)
paramsKeeper.Subspace(superfluidtypes.ModuleName)
Expand Down Expand Up @@ -492,6 +514,7 @@ func (appKeepers *AppKeepers) SetupHooks() {
)
}

// TODO: We need to automate this, by bundling with a module struct...
func KVStoreKeys() []string {
return []string{
authtypes.StoreKey,
Expand All @@ -503,6 +526,7 @@ func KVStoreKeys() []string {
govtypes.StoreKey,
paramstypes.StoreKey,
ibchost.StoreKey,
icahosttypes.StoreKey,
upgradetypes.StoreKey,
evidencetypes.StoreKey,
ibctransfertypes.StoreKey,
Expand Down
8 changes: 8 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v3/modules/core"
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"

ica "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts"
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
"github.com/osmosis-labs/bech32-ibc/x/bech32ibc"
bech32ibctypes "github.com/osmosis-labs/bech32-ibc/x/bech32ibc/types"
"github.com/osmosis-labs/bech32-ibc/x/bech32ics20"
Expand Down Expand Up @@ -64,9 +67,11 @@ import (
)

// moduleAccountPermissions defines module account permissions
// TODO: Having to input nil's here is unacceptable, we need a way to automatically derive this.
var moduleAccountPermissions = map[string][]string{
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
icatypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter, authtypes.Burner},
minttypes.DeveloperVestingModuleAcctName: nil,
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
Expand Down Expand Up @@ -114,6 +119,7 @@ func appModules(
evidence.NewAppModule(*app.EvidenceKeeper),
authzmodule.NewAppModule(appCodec, *app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
ibc.NewAppModule(app.IBCKeeper),
ica.NewAppModule(nil, app.ICAHostKeeper),
params.NewAppModule(*app.ParamsKeeper),
app.TransferModule,
gamm.NewAppModule(appCodec, *app.GAMMKeeper, app.AccountKeeper, app.BankKeeper),
Expand Down Expand Up @@ -154,6 +160,7 @@ func orderBeginBlockers() []string {
stakingtypes.ModuleName,
ibchost.ModuleName,
ibctransfertypes.ModuleName,
icatypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
govtypes.ModuleName,
Expand Down Expand Up @@ -199,6 +206,7 @@ var modulesOrderInitGenesis = []string{
minttypes.ModuleName,
crisistypes.ModuleName,
ibchost.ModuleName,
icatypes.ModuleName,
gammtypes.ModuleName,
txfeestypes.ModuleName,
genutiltypes.ModuleName,
Expand Down
4 changes: 3 additions & 1 deletion app/upgrades/v9/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

store "github.com/cosmos/cosmos-sdk/store/types"

icahosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"

tokenfactorytypes "github.com/osmosis-labs/osmosis/v9/x/tokenfactory/types"
)

Expand All @@ -19,7 +21,7 @@ var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{tokenfactorytypes.ModuleName},
Added: []string{tokenfactorytypes.ModuleName, icahosttypes.StoreKey},
Deleted: []string{ClaimsModuleName},
},
}
58 changes: 58 additions & 0 deletions app/upgrades/v9/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v9_test

import (
"fmt"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
abci "github.com/tendermint/tendermint/abci/types"
)

const dummyUpgradeHeight = 5

func (suite *UpgradeTestSuite) TestUpgradePayments() {
testCases := []struct {
msg string
pre_update func()
update func()
post_update func()
expPass bool
}{
{
"Test that upgrade does not panic",
func() {
// Create pool 1
suite.PrepareBalancerPool()
},
func() {
// run upgrade
// TODO: Refactor this all into a helper fn
suite.Ctx = suite.Ctx.WithBlockHeight(dummyUpgradeHeight - 1)
plan := upgradetypes.Plan{Name: "v9", Height: dummyUpgradeHeight}
err := suite.App.UpgradeKeeper.ScheduleUpgrade(suite.Ctx, plan)
suite.Require().NoError(err)
plan, exists := suite.App.UpgradeKeeper.GetUpgradePlan(suite.Ctx)
suite.Require().True(exists)

suite.Ctx = suite.Ctx.WithBlockHeight(dummyUpgradeHeight)
suite.Require().NotPanics(func() {
beginBlockRequest := abci.RequestBeginBlock{}
suite.App.BeginBlocker(suite.Ctx, beginBlockRequest)
})
},
func() {

},
true,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

tc.pre_update()
tc.update()
tc.post_update()
})
}
}
59 changes: 57 additions & 2 deletions app/upgrades/v9/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ package v9
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

gammtypes "github.com/osmosis-labs/osmosis/v9/x/gamm/types"

ica "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts"
icacontrollertypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"

"github.com/osmosis-labs/osmosis/v9/app/keepers"
)

Expand All @@ -15,13 +27,56 @@ func CreateUpgradeHandler(
configurator module.Configurator,
keepers *keepers.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ExecuteProp214(ctx, keepers.GAMMKeeper)

// We set the app version to pre-upgrade because it will be incremented by one
// after the upgrade is applied by the handler.
if err := keepers.UpgradeKeeper.SetAppVersion(ctx, preUpgradeAppVersion); err != nil {
return nil, err
}
return mm.RunMigrations(ctx, configurator, vm)

// Add Interchain Accounts host module
// set the ICS27 consensus version so InitGenesis is not run
fromVM[icatypes.ModuleName] = mm.Modules[icatypes.ModuleName].ConsensusVersion()

// create ICS27 Controller submodule params, controller module not enabled.
controllerParams := icacontrollertypes.Params{}

// create ICS27 Host submodule params
hostParams := icahosttypes.Params{
HostEnabled: true,
AllowMessages: []string{
sdk.MsgTypeURL(&banktypes.MsgSend{}),
sdk.MsgTypeURL(&stakingtypes.MsgDelegate{}),
sdk.MsgTypeURL(&stakingtypes.MsgBeginRedelegate{}),
sdk.MsgTypeURL(&stakingtypes.MsgCreateValidator{}),
sdk.MsgTypeURL(&stakingtypes.MsgEditValidator{}),
sdk.MsgTypeURL(&distrtypes.MsgWithdrawDelegatorReward{}),
sdk.MsgTypeURL(&distrtypes.MsgSetWithdrawAddress{}),
sdk.MsgTypeURL(&distrtypes.MsgWithdrawValidatorCommission{}),
sdk.MsgTypeURL(&distrtypes.MsgFundCommunityPool{}),
sdk.MsgTypeURL(&govtypes.MsgVote{}),
sdk.MsgTypeURL(&authz.MsgExec{}),
sdk.MsgTypeURL(&authz.MsgGrant{}),
sdk.MsgTypeURL(&authz.MsgRevoke{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinPool{}),
sdk.MsgTypeURL(&gammtypes.MsgExitPool{}),
sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountIn{}),
sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinSwapExternAmountIn{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinSwapShareAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgExitSwapExternAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgExitSwapShareAmountIn{}),
},
}

// initialize ICS27 module
icamodule, correctTypecast := mm.Modules[icatypes.ModuleName].(ica.AppModule)
if !correctTypecast {
panic("mm.Modules[icatypes.ModuleName] is not of type ica.AppModule")
}
icamodule.InitModule(ctx, controllerParams, hostParams)
return mm.RunMigrations(ctx, configurator, fromVM)
}
}

0 comments on commit 3b89365

Please sign in to comment.