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

     1  package mint
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
     9  
    10  	modulev1 "cosmossdk.io/api/cosmos/mint/module/v1"
    11  	"cosmossdk.io/core/appmodule"
    12  	"cosmossdk.io/core/store"
    13  	"cosmossdk.io/depinject"
    14  
    15  	"github.com/cosmos/cosmos-sdk/client"
    16  	"github.com/cosmos/cosmos-sdk/codec"
    17  	cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
    18  	sdk "github.com/cosmos/cosmos-sdk/types"
    19  	"github.com/cosmos/cosmos-sdk/types/module"
    20  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    21  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
    22  	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
    23  	"github.com/cosmos/cosmos-sdk/x/mint/exported"
    24  	"github.com/cosmos/cosmos-sdk/x/mint/keeper"
    25  	"github.com/cosmos/cosmos-sdk/x/mint/simulation"
    26  	"github.com/cosmos/cosmos-sdk/x/mint/types"
    27  )
    28  
    29  // ConsensusVersion defines the current x/mint module consensus version.
    30  const ConsensusVersion = 2
    31  
    32  var (
    33  	_ module.AppModuleBasic      = AppModule{}
    34  	_ module.AppModuleSimulation = AppModule{}
    35  	_ module.HasGenesis          = AppModule{}
    36  	_ module.HasServices         = AppModule{}
    37  
    38  	_ appmodule.AppModule       = AppModule{}
    39  	_ appmodule.HasBeginBlocker = AppModule{}
    40  )
    41  
    42  // AppModuleBasic defines the basic application module used by the mint module.
    43  type AppModuleBasic struct {
    44  	cdc codec.Codec
    45  }
    46  
    47  // Name returns the mint module's name.
    48  func (AppModuleBasic) Name() string {
    49  	return types.ModuleName
    50  }
    51  
    52  // RegisterLegacyAminoCodec registers the mint module's types on the given LegacyAmino codec.
    53  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    54  	types.RegisterLegacyAminoCodec(cdc)
    55  }
    56  
    57  // RegisterInterfaces registers the module's interface types
    58  func (b AppModuleBasic) RegisterInterfaces(r cdctypes.InterfaceRegistry) {
    59  	types.RegisterInterfaces(r)
    60  }
    61  
    62  // DefaultGenesis returns default genesis state as raw bytes for the mint
    63  // module.
    64  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    65  	return cdc.MustMarshalJSON(types.DefaultGenesisState())
    66  }
    67  
    68  // ValidateGenesis performs genesis state validation for the mint module.
    69  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
    70  	var data types.GenesisState
    71  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    72  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    73  	}
    74  
    75  	return types.ValidateGenesis(data)
    76  }
    77  
    78  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the mint module.
    79  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
    80  	if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
    81  		panic(err)
    82  	}
    83  }
    84  
    85  // AppModule implements an application module for the mint module.
    86  type AppModule struct {
    87  	AppModuleBasic
    88  
    89  	keeper     keeper.Keeper
    90  	authKeeper types.AccountKeeper
    91  
    92  	// legacySubspace is used solely for migration of x/params managed parameters
    93  	legacySubspace exported.Subspace
    94  
    95  	// inflationCalculator is used to calculate the inflation rate during BeginBlock.
    96  	// If inflationCalculator is nil, the default inflation calculation logic is used.
    97  	inflationCalculator types.InflationCalculationFn
    98  }
    99  
   100  // NewAppModule creates a new AppModule object. If the InflationCalculationFn
   101  // argument is nil, then the SDK's default inflation function will be used.
   102  func NewAppModule(
   103  	cdc codec.Codec,
   104  	keeper keeper.Keeper,
   105  	ak types.AccountKeeper,
   106  	ic types.InflationCalculationFn,
   107  	ss exported.Subspace,
   108  ) AppModule {
   109  	if ic == nil {
   110  		ic = types.DefaultInflationCalculationFn
   111  	}
   112  
   113  	return AppModule{
   114  		AppModuleBasic:      AppModuleBasic{cdc: cdc},
   115  		keeper:              keeper,
   116  		authKeeper:          ak,
   117  		inflationCalculator: ic,
   118  		legacySubspace:      ss,
   119  	}
   120  }
   121  
   122  // IsOnePerModuleType implements the depinject.OnePerModuleType interface.
   123  func (am AppModule) IsOnePerModuleType() {}
   124  
   125  // IsAppModule implements the appmodule.AppModule interface.
   126  func (am AppModule) IsAppModule() {}
   127  
   128  // RegisterServices registers a gRPC query service to respond to the
   129  // module-specific gRPC queries.
   130  func (am AppModule) RegisterServices(cfg module.Configurator) {
   131  	types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
   132  	types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
   133  
   134  	m := keeper.NewMigrator(am.keeper, am.legacySubspace)
   135  
   136  	if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
   137  		panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
   138  	}
   139  }
   140  
   141  // InitGenesis performs genesis initialization for the mint module. It returns
   142  // no validator updates.
   143  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
   144  	var genesisState types.GenesisState
   145  	cdc.MustUnmarshalJSON(data, &genesisState)
   146  
   147  	am.keeper.InitGenesis(ctx, am.authKeeper, &genesisState)
   148  }
   149  
   150  // ExportGenesis returns the exported genesis state as raw bytes for the mint
   151  // module.
   152  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   153  	gs := am.keeper.ExportGenesis(ctx)
   154  	return cdc.MustMarshalJSON(gs)
   155  }
   156  
   157  // ConsensusVersion implements AppModule/ConsensusVersion.
   158  func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
   159  
   160  // BeginBlock returns the begin blocker for the mint module.
   161  func (am AppModule) BeginBlock(ctx context.Context) error {
   162  	return BeginBlocker(ctx, am.keeper, am.inflationCalculator)
   163  }
   164  
   165  // AppModuleSimulation functions
   166  
   167  // GenerateGenesisState creates a randomized GenState of the mint module.
   168  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   169  	simulation.RandomizedGenState(simState)
   170  }
   171  
   172  // ProposalMsgs returns msgs used for governance proposals for simulations.
   173  func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
   174  	return simulation.ProposalMsgs()
   175  }
   176  
   177  // RegisterStoreDecoder registers a decoder for mint module's types.
   178  func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
   179  	sdr[types.StoreKey] = simtypes.NewStoreDecoderFuncFromCollectionsSchema(am.keeper.Schema)
   180  }
   181  
   182  // WeightedOperations doesn't return any mint module operation.
   183  func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation {
   184  	return nil
   185  }
   186  
   187  //
   188  // App Wiring Setup
   189  //
   190  
   191  func init() {
   192  	appmodule.Register(&modulev1.Module{},
   193  		appmodule.Provide(ProvideModule),
   194  	)
   195  }
   196  
   197  type ModuleInputs struct {
   198  	depinject.In
   199  
   200  	ModuleKey              depinject.OwnModuleKey
   201  	Config                 *modulev1.Module
   202  	StoreService           store.KVStoreService
   203  	Cdc                    codec.Codec
   204  	InflationCalculationFn types.InflationCalculationFn `optional:"true"`
   205  
   206  	// LegacySubspace is used solely for migration of x/params managed parameters
   207  	LegacySubspace exported.Subspace `optional:"true"`
   208  
   209  	AccountKeeper types.AccountKeeper
   210  	BankKeeper    types.BankKeeper
   211  	StakingKeeper types.StakingKeeper
   212  }
   213  
   214  type ModuleOutputs struct {
   215  	depinject.Out
   216  
   217  	MintKeeper keeper.Keeper
   218  	Module     appmodule.AppModule
   219  }
   220  
   221  func ProvideModule(in ModuleInputs) ModuleOutputs {
   222  	feeCollectorName := in.Config.FeeCollectorName
   223  	if feeCollectorName == "" {
   224  		feeCollectorName = authtypes.FeeCollectorName
   225  	}
   226  
   227  	// default to governance authority if not provided
   228  	authority := authtypes.NewModuleAddress(govtypes.ModuleName)
   229  	if in.Config.Authority != "" {
   230  		authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
   231  	}
   232  
   233  	k := keeper.NewKeeper(
   234  		in.Cdc,
   235  		in.StoreService,
   236  		in.StakingKeeper,
   237  		in.AccountKeeper,
   238  		in.BankKeeper,
   239  		feeCollectorName,
   240  		authority.String(),
   241  	)
   242  
   243  	// when no inflation calculation function is provided it will use the default types.DefaultInflationCalculationFn
   244  	m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.InflationCalculationFn, in.LegacySubspace)
   245  
   246  	return ModuleOutputs{MintKeeper: k, Module: m}
   247  }