2019-05-16 08:25:32 -07:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-01-09 12:14:23 -08:00
|
|
|
"fmt"
|
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"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
"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"
|
2020-03-23 04:55:44 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/client/cli"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/simulation"
|
2019-06-28 13:11:27 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2019-12-05 01:29:54 -08:00
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2019-05-16 08:25:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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 distribution module.
|
2019-05-16 08:25:32 -07:00
|
|
|
type AppModuleBasic struct{}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the distribution 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 distribution 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 distribution
|
|
|
|
// module.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
|
|
|
|
return cdc.MustMarshalJSON(DefaultGenesisState())
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ValidateGenesis performs genesis state validation for the distribution module.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error {
|
2019-05-16 08:25:32 -07:00
|
|
|
var data GenesisState
|
2020-02-18 04:50:13 -08:00
|
|
|
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
|
2020-01-09 12:14:23 -08:00
|
|
|
return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
2020-01-09 12:14:23 -08:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
return ValidateGenesis(data)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterRESTRoutes registers the REST routes for the distribution module.
|
2019-06-06 08:43:15 -07:00
|
|
|
func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
|
|
|
|
rest.RegisterRoutes(ctx, rtr, StoreKey)
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetTxCmd returns the root tx command for the distribution 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 the root query command for the distribution module.
|
2019-06-05 16:26:17 -07:00
|
|
|
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return cli.GetQueryCmd(StoreKey, cdc)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
//____________________________________________________________________________
|
|
|
|
|
|
|
|
// AppModule implements an application module for the distribution module.
|
2019-05-16 08:25:32 -07:00
|
|
|
type AppModule struct {
|
|
|
|
AppModuleBasic
|
2019-08-13 15:16:03 -07:00
|
|
|
|
2019-12-05 01:29:54 -08:00
|
|
|
keeper Keeper
|
|
|
|
accountKeeper types.AccountKeeper
|
2020-01-30 13:31:16 -08:00
|
|
|
bankKeeper types.BankKeeper
|
2019-12-05 01:29:54 -08:00
|
|
|
stakingKeeper stakingkeeper.Keeper
|
|
|
|
supplyKeeper types.SupplyKeeper
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppModule creates a new AppModule object
|
2020-01-30 13:31:16 -08:00
|
|
|
func NewAppModule(
|
|
|
|
keeper Keeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper,
|
|
|
|
supplyKeeper types.SupplyKeeper, stakingKeeper stakingkeeper.Keeper,
|
|
|
|
) 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,
|
2020-01-30 13:31:16 -08:00
|
|
|
bankKeeper: bankKeeper,
|
2019-12-05 01:29:54 -08:00
|
|
|
supplyKeeper: supplyKeeper,
|
|
|
|
stakingKeeper: stakingKeeper,
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the distribution 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 distribution module invariants.
|
2019-06-06 13:32:38 -07:00
|
|
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
2019-05-16 08:25:32 -07:00
|
|
|
RegisterInvariants(ir, am.keeper)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Route returns the message routing key for the distribution 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 distribution 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 distribution 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 distribution 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 distribution module. It returns
|
|
|
|
// no validator updates.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
|
2019-05-16 08:25:32 -07:00
|
|
|
var genesisState GenesisState
|
2020-02-18 04:50:13 -08:00
|
|
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
2020-01-30 13:31:16 -08:00
|
|
|
InitGenesis(ctx, am.bankKeeper, am.supplyKeeper, am.keeper, genesisState)
|
2019-05-16 08:25:32 -07:00
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the distribution
|
|
|
|
// module.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
|
2019-05-16 08:25:32 -07:00
|
|
|
gs := ExportGenesis(ctx, am.keeper)
|
2020-02-18 04:50:13 -08:00
|
|
|
return cdc.MustMarshalJSON(gs)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// BeginBlock returns the begin blocker for the distribution module.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
|
2019-05-16 08:25:32 -07:00
|
|
|
BeginBlocker(ctx, req, am.keeper)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// EndBlock returns the end blocker for the distribution module. It returns no validator
|
|
|
|
// updates.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
|
|
|
|
return []abci.ValidatorUpdate{}
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
2019-12-05 01:29:54 -08:00
|
|
|
|
|
|
|
//____________________________________________________________________________
|
|
|
|
|
|
|
|
// AppModuleSimulation functions
|
|
|
|
|
|
|
|
// GenerateGenesisState creates a randomized GenState of the distribution module.
|
|
|
|
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
|
|
|
|
simulation.RandomizedGenState(simState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProposalContents returns all the distribution content functions used to
|
|
|
|
// simulate governance proposals.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
|
2019-12-05 01:29:54 -08:00
|
|
|
return simulation.ProposalContents(am.keeper)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomizedParams creates randomized distribution param changes for the simulator.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
|
2019-12-05 01:29:54 -08:00
|
|
|
return simulation.ParamChanges(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStoreDecoder registers a decoder for distribution module's types
|
|
|
|
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
|
|
|
|
sdr[StoreKey] = simulation.DecodeStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// WeightedOperations returns the all the gov module operations with their respective weights.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
|
2020-01-30 13:31:16 -08:00
|
|
|
return simulation.WeightedOperations(
|
|
|
|
simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper,
|
|
|
|
)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|