github.com/cosmos/cosmos-sdk@v0.50.10/x/distribution/module.go (about)

     1  package distribution
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
     9  	"github.com/spf13/cobra"
    10  
    11  	modulev1 "cosmossdk.io/api/cosmos/distribution/module/v1"
    12  	"cosmossdk.io/core/address"
    13  	"cosmossdk.io/core/appmodule"
    14  	"cosmossdk.io/core/store"
    15  	"cosmossdk.io/depinject"
    16  
    17  	sdkclient "github.com/cosmos/cosmos-sdk/client"
    18  	"github.com/cosmos/cosmos-sdk/codec"
    19  	cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
    20  	sdk "github.com/cosmos/cosmos-sdk/types"
    21  	"github.com/cosmos/cosmos-sdk/types/module"
    22  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    23  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
    24  	"github.com/cosmos/cosmos-sdk/x/distribution/client/cli"
    25  	"github.com/cosmos/cosmos-sdk/x/distribution/exported"
    26  	"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
    27  	"github.com/cosmos/cosmos-sdk/x/distribution/simulation"
    28  	"github.com/cosmos/cosmos-sdk/x/distribution/types"
    29  	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
    30  	staking "github.com/cosmos/cosmos-sdk/x/staking/types"
    31  )
    32  
    33  // ConsensusVersion defines the current x/distribution module consensus version.
    34  const ConsensusVersion = 3
    35  
    36  var (
    37  	_ module.AppModuleBasic      = AppModule{}
    38  	_ module.AppModuleSimulation = AppModule{}
    39  	_ module.HasGenesis          = AppModule{}
    40  	_ module.HasServices         = AppModule{}
    41  	_ module.HasInvariants       = AppModule{}
    42  
    43  	_ appmodule.AppModule       = AppModule{}
    44  	_ appmodule.HasBeginBlocker = AppModule{}
    45  )
    46  
    47  // AppModuleBasic defines the basic application module used by the distribution module.
    48  type AppModuleBasic struct {
    49  	cdc codec.Codec
    50  	ac  address.Codec
    51  }
    52  
    53  // Name returns the distribution module's name.
    54  func (AppModuleBasic) Name() string {
    55  	return types.ModuleName
    56  }
    57  
    58  // RegisterLegacyAminoCodec registers the distribution module's types for the given codec.
    59  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    60  	types.RegisterLegacyAminoCodec(cdc)
    61  }
    62  
    63  // DefaultGenesis returns default genesis state as raw bytes for the distribution
    64  // module.
    65  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    66  	return cdc.MustMarshalJSON(types.DefaultGenesisState())
    67  }
    68  
    69  // ValidateGenesis performs genesis state validation for the distribution module.
    70  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ sdkclient.TxEncodingConfig, bz json.RawMessage) error {
    71  	var data types.GenesisState
    72  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    73  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    74  	}
    75  
    76  	return types.ValidateGenesis(&data)
    77  }
    78  
    79  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the distribution module.
    80  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *gwruntime.ServeMux) {
    81  	if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
    82  		panic(err)
    83  	}
    84  }
    85  
    86  // GetTxCmd returns the root tx command for the distribution module.
    87  func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
    88  	return cli.NewTxCmd(ab.cdc.InterfaceRegistry().SigningContext().ValidatorAddressCodec(), ab.cdc.InterfaceRegistry().SigningContext().AddressCodec())
    89  }
    90  
    91  // RegisterInterfaces implements InterfaceModule
    92  func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
    93  	types.RegisterInterfaces(registry)
    94  }
    95  
    96  // AppModule implements an application module for the distribution module.
    97  type AppModule struct {
    98  	AppModuleBasic
    99  
   100  	keeper        keeper.Keeper
   101  	accountKeeper types.AccountKeeper
   102  	bankKeeper    types.BankKeeper
   103  	stakingKeeper types.StakingKeeper
   104  
   105  	// legacySubspace is used solely for migration of x/params managed parameters
   106  	legacySubspace exported.Subspace
   107  }
   108  
   109  // NewAppModule creates a new AppModule object
   110  func NewAppModule(
   111  	cdc codec.Codec, keeper keeper.Keeper, accountKeeper types.AccountKeeper,
   112  	bankKeeper types.BankKeeper, stakingKeeper types.StakingKeeper, ss exported.Subspace,
   113  ) AppModule {
   114  	return AppModule{
   115  		AppModuleBasic: AppModuleBasic{cdc: cdc, ac: accountKeeper.AddressCodec()},
   116  		keeper:         keeper,
   117  		accountKeeper:  accountKeeper,
   118  		bankKeeper:     bankKeeper,
   119  		stakingKeeper:  stakingKeeper,
   120  		legacySubspace: ss,
   121  	}
   122  }
   123  
   124  // IsOnePerModuleType implements the depinject.OnePerModuleType interface.
   125  func (am AppModule) IsOnePerModuleType() {}
   126  
   127  // IsAppModule implements the appmodule.AppModule interface.
   128  func (am AppModule) IsAppModule() {}
   129  
   130  // RegisterInvariants registers the distribution module invariants.
   131  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
   132  	keeper.RegisterInvariants(ir, am.keeper)
   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(), keeper.NewQuerier(am.keeper))
   139  
   140  	m := keeper.NewMigrator(am.keeper, am.legacySubspace)
   141  	if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
   142  		panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
   143  	}
   144  
   145  	if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
   146  		panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err))
   147  	}
   148  }
   149  
   150  // InitGenesis performs genesis initialization for the distribution module. It returns
   151  // no validator updates.
   152  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
   153  	var genesisState types.GenesisState
   154  	cdc.MustUnmarshalJSON(data, &genesisState)
   155  	am.keeper.InitGenesis(ctx, genesisState)
   156  }
   157  
   158  // ExportGenesis returns the exported genesis state as raw bytes for the distribution
   159  // module.
   160  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   161  	gs := am.keeper.ExportGenesis(ctx)
   162  	return cdc.MustMarshalJSON(gs)
   163  }
   164  
   165  // ConsensusVersion implements AppModule/ConsensusVersion.
   166  func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
   167  
   168  // BeginBlock returns the begin blocker for the distribution module.
   169  func (am AppModule) BeginBlock(ctx context.Context) error {
   170  	c := sdk.UnwrapSDKContext(ctx)
   171  	return BeginBlocker(c, am.keeper)
   172  }
   173  
   174  // AppModuleSimulation functions
   175  
   176  // GenerateGenesisState creates a randomized GenState of the distribution module.
   177  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   178  	simulation.RandomizedGenState(simState)
   179  }
   180  
   181  // ProposalMsgs returns msgs used for governance proposals for simulations.
   182  func (AppModule) ProposalMsgs(_ module.SimulationState) []simtypes.WeightedProposalMsg {
   183  	return simulation.ProposalMsgs()
   184  }
   185  
   186  // RegisterStoreDecoder registers a decoder for distribution module's types
   187  func (am AppModule) RegisterStoreDecoder(sdr simtypes.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, simState.TxConfig,
   195  		am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper,
   196  	)
   197  }
   198  
   199  //
   200  // App Wiring Setup
   201  //
   202  
   203  func init() {
   204  	appmodule.Register(&modulev1.Module{},
   205  		appmodule.Provide(ProvideModule),
   206  	)
   207  }
   208  
   209  type ModuleInputs struct {
   210  	depinject.In
   211  
   212  	Config       *modulev1.Module
   213  	StoreService store.KVStoreService
   214  	Cdc          codec.Codec
   215  
   216  	AccountKeeper types.AccountKeeper
   217  	BankKeeper    types.BankKeeper
   218  	StakingKeeper types.StakingKeeper
   219  
   220  	// LegacySubspace is used solely for migration of x/params managed parameters
   221  	LegacySubspace exported.Subspace `optional:"true"`
   222  }
   223  
   224  type ModuleOutputs struct {
   225  	depinject.Out
   226  
   227  	DistrKeeper keeper.Keeper
   228  	Module      appmodule.AppModule
   229  	Hooks       staking.StakingHooksWrapper
   230  }
   231  
   232  func ProvideModule(in ModuleInputs) ModuleOutputs {
   233  	feeCollectorName := in.Config.FeeCollectorName
   234  	if feeCollectorName == "" {
   235  		feeCollectorName = authtypes.FeeCollectorName
   236  	}
   237  
   238  	// default to governance authority if not provided
   239  	authority := authtypes.NewModuleAddress(govtypes.ModuleName)
   240  	if in.Config.Authority != "" {
   241  		authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
   242  	}
   243  
   244  	k := keeper.NewKeeper(
   245  		in.Cdc,
   246  		in.StoreService,
   247  		in.AccountKeeper,
   248  		in.BankKeeper,
   249  		in.StakingKeeper,
   250  		feeCollectorName,
   251  		authority.String(),
   252  	)
   253  
   254  	m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.LegacySubspace)
   255  
   256  	return ModuleOutputs{
   257  		DistrKeeper: k,
   258  		Module:      m,
   259  		Hooks:       staking.StakingHooksWrapper{StakingHooks: k.Hooks()},
   260  	}
   261  }