2019-05-16 08:25:32 -07:00
|
|
|
package staking
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-08-28 07:58:25 -07:00
|
|
|
"math/rand"
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/cobra"
|
2019-05-28 01:44:04 -07:00
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-08-28 07:58:25 -07:00
|
|
|
sim "github.com/cosmos/cosmos-sdk/x/simulation"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/client/rest"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-13 15:16:03 -07:00
|
|
|
_ module.AppModule = AppModule{}
|
|
|
|
_ module.AppModuleBasic = AppModuleBasic{}
|
2019-12-05 01:29:54 -08:00
|
|
|
_ module.AppModuleSimulation = AppModule{}
|
2019-05-16 08:25:32 -07:00
|
|
|
)
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// AppModuleBasic defines the basic application module used by the staking module.
|
2019-05-16 08:25:32 -07:00
|
|
|
type AppModuleBasic struct{}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
var _ module.AppModuleBasic = AppModuleBasic{}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the staking module's name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModuleBasic) Name() string {
|
|
|
|
return ModuleName
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterCodec registers the staking module's types for the given codec.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
|
|
|
|
RegisterCodec(cdc)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// DefaultGenesis returns default genesis state as raw bytes for the staking
|
|
|
|
// module.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModuleBasic) DefaultGenesis() json.RawMessage {
|
|
|
|
return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ValidateGenesis performs genesis state validation for the staking module.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
|
|
|
|
var data GenesisState
|
|
|
|
err := ModuleCdc.UnmarshalJSON(bz, &data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ValidateGenesis(data)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterRESTRoutes registers the REST routes for the staking module.
|
2019-06-06 08:43:15 -07:00
|
|
|
func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
|
|
|
|
rest.RegisterRoutes(ctx, rtr)
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetTxCmd returns the root tx command for the staking module.
|
2019-06-05 16:26:17 -07:00
|
|
|
func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return cli.GetTxCmd(StoreKey, cdc)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetQueryCmd returns no root query command for the staking module.
|
2019-06-05 16:26:17 -07:00
|
|
|
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return cli.GetQueryCmd(StoreKey, cdc)
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
//_____________________________________
|
|
|
|
// extra helpers
|
|
|
|
|
|
|
|
// CreateValidatorMsgHelpers - used for gen-tx
|
|
|
|
func (AppModuleBasic) CreateValidatorMsgHelpers(ipDefault string) (
|
|
|
|
fs *flag.FlagSet, nodeIDFlag, pubkeyFlag, amountFlag, defaultsDesc string) {
|
|
|
|
return cli.CreateValidatorMsgHelpers(ipDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareFlagsForTxCreateValidator - used for gen-tx
|
|
|
|
func (AppModuleBasic) PrepareFlagsForTxCreateValidator(config *cfg.Config, nodeID,
|
|
|
|
chainID string, valPubKey crypto.PubKey) {
|
|
|
|
cli.PrepareFlagsForTxCreateValidator(config, nodeID, chainID, valPubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildCreateValidatorMsg - used for gen-tx
|
|
|
|
func (AppModuleBasic) BuildCreateValidatorMsg(cliCtx context.CLIContext,
|
2019-06-05 16:26:17 -07:00
|
|
|
txBldr authtypes.TxBuilder) (authtypes.TxBuilder, sdk.Msg, error) {
|
2019-05-28 01:44:04 -07:00
|
|
|
return cli.BuildCreateValidatorMsg(cliCtx, txBldr)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
//____________________________________________________________________________
|
|
|
|
|
|
|
|
// AppModule implements an application module for the staking module.
|
2019-05-16 08:25:32 -07:00
|
|
|
type AppModule struct {
|
|
|
|
AppModuleBasic
|
2019-08-13 15:16:03 -07:00
|
|
|
|
2019-08-26 07:57:21 -07:00
|
|
|
keeper Keeper
|
|
|
|
accountKeeper types.AccountKeeper
|
|
|
|
supplyKeeper types.SupplyKeeper
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppModule creates a new AppModule object
|
2019-08-26 07:57:21 -07:00
|
|
|
func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, supplyKeeper types.SupplyKeeper) AppModule {
|
2019-05-16 08:25:32 -07:00
|
|
|
|
|
|
|
return AppModule{
|
2019-12-05 01:29:54 -08:00
|
|
|
AppModuleBasic: AppModuleBasic{},
|
|
|
|
keeper: keeper,
|
|
|
|
accountKeeper: accountKeeper,
|
|
|
|
supplyKeeper: supplyKeeper,
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the staking module's name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) Name() string {
|
|
|
|
return ModuleName
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterInvariants registers the staking module invariants.
|
2019-06-06 13:32:38 -07:00
|
|
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
2019-06-28 13:11:27 -07:00
|
|
|
RegisterInvariants(ir, am.keeper)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Route returns the message routing key for the staking module.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) Route() string {
|
|
|
|
return RouterKey
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// NewHandler returns an sdk.Handler for the staking module.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (am AppModule) NewHandler() sdk.Handler {
|
|
|
|
return NewHandler(am.keeper)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// QuerierRoute returns the staking module's querier route name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) QuerierRoute() string {
|
|
|
|
return QuerierRoute
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// NewQuerierHandler returns the staking module sdk.Querier.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (am AppModule) NewQuerierHandler() sdk.Querier {
|
|
|
|
return NewQuerier(am.keeper)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// InitGenesis performs genesis initialization for the staking module. It returns
|
|
|
|
// no validator updates.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
|
|
|
|
var genesisState GenesisState
|
|
|
|
ModuleCdc.MustUnmarshalJSON(data, &genesisState)
|
2019-08-26 07:57:21 -07:00
|
|
|
return InitGenesis(ctx, am.keeper, am.accountKeeper, am.supplyKeeper, genesisState)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the staking
|
|
|
|
// module.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
|
|
|
|
gs := ExportGenesis(ctx, am.keeper)
|
|
|
|
return ModuleCdc.MustMarshalJSON(gs)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// BeginBlock returns the begin blocker for the staking module.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// EndBlock returns the end blocker for the staking module. It returns no validator
|
|
|
|
// updates.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
|
2019-05-16 08:25:32 -07:00
|
|
|
return EndBlocker(ctx, am.keeper)
|
|
|
|
}
|
2019-12-05 01:29:54 -08:00
|
|
|
|
|
|
|
//____________________________________________________________________________
|
|
|
|
|
|
|
|
// AppModuleSimulation functions
|
|
|
|
|
|
|
|
// GenerateGenesisState creates a randomized GenState of the staking module.
|
|
|
|
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
|
|
|
|
simulation.RandomizedGenState(simState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProposalContents doesn't return any content functions for governance proposals.
|
|
|
|
func (AppModule) ProposalContents(_ module.SimulationState) []sim.WeightedProposalContent {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomizedParams creates randomized staking param changes for the simulator.
|
|
|
|
func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange {
|
|
|
|
return simulation.ParamChanges(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStoreDecoder registers a decoder for staking module's types
|
|
|
|
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
|
|
|
|
sdr[StoreKey] = simulation.DecodeStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// WeightedOperations returns the all the staking module operations with their respective weights.
|
|
|
|
func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation {
|
|
|
|
return simulation.WeightedOperations(simState.AppParams, simState.Cdc,
|
|
|
|
am.accountKeeper, am.keeper)
|
|
|
|
}
|