github.com/Finschia/finschia-sdk@v0.48.1/x/auth/module.go (about) 1 package auth 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "math/rand" 8 9 "github.com/grpc-ecosystem/grpc-gateway/runtime" 10 11 "github.com/spf13/cobra" 12 abci "github.com/tendermint/tendermint/abci/types" 13 14 "github.com/Finschia/finschia-sdk/client" 15 "github.com/Finschia/finschia-sdk/codec" 16 codectypes "github.com/Finschia/finschia-sdk/codec/types" 17 sdk "github.com/Finschia/finschia-sdk/types" 18 "github.com/Finschia/finschia-sdk/types/module" 19 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 20 "github.com/Finschia/finschia-sdk/x/auth/client/cli" 21 "github.com/Finschia/finschia-sdk/x/auth/keeper" 22 "github.com/Finschia/finschia-sdk/x/auth/simulation" 23 "github.com/Finschia/finschia-sdk/x/auth/types" 24 ) 25 26 var ( 27 _ module.AppModule = AppModule{} 28 _ module.AppModuleBasic = AppModuleBasic{} 29 _ module.AppModuleSimulation = AppModule{} 30 ) 31 32 // AppModuleBasic defines the basic application module used by the auth module. 33 type AppModuleBasic struct{} 34 35 // Name returns the auth module's name. 36 func (AppModuleBasic) Name() string { 37 return types.ModuleName 38 } 39 40 // RegisterLegacyAminoCodec registers the auth module's types for the given codec. 41 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { 42 types.RegisterLegacyAminoCodec(cdc) 43 } 44 45 // DefaultGenesis returns default genesis state as raw bytes for the auth 46 // module. 47 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 48 return cdc.MustMarshalJSON(types.DefaultGenesisState()) 49 } 50 51 // ValidateGenesis performs genesis state validation for the auth module. 52 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { 53 var data types.GenesisState 54 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 55 return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) 56 } 57 58 return types.ValidateGenesis(data) 59 } 60 61 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the auth module. 62 func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { 63 if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { 64 panic(err) 65 } 66 } 67 68 // GetTxCmd returns the root tx command for the auth module. 69 func (AppModuleBasic) GetTxCmd() *cobra.Command { 70 return nil 71 } 72 73 // GetQueryCmd returns the root query command for the auth module. 74 func (AppModuleBasic) GetQueryCmd() *cobra.Command { 75 return cli.GetQueryCmd() 76 } 77 78 // RegisterInterfaces registers interfaces and implementations of the auth module. 79 func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { 80 types.RegisterInterfaces(registry) 81 } 82 83 // AppModule implements an application module for the auth module. 84 type AppModule struct { 85 AppModuleBasic 86 87 accountKeeper keeper.AccountKeeper 88 randGenAccountsFn types.RandomGenesisAccountsFn 89 } 90 91 // NewAppModule creates a new AppModule object 92 func NewAppModule(cdc codec.Codec, accountKeeper keeper.AccountKeeper, randGenAccountsFn types.RandomGenesisAccountsFn) AppModule { 93 return AppModule{ 94 AppModuleBasic: AppModuleBasic{}, 95 accountKeeper: accountKeeper, 96 randGenAccountsFn: randGenAccountsFn, 97 } 98 } 99 100 // Name returns the auth module's name. 101 func (AppModule) Name() string { 102 return types.ModuleName 103 } 104 105 // RegisterInvariants performs a no-op. 106 func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} 107 108 // Route returns the message routing key for the auth module. 109 func (AppModule) Route() sdk.Route { return sdk.Route{} } 110 111 // QuerierRoute returns the auth module's querier route name. 112 func (AppModule) QuerierRoute() string { 113 return types.QuerierRoute 114 } 115 116 // LegacyQuerierHandler returns the auth module sdk.Querier. 117 func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { 118 return keeper.NewQuerier(am.accountKeeper, legacyQuerierCdc) 119 } 120 121 // RegisterServices registers a GRPC query service to respond to the 122 // module-specific GRPC queries. 123 func (am AppModule) RegisterServices(cfg module.Configurator) { 124 types.RegisterQueryServer(cfg.QueryServer(), am.accountKeeper) 125 126 // m := keeper.NewMigrator(am.accountKeeper) 127 // if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { 128 // panic(fmt.Sprintf("failed to migrate x/auth from version 1 to 2: %v", err)) 129 // } 130 } 131 132 // InitGenesis performs genesis initialization for the auth module. It returns 133 // no validator updates. 134 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { 135 var genesisState types.GenesisState 136 cdc.MustUnmarshalJSON(data, &genesisState) 137 InitGenesis(ctx, am.accountKeeper, genesisState) 138 return []abci.ValidatorUpdate{} 139 } 140 141 // ExportGenesis returns the exported genesis state as raw bytes for the auth 142 // module. 143 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 144 gs := ExportGenesis(ctx, am.accountKeeper) 145 return cdc.MustMarshalJSON(gs) 146 } 147 148 // ConsensusVersion implements AppModule/ConsensusVersion. 149 func (AppModule) ConsensusVersion() uint64 { return 1 } 150 151 // AppModuleSimulation functions 152 153 // GenerateGenesisState creates a randomized GenState of the auth module 154 func (am AppModule) GenerateGenesisState(simState *module.SimulationState) { 155 simulation.RandomizedGenState(simState, am.randGenAccountsFn) 156 } 157 158 // ProposalContents doesn't return any content functions for governance proposals. 159 func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { 160 return nil 161 } 162 163 // RandomizedParams creates randomized auth param changes for the simulator. 164 func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { 165 return simulation.ParamChanges(r) 166 } 167 168 // RegisterStoreDecoder registers a decoder for auth module's types 169 func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 170 sdr[types.StoreKey] = simulation.NewDecodeStore(am.accountKeeper) 171 } 172 173 // WeightedOperations doesn't return any auth module operation. 174 func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { 175 return nil 176 }