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

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