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