Skip to content

Commit

Permalink
feat!: interchain accounts (#3182)
Browse files Browse the repository at this point in the history
Closes #3062
Opens #3207
  • Loading branch information
rootulp authored Apr 5, 2024
1 parent 2661ffb commit 21f53b3
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 34 deletions.
66 changes: 50 additions & 16 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ import (
packetforward "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6/router"
packetforwardkeeper "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6/router/keeper"
packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6/router/types"

ica "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts"
icahost "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/host"
icahostkeeper "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/host/keeper"
icahosttypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/types"
)

var (
Expand Down Expand Up @@ -131,13 +137,14 @@ var (
upgrade.AppModuleBasic{},
minfee.AppModuleBasic{},
packetforward.AppModuleBasic{},
icaModule{},
)

// ModuleEncodingRegisters keeps track of all the module methods needed to
// register interfaces and specific type to encoding config
ModuleEncodingRegisters = extractRegisters(ModuleBasics)

// module account permissions
// maccPerms is short for module account permissions.
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
Expand All @@ -146,6 +153,7 @@ var (
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
icatypes.ModuleName: nil,
}
)

Expand Down Expand Up @@ -188,14 +196,15 @@ type App struct {
CrisisKeeper crisiskeeper.Keeper
UpgradeKeeper upgrade.Keeper
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCKeeper *ibckeeper.Keeper // IBCKeeper must be a pointer in the app, so we can SetRouter on it correctly
EvidenceKeeper evidencekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedIBCKeeper capabilitykeeper.ScopedKeeper // This keeper is public for test purposes
ScopedTransferKeeper capabilitykeeper.ScopedKeeper // This keeper is public for test purposes
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper // This keeper is public for test purposes

BlobKeeper blobkeeper.Keeper
BlobstreamKeeper blobstreamkeeper.Keeper
Expand Down Expand Up @@ -244,6 +253,7 @@ func New(
ibctransfertypes.StoreKey,
ibchost.StoreKey,
packetforwardtypes.StoreKey,
icahosttypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand All @@ -270,6 +280,7 @@ func New(
// grant capabilities for the ibc and ibc-transfer modules
app.ScopedIBCKeeper = app.CapabilityKeeper.ScopeToModule(ibchost.ModuleName)
app.ScopedTransferKeeper = app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
app.ScopedICAHostKeeper = app.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName)

// add keepers
app.AccountKeeper = authkeeper.NewAccountKeeper(
Expand Down Expand Up @@ -325,9 +336,25 @@ func New(

// ... other modules keepers

// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, app.UpgradeKeeper, app.ScopedIBCKeeper,
appCodec,
keys[ibchost.StoreKey],
app.GetSubspace(ibchost.ModuleName),
app.StakingKeeper,
app.UpgradeKeeper,
app.ScopedIBCKeeper,
)

app.ICAHostKeeper = icahostkeeper.NewKeeper(
appCodec,
keys[icahosttypes.StoreKey],
app.GetSubspace(icahosttypes.SubModuleName),
app.IBCKeeper.ChannelKeeper, // ICS4Wrapper
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
app.ScopedICAHostKeeper,
app.MsgServiceRouter(),
)

paramBlockList := paramfilter.NewParamBlockList(app.BlockedParams()...)
Expand Down Expand Up @@ -391,10 +418,9 @@ func New(
)

app.PacketForwardKeeper.SetTransferKeeper(app.TransferKeeper)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
ibcRouter := ibcporttypes.NewRouter() // Create static IBC router
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack) // Add transfer route
ibcRouter.AddRoute(icahosttypes.SubModuleName, icahost.NewIBCModule(app.ICAHostKeeper)) // Add ICA route
app.IBCKeeper.SetRouter(ibcRouter)

/**** Module Options ****/
Expand Down Expand Up @@ -495,6 +521,10 @@ func New(
Module: packetforward.NewAppModule(app.PacketForwardKeeper),
FromVersion: v2, ToVersion: v2,
},
{
Module: ica.NewAppModule(nil, &app.ICAHostKeeper),
FromVersion: v2, ToVersion: v2,
},
})
if err != nil {
panic(err)
Expand Down Expand Up @@ -526,6 +556,7 @@ func New(
vestingtypes.ModuleName,
upgradetypes.ModuleName,
minfee.ModuleName,
icatypes.ModuleName,
packetforwardtypes.ModuleName,
)

Expand All @@ -552,6 +583,7 @@ func New(
upgradetypes.ModuleName,
minfee.ModuleName,
packetforwardtypes.ModuleName,
icatypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -584,6 +616,7 @@ func New(
authz.ModuleName,
upgradetypes.ModuleName,
packetforwardtypes.ModuleName,
icatypes.ModuleName,
)

app.QueryRouter().AddRoute(proof.TxInclusionQueryPath, proof.QueryTxInclusionProof)
Expand Down Expand Up @@ -746,15 +779,15 @@ func (app *App) LegacyAmino() *codec.LegacyAmino {
return app.legacyAmino
}

// AppCodec returns Gaia's app codec.
// AppCodec returns the app's appCodec.
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
// for modules to register their own custom testing types.
func (app *App) AppCodec() codec.Codec {
return app.appCodec
}

// InterfaceRegistry returns Gaia's InterfaceRegistry
// InterfaceRegistry returns the app's InterfaceRegistry
func (app *App) InterfaceRegistry() types.InterfaceRegistry {
return app.interfaceRegistry
}
Expand Down Expand Up @@ -818,9 +851,9 @@ func (app *App) RegisterNodeService(clientCtx client.Context) {
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter())
}

// BlockedParams are params that require a hardfork to change, and cannot be changed via
// governance.
func (*App) BlockedParams() [][2]string {
// BlockedParams returns the params that require a hardfork to change, and
// cannot be changed via governance.
func (app *App) BlockedParams() [][2]string {
return [][2]string{
// bank.SendEnabled
{banktypes.ModuleName, string(banktypes.KeySendEnabled)},
Expand All @@ -847,6 +880,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(blobtypes.ModuleName)
paramsKeeper.Subspace(blobstreamtypes.ModuleName)
paramsKeeper.Subspace(minfee.ModuleName)
Expand Down
45 changes: 45 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package app_test

import (
"testing"

"github.com/celestiaorg/celestia-app/v2/app"
"github.com/celestiaorg/celestia-app/v2/app/encoding"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/log"
tmdb "github.com/tendermint/tm-db"
)

func TestNew(t *testing.T) {
logger := log.NewNopLogger()
db := tmdb.NewMemDB()
traceStore := &NoopWriter{}
loadLatest := true
invCheckPeriod := uint(1)
encodingConfig := encoding.MakeConfig(app.ModuleEncodingRegisters...)
upgradeHeight := int64(0)
appOptions := NoopAppOptions{}

got := app.New(logger, db, traceStore, loadLatest, invCheckPeriod, encodingConfig, upgradeHeight, appOptions)

t.Run("initializes ICAHostKeeper", func(t *testing.T) {
assert.NotNil(t, got.ICAHostKeeper)
})
t.Run("initializes ScopedICAHostKeeper", func(t *testing.T) {
assert.NotNil(t, got.ScopedICAHostKeeper)
})
}

// NoopWriter is a no-op implementation of a writer.
type NoopWriter struct{}

func (nw *NoopWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

// NoopAppOptions is a no-op implementation of servertypes.AppOptions.
type NoopAppOptions struct{}

func (nao NoopAppOptions) Get(string) interface{} {
return nil
}
17 changes: 17 additions & 0 deletions app/default_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ica "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts"
icagenesistypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/genesis/types"
ibc "github.com/cosmos/ibc-go/v6/modules/core"
ibcclientclient "github.com/cosmos/ibc-go/v6/modules/core/02-client/client"
ibctypes "github.com/cosmos/ibc-go/v6/modules/core/types"
Expand Down Expand Up @@ -149,6 +151,21 @@ func (ibcModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(gs)
}

// icaModule defines a custom wrapper around the ica module to provide custom
// default genesis state.
type icaModule struct {
ica.AppModuleBasic
}

// DefaultGenesis returns custom ica module genesis state.
func (icaModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
gs := icagenesistypes.DefaultGenesis()
gs.HostGenesisState.Params.AllowMessages = icaAllowMessages()
gs.HostGenesisState.Params.HostEnabled = true
gs.ControllerGenesisState.Params.ControllerEnabled = false
return cdc.MustMarshalJSON(gs)
}

type mintModule struct {
mint.AppModuleBasic
}
Expand Down
13 changes: 13 additions & 0 deletions app/default_overrides_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
icagenesistypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/genesis/types"
"github.com/stretchr/testify/assert"
tmcfg "github.com/tendermint/tendermint/config"
)
Expand Down Expand Up @@ -89,3 +90,15 @@ func TestDefaultConsensusConfig(t *testing.T) {
assert.Equal(t, want, *got.Mempool)
})
}

func Test_icaDefaultGenesis(t *testing.T) {
encCfg := encoding.MakeConfig(ModuleEncodingRegisters...)
ica := icaModule{}
raw := ica.DefaultGenesis(encCfg.Codec)
got := icagenesistypes.GenesisState{}
encCfg.Codec.MustUnmarshalJSON(raw, &got)

assert.Equal(t, got.HostGenesisState.Params.AllowMessages, icaAllowMessages())
assert.True(t, got.HostGenesisState.Params.HostEnabled)
assert.False(t, got.ControllerGenesisState.Params.ControllerEnabled)
}
18 changes: 18 additions & 0 deletions app/ica_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package app

func icaAllowMessages() []string {
return []string{
"/ibc.applications.transfer.v1.MsgTransfer",
"/cosmos.bank.v1beta1.MsgSend",
"/cosmos.staking.v1beta1.MsgDelegate",
"/cosmos.staking.v1beta1.MsgBeginRedelegate",
"/cosmos.staking.v1beta1.MsgUndelegate",
"/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation",
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.distribution.v1beta1.MsgFundCommunityPool",
"/cosmos.gov.v1.MsgVote",
"/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance",
}
}
26 changes: 26 additions & 0 deletions app/ica_host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_icaAllowMessages(t *testing.T) {
got := icaAllowMessages()
want := []string{
"/ibc.applications.transfer.v1.MsgTransfer",
"/cosmos.bank.v1beta1.MsgSend",
"/cosmos.staking.v1beta1.MsgDelegate",
"/cosmos.staking.v1beta1.MsgBeginRedelegate",
"/cosmos.staking.v1beta1.MsgUndelegate",
"/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation",
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.distribution.v1beta1.MsgFundCommunityPool",
"/cosmos.gov.v1.MsgVote",
"/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance",
}
assert.Equal(t, want, got)
}
Loading

0 comments on commit 21f53b3

Please sign in to comment.