Skip to content

Commit

Permalink
Adding ICA Host module (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed May 25, 2022
1 parent 78ed877 commit 09f3184
Show file tree
Hide file tree
Showing 5 changed files with 150 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/v7/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()
})
}
}
58 changes: 56 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/v7/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/v7/app/keepers"
)

Expand All @@ -13,8 +25,50 @@ 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)
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 09f3184

Please sign in to comment.