github.com/Finschia/finschia-sdk@v0.48.1/simapp/app.go (about)

     1  package simapp
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"net/http"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/gorilla/mux"
    11  	"github.com/rakyll/statik/fs"
    12  	"github.com/spf13/cast"
    13  	abci "github.com/tendermint/tendermint/abci/types"
    14  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    15  	dbm "github.com/tendermint/tm-db"
    16  
    17  	ocabci "github.com/Finschia/ostracon/abci/types"
    18  	"github.com/Finschia/ostracon/libs/log"
    19  	ostos "github.com/Finschia/ostracon/libs/os"
    20  
    21  	"github.com/Finschia/finschia-sdk/baseapp"
    22  	"github.com/Finschia/finschia-sdk/client"
    23  	nodeservice "github.com/Finschia/finschia-sdk/client/grpc/node"
    24  	"github.com/Finschia/finschia-sdk/client/grpc/ocservice"
    25  	"github.com/Finschia/finschia-sdk/client/grpc/tmservice"
    26  	"github.com/Finschia/finschia-sdk/codec"
    27  	"github.com/Finschia/finschia-sdk/codec/types"
    28  	"github.com/Finschia/finschia-sdk/server/api"
    29  	"github.com/Finschia/finschia-sdk/server/config"
    30  	servertypes "github.com/Finschia/finschia-sdk/server/types"
    31  	simappparams "github.com/Finschia/finschia-sdk/simapp/params"
    32  	"github.com/Finschia/finschia-sdk/store/streaming"
    33  	"github.com/Finschia/finschia-sdk/testutil/testdata"
    34  	sdk "github.com/Finschia/finschia-sdk/types"
    35  	"github.com/Finschia/finschia-sdk/types/module"
    36  	"github.com/Finschia/finschia-sdk/version"
    37  	"github.com/Finschia/finschia-sdk/x/auth"
    38  	"github.com/Finschia/finschia-sdk/x/auth/ante"
    39  	authkeeper "github.com/Finschia/finschia-sdk/x/auth/keeper"
    40  	authsims "github.com/Finschia/finschia-sdk/x/auth/simulation"
    41  	authtx "github.com/Finschia/finschia-sdk/x/auth/tx"
    42  	authtx2 "github.com/Finschia/finschia-sdk/x/auth/tx2"
    43  	authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
    44  	"github.com/Finschia/finschia-sdk/x/auth/vesting"
    45  	vestingtypes "github.com/Finschia/finschia-sdk/x/auth/vesting/types"
    46  	"github.com/Finschia/finschia-sdk/x/authz"
    47  	authzkeeper "github.com/Finschia/finschia-sdk/x/authz/keeper"
    48  	authzmodule "github.com/Finschia/finschia-sdk/x/authz/module"
    49  	"github.com/Finschia/finschia-sdk/x/bank"
    50  	bankkeeper "github.com/Finschia/finschia-sdk/x/bank/keeper"
    51  	banktypes "github.com/Finschia/finschia-sdk/x/bank/types"
    52  	"github.com/Finschia/finschia-sdk/x/bankplus"
    53  	bankpluskeeper "github.com/Finschia/finschia-sdk/x/bankplus/keeper"
    54  	"github.com/Finschia/finschia-sdk/x/capability"
    55  	capabilitykeeper "github.com/Finschia/finschia-sdk/x/capability/keeper"
    56  	capabilitytypes "github.com/Finschia/finschia-sdk/x/capability/types"
    57  	"github.com/Finschia/finschia-sdk/x/collection"
    58  	collectionkeeper "github.com/Finschia/finschia-sdk/x/collection/keeper"
    59  	collectionmodule "github.com/Finschia/finschia-sdk/x/collection/module"
    60  	"github.com/Finschia/finschia-sdk/x/crisis"
    61  	crisiskeeper "github.com/Finschia/finschia-sdk/x/crisis/keeper"
    62  	crisistypes "github.com/Finschia/finschia-sdk/x/crisis/types"
    63  	distr "github.com/Finschia/finschia-sdk/x/distribution"
    64  	distrclient "github.com/Finschia/finschia-sdk/x/distribution/client"
    65  	distrkeeper "github.com/Finschia/finschia-sdk/x/distribution/keeper"
    66  	distrtypes "github.com/Finschia/finschia-sdk/x/distribution/types"
    67  	"github.com/Finschia/finschia-sdk/x/evidence"
    68  	evidencekeeper "github.com/Finschia/finschia-sdk/x/evidence/keeper"
    69  	evidencetypes "github.com/Finschia/finschia-sdk/x/evidence/types"
    70  	"github.com/Finschia/finschia-sdk/x/feegrant"
    71  	feegrantkeeper "github.com/Finschia/finschia-sdk/x/feegrant/keeper"
    72  	feegrantmodule "github.com/Finschia/finschia-sdk/x/feegrant/module"
    73  	"github.com/Finschia/finschia-sdk/x/foundation"
    74  	foundationclient "github.com/Finschia/finschia-sdk/x/foundation/client"
    75  	foundationkeeper "github.com/Finschia/finschia-sdk/x/foundation/keeper"
    76  	foundationmodule "github.com/Finschia/finschia-sdk/x/foundation/module"
    77  	"github.com/Finschia/finschia-sdk/x/genutil"
    78  	genutiltypes "github.com/Finschia/finschia-sdk/x/genutil/types"
    79  	"github.com/Finschia/finschia-sdk/x/gov"
    80  	govkeeper "github.com/Finschia/finschia-sdk/x/gov/keeper"
    81  	govtypes "github.com/Finschia/finschia-sdk/x/gov/types"
    82  	"github.com/Finschia/finschia-sdk/x/mint"
    83  	mintkeeper "github.com/Finschia/finschia-sdk/x/mint/keeper"
    84  	minttypes "github.com/Finschia/finschia-sdk/x/mint/types"
    85  	"github.com/Finschia/finschia-sdk/x/params"
    86  	paramsclient "github.com/Finschia/finschia-sdk/x/params/client"
    87  	paramskeeper "github.com/Finschia/finschia-sdk/x/params/keeper"
    88  	paramstypes "github.com/Finschia/finschia-sdk/x/params/types"
    89  	paramproposal "github.com/Finschia/finschia-sdk/x/params/types/proposal"
    90  	"github.com/Finschia/finschia-sdk/x/slashing"
    91  	slashingkeeper "github.com/Finschia/finschia-sdk/x/slashing/keeper"
    92  	slashingtypes "github.com/Finschia/finschia-sdk/x/slashing/types"
    93  	"github.com/Finschia/finschia-sdk/x/staking"
    94  	stakingkeeper "github.com/Finschia/finschia-sdk/x/staking/keeper"
    95  	stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
    96  	stakingplusmodule "github.com/Finschia/finschia-sdk/x/stakingplus/module"
    97  	"github.com/Finschia/finschia-sdk/x/token"
    98  	"github.com/Finschia/finschia-sdk/x/token/class"
    99  	classkeeper "github.com/Finschia/finschia-sdk/x/token/class/keeper"
   100  	tokenkeeper "github.com/Finschia/finschia-sdk/x/token/keeper"
   101  	tokenmodule "github.com/Finschia/finschia-sdk/x/token/module"
   102  	"github.com/Finschia/finschia-sdk/x/upgrade"
   103  	upgradeclient "github.com/Finschia/finschia-sdk/x/upgrade/client"
   104  	upgradekeeper "github.com/Finschia/finschia-sdk/x/upgrade/keeper"
   105  	upgradetypes "github.com/Finschia/finschia-sdk/x/upgrade/types"
   106  
   107  	// unnamed import of statik for swagger UI support
   108  	_ "github.com/Finschia/finschia-sdk/client/docs/statik"
   109  )
   110  
   111  const appName = "SimApp"
   112  
   113  var (
   114  	// DefaultNodeHome default home directories for the application daemon
   115  	DefaultNodeHome string
   116  
   117  	// ModuleBasics defines the module BasicManager is in charge of setting up basic,
   118  	// non-dependant module elements, such as codec registration
   119  	// and genesis verification.
   120  	ModuleBasics = module.NewBasicManager(
   121  		auth.AppModuleBasic{},
   122  		genutil.AppModuleBasic{},
   123  		bank.AppModuleBasic{},
   124  		capability.AppModuleBasic{},
   125  		stakingplusmodule.AppModuleBasic{},
   126  		mint.AppModuleBasic{},
   127  		distr.AppModuleBasic{},
   128  		foundationmodule.AppModuleBasic{},
   129  		gov.NewAppModuleBasic(
   130  			paramsclient.ProposalHandler,
   131  			distrclient.ProposalHandler,
   132  			upgradeclient.ProposalHandler,
   133  			upgradeclient.CancelProposalHandler,
   134  			foundationclient.ProposalHandler,
   135  		),
   136  		params.AppModuleBasic{},
   137  		crisis.AppModuleBasic{},
   138  		slashing.AppModuleBasic{},
   139  		feegrantmodule.AppModuleBasic{},
   140  		upgrade.AppModuleBasic{},
   141  		evidence.AppModuleBasic{},
   142  		authzmodule.AppModuleBasic{},
   143  		vesting.AppModuleBasic{},
   144  		tokenmodule.AppModuleBasic{},
   145  		collectionmodule.AppModuleBasic{},
   146  	)
   147  
   148  	// module account permissions
   149  	maccPerms = map[string][]string{
   150  		authtypes.FeeCollectorName:     nil,
   151  		distrtypes.ModuleName:          nil,
   152  		foundation.ModuleName:          nil,
   153  		foundation.TreasuryName:        nil,
   154  		minttypes.ModuleName:           {authtypes.Minter},
   155  		stakingtypes.BondedPoolName:    {authtypes.Burner, authtypes.Staking},
   156  		stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
   157  		govtypes.ModuleName:            {authtypes.Burner},
   158  	}
   159  
   160  	// module accounts that are allowed to receive tokens
   161  	allowedReceivingModAcc = map[string]bool{
   162  		// govtypes.ModuleName: true, // TODO: uncomment it when authority is ready
   163  	}
   164  )
   165  
   166  var (
   167  	_ App                     = (*SimApp)(nil)
   168  	_ servertypes.Application = (*SimApp)(nil)
   169  )
   170  
   171  // SimApp extends an ABCI application, but with most of its parameters exported.
   172  // They are exported for convenience in creating helper functions, as object
   173  // capabilities aren't needed for testing.
   174  type SimApp struct {
   175  	*baseapp.BaseApp
   176  	legacyAmino       *codec.LegacyAmino
   177  	appCodec          codec.Codec
   178  	interfaceRegistry types.InterfaceRegistry
   179  
   180  	invCheckPeriod uint
   181  
   182  	// keys to access the substores
   183  	keys    map[string]*sdk.KVStoreKey
   184  	memKeys map[string]*sdk.MemoryStoreKey
   185  
   186  	// keepers
   187  	AccountKeeper    authkeeper.AccountKeeper
   188  	BankKeeper       bankkeeper.Keeper
   189  	CapabilityKeeper *capabilitykeeper.Keeper
   190  	StakingKeeper    stakingkeeper.Keeper
   191  	SlashingKeeper   slashingkeeper.Keeper
   192  	MintKeeper       mintkeeper.Keeper
   193  	DistrKeeper      distrkeeper.Keeper
   194  	FoundationKeeper foundationkeeper.Keeper
   195  	GovKeeper        govkeeper.Keeper
   196  	CrisisKeeper     crisiskeeper.Keeper
   197  	UpgradeKeeper    upgradekeeper.Keeper
   198  	ParamsKeeper     paramskeeper.Keeper
   199  	AuthzKeeper      authzkeeper.Keeper
   200  	EvidenceKeeper   evidencekeeper.Keeper
   201  	FeeGrantKeeper   feegrantkeeper.Keeper
   202  	ClassKeeper      classkeeper.Keeper
   203  	TokenKeeper      tokenkeeper.Keeper
   204  	CollectionKeeper collectionkeeper.Keeper
   205  
   206  	// the module manager
   207  	mm *module.Manager
   208  
   209  	// simulation manager
   210  	sm *module.SimulationManager
   211  
   212  	// the configurator
   213  	configurator module.Configurator
   214  }
   215  
   216  func init() {
   217  	userHomeDir, err := os.UserHomeDir()
   218  	if err != nil {
   219  		panic(err)
   220  	}
   221  
   222  	DefaultNodeHome = filepath.Join(userHomeDir, ".simapp")
   223  }
   224  
   225  // NewSimApp returns a reference to an initialized SimApp.
   226  func NewSimApp(
   227  	logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, skipUpgradeHeights map[int64]bool,
   228  	homePath string, invCheckPeriod uint, encodingConfig simappparams.EncodingConfig,
   229  	appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
   230  ) *SimApp {
   231  	appCodec := encodingConfig.Marshaler
   232  	legacyAmino := encodingConfig.Amino
   233  	interfaceRegistry := encodingConfig.InterfaceRegistry
   234  
   235  	bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
   236  	bApp.SetCommitMultiStoreTracer(traceStore)
   237  	bApp.SetVersion(version.Version)
   238  	bApp.SetInterfaceRegistry(interfaceRegistry)
   239  
   240  	keys := sdk.NewKVStoreKeys(
   241  		authtypes.StoreKey,
   242  		banktypes.StoreKey,
   243  		stakingtypes.StoreKey,
   244  		minttypes.StoreKey,
   245  		distrtypes.StoreKey,
   246  		slashingtypes.StoreKey,
   247  		govtypes.StoreKey,
   248  		paramstypes.StoreKey,
   249  		upgradetypes.StoreKey,
   250  		evidencetypes.StoreKey,
   251  		capabilitytypes.StoreKey,
   252  		feegrant.StoreKey,
   253  		foundation.StoreKey,
   254  		class.StoreKey,
   255  		token.StoreKey,
   256  		collection.StoreKey,
   257  		authzkeeper.StoreKey,
   258  	)
   259  	tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
   260  	// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
   261  	// not include this key.
   262  	memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, "testingkey")
   263  
   264  	// configure state listening capabilities using AppOptions
   265  	// we are doing nothing with the returned streamingServices and waitGroup in this case
   266  	if _, _, err := streaming.LoadStreamingServices(bApp, appOpts, appCodec, keys); err != nil {
   267  		ostos.Exit(err.Error())
   268  	}
   269  
   270  	app := &SimApp{
   271  		BaseApp:           bApp,
   272  		legacyAmino:       legacyAmino,
   273  		appCodec:          appCodec,
   274  		interfaceRegistry: interfaceRegistry,
   275  		invCheckPeriod:    invCheckPeriod,
   276  		keys:              keys,
   277  		memKeys:           memKeys,
   278  	}
   279  
   280  	app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])
   281  
   282  	// set the BaseApp's parameter store
   283  	bApp.SetParamStore(app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramskeeper.ConsensusParamsKeyTable()))
   284  
   285  	// add capability keeper and ScopeToModule for ibc module
   286  	app.CapabilityKeeper = capabilitykeeper.NewKeeper(appCodec, keys[capabilitytypes.StoreKey], memKeys[capabilitytypes.MemStoreKey])
   287  
   288  	// Applications that wish to enforce statically created ScopedKeepers should call `Seal` after creating
   289  	// their scoped modules in `NewApp` with `ScopeToModule`
   290  	app.CapabilityKeeper.Seal()
   291  
   292  	// add keepers
   293  	app.AccountKeeper = authkeeper.NewAccountKeeper(
   294  		appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms,
   295  	)
   296  	app.BankKeeper = bankpluskeeper.NewBaseKeeper(
   297  		appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.BlockedAddrs(), false)
   298  	stakingKeeper := stakingkeeper.NewKeeper(
   299  		appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName),
   300  	)
   301  	app.MintKeeper = mintkeeper.NewKeeper(
   302  		appCodec, keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), &stakingKeeper,
   303  		app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName,
   304  	)
   305  	app.DistrKeeper = distrkeeper.NewKeeper(
   306  		appCodec, keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
   307  		&stakingKeeper, authtypes.FeeCollectorName, app.ModuleAccountAddrs(),
   308  	)
   309  	app.SlashingKeeper = slashingkeeper.NewKeeper(
   310  		appCodec, keys[slashingtypes.StoreKey], &stakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
   311  	)
   312  	app.CrisisKeeper = crisiskeeper.NewKeeper(
   313  		app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName,
   314  	)
   315  
   316  	app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, keys[feegrant.StoreKey], app.AccountKeeper)
   317  	app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp)
   318  
   319  	foundationConfig := foundation.DefaultConfig()
   320  	app.FoundationKeeper = foundationkeeper.NewKeeper(appCodec, keys[foundation.StoreKey], app.BaseApp.MsgServiceRouter(), app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName, foundationConfig, foundation.DefaultAuthority().String(), app.GetSubspace(foundation.ModuleName))
   321  
   322  	app.ClassKeeper = classkeeper.NewKeeper(appCodec, keys[class.StoreKey])
   323  	app.TokenKeeper = tokenkeeper.NewKeeper(appCodec, keys[token.StoreKey], app.ClassKeeper)
   324  	app.CollectionKeeper = collectionkeeper.NewKeeper(appCodec, keys[collection.StoreKey], app.ClassKeeper)
   325  
   326  	// register the staking hooks
   327  	// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
   328  	app.StakingKeeper = *stakingKeeper.SetHooks(
   329  		stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
   330  	)
   331  
   332  	app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.BaseApp.MsgServiceRouter())
   333  
   334  	// register the proposal types
   335  	govRouter := govtypes.NewRouter()
   336  	govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
   337  		AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
   338  		AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
   339  		AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
   340  		AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper))
   341  
   342  	govKeeper := govkeeper.NewKeeper(
   343  		appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
   344  		&stakingKeeper, govRouter,
   345  	)
   346  
   347  	app.GovKeeper = *govKeeper.SetHooks(
   348  		govtypes.NewMultiGovHooks(
   349  		// register the governance hooks
   350  		),
   351  	)
   352  
   353  	// create evidence keeper with router
   354  	evidenceKeeper := evidencekeeper.NewKeeper(
   355  		appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
   356  	)
   357  	// If evidence needs to be handled for the app, set routes in router here and seal
   358  	app.EvidenceKeeper = *evidenceKeeper
   359  
   360  	/****  Module Options ****/
   361  
   362  	// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
   363  	// we prefer to be more strict in what arguments the modules expect.
   364  	skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))
   365  
   366  	// NOTE: Any module instantiated in the module manager that is later modified
   367  	// must be passed by reference here.
   368  	app.mm = module.NewManager(
   369  		genutil.NewAppModule(
   370  			app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx,
   371  			encodingConfig.TxConfig,
   372  		),
   373  		auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
   374  		vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
   375  		bankplus.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
   376  		capability.NewAppModule(appCodec, *app.CapabilityKeeper),
   377  		crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants),
   378  		feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
   379  		foundationmodule.NewAppModule(appCodec, app.FoundationKeeper),
   380  		gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
   381  		mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
   382  		slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
   383  		distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
   384  		stakingplusmodule.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.FoundationKeeper),
   385  		upgrade.NewAppModule(app.UpgradeKeeper),
   386  		evidence.NewAppModule(app.EvidenceKeeper),
   387  		params.NewAppModule(app.ParamsKeeper),
   388  		tokenmodule.NewAppModule(appCodec, app.TokenKeeper),
   389  		collectionmodule.NewAppModule(appCodec, app.CollectionKeeper),
   390  		authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
   391  	)
   392  
   393  	// During begin block slashing happens after distr.BeginBlocker so that
   394  	// there is nothing left over in the validator fee pool, so as to keep the
   395  	// CanWithdrawInvariant invariant.
   396  	// NOTE: staking module is required if HistoricalEntries param > 0
   397  	// NOTE: capability module's beginblocker must come before any modules using capabilities (e.g. IBC)
   398  	app.mm.SetOrderBeginBlockers(
   399  		upgradetypes.ModuleName,
   400  		capabilitytypes.ModuleName,
   401  		minttypes.ModuleName,
   402  		foundation.ModuleName,
   403  		distrtypes.ModuleName,
   404  		slashingtypes.ModuleName,
   405  		evidencetypes.ModuleName,
   406  		stakingtypes.ModuleName,
   407  		authtypes.ModuleName,
   408  		banktypes.ModuleName,
   409  		govtypes.ModuleName,
   410  		crisistypes.ModuleName,
   411  		genutiltypes.ModuleName,
   412  		authz.ModuleName,
   413  		feegrant.ModuleName,
   414  		paramstypes.ModuleName,
   415  		vestingtypes.ModuleName,
   416  		token.ModuleName,
   417  		collection.ModuleName,
   418  	)
   419  	app.mm.SetOrderEndBlockers(
   420  		crisistypes.ModuleName,
   421  		govtypes.ModuleName,
   422  		stakingtypes.ModuleName,
   423  		capabilitytypes.ModuleName,
   424  		authtypes.ModuleName,
   425  		banktypes.ModuleName,
   426  		distrtypes.ModuleName,
   427  		slashingtypes.ModuleName,
   428  		minttypes.ModuleName,
   429  		genutiltypes.ModuleName,
   430  		evidencetypes.ModuleName,
   431  		authz.ModuleName,
   432  		feegrant.ModuleName,
   433  		paramstypes.ModuleName,
   434  		upgradetypes.ModuleName,
   435  		vestingtypes.ModuleName,
   436  		foundation.ModuleName,
   437  		token.ModuleName,
   438  		collection.ModuleName,
   439  	)
   440  
   441  	// NOTE: The genutils module must occur after staking so that pools are
   442  	// properly initialized with tokens from genesis accounts.
   443  	// NOTE: Capability module must occur first so that it can initialize any capabilities
   444  	// so that other modules that want to create or claim capabilities afterwards in InitChain
   445  	// can do so safely.
   446  	app.mm.SetOrderInitGenesis(
   447  		capabilitytypes.ModuleName,
   448  		authtypes.ModuleName,
   449  		banktypes.ModuleName,
   450  		distrtypes.ModuleName,
   451  		stakingtypes.ModuleName,
   452  		slashingtypes.ModuleName,
   453  		govtypes.ModuleName,
   454  		minttypes.ModuleName,
   455  		foundation.ModuleName,
   456  		crisistypes.ModuleName,
   457  		genutiltypes.ModuleName,
   458  		evidencetypes.ModuleName,
   459  		authz.ModuleName,
   460  		feegrant.ModuleName,
   461  		paramstypes.ModuleName,
   462  		upgradetypes.ModuleName,
   463  		vestingtypes.ModuleName,
   464  		token.ModuleName,
   465  		collection.ModuleName,
   466  	)
   467  
   468  	// Uncomment if you want to set a custom migration order here.
   469  	// app.mm.SetOrderMigrations(custom order)
   470  
   471  	app.mm.RegisterInvariants(&app.CrisisKeeper)
   472  	app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
   473  	app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
   474  	app.mm.RegisterServices(app.configurator)
   475  
   476  	// add test gRPC service for testing gRPC queries in isolation
   477  	testdata.RegisterQueryServer(app.GRPCQueryRouter(), testdata.QueryImpl{})
   478  
   479  	// create the simulation manager and define the order of the modules for deterministic simulations
   480  	//
   481  	// NOTE: this is not required apps that don't use the simulator for fuzz testing
   482  	// transactions
   483  	overrideModules := map[string]module.AppModuleSimulation{
   484  		authtypes.ModuleName:    auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
   485  		stakingtypes.ModuleName: staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
   486  	}
   487  	app.sm = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules)
   488  
   489  	app.sm.RegisterStoreDecoders()
   490  
   491  	// initialize stores
   492  	app.MountKVStores(keys)
   493  	app.MountMemoryStores(memKeys)
   494  
   495  	// initialize BaseApp
   496  	app.SetInitChainer(app.InitChainer)
   497  	app.SetBeginBlocker(app.BeginBlocker)
   498  
   499  	anteHandler, err := ante.NewAnteHandler(
   500  		ante.HandlerOptions{
   501  			AccountKeeper:   app.AccountKeeper,
   502  			BankKeeper:      app.BankKeeper,
   503  			SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
   504  			FeegrantKeeper:  app.FeeGrantKeeper,
   505  			SigGasConsumer:  ante.DefaultSigVerificationGasConsumer,
   506  		},
   507  	)
   508  	if err != nil {
   509  		panic(err)
   510  	}
   511  
   512  	app.SetAnteHandler(anteHandler)
   513  	app.SetEndBlocker(app.EndBlocker)
   514  
   515  	if loadLatest {
   516  		if err := app.LoadLatestVersion(); err != nil {
   517  			ostos.Exit(err.Error())
   518  		}
   519  
   520  		ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})
   521  		app.BankKeeper.(bankpluskeeper.Keeper).InitializeBankPlus(ctx)
   522  	}
   523  
   524  	return app
   525  }
   526  
   527  // Name returns the name of the App
   528  func (app *SimApp) Name() string { return app.BaseApp.Name() }
   529  
   530  // BeginBlocker application updates every begin block
   531  func (app *SimApp) BeginBlocker(ctx sdk.Context, req ocabci.RequestBeginBlock) abci.ResponseBeginBlock {
   532  	return app.mm.BeginBlock(ctx, req)
   533  }
   534  
   535  // EndBlocker application updates every end block
   536  func (app *SimApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
   537  	return app.mm.EndBlock(ctx, req)
   538  }
   539  
   540  // InitChainer application update at chain initialization
   541  func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
   542  	var genesisState GenesisState
   543  	if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
   544  		panic(err)
   545  	}
   546  	app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
   547  	return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
   548  }
   549  
   550  // LoadHeight loads a particular height
   551  func (app *SimApp) LoadHeight(height int64) error {
   552  	return app.LoadVersion(height)
   553  }
   554  
   555  // ModuleAccountAddrs returns all the app's module account addresses.
   556  func (app *SimApp) ModuleAccountAddrs() map[string]bool {
   557  	modAccAddrs := make(map[string]bool)
   558  	for acc := range maccPerms {
   559  		modAccAddrs[authtypes.NewModuleAddress(acc).String()] = true
   560  	}
   561  
   562  	return modAccAddrs
   563  }
   564  
   565  // BlockedAddrs returns all the app's module account addresses that are not
   566  // allowed to receive external tokens.
   567  func (app *SimApp) BlockedAddrs() map[string]bool {
   568  	blockedAddrs := make(map[string]bool)
   569  	for acc := range maccPerms {
   570  		blockedAddrs[authtypes.NewModuleAddress(acc).String()] = !allowedReceivingModAcc[acc]
   571  	}
   572  
   573  	return blockedAddrs
   574  }
   575  
   576  // GetModuleManager returns the app module manager
   577  // NOTE: used for testing purposes
   578  func (app *SimApp) GetModuleManager() *module.Manager {
   579  	return app.mm
   580  }
   581  
   582  // LegacyAmino returns SimApp's amino codec.
   583  //
   584  // NOTE: This is solely to be used for testing purposes as it may be desirable
   585  // for modules to register their own custom testing types.
   586  func (app *SimApp) LegacyAmino() *codec.LegacyAmino {
   587  	return app.legacyAmino
   588  }
   589  
   590  // TestingApp functions
   591  // GetBaseApp implements the TestingApp interface.
   592  func (app *SimApp) GetBaseApp() *baseapp.BaseApp {
   593  	return app.BaseApp
   594  }
   595  
   596  // GetStakingKeeper implements the TestingApp interface.
   597  func (app *SimApp) GetStakingKeeper() stakingkeeper.Keeper {
   598  	return app.StakingKeeper
   599  }
   600  
   601  // GetTxConfig implements the TestingApp interface.
   602  func (app *SimApp) GetTxConfig() client.TxConfig {
   603  	return MakeTestEncodingConfig().TxConfig
   604  }
   605  
   606  // AppCodec returns SimApp's app codec.
   607  //
   608  // NOTE: This is solely to be used for testing purposes as it may be desirable
   609  // for modules to register their own custom testing types.
   610  func (app *SimApp) AppCodec() codec.Codec {
   611  	return app.appCodec
   612  }
   613  
   614  // InterfaceRegistry returns SimApp's InterfaceRegistry
   615  func (app *SimApp) InterfaceRegistry() types.InterfaceRegistry {
   616  	return app.interfaceRegistry
   617  }
   618  
   619  // GetKey returns the KVStoreKey for the provided store key.
   620  //
   621  // NOTE: This is solely to be used for testing purposes.
   622  func (app *SimApp) GetKey(storeKey string) *sdk.KVStoreKey {
   623  	return app.keys[storeKey]
   624  }
   625  
   626  // GetMemKey returns the MemStoreKey for the provided mem key.
   627  //
   628  // NOTE: This is solely used for testing purposes.
   629  func (app *SimApp) GetMemKey(storeKey string) *sdk.MemoryStoreKey {
   630  	return app.memKeys[storeKey]
   631  }
   632  
   633  // GetSubspace returns a param subspace for a given module name.
   634  //
   635  // NOTE: This is solely to be used for testing purposes.
   636  func (app *SimApp) GetSubspace(moduleName string) paramstypes.Subspace {
   637  	subspace, _ := app.ParamsKeeper.GetSubspace(moduleName)
   638  	return subspace
   639  }
   640  
   641  // SimulationManager implements the SimulationApp interface
   642  func (app *SimApp) SimulationManager() *module.SimulationManager {
   643  	return app.sm
   644  }
   645  
   646  // RegisterAPIRoutes registers all application module routes with the provided
   647  // API server.
   648  func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
   649  	clientCtx := apiSvr.ClientCtx
   650  
   651  	// Register new tx routes from grpc-gateway.
   652  	authtx.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
   653  	authtx2.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
   654  	// Register new tendermint queries routes from grpc-gateway.
   655  	tmservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
   656  	ocservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
   657  
   658  	// Register node gRPC service for grpc-gateway.
   659  	nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
   660  
   661  	// Register grpc-gateway routes for all modules.
   662  	ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
   663  
   664  	// register swagger API from root so that other applications can override easily
   665  	if apiConfig.Swagger {
   666  		RegisterSwaggerAPI(clientCtx, apiSvr.Router)
   667  	}
   668  }
   669  
   670  // RegisterTxService implements the Application.RegisterTxService method.
   671  func (app *SimApp) RegisterTxService(clientCtx client.Context) {
   672  	authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.BaseApp.Simulate, app.interfaceRegistry)
   673  	authtx2.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
   674  }
   675  
   676  // RegisterTendermintService implements the Application.RegisterTendermintService method.
   677  func (app *SimApp) RegisterTendermintService(clientCtx client.Context) {
   678  	tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
   679  	ocservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
   680  }
   681  
   682  func (app *SimApp) RegisterNodeService(clientCtx client.Context) {
   683  	nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter())
   684  }
   685  
   686  // RegisterSwaggerAPI registers swagger route with API Server
   687  func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) {
   688  	statikFS, err := fs.New()
   689  	if err != nil {
   690  		panic(err)
   691  	}
   692  
   693  	staticServer := http.FileServer(statikFS)
   694  	rtr.PathPrefix("/swagger/").Handler(http.StripPrefix("/swagger/", staticServer))
   695  }
   696  
   697  // GetMaccPerms returns a copy of the module account permissions
   698  func GetMaccPerms() map[string][]string {
   699  	dupMaccPerms := make(map[string][]string)
   700  	for k, v := range maccPerms {
   701  		dupMaccPerms[k] = v
   702  	}
   703  	return dupMaccPerms
   704  }
   705  
   706  // initParamsKeeper init params keeper and its subspaces
   707  func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key, tkey sdk.StoreKey) paramskeeper.Keeper {
   708  	paramsKeeper := paramskeeper.NewKeeper(appCodec, legacyAmino, key, tkey)
   709  
   710  	paramsKeeper.Subspace(authtypes.ModuleName)
   711  	paramsKeeper.Subspace(banktypes.ModuleName)
   712  	paramsKeeper.Subspace(stakingtypes.ModuleName)
   713  	paramsKeeper.Subspace(minttypes.ModuleName)
   714  	paramsKeeper.Subspace(distrtypes.ModuleName)
   715  	paramsKeeper.Subspace(slashingtypes.ModuleName)
   716  	paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable())
   717  	paramsKeeper.Subspace(crisistypes.ModuleName)
   718  	paramsKeeper.Subspace(foundation.ModuleName)
   719  
   720  	return paramsKeeper
   721  }