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

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