Skip to content

Commit

Permalink
[CORE-848] Add ICA Host Submodule (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding authored Dec 22, 2023
1 parent 24adb6c commit 0459917
Show file tree
Hide file tree
Showing 23 changed files with 250 additions and 21 deletions.
5 changes: 3 additions & 2 deletions protocol/app/ante/msg_type_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ante_test

import (
errorsmod "cosmossdk.io/errors"
"fmt"
"golang.org/x/exp/slices"
"testing"

errorsmod "cosmossdk.io/errors"
"golang.org/x/exp/slices"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
51 changes: 39 additions & 12 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ import (
vestmoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/vest/types"

// IBC
ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
icahost "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host"
icahostkeeper "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/keeper"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
"github.com/cosmos/ibc-go/v7/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
Expand Down Expand Up @@ -238,16 +243,13 @@ type App struct {
ParamsKeeper paramskeeper.Keeper
// IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCKeeper *ibckeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
RatelimitKeeper ratelimitmodulekeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
ConsensusParamsKeeper consensusparamkeeper.Keeper

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

PricesKeeper pricesmodulekeeper.Keeper

AssetsKeeper assetsmodulekeeper.Keeper
Expand Down Expand Up @@ -348,6 +350,7 @@ func New(
govtypes.StoreKey, paramstypes.StoreKey, consensusparamtypes.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
ibcexported.StoreKey, ibctransfertypes.StoreKey,
ratelimitmoduletypes.StoreKey,
icahosttypes.StoreKey,
evidencetypes.StoreKey,
capabilitytypes.StoreKey,
pricesmoduletypes.StoreKey,
Expand Down Expand Up @@ -416,10 +419,6 @@ func New(
memKeys[capabilitytypes.MemStoreKey],
)

// grant capabilities for the ibc and ibc-transfer modules
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibcexported.ModuleName)
scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)

// add keepers
app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec,
Expand Down Expand Up @@ -519,6 +518,14 @@ func New(
// Set legacy router for backwards compatibility with gov v1beta1
govKeeper.SetLegacyRouter(govRouter)

// grant capabilities for the ibc, ibc-transfer and ICAHostKeeper modules

scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibcexported.ModuleName)
scopedIBCTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
scopedICAHostKeeper := app.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName)

app.CapabilityKeeper.Seal()

// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
Expand All @@ -529,11 +536,24 @@ func New(
scopedIBCKeeper,
)

// Create ICA Host Keeper
app.ICAHostKeeper = icahostkeeper.NewKeeper(
appCodec,
keys[icahosttypes.StoreKey], // key
app.getSubspace(icahosttypes.SubModuleName), // paramSpace
app.IBCKeeper.ChannelKeeper, // ics4Wrapper, may be replaced with middleware such as ics29 fee
app.IBCKeeper.ChannelKeeper, // channelKeeper
&app.IBCKeeper.PortKeeper, // portKeeper
app.AccountKeeper, // accountKeeper
scopedICAHostKeeper, // scopedKeeper
app.MsgServiceRouter(), // msgRouter
)

// Create Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.getSubspace(ibctransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
app.AccountKeeper, app.BankKeeper, scopedIBCTransferKeeper,
)
transferModule := transfer.NewAppModule(app.TransferKeeper)
transferIBCModule := transfer.NewIBCModule(app.TransferKeeper)
Expand All @@ -550,9 +570,13 @@ func New(

// TODO(CORE-834): Add ratelimitKeeper to the IBC transfer stack.

icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)
// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
// Ordering of `AddRoute` does not matter.
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
ibcRouter.AddRoute(icahosttypes.SubModuleName, icaHostIBCModule)

app.IBCKeeper.SetRouter(ibcRouter)

// create evidence keeper with router
Expand Down Expand Up @@ -956,6 +980,7 @@ func New(
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
ibc.NewAppModule(app.IBCKeeper),
ica.NewAppModule(nil, &app.ICAHostKeeper),
params.NewAppModule(app.ParamsKeeper),
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
transferModule,
Expand Down Expand Up @@ -999,6 +1024,7 @@ func New(
feegrant.ModuleName,
paramstypes.ModuleName,
consensusparamtypes.ModuleName,
icatypes.ModuleName,
pricesmoduletypes.ModuleName,
assetsmoduletypes.ModuleName,
bridgemoduletypes.ModuleName,
Expand Down Expand Up @@ -1035,6 +1061,7 @@ func New(
ibctransfertypes.ModuleName,
ratelimitmoduletypes.ModuleName,
consensusparamtypes.ModuleName,
icatypes.ModuleName,
pricesmoduletypes.ModuleName,
assetsmoduletypes.ModuleName,
bridgemoduletypes.ModuleName,
Expand Down Expand Up @@ -1075,6 +1102,7 @@ func New(
ratelimitmoduletypes.ModuleName,
feegrant.ModuleName,
consensusparamtypes.ModuleName,
icatypes.ModuleName,
pricesmoduletypes.ModuleName,
assetsmoduletypes.ModuleName,
blocktimemoduletypes.ModuleName,
Expand Down Expand Up @@ -1111,6 +1139,7 @@ func New(
ratelimitmoduletypes.ModuleName,
feegrant.ModuleName,
consensusparamtypes.ModuleName,
icatypes.ModuleName,
pricesmoduletypes.ModuleName,
assetsmoduletypes.ModuleName,
blocktimemoduletypes.ModuleName,
Expand Down Expand Up @@ -1232,9 +1261,6 @@ func New(
}
app.initializeRateLimiters()

app.ScopedIBCKeeper = scopedIBCKeeper
app.ScopedTransferKeeper = scopedTransferKeeper

// Report out app version and git commit. This will be run when validators restart.
version := version.NewInfo()
app.Logger().Info(
Expand Down Expand Up @@ -1524,6 +1550,7 @@ func initParamsKeeper(
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable()) //nolint:staticcheck
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(ibcexported.ModuleName)

return paramsKeeper
Expand Down
7 changes: 5 additions & 2 deletions protocol/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package app_test

import (
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"gopkg.in/typ.v4/slices"
"reflect"
"strings"
"testing"
"time"

"github.com/dydxprotocol/v4-chain/protocol/mocks"
"gopkg.in/typ.v4/slices"

delaymsgmodule "github.com/dydxprotocol/v4-chain/protocol/x/delaymsg"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
"github.com/cosmos/ibc-go/v7/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v7/modules/core"
ibcclientclient "github.com/cosmos/ibc-go/v7/modules/core/02-client/client"
Expand Down Expand Up @@ -199,6 +201,7 @@ func TestModuleBasics(t *testing.T) {
feegrantmodule.AppModuleBasic{},
ibc.AppModuleBasic{},
ibctm.AppModuleBasic{},
ica.AppModuleBasic{},
upgrade.AppModuleBasic{},
transfer.AppModuleBasic{},
consensus.AppModuleBasic{},
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/basic_manager/basic_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
subaccountsmodule "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts"
vestmodule "github.com/dydxprotocol/v4-chain/protocol/x/vest"

ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
"github.com/cosmos/ibc-go/v7/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v7/modules/core"
ibcclientclient "github.com/cosmos/ibc-go/v7/modules/core/02-client/client"
Expand Down Expand Up @@ -69,6 +70,7 @@ var (
feegrantmodule.AppModuleBasic{},
ibc.AppModuleBasic{},
ibctm.AppModuleBasic{},
ica.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
Expand Down
5 changes: 4 additions & 1 deletion protocol/app/module_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
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"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"

"github.com/dydxprotocol/v4-chain/protocol/app/config"
Expand Down Expand Up @@ -33,6 +34,7 @@ var (
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
icatypes.ModuleName: nil,
// -------- dYdX custom module accounts --------
// bridge module account mints tokens for bridged funds.
bridgemoduletypes.ModuleName: {authtypes.Minter},
Expand All @@ -53,14 +55,15 @@ var (
delaymsgtypes.ModuleName: nil,
}
// Blocked module accounts which cannot receive external funds.
// By default, all native SDK module accounts are blocked. This prevents
// By default, all non-custom modules (except for gov) are blocked. This prevents
// unexpected violation of invariants (for example, https://github.com/cosmos/cosmos-sdk/issues/4795)
blockedModuleAccounts = map[string]bool{
authtypes.FeeCollectorName: true,
distrtypes.ModuleName: true,
stakingtypes.BondedPoolName: true,
stakingtypes.NotBondedPoolName: true,
ibctransfertypes.ModuleName: true,
icatypes.ModuleName: true,
}
)

Expand Down
5 changes: 5 additions & 0 deletions protocol/app/module_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
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"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/dydxprotocol/v4-chain/protocol/app"
bridgemoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/bridge/types"
Expand Down Expand Up @@ -34,6 +35,7 @@ func TestModuleAccountsToAddresses(t *testing.T) {
vestmoduletypes.CommunityTreasuryAccountName: "dydx15ztc7xy42tn2ukkc0qjthkucw9ac63pgp70urn",
vestmoduletypes.CommunityVesterAccountName: "dydx1wxje320an3karyc6mjw4zghs300dmrjkwn7xtk",
delaymsgtypes.ModuleName: "dydx1mkkvp26dngu6n8rmalaxyp3gwkjuzztq5zx6tr",
icatypes.ModuleName: "dydx1vlthgax23ca9syk7xgaz347xmf4nunefw3cnv8",
}

require.True(t, len(expectedModuleAccToAddresses) == len(app.GetMaccPerms()))
Expand All @@ -50,6 +52,7 @@ func TestBlockedAddresses(t *testing.T) {
"dydx1tygms3xhhs3yv487phx3dw4a95jn7t7lgzm605": true,
"dydx1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3uz8teq": true,
"dydx1yl6hdjhmkf37639730gffanpzndzdpmh8xcdh5": true,
"dydx1vlthgax23ca9syk7xgaz347xmf4nunefw3cnv8": true,
}
require.Equal(t, expectedBlockedAddresses, app.BlockedAddresses())
}
Expand All @@ -66,6 +69,7 @@ func TestMaccPerms(t *testing.T) {
"not_bonded_tokens_pool": {"burner", "staking"},
"subaccounts": nil,
"transfer": {"minter", "burner"},
"interchainaccounts": nil,
"rewards_treasury": nil,
"rewards_vester": nil,
"community_treasury": nil,
Expand All @@ -84,6 +88,7 @@ func TestModuleAccountAddrs(t *testing.T) {
"dydx1tygms3xhhs3yv487phx3dw4a95jn7t7lgzm605": true, // x/staking.notBondedPool
"dydx10d07y265gmmuvt4z0w9aw880jnsr700jnmapky": true, // x/ gov
"dydx1yl6hdjhmkf37639730gffanpzndzdpmh8xcdh5": true, // ibc transfer
"dydx1vlthgax23ca9syk7xgaz347xmf4nunefw3cnv8": true, // interchainaccounts
"dydx1v88c3xv9xyv3eetdx0tvcmq7ung3dywp5upwc6": true, // x/subaccount
"dydx1c7ptc87hkd54e3r7zjy92q29xkq7t79w64slrq": true, // x/clob.insuranceFund
"dydx16wrau2x4tsg033xfrrdpae6kxfn9kyuerr5jjp": true, // x/rewards.treasury
Expand Down
9 changes: 9 additions & 0 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ var (
"/ibc.lightclients.tendermint.v1.ConsensusState": {},
"/ibc.lightclients.tendermint.v1.Header": {},
"/ibc.lightclients.tendermint.v1.Misbehaviour": {},

// ica messages
// Note: the `interchain_accounts.controller` messages are not actually used by the app,
// since ICA Controller Keeper is initialized as nil.
// However, since the ica.AppModuleBasic{} needs to be passed to basic_mananger as a whole, these messages
// registered in the interface registry.
"/ibc.applications.interchain_accounts.v1.InterchainAccount": {},
"/ibc.applications.interchain_accounts.controller.v1.MsgSendTx": {},
"/ibc.applications.interchain_accounts.controller.v1.MsgRegisterInterchainAccount": {},
}

// DisallowMsgs are messages that cannot be externally submitted.
Expand Down
3 changes: 3 additions & 0 deletions protocol/app/msgs/normal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,8 @@ var (
"/ibc.lightclients.tendermint.v1.ConsensusState": nil,
"/ibc.lightclients.tendermint.v1.Header": nil,
"/ibc.lightclients.tendermint.v1.Misbehaviour": nil,

// ica
"/ibc.applications.interchain_accounts.v1.InterchainAccount": nil,
}
)
3 changes: 3 additions & 0 deletions protocol/app/msgs/normal_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func TestNormalMsgs_Key(t *testing.T) {
"/dydxprotocol.sending.MsgWithdrawFromSubaccount",
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse",

// ibc application module: ICA
"/ibc.applications.interchain_accounts.v1.InterchainAccount",

// ibc.applications
"/ibc.applications.transfer.v1.MsgTransfer",
"/ibc.applications.transfer.v1.MsgTransferResponse",
Expand Down
7 changes: 7 additions & 0 deletions protocol/app/msgs/unsupported_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package msgs
import (
sdk "github.com/cosmos/cosmos-sdk/types"
govbeta "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
)

var (
Expand All @@ -11,5 +13,10 @@ var (
// gov
"/cosmos.gov.v1beta1.MsgSubmitProposal": &govbeta.MsgSubmitProposal{},
"/cosmos.gov.v1beta1.MsgSubmitProposalResponse": nil,

// ICA Controller messages - these are not used since ICA Controller is disabled.
"/ibc.applications.interchain_accounts.controller.v1.MsgSendTx": &icacontrollertypes.MsgSendTx{},
"/ibc.applications.interchain_accounts.controller.v1.MsgRegisterInterchainAccount": &icacontrollertypes.
MsgRegisterInterchainAccount{},
}
)
4 changes: 4 additions & 0 deletions protocol/app/msgs/unsupported_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func TestUnsupportedMsgSamples_Key(t *testing.T) {
expectedMsgs := []string{
"/cosmos.gov.v1beta1.MsgSubmitProposal",
"/cosmos.gov.v1beta1.MsgSubmitProposalResponse",

// ICA Controller messages
"/ibc.applications.interchain_accounts.controller.v1.MsgRegisterInterchainAccount",
"/ibc.applications.interchain_accounts.controller.v1.MsgSendTx",
}

require.Equal(t, expectedMsgs, lib.GetSortedKeys[sort.StringSlice](msgs.UnsupportedMsgSamples))
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
exportedtypes "github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/dydxprotocol/v4-chain/protocol/app"
Expand Down Expand Up @@ -114,6 +115,7 @@ var genesisModuleOrder = []string{
exportedtypes.ModuleName,
evidencetypes.ModuleName,
ibctransfertypes.ModuleName,
icatypes.ModuleName,
pricestypes.ModuleName,
assetstypes.ModuleName,
perpetualstypes.ModuleName,
Expand Down
Loading

0 comments on commit 0459917

Please sign in to comment.