Skip to content

Commit

Permalink
Remove global ModuleBasics (#3208)
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Jul 19, 2024
1 parent b06e7ea commit 9d84e37
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 156 deletions.
45 changes: 32 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/x/tx/signing"
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -65,7 +68,6 @@ import (

gaiaante "github.com/cosmos/gaia/v19/ante"
"github.com/cosmos/gaia/v19/app/keepers"
"github.com/cosmos/gaia/v19/app/params"
"github.com/cosmos/gaia/v19/app/upgrades"
v19 "github.com/cosmos/gaia/v19/app/upgrades/v19"
)
Expand Down Expand Up @@ -98,7 +100,9 @@ type GaiaApp struct { //nolint: revive
invCheckPeriod uint

// the module manager
mm *module.Manager
mm *module.Manager
ModuleBasics module.BasicManager

// simulation manager
sm *module.SimulationManager
configurator module.Configurator
Expand All @@ -121,15 +125,30 @@ func NewGaiaApp(
loadLatest bool,
skipUpgradeHeights map[int64]bool,
homePath string,
encodingConfig params.EncodingConfig,
appOpts servertypes.AppOptions,
wasmOpts []wasmkeeper.Option,
baseAppOptions ...func(*baseapp.BaseApp),
) *GaiaApp {
appCodec := encodingConfig.Marshaler
legacyAmino := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry
txConfig := encodingConfig.TxConfig
legacyAmino := codec.NewLegacyAmino()
interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: signing.Options{
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
},
})
if err != nil {
panic(err)
}
appCodec := codec.NewProtoCodec(interfaceRegistry)
txConfig := authtx.NewTxConfig(appCodec, authtx.DefaultSignModes)

std.RegisterLegacyAminoCodec(legacyAmino)
std.RegisterInterfaces(interfaceRegistry)

// App Opts
skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))
Expand Down Expand Up @@ -176,8 +195,8 @@ func NewGaiaApp(

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
app.mm = module.NewManager(appModules(app, encodingConfig, skipGenesisInvariants)...)
ModuleBasics = newBasicManagerFromManager(app)
app.mm = module.NewManager(appModules(app, appCodec, txConfig, skipGenesisInvariants)...)
app.ModuleBasics = newBasicManagerFromManager(app)

enabledSignModes := append([]sigtypes.SignMode(nil), authtx.DefaultSignModes...)
enabledSignModes = append(enabledSignModes, sigtypes.SignMode_SIGN_MODE_TEXTUAL)
Expand All @@ -186,7 +205,7 @@ func NewGaiaApp(
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewBankKeeperCoinMetadataQueryFn(app.BankKeeper),
}
txConfig, err := authtx.NewTxConfigWithOptions(
txConfig, err = authtx.NewTxConfigWithOptions(
appCodec,
txConfigOpts,
)
Expand Down Expand Up @@ -242,7 +261,7 @@ func NewGaiaApp(
//
// NOTE: this is not required apps that don't use the simulator for fuzz testing
// transactions
app.sm = module.NewSimulationManager(simulationModules(app, encodingConfig, skipGenesisInvariants)...)
app.sm = module.NewSimulationManager(simulationModules(app, appCodec, skipGenesisInvariants)...)

app.sm.RegisterStoreDecoders()

Expand All @@ -262,7 +281,7 @@ func NewGaiaApp(
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SignModeHandler: txConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
Codec: appCodec,
Expand Down Expand Up @@ -438,7 +457,7 @@ func (app *GaiaApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APICo
cmtservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

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

// Register nodeservice grpc-gateway routes.
nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
Expand Down
2 changes: 0 additions & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ func (ao EmptyAppOptions) Get(_ string) interface{} {
}

func TestGaiaApp_BlockedModuleAccountAddrs(t *testing.T) {
encConfig := gaia.RegisterEncodingConfig()
app := gaia.NewGaiaApp(
log.NewNopLogger(),
db.NewMemDB(),
nil,
true,
map[int64]bool{},
gaia.DefaultNodeHome,
encConfig,
EmptyAppOptions{},
emptyWasmOption,
)
Expand Down
18 changes: 0 additions & 18 deletions app/encoding.go

This file was deleted.

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 gaia

import (
"encoding/json"

"github.com/cosmos/gaia/v19/app/params"
)

// 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(encConfig params.EncodingConfig) GenesisState {
return ModuleBasics.DefaultGenesis(encConfig.Marshaler)
}
5 changes: 1 addition & 4 deletions app/helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,17 @@ func setup() (*gaiaapp.GaiaApp, gaiaapp.GenesisState) {
appOptions[server.FlagInvCheckPeriod] = 5
appOptions[server.FlagMinGasPrices] = "0uatom"

encConfig := gaiaapp.RegisterEncodingConfig()

gaiaApp := gaiaapp.NewGaiaApp(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
gaiaapp.DefaultNodeHome,
encConfig,
appOptions,
emptyWasmOpts,
)
return gaiaApp, gaiaapp.NewDefaultGenesisState(encConfig)
return gaiaApp, gaiaApp.ModuleBasics.DefaultGenesis(gaiaApp.AppCodec())
}

func genesisStateWithValSet(t *testing.T,
Expand Down
67 changes: 11 additions & 56 deletions app/modules.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package gaia

import (
ratelimit "github.com/Stride-Labs/ibc-rate-limiting/ratelimit"
ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"
feemarket "github.com/skip-mev/feemarket/x/feemarket"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"

pfmrouter "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward"
pfmroutertypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/types"
"github.com/cosmos/ibc-go/modules/capability"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
icsprovider "github.com/cosmos/interchain-security/v5/x/ccv/provider"
icsproviderclient "github.com/cosmos/interchain-security/v5/x/ccv/provider/client"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"

Expand All @@ -30,6 +25,8 @@ import (
"cosmossdk.io/x/upgrade"
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
Expand Down Expand Up @@ -64,7 +61,6 @@ import (
wasm "github.com/CosmWasm/wasmd/x/wasm"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

gaiaappparams "github.com/cosmos/gaia/v19/app/params"
"github.com/cosmos/gaia/v19/x/metaprotocols"
metaprotocolstypes "github.com/cosmos/gaia/v19/x/metaprotocols/types"
)
Expand All @@ -86,61 +82,18 @@ var maccPerms = map[string][]string{
feemarkettypes.FeeCollectorName: nil,
}

// ModuleBasics defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
var ModuleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
bank.AppModuleBasic{},
capability.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(
[]govclient.ProposalHandler{
paramsclient.ProposalHandler,
icsproviderclient.ConsumerAdditionProposalHandler,
icsproviderclient.ConsumerRemovalProposalHandler,
icsproviderclient.ConsumerModificationProposalHandler,
icsproviderclient.ChangeRewardDenomsProposalHandler,
},
),
sdkparams.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
authzmodule.AppModuleBasic{},
ibc.AppModuleBasic{},
ibctm.AppModuleBasic{},
ibcfee.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
vesting.AppModuleBasic{},
pfmrouter.AppModuleBasic{},
ratelimit.AppModuleBasic{},
ica.AppModuleBasic{},
icsprovider.AppModuleBasic{},
consensus.AppModuleBasic{},
metaprotocols.AppModuleBasic{},
wasm.AppModuleBasic{},
feemarket.AppModuleBasic{},
)

func appModules(
app *GaiaApp,
encodingConfig gaiaappparams.EncodingConfig,
appCodec codec.Codec,
txConfig client.TxEncodingConfig,
skipGenesisInvariants bool,
) []module.AppModule {
appCodec := encodingConfig.Marshaler

return []module.AppModule{
genutil.NewAppModule(
app.AccountKeeper,
app.StakingKeeper,
app,
encodingConfig.TxConfig,
txConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper, nil, app.GetSubspace(authtypes.ModuleName)),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
Expand Down Expand Up @@ -172,6 +125,9 @@ func appModules(
}
}

// ModuleBasics defines the module BasicManager that is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
func newBasicManagerFromManager(app *GaiaApp) module.BasicManager {
basicManager := module.NewBasicManagerFromManager(
app.mm,
Expand All @@ -187,19 +143,18 @@ func newBasicManagerFromManager(app *GaiaApp) module.BasicManager {
},
),
})

basicManager.RegisterLegacyAminoCodec(app.legacyAmino)
basicManager.RegisterInterfaces(app.interfaceRegistry)
return basicManager
}

// simulationModules returns modules for simulation manager
// define the order of the modules for deterministic simulations
func simulationModules(
app *GaiaApp,
encodingConfig gaiaappparams.EncodingConfig,
appCodec codec.Codec,
_ bool,
) []module.AppModuleSimulation {
appCodec := encodingConfig.Marshaler

return []module.AppModuleSimulation{
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(banktypes.ModuleName)),
Expand Down
15 changes: 5 additions & 10 deletions app/sim/sim_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

gaia "github.com/cosmos/gaia/v19/app"
"github.com/cosmos/gaia/v19/app/params"
)

// Simulation parameter constants
Expand All @@ -35,11 +34,9 @@ const (
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
// It panics if the user provides files for both of them.
// If a file is not given for the genesis or the sim params, it creates a randomized one.
func AppStateFn(encConfig params.EncodingConfig, simManager *module.SimulationManager) simtypes.AppStateFn {
func AppStateFn(cdc codec.Codec, simManager *module.SimulationManager, genesisState map[string]json.RawMessage) simtypes.AppStateFn {
return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config,
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {
cdc := encConfig.Marshaler

if FlagGenesisTimeValue == 0 {
genesisTimestamp = simtypes.RandTimestamp(r)
} else {
Expand Down Expand Up @@ -78,11 +75,11 @@ func AppStateFn(encConfig params.EncodingConfig, simManager *module.SimulationMa
if err != nil {
panic(err)
}
appState, simAccs = AppStateRandomizedFn(simManager, r, encConfig, accs, genesisTimestamp, appParams)
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState)

default:
appParams := make(simtypes.AppParams)
appState, simAccs = AppStateRandomizedFn(simManager, r, encConfig, accs, genesisTimestamp, appParams)
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState)
}

rawState := make(map[string]json.RawMessage)
Expand Down Expand Up @@ -153,13 +150,11 @@ func AppStateFn(encConfig params.EncodingConfig, simManager *module.SimulationMa
// AppStateRandomizedFn creates calls each module's GenesisState generator function
// and creates the simulation params
func AppStateRandomizedFn(
simManager *module.SimulationManager, r *rand.Rand, encConfig params.EncodingConfig,
simManager *module.SimulationManager, r *rand.Rand, cdc codec.Codec,
accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams,
genesisState map[string]json.RawMessage,
) (json.RawMessage, []simtypes.Account) {
numAccs := int64(len(accs))
cdc := encConfig.Marshaler
genesisState := gaia.NewDefaultGenesisState(encConfig)

// generate a random amount of initial stake coins and a random initial
// number of bonded accounts
var (
Expand Down
Loading

0 comments on commit 9d84e37

Please sign in to comment.