github.com/Finschia/finschia-sdk@v0.49.1/x/evidence/module.go (about)

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