Skip to content

Commit

Permalink
Adds IBC and BasicModuleManager (#1879)
Browse files Browse the repository at this point in the history
* Modify changelog.

* Add skeleton for ibc upgrade.

* Add logic for v7 upgrade.

* Add param migration.

* Add upgrades for ibcv8 and add logging. Updated umber tests as well

* Remove ModuleBasics, and migrate ibc params.

* Update changelog to account for ModuleBasics.

* Fix typo in changelog.

* Add signing options.

* Add finalize block to make test pass and help ensure encoder/decoders are working.

* Fix remaining test in app_test.go

* Remove replace comment for ibc-go dependency.

* Remove commented out code.

* Update interfaces for modules.
  • Loading branch information
Taztingo authored Apr 5, 2024
1 parent 9c3cda2 commit f011a89
Show file tree
Hide file tree
Showing 27 changed files with 242 additions and 161 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* Remove unsupported database types [#1760](https://github.com/provenance-io/provenance/issues/1760).
* Update ibc and migrate params [#1760](https://github.com/provenance-io/provenance/issues/1760).
* Replace ModuleBasics with BasicModuleManager [#1760](https://github.com/provenance-io/provenance/issues/1760).
* Remove handlers from provenance modules [#1760](https://github.com/provenance-io/provenance/issues/1760).
* Updated app.go to use RegisterStreamingServices on BaseApp [#1760](https://github.com/provenance-io/provenance/issues/1760).
* Bump the SDK to `v0.50.5-pio-1` (from an earlier ephemeral version) [#1897](https://github.com/provenance-io/provenance/pull/1897).
Expand Down
97 changes: 23 additions & 74 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,56 +191,6 @@ var (
// DefaultPowerReduction pio specific value for power reduction for TokensFromConsensusPower
DefaultPowerReduction = sdkmath.NewIntFromUint64(1_000_000_000)

// ModuleBasics defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
ModuleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
genutil.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(append(
[]govclient.ProposalHandler{},
paramsclient.ProposalHandler,
nameclient.RootNameProposalHandler,
),
),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
authzmodule.AppModuleBasic{},
groupmodule.AppModuleBasic{},
vesting.AppModuleBasic{},
// quarantinemodule.AppModuleBasic{}, // TODO[1760]: quarantine
// sanctionmodule.AppModuleBasic{}, // TODO[1760]: sanction
consensus.AppModuleBasic{},

ibc.AppModuleBasic{},
ibctransfer.AppModuleBasic{},
ica.AppModuleBasic{},
icq.AppModuleBasic{},
ibchooks.AppModuleBasic{},
ibcratelimitmodule.AppModuleBasic{},

marker.AppModuleBasic{},
attribute.AppModuleBasic{},
name.AppModuleBasic{},
metadata.AppModuleBasic{},
wasm.AppModuleBasic{},
msgfeesmodule.AppModuleBasic{},
rewardmodule.AppModuleBasic{},
triggermodule.AppModuleBasic{},
oraclemodule.AppModuleBasic{},
holdmodule.AppModuleBasic{},
exchangemodule.AppModuleBasic{},
)

// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
Expand Down Expand Up @@ -449,7 +399,7 @@ func New(
app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])

// set the BaseApp's parameter store
// TODO[1760]: Update upgrade handler

app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]),
Expand Down Expand Up @@ -838,25 +788,24 @@ func New(
icaModule,
)

// TODO[1760]: app-module: BasicModuleManager: Make sure that this setup has everything we need (it was just copied from the SDK).
// BasicModuleManager defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration and genesis verification.
// By default it is composed of all the module from the module manager.
// Additionally, app module basics can be overwritten by passing them as argument.
/*
app.BasicModuleManager = module.NewBasicManagerFromManager(
app.mm,
map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
govtypes.ModuleName: gov.NewAppModuleBasic(
[]govclient.ProposalHandler{
paramsclient.ProposalHandler,
},
app.BasicModuleManager = module.NewBasicManagerFromManager(
app.mm,
map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
govtypes.ModuleName: gov.NewAppModuleBasic(
append(
[]govclient.ProposalHandler{},
paramsclient.ProposalHandler,
nameclient.RootNameProposalHandler,
),
})
app.BasicModuleManager.RegisterLegacyAminoCodec(legacyAmino)
app.BasicModuleManager.RegisterInterfaces(interfaceRegistry)
*/
),
})
app.BasicModuleManager.RegisterLegacyAminoCodec(legacyAmino)
app.BasicModuleManager.RegisterInterfaces(interfaceRegistry)

// NOTE: upgrade module is required to be prioritized
app.mm.SetOrderPreBlockers(
Expand Down Expand Up @@ -1299,9 +1248,8 @@ func (app *App) InterfaceRegistry() types.InterfaceRegistry {
}

// DefaultGenesis returns a default genesis from the registered AppModuleBasic's.
func (a *App) DefaultGenesis() map[string]json.RawMessage {
// TODO[1760] This was changed to ModuleBasics, but it will be removed
return ModuleBasics.DefaultGenesis(a.appCodec)
func (app *App) DefaultGenesis() map[string]json.RawMessage {
return app.BasicModuleManager.DefaultGenesis(app.appCodec)
}

// GetKey returns the KVStoreKey for the provided store key.
Expand Down Expand Up @@ -1352,7 +1300,7 @@ func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig serverconfig.API
nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// Register grpc-gateway routes for all modules.
ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
app.BasicModuleManager.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// register swagger API from root so that other applications can override easily
if apiConfig.Swagger {
Expand Down Expand Up @@ -1420,11 +1368,12 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
// register the key tables for legacy param subspaces
keyTable := ibcclienttypes.ParamKeyTable()
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
paramsKeeper.Subspace(ibctransfertypes.ModuleName) // TODO[1760]: params: Migrate ibc-transfer params.
paramsKeeper.Subspace(ibcexported.ModuleName) // TODO[1760]: params: Migrate ibc-host params.
paramsKeeper.Subspace(icahosttypes.SubModuleName) // TODO[1760]: params: Migrate ica-host params.
paramsKeeper.Subspace(icqtypes.ModuleName) // TODO[1760]: params: Migrate icq params.
paramsKeeper.Subspace(ibchookstypes.ModuleName) // TODO[1760]: params: Migrate ibc-hooks params.
paramsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(keyTable)
paramsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())
paramsKeeper.Subspace(icahosttypes.SubModuleName).WithKeyTable(icahosttypes.ParamKeyTable())

paramsKeeper.Subspace(icqtypes.ModuleName) // TODO[1760]: params: Migrate icq params.
paramsKeeper.Subspace(ibchookstypes.ModuleName) // TODO[1760]: params: Migrate ibc-hooks params.

return paramsKeeper
}
Expand Down
15 changes: 13 additions & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {
)
}

// finalize block so we have CheckTx state set
_, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: 1,
})
require.NoError(t, err)

app.Commit()

// Making a new app object with the db, so that initchain hasn't been called
app2 := New(log.NewTestLogger(t), opts.DB, nil, true,
map[int64]bool{}, opts.HomePath, 0, opts.EncConfig, simtestutil.EmptyAppOptions{})
var err error
map[int64]bool{}, opts.HomePath, 0, MakeEncodingConfig(), simtestutil.EmptyAppOptions{})
require.NotPanics(t, func() {
_, err = app2.ExportAppStateAndValidators(false, nil, nil)
}, "exporting app state at current height")
Expand Down Expand Up @@ -144,6 +149,12 @@ func TestExportAppStateAndValidators(t *testing.T) {
allAccounts := app.AccountKeeper.GetAllAccounts(ctx)
logAccounts(t, allAccounts, "allAccounts")

// finalize block so we have CheckTx state set
_, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: 1,
})
require.NoError(t, err)

app.Commit()

// Get an export
Expand Down
4 changes: 0 additions & 4 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"github.com/cosmos/cosmos-sdk/std"

"github.com/provenance-io/provenance/app/params"
)

Expand All @@ -14,8 +13,5 @@ func MakeEncodingConfig() params.EncodingConfig {
encodingConfig := params.MakeTestEncodingConfig()
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)

ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
7 changes: 0 additions & 7 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package app

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
)

// The genesis state of the blockchain is represented here as a map of raw json
Expand All @@ -14,8 +12,3 @@ import (
// the ModuleBasicManager which populates json from each BasicModule
// object provided to it during init.
type GenesisState map[string]json.RawMessage

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
}
8 changes: 4 additions & 4 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func setup(t *testing.T, withGenesis bool, invCheckPeriod uint, chainID string)

app := New(loggerMaker(), db, nil, true, map[int64]bool{}, t.TempDir(), invCheckPeriod, encCdc, simtestutil.EmptyAppOptions{}, baseapp.SetChainID(chainID))
if withGenesis {
return app, NewDefaultGenesisState(encCdc.Marshaler)
return app, app.DefaultGenesis()
}
return app, GenesisState{}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func NewAppWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions)
}

app := New(options.Logger, options.DB, nil, true, options.SkipUpgradeHeights, options.HomePath, options.InvCheckPeriod, options.EncConfig, options.AppOpts)
genesisState := NewDefaultGenesisState(app.appCodec)
genesisState := app.DefaultGenesis()
genesisState = genesisStateWithValSet(t, app, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)

if !isCheckTx {
Expand Down Expand Up @@ -224,7 +224,7 @@ func genesisStateWithValSet(t *testing.T,
bondAmt := sdk.DefaultPowerReduction

for _, val := range valSet.Validators {
pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
pk, err := cryptocodec.FromCmtPubKeyInterface(val.PubKey)
require.NoError(t, err)
pkAny, err := codectypes.NewAnyWithValue(pk)
require.NoError(t, err)
Expand Down Expand Up @@ -353,7 +353,7 @@ func GenesisStateWithSingleValidator(t *testing.T, app *App) GenesisState {
},
}

genesisState := NewDefaultGenesisState(app.appCodec)
genesisState := app.DefaultGenesis()
genesisState = genesisStateWithValSet(t, app, genesisState, valSet, []authtypes.GenesisAccount{acc}, balances...)

return genesisState
Expand Down
75 changes: 72 additions & 3 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"

icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v8/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v8/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"

ibctmmigrations "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint/migrations"
"github.com/provenance-io/provenance/x/exchange"
"github.com/provenance-io/provenance/x/hold"
ibchookstypes "github.com/provenance-io/provenance/x/ibchooks/types"
Expand Down Expand Up @@ -168,11 +170,26 @@ var upgrades = map[string]appUpgrade{
Added: []string{crisistypes.ModuleName},
Handler: func(ctx sdk.Context, app *App, vm module.VersionMap) (module.VersionMap, error) {
var err error

if err := pruneIBCExpiredConsensusStates(ctx, app); err != nil {
return nil, err
}

err = migrateBaseappParams(ctx, app)
if err != nil {
return nil, err
}

vm, err = runModuleMigrations(ctx, app, vm)
if err != nil {
return nil, err
}

err = updateIBCClients(ctx, app)
if err != nil {
return nil, err
}

removeInactiveValidatorDelegations(ctx, app)

return vm, nil
Expand All @@ -182,11 +199,26 @@ var upgrades = map[string]appUpgrade{
Added: []string{crisistypes.ModuleName},
Handler: func(ctx sdk.Context, app *App, vm module.VersionMap) (module.VersionMap, error) {
var err error

if err := pruneIBCExpiredConsensusStates(ctx, app); err != nil {
return nil, err
}

err = migrateBaseappParams(ctx, app)
if err != nil {
return nil, err
}

vm, err = runModuleMigrations(ctx, app, vm)
if err != nil {
return nil, err
}

err = updateIBCClients(ctx, app)
if err != nil {
return nil, err
}

removeInactiveValidatorDelegations(ctx, app)

return vm, nil
Expand Down Expand Up @@ -447,6 +479,43 @@ func updateIbcMarkerDenomMetadata(ctx sdk.Context, app *App) {
ctx.Logger().Info("Done updating ibc marker denom metadata")
}

// pruneIBCExpiredConsensusStates prunes expired consensus states for IBC.
func pruneIBCExpiredConsensusStates(ctx sdk.Context, app *App) error {
ctx.Logger().Info("Pruning expired consensus states for IBC.")
_, err := ibctmmigrations.PruneExpiredConsensusStates(ctx, app.appCodec, app.IBCKeeper.ClientKeeper)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("unable to prune expired consensus states, error: %s.", err))
return err
}
ctx.Logger().Info("Done pruning expired consensus states for IBC.")
return nil
}

// updateIBCClients updates the allowed clients for IBC.
// TODO: Remove with the umber handlers.
func updateIBCClients(ctx sdk.Context, app *App) error {
ctx.Logger().Info("Updating IBC AllowedClients.")
params := app.IBCKeeper.ClientKeeper.GetParams(ctx)
params.AllowedClients = append(params.AllowedClients, exported.Localhost)
app.IBCKeeper.ClientKeeper.SetParams(ctx, params)
ctx.Logger().Info("Done updating IBC AllowedClients.")
return nil
}

// migrateBaseappParams migrates to new ConsensusParamsKeeper
// TODO: Remove with the umber handlers.
func migrateBaseappParams(ctx sdk.Context, app *App) error {
ctx.Logger().Info("Migrating legacy params.")
legacyBaseAppSubspace := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
err := baseapp.MigrateParams(ctx, legacyBaseAppSubspace, app.ConsensusParamsKeeper.ParamsStore)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("unable to migrate legacy params to ConsensusParamsKeeper, error: %s.", err))
return err
}
ctx.Logger().Info("Done migrating legacy params.")
return nil
}

// convertNavUnits iterates all the net asset values and updates their units if they are using usd.
// TODO: Remove with the tourmaline handlers.
func convertNavUnits(ctx sdk.Context, app *App) {
Expand Down
Loading

0 comments on commit f011a89

Please sign in to comment.