feat: streamline app initialization and dependency management

This commit is contained in:
Prad N
2025-03-19 14:47:22 -04:00
parent 00b23353c9
commit 8b4d51ee30
7 changed files with 50 additions and 99 deletions
+13 -17
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"maps"
"os"
"path/filepath"
"sort"
@@ -60,7 +61,6 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@@ -127,7 +127,7 @@ import (
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
@@ -373,8 +373,8 @@ func NewChainApp(
memKeys := storetypes.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
// register streaming services
if err := bApp.RegisterStreamingServices(appOpts, keys); err != nil {
panic(err)
if errb := bApp.RegisterStreamingServices(appOpts, keys); errb != nil {
panic(errb)
}
app := &SonrApp{
@@ -440,12 +440,12 @@ func NewChainApp(
)
// enable sign mode textual by overwriting the default tx config (after setting the bank keeper)
enabledSignModes := append(tx.DefaultSignModes, signingtype.SignMode_SIGN_MODE_TEXTUAL)
txConfigOpts := tx.ConfigOptions{
enabledSignModes := append(authtx.DefaultSignModes, signingtype.SignMode_SIGN_MODE_TEXTUAL)
txConfigOpts := authtx.ConfigOptions{
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewBankKeeperCoinMetadataQueryFn(app.BankKeeper),
}
txConfig, err = tx.NewTxConfigWithOptions(
txConfig, err = authtx.NewTxConfigWithOptions(
appCodec,
txConfigOpts,
)
@@ -520,7 +520,7 @@ func NewChainApp(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
app.AccountKeeper.AddressCodec(),
)
app.BaseApp.SetCircuitBreaker(&app.CircuitKeeper)
app.SetCircuitBreaker(&app.CircuitKeeper)
app.AuthzKeeper = authzkeeper.NewKeeper(
sdkruntime.NewKVStoreService(keys[authzkeeper.StoreKey]),
@@ -1158,7 +1158,7 @@ func NewChainApp(
panic(fmt.Errorf("error loading last version: %w", err))
}
ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})
ctx := app.NewUncachedContext(true, tmproto.Header{})
_ = ctx
}
@@ -1399,9 +1399,9 @@ func (app *SonrApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APICo
// RegisterTxService implements the Application.RegisterTxService method.
func (app *SonrApp) RegisterTxService(clientCtx client.Context) {
authtx.RegisterTxService(
app.BaseApp.GRPCQueryRouter(),
app.GRPCQueryRouter(),
clientCtx,
app.BaseApp.Simulate,
app.Simulate,
app.interfaceRegistry,
)
}
@@ -1411,7 +1411,7 @@ func (app *SonrApp) RegisterTendermintService(clientCtx client.Context) {
cmtApp := server.NewCometABCIWrapper(app)
cmtservice.RegisterTendermintService(
clientCtx,
app.BaseApp.GRPCQueryRouter(),
app.GRPCQueryRouter(),
app.interfaceRegistry,
cmtApp.Query,
)
@@ -1422,13 +1422,9 @@ func (app *SonrApp) RegisterNodeService(clientCtx client.Context, cfg config.Con
}
// GetMaccPerms returns a copy of the module account permissions
//
// NOTE: This is solely to be used for testing purposes.
func GetMaccPerms() map[string][]string {
dupMaccPerms := make(map[string][]string)
for k, v := range maccPerms {
dupMaccPerms[k] = v
}
maps.Copy(dupMaccPerms, maccPerms)
return dupMaccPerms
}
+29 -29
View File
@@ -90,9 +90,9 @@ func (app *SonrApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
}
for _, delegation := range dels {
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
valAddr, errB := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if errB != nil {
panic(errB)
}
delAddr := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)
@@ -114,26 +114,26 @@ func (app *SonrApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
// reinitialize all validators
err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
valBz, errB := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if errB != nil {
panic(errB)
}
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if err != nil {
panic(err)
scraps, errC := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if errC != nil {
panic(errC)
}
feePool, err := app.DistrKeeper.FeePool.Get(ctx)
if err != nil {
panic(err)
feePool, errD := app.DistrKeeper.FeePool.Get(ctx)
if errD != nil {
panic(errD)
}
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
if err := app.DistrKeeper.FeePool.Set(ctx, feePool); err != nil {
panic(err)
if errE := app.DistrKeeper.FeePool.Set(ctx, feePool); errE != nil {
panic(errE)
}
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz); err != nil {
panic(err)
if errF := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz); errF != nil {
panic(errF)
}
return false
})
@@ -143,20 +143,20 @@ func (app *SonrApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
// reinitialize all delegations
for _, del := range dels {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
valAddr, errcc := sdk.ValAddressFromBech32(del.ValidatorAddress)
if errcc != nil {
panic(errcc)
}
delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress)
if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
if errcb := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); errcb != nil {
// never called as BeforeDelegationCreated always returns nil
panic(fmt.Errorf("error while incrementing period: %w", err))
panic(fmt.Errorf("error while incrementing period: %w", errcb))
}
if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
if errcd := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); errcd != nil {
// never called as AfterDelegationModified always returns nil
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
panic(fmt.Errorf("error while creating a new delegation period record: %w", errcd))
}
}
@@ -202,8 +202,8 @@ func (app *SonrApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, err := app.StakingKeeper.GetValidator(ctx, addr)
if err != nil {
validator, errr := app.StakingKeeper.GetValidator(ctx, addr)
if errr != nil {
panic("expected validator, not found")
}
@@ -218,8 +218,8 @@ func (app *SonrApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
}
}
if err := iter.Close(); err != nil {
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
if erra := iter.Close(); erra != nil {
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", erra)
return
}
@@ -235,8 +235,8 @@ func (app *SonrApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
ctx,
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
if err := app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info); err != nil {
panic(err)
if errb := app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info); errb != nil {
panic(errb)
}
return false
},