github.com/Finschia/finschia-sdk@v0.48.1/x/evidence/module.go (about) 1 package evidence 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 13 ocabci "github.com/Finschia/ostracon/abci/types" 14 abci "github.com/tendermint/tendermint/abci/types" 15 16 "github.com/Finschia/finschia-sdk/client" 17 "github.com/Finschia/finschia-sdk/codec" 18 codectypes "github.com/Finschia/finschia-sdk/codec/types" 19 sdk "github.com/Finschia/finschia-sdk/types" 20 "github.com/Finschia/finschia-sdk/types/module" 21 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 22 eviclient "github.com/Finschia/finschia-sdk/x/evidence/client" 23 "github.com/Finschia/finschia-sdk/x/evidence/client/cli" 24 "github.com/Finschia/finschia-sdk/x/evidence/keeper" 25 "github.com/Finschia/finschia-sdk/x/evidence/simulation" 26 "github.com/Finschia/finschia-sdk/x/evidence/types" 27 ) 28 29 var ( 30 _ module.AppModule = AppModule{} 31 _ module.AppModuleBasic = AppModuleBasic{} 32 _ module.AppModuleSimulation = AppModule{} 33 _ module.BeginBlockAppModule = AppModule{} 34 ) 35 36 // ---------------------------------------------------------------------------- 37 // AppModuleBasic 38 // ---------------------------------------------------------------------------- 39 40 // AppModuleBasic implements the AppModuleBasic interface for the evidence module. 41 type AppModuleBasic struct { 42 evidenceHandlers []eviclient.EvidenceHandler // eviclient evidence submission handlers 43 } 44 45 // NewAppModuleBasic crates a AppModuleBasic without the codec. 46 func NewAppModuleBasic(evidenceHandlers ...eviclient.EvidenceHandler) AppModuleBasic { 47 return AppModuleBasic{ 48 evidenceHandlers: evidenceHandlers, 49 } 50 } 51 52 // Name returns the evidence module's name. 53 func (AppModuleBasic) Name() string { 54 return types.ModuleName 55 } 56 57 // RegisterLegacyAminoCodec registers the evidence module's types to the LegacyAmino codec. 58 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { 59 types.RegisterLegacyAminoCodec(cdc) 60 } 61 62 // DefaultGenesis returns the evidence module's default genesis state. 63 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 64 return cdc.MustMarshalJSON(types.DefaultGenesisState()) 65 } 66 67 // ValidateGenesis performs genesis state validation for the evidence module. 68 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { 69 var gs types.GenesisState 70 if err := cdc.UnmarshalJSON(bz, &gs); err != nil { 71 return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) 72 } 73 74 return gs.Validate() 75 } 76 77 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the evidence module. 78 func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { 79 if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { 80 panic(err) 81 } 82 } 83 84 // GetTxCmd returns the evidence module's root tx command. 85 func (a AppModuleBasic) GetTxCmd() *cobra.Command { 86 evidenceCLIHandlers := make([]*cobra.Command, len(a.evidenceHandlers)) 87 88 for i, evidenceHandler := range a.evidenceHandlers { 89 evidenceCLIHandlers[i] = evidenceHandler.CLIHandler() 90 } 91 92 return cli.GetTxCmd(evidenceCLIHandlers) 93 } 94 95 // GetQueryCmd returns the evidence module's root query command. 96 func (AppModuleBasic) GetQueryCmd() *cobra.Command { 97 return cli.GetQueryCmd() 98 } 99 100 func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { 101 types.RegisterInterfaces(registry) 102 } 103 104 // ---------------------------------------------------------------------------- 105 // AppModule 106 // ---------------------------------------------------------------------------- 107 108 // AppModule implements the AppModule interface for the evidence module. 109 type AppModule struct { 110 AppModuleBasic 111 112 keeper keeper.Keeper 113 } 114 115 func NewAppModule(keeper keeper.Keeper) AppModule { 116 return AppModule{ 117 AppModuleBasic: AppModuleBasic{}, 118 keeper: keeper, 119 } 120 } 121 122 // Name returns the evidence module's name. 123 func (am AppModule) Name() string { 124 return am.AppModuleBasic.Name() 125 } 126 127 // Route returns the evidence module's message routing key. 128 func (am AppModule) Route() sdk.Route { 129 return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper)) 130 } 131 132 // QuerierRoute returns the evidence module's query routing key. 133 func (AppModule) QuerierRoute() string { 134 return types.QuerierRoute 135 } 136 137 // LegacyQuerierHandler returns the evidence module's Querier. 138 func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { 139 return keeper.NewQuerier(am.keeper, legacyQuerierCdc) 140 } 141 142 // RegisterServices registers module services. 143 func (am AppModule) RegisterServices(cfg module.Configurator) { 144 types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) 145 types.RegisterQueryServer(cfg.QueryServer(), am.keeper) 146 147 // m := keeper.NewMigrator(am.keeper) 148 // if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { 149 // panic(fmt.Sprintf("failed to migrate x/evidence from version 1 to 2: %v", err)) 150 // } 151 } 152 153 // RegisterInvariants registers the evidence module's invariants. 154 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {} 155 156 // InitGenesis performs the evidence module's genesis initialization It returns 157 // no validator updates. 158 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate { 159 var gs types.GenesisState 160 err := cdc.UnmarshalJSON(bz, &gs) 161 if err != nil { 162 panic(fmt.Sprintf("failed to unmarshal %s genesis state: %s", types.ModuleName, err)) 163 } 164 165 InitGenesis(ctx, am.keeper, &gs) 166 return []abci.ValidatorUpdate{} 167 } 168 169 // ExportGenesis returns the evidence module's exported genesis state as raw JSON bytes. 170 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 171 return cdc.MustMarshalJSON(ExportGenesis(ctx, am.keeper)) 172 } 173 174 // ConsensusVersion implements AppModule/ConsensusVersion. 175 func (AppModule) ConsensusVersion() uint64 { return 1 } 176 177 // BeginBlock executes all ABCI BeginBlock logic respective to the evidence module. 178 func (am AppModule) BeginBlock(ctx sdk.Context, req ocabci.RequestBeginBlock) { 179 BeginBlocker(ctx, req, am.keeper) 180 } 181 182 // AppModuleSimulation functions 183 184 // GenerateGenesisState creates a randomized GenState of the evidence module. 185 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 186 simulation.RandomizedGenState(simState) 187 } 188 189 // ProposalContents returns all the evidence content functions used to 190 // simulate governance proposals. 191 func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { 192 return nil 193 } 194 195 // RandomizedParams creates randomized evidence param changes for the simulator. 196 func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { 197 return nil 198 } 199 200 // RegisterStoreDecoder registers a decoder for evidence module's types 201 func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 202 sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper) 203 } 204 205 // WeightedOperations returns the all the gov module operations with their respective weights. 206 func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { 207 return nil 208 }