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

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