From be67e9ac177181f7711b9b2ab226697ec03a5503 Mon Sep 17 00:00:00 2001 From: yys Date: Wed, 12 Aug 2020 16:30:28 +0900 Subject: [PATCH] append mint module --- app/app.go | 24 ++++-- app/sim_test.go | 2 + cli_test/cli_test.go | 1 + cli_test/test_helpers.go | 2 +- x/mint/cosmos_alias.go | 37 +++++++++ x/mint/module.go | 160 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 218 insertions(+), 8 deletions(-) create mode 100644 x/mint/cosmos_alias.go create mode 100644 x/mint/module.go diff --git a/app/app.go b/app/app.go index 33c2b52ff..89f113a2f 100644 --- a/app/app.go +++ b/app/app.go @@ -31,6 +31,7 @@ import ( "github.com/terra-project/core/x/genutil" "github.com/terra-project/core/x/gov" "github.com/terra-project/core/x/market" + "github.com/terra-project/core/x/mint" "github.com/terra-project/core/x/msgauth" "github.com/terra-project/core/x/oracle" "github.com/terra-project/core/x/params" @@ -65,6 +66,7 @@ var ( auth.AppModuleBasic{}, bank.AppModuleBasic{}, staking.AppModuleBasic{}, + mint.AppModuleBasic{}, distr.AppModuleBasic{}, gov.NewAppModuleBasic( paramsclient.ProposalHandler, @@ -90,6 +92,7 @@ var ( maccPerms = map[string][]string{ auth.FeeCollectorName: nil, // just added to enable align fee bank.BurnModuleName: {supply.Burner}, + mint.ModuleName: {supply.Minter}, market.ModuleName: {supply.Minter, supply.Burner}, oracle.ModuleName: nil, distr.ModuleName: nil, @@ -148,6 +151,7 @@ type TerraApp struct { evidenceKeeper evidence.Keeper oracleKeeper oracle.Keeper marketKeeper market.Keeper + mintKeeper mint.Keeper treasuryKeeper treasury.Keeper wasmKeeper wasm.Keeper msgauthKeeper msgauth.Keeper @@ -174,8 +178,9 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bam.MainStoreKey, auth.StoreKey, staking.StoreKey, supply.StoreKey, distr.StoreKey, slashing.StoreKey, gov.StoreKey, params.StoreKey, oracle.StoreKey, - market.StoreKey, treasury.StoreKey, upgrade.StoreKey, - evidence.StoreKey, wasm.StoreKey, msgauth.StoreKey, + market.StoreKey, mint.StoreKey, treasury.StoreKey, + upgrade.StoreKey, evidence.StoreKey, wasm.StoreKey, + msgauth.StoreKey, ) tkeys := sdk.NewTransientStoreKeys(params.TStoreKey) @@ -200,8 +205,9 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest app.subspaces[evidence.ModuleName] = app.paramsKeeper.Subspace(evidence.DefaultParamspace) app.subspaces[oracle.ModuleName] = app.paramsKeeper.Subspace(oracle.DefaultParamspace) app.subspaces[market.ModuleName] = app.paramsKeeper.Subspace(market.DefaultParamspace) - app.subspaces[treasury.ModuleName] = app.paramsKeeper.Subspace(treasury.DefaultParamspace) + app.subspaces[mint.ModuleName] = app.paramsKeeper.Subspace(mint.DefaultParamspace) app.subspaces[wasm.ModuleName] = app.paramsKeeper.Subspace(wasm.DefaultParamspace) + app.subspaces[treasury.ModuleName] = app.paramsKeeper.Subspace(treasury.DefaultParamspace) // add keepers app.accountKeeper = auth.NewAccountKeeper(app.cdc, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount) @@ -218,6 +224,7 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest &stakingKeeper, app.supplyKeeper, distr.ModuleName) app.marketKeeper = market.NewKeeper(app.cdc, keys[market.StoreKey], app.subspaces[market.ModuleName], app.oracleKeeper, app.supplyKeeper) + app.mintKeeper = mint.NewKeeper(app.cdc, keys[mint.StoreKey], app.subspaces[mint.ModuleName], &stakingKeeper, app.supplyKeeper, auth.FeeCollectorName) app.treasuryKeeper = treasury.NewKeeper(app.cdc, keys[treasury.StoreKey], app.subspaces[treasury.ModuleName], app.supplyKeeper, app.marketKeeper, &stakingKeeper, app.distrKeeper, oracle.ModuleName, distr.ModuleName) @@ -273,6 +280,7 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest supply.NewAppModule(app.supplyKeeper, app.accountKeeper), distr.NewAppModule(app.distrKeeper, app.accountKeeper, app.supplyKeeper, app.stakingKeeper), gov.NewAppModule(app.govKeeper, app.accountKeeper, app.supplyKeeper), + mint.NewAppModule(app.mintKeeper), slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), upgrade.NewAppModule(app.upgradeKeeper), @@ -287,7 +295,7 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest // During begin block slashing happens after distr.BeginBlocker so that // there is nothing left over in the validator fee pool, so as to keep the // CanWithdrawInvariant invariant. - app.mm.SetOrderBeginBlockers(upgrade.ModuleName, distr.ModuleName, slashing.ModuleName, evidence.ModuleName) + app.mm.SetOrderBeginBlockers(upgrade.ModuleName, mint.ModuleName, distr.ModuleName, slashing.ModuleName, evidence.ModuleName) app.mm.SetOrderEndBlockers(crisis.ModuleName, oracle.ModuleName, gov.ModuleName, market.ModuleName, treasury.ModuleName, msgauth.ModuleName, staking.ModuleName) @@ -296,9 +304,10 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest // initialized with tokens from genesis accounts. app.mm.SetOrderInitGenesis(auth.ModuleName, distr.ModuleName, staking.ModuleName, bank.ModuleName, slashing.ModuleName, - gov.ModuleName, supply.ModuleName, oracle.ModuleName, treasury.ModuleName, - market.ModuleName, wasm.ModuleName, msgauth.ModuleName, - crisis.ModuleName, genutil.ModuleName, evidence.ModuleName) + gov.ModuleName, mint.ModuleName, supply.ModuleName, + oracle.ModuleName, treasury.ModuleName, market.ModuleName, + wasm.ModuleName, msgauth.ModuleName, crisis.ModuleName, + genutil.ModuleName, evidence.ModuleName) app.mm.RegisterInvariants(&app.crisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) @@ -309,6 +318,7 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bank.NewAppModule(app.bankKeeper, app.accountKeeper, app.supplyKeeper), supply.NewAppModule(app.supplyKeeper, app.accountKeeper), gov.NewAppModule(app.govKeeper, app.accountKeeper, app.supplyKeeper), + mint.NewAppModule(app.mintKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), distr.NewAppModule(app.distrKeeper, app.accountKeeper, app.supplyKeeper, app.stakingKeeper), slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper), diff --git a/app/sim_test.go b/app/sim_test.go index c97302bab..8a3f80c9a 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -29,6 +29,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" distr "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/gov" + "github.com/cosmos/cosmos-sdk/x/mint" "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/slashing" @@ -167,6 +168,7 @@ func TestAppImportExport(t *testing.T) { {app.keys[gov.StoreKey], newApp.keys[gov.StoreKey], [][]byte{}}, {app.keys[oracle.StoreKey], newApp.keys[oracle.StoreKey], [][]byte{}}, {app.keys[market.StoreKey], newApp.keys[market.StoreKey], [][]byte{}}, + {app.keys[mint.StoreKey], newApp.keys[mint.StoreKey], [][]byte{}}, {app.keys[treasury.StoreKey], newApp.keys[treasury.StoreKey], [][]byte{}}, {app.keys[wasm.StoreKey], newApp.keys[wasm.StoreKey], [][]byte{}}, } diff --git a/cli_test/cli_test.go b/cli_test/cli_test.go index bbff8bc5e..50afd35fc 100644 --- a/cli_test/cli_test.go +++ b/cli_test/cli_test.go @@ -475,6 +475,7 @@ func TestTerraCLIQuerySupply(t *testing.T) { totalSupply := f.QueryTotalSupply() totalSupplyOf := f.QueryTotalSupplyOf(fooDenom) + fmt.Println(totalCoins, totalSupply) require.Equal(t, totalCoins, totalSupply) require.True(sdk.IntEq(t, totalCoins.AmountOf(fooDenom), totalSupplyOf)) diff --git a/cli_test/test_helpers.go b/cli_test/test_helpers.go index 6cc4e5f54..ca253356f 100644 --- a/cli_test/test_helpers.go +++ b/cli_test/test_helpers.go @@ -51,7 +51,7 @@ var ( sdk.NewCoin(fee2Denom, sdk.TokensFromConsensusPower(2000000)), sdk.NewCoin(feeDenom, sdk.TokensFromConsensusPower(2000000)), sdk.NewCoin(fooDenom, sdk.TokensFromConsensusPower(2000)), - sdk.NewCoin(denom, sdk.TokensFromConsensusPower(300)), + sdk.NewCoin(denom, sdk.TokensFromConsensusPower(300).Add(sdk.NewInt(12))), ) startCoins = sdk.NewCoins( diff --git a/x/mint/cosmos_alias.go b/x/mint/cosmos_alias.go new file mode 100644 index 000000000..f21d35488 --- /dev/null +++ b/x/mint/cosmos_alias.go @@ -0,0 +1,37 @@ +// nolint +package mint + +import ( + "github.com/cosmos/cosmos-sdk/x/mint" +) + +const ( + ModuleName = mint.ModuleName + StoreKey = mint.StoreKey + QuerierRoute = mint.QuerierRoute + QueryParameters = mint.QueryParameters + DefaultParamspace = mint.DefaultParamspace +) + +var ( + // functions aliases + NewGenesisState = mint.NewGenesisState + DefaultGenesisState = mint.DefaultGenesisState + ValidateGenesis = mint.ValidateGenesis + ParamKeyTable = mint.ParamKeyTable + NewParams = mint.NewParams + DefaultParams = mint.DefaultParams + NewCosmosAppModule = mint.NewAppModule + NewKeeper = mint.NewKeeper + + // variable aliases + CosmosModuleCdc = mint.ModuleCdc +) + +type ( + GenesisState = mint.GenesisState + Params = mint.Params + Keeper = mint.Keeper + CosmosAppModule = mint.AppModule + CosmosAppModuleBasic = mint.AppModuleBasic +) diff --git a/x/mint/module.go b/x/mint/module.go new file mode 100644 index 000000000..d996fffcb --- /dev/null +++ b/x/mint/module.go @@ -0,0 +1,160 @@ +package mint + +import ( + "encoding/json" + "math/rand" + + "github.com/gorilla/mux" + "github.com/spf13/cobra" + + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + sim "github.com/cosmos/cosmos-sdk/x/simulation" + + core "github.com/terra-project/core/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModule{} +) + +// AppModuleBasic defines the basic application module used by the slashing module. +type AppModuleBasic struct{} + +// Name returns the slashing module's name +func (AppModuleBasic) Name() string { + return CosmosAppModuleBasic{}.Name() +} + +// RegisterCodec registers the slashing module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + CosmosAppModuleBasic{}.RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the slashing +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + // customize to set default genesis state bond denom to uluna + defaultGenesisState := DefaultGenesisState() + defaultGenesisState.Params.MintDenom = core.MicroLunaDenom + + return CosmosModuleCdc.MustMarshalJSON(defaultGenesisState) +} + +// ValidateGenesis performs genesis state validation for the slashing module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + return CosmosAppModuleBasic{}.ValidateGenesis(bz) +} + +// RegisterRESTRoutes registers the REST routes for the slashing module. +func (AppModuleBasic) RegisterRESTRoutes(cliCtx context.CLIContext, route *mux.Router) { + CosmosAppModuleBasic{}.RegisterRESTRoutes(cliCtx, route) +} + +// GetTxCmd returns the root tx command for the slashing module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return CosmosAppModuleBasic{}.GetTxCmd(cdc) +} + +// GetQueryCmd returns the root query command for the slashing module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return CosmosAppModuleBasic{}.GetQueryCmd(cdc) +} + +//___________________________ + +// AppModule implements an application module for the slashing module. +type AppModule struct { + AppModuleBasic + cosmosAppModule CosmosAppModule +} + +// NewAppModule creates a new AppModule object +func NewAppModule(keeper Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + cosmosAppModule: NewCosmosAppModule(keeper), + } +} + +// Name returns the slashing module's name. +func (am AppModule) Name() string { + return am.cosmosAppModule.Name() +} + +// RegisterInvariants registers the slashing module invariants. +func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { + am.cosmosAppModule.RegisterInvariants(ir) +} + +// Route returns the message routing key for the slashing module. +func (am AppModule) Route() string { + return am.cosmosAppModule.Route() +} + +// NewHandler returns an sdk.Handler for the slashing module. +func (am AppModule) NewHandler() sdk.Handler { + return am.cosmosAppModule.NewHandler() +} + +// QuerierRoute returns the slashing module's querier route name. +func (am AppModule) QuerierRoute() string { return am.cosmosAppModule.QuerierRoute() } + +// NewQuerierHandler returns the slashing module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { return am.cosmosAppModule.NewQuerierHandler() } + +// InitGenesis performs genesis initialization for the slashing module. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + return am.cosmosAppModule.InitGenesis(ctx, data) +} + +// ExportGenesis returns the exported genesis state as raw bytes for the slashing +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + return am.cosmosAppModule.ExportGenesis(ctx) +} + +// BeginBlock returns the begin blocker for the slashing module. +func (am AppModule) BeginBlock(ctx sdk.Context, rbb abci.RequestBeginBlock) { + am.cosmosAppModule.BeginBlock(ctx, rbb) +} + +// EndBlock returns the end blocker for the slashing module. +func (am AppModule) EndBlock(ctx sdk.Context, rbb abci.RequestEndBlock) []abci.ValidatorUpdate { + return am.cosmosAppModule.EndBlock(ctx, rbb) +} + +//____________________________________________________________________________ + +// AppModuleSimulation functions + +// GenerateGenesisState creates a randomized GenState of the auth module +func (am AppModule) GenerateGenesisState(simState *module.SimulationState) { + am.cosmosAppModule.GenerateGenesisState(simState) +} + +// ProposalContents doesn't return any content functions for governance proposals. +func (am AppModule) ProposalContents(simState module.SimulationState) []sim.WeightedProposalContent { + return am.cosmosAppModule.ProposalContents(simState) +} + +// RandomizedParams creates randomized auth param changes for the simulator. +func (am AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange { + return am.cosmosAppModule.RandomizedParams(r) +} + +// RegisterStoreDecoder registers a decoder for auth module's types +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + am.cosmosAppModule.RegisterStoreDecoder(sdr) +} + +// WeightedOperations doesn't return any auth module operation. +func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation { + return am.cosmosAppModule.WeightedOperations(simState) +}