github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/gov/module.go (about)

     1  package gov
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"math/rand"
     9  
    10  	"github.com/gorilla/mux"
    11  	"github.com/spf13/cobra"
    12  
    13  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    14  
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    16  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    17  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    18  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    19  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/client"
    20  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/client/cli"
    21  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/client/rest"
    22  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/simulation"
    23  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types"
    24  	sim "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
    25  )
    26  
    27  var (
    28  	_ module.AppModule           = AppModule{}
    29  	_ module.AppModuleBasic      = AppModuleBasic{}
    30  	_ module.AppModuleSimulation = AppModule{}
    31  )
    32  
    33  // AppModuleBasic defines the basic application module used by the gov module.
    34  type AppModuleBasic struct {
    35  	proposalHandlers []client.ProposalHandler // proposal handlers which live in governance cli and rest
    36  }
    37  
    38  // NewAppModuleBasic creates a new AppModuleBasic object
    39  func NewAppModuleBasic(proposalHandlers ...client.ProposalHandler) AppModuleBasic {
    40  	return AppModuleBasic{
    41  		proposalHandlers: proposalHandlers,
    42  	}
    43  }
    44  
    45  // Name returns the gov module's name.
    46  func (AppModuleBasic) Name() string {
    47  	return types.ModuleName
    48  }
    49  
    50  // RegisterCodec registers the gov module's types for the given codec.
    51  func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
    52  	RegisterCodec(cdc)
    53  }
    54  
    55  // DefaultGenesis returns default genesis state as raw bytes for the gov
    56  // module.
    57  func (AppModuleBasic) DefaultGenesis() json.RawMessage {
    58  	return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
    59  }
    60  
    61  // ValidateGenesis performs genesis state validation for the gov module.
    62  func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
    63  	var data GenesisState
    64  	if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil {
    65  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    66  	}
    67  
    68  	return ValidateGenesis(data)
    69  }
    70  
    71  // RegisterRESTRoutes registers the REST routes for the gov module.
    72  func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
    73  	proposalRESTHandlers := make([]rest.ProposalRESTHandler, 0, len(a.proposalHandlers))
    74  	for _, proposalHandler := range a.proposalHandlers {
    75  		proposalRESTHandlers = append(proposalRESTHandlers, proposalHandler.RESTHandler(ctx))
    76  	}
    77  
    78  	rest.RegisterRoutes(ctx, rtr, proposalRESTHandlers)
    79  }
    80  
    81  // GetTxCmd returns the root tx command for the gov module.
    82  func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
    83  
    84  	proposalCLIHandlers := make([]*cobra.Command, 0, len(a.proposalHandlers))
    85  	for _, proposalHandler := range a.proposalHandlers {
    86  		proposalCLIHandlers = append(proposalCLIHandlers, proposalHandler.CLIHandler(cdc))
    87  	}
    88  
    89  	return cli.GetTxCmd(StoreKey, cdc, proposalCLIHandlers)
    90  }
    91  
    92  // GetQueryCmd returns the root query command for the gov module.
    93  func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    94  	return cli.GetQueryCmd(StoreKey, cdc)
    95  }
    96  
    97  //____________________________________________________________________________
    98  
    99  // AppModule implements an application module for the gov module.
   100  type AppModule struct {
   101  	AppModuleBasic
   102  
   103  	keeper        Keeper
   104  	accountKeeper types.AccountKeeper
   105  	supplyKeeper  types.SupplyKeeper
   106  }
   107  
   108  // NewAppModule creates a new AppModule object
   109  func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, supplyKeeper types.SupplyKeeper) AppModule {
   110  	return AppModule{
   111  		AppModuleBasic: AppModuleBasic{},
   112  		keeper:         keeper,
   113  		accountKeeper:  accountKeeper,
   114  		supplyKeeper:   supplyKeeper,
   115  	}
   116  }
   117  
   118  // Name returns the gov module's name.
   119  func (AppModule) Name() string {
   120  	return ModuleName
   121  }
   122  
   123  // RegisterInvariants registers module invariants
   124  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
   125  	RegisterInvariants(ir, am.keeper)
   126  }
   127  
   128  // Route returns the message routing key for the gov module.
   129  func (AppModule) Route() string {
   130  	return RouterKey
   131  }
   132  
   133  // NewHandler returns an sdk.Handler for the gov module.
   134  func (am AppModule) NewHandler() sdk.Handler {
   135  	return NewHandler(am.keeper)
   136  }
   137  
   138  // QuerierRoute returns the gov module's querier route name.
   139  func (AppModule) QuerierRoute() string {
   140  	return QuerierRoute
   141  }
   142  
   143  // NewQuerierHandler returns no sdk.Querier.
   144  func (am AppModule) NewQuerierHandler() sdk.Querier {
   145  	return NewQuerier(am.keeper)
   146  }
   147  
   148  // InitGenesis performs genesis initialization for the gov module. It returns
   149  // no validator updates.
   150  func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
   151  	var genesisState GenesisState
   152  	ModuleCdc.MustUnmarshalJSON(data, &genesisState)
   153  	InitGenesis(ctx, am.keeper, am.supplyKeeper, genesisState)
   154  	return []abci.ValidatorUpdate{}
   155  }
   156  
   157  // ExportGenesis returns the exported genesis state as raw bytes for the gov
   158  // module.
   159  func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
   160  	gs := ExportGenesis(ctx, am.keeper)
   161  	return ModuleCdc.MustMarshalJSON(gs)
   162  }
   163  
   164  // BeginBlock performs a no-op.
   165  func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
   166  
   167  // EndBlock returns the end blocker for the gov module. It returns no validator
   168  // updates.
   169  func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   170  	EndBlocker(ctx, am.keeper)
   171  	return []abci.ValidatorUpdate{}
   172  }
   173  
   174  //____________________________________________________________________________
   175  
   176  // AppModuleSimulation functions
   177  
   178  // GenerateGenesisState creates a randomized GenState of the gov module.
   179  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   180  	simulation.RandomizedGenState(simState)
   181  }
   182  
   183  // ProposalContents returns all the gov content functions used to
   184  // simulate governance proposals.
   185  func (AppModule) ProposalContents(_ module.SimulationState) []sim.WeightedProposalContent {
   186  	return simulation.ProposalContents()
   187  }
   188  
   189  // RandomizedParams creates randomized gov param changes for the simulator.
   190  func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange {
   191  	return simulation.ParamChanges(r)
   192  }
   193  
   194  // RegisterStoreDecoder registers a decoder for gov module's types
   195  func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   196  	sdr[StoreKey] = simulation.DecodeStore
   197  }
   198  
   199  // WeightedOperations returns the all the gov module operations with their respective weights.
   200  func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation {
   201  	return simulation.WeightedOperations(
   202  		simState.AppParams, simState.Cdc,
   203  		am.accountKeeper, am.keeper, simState.Contents)
   204  }