Skip to content

Commit

Permalink
Merge pull request #33 from unification-com/throttle
Browse files Browse the repository at this point in the history
Mint Throttle
  • Loading branch information
Codegnosis committed Nov 8, 2019
2 parents 66f4326 + f30ee1c commit 6d0231d
Show file tree
Hide file tree
Showing 31 changed files with 1,551 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package app
import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/x/mint"

"github.com/unification-com/mainchain-cosmos/app/ante"
"github.com/unification-com/mainchain-cosmos/x/enterprise"
"github.com/unification-com/mainchain-cosmos/x/mint"
"io"
"os"

Expand Down Expand Up @@ -224,7 +225,7 @@ func NewMainchainApp(
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
supply.NewAppModule(app.supplyKeeper, app.accountKeeper),
distr.NewAppModule(app.distrKeeper, app.supplyKeeper),
mint.NewAppModule(app.mintKeeper),
mint.NewAppModule(app.mintKeeper, app.enterpriseKeeper),
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
wrkchain.NewAppModule(app.wrkChainKeeper),
Expand Down
49 changes: 49 additions & 0 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mint

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/unification-com/mainchain-cosmos/x/enterprise"
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types"
)

// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k Keeper, keeper enterprise.Keeper) {
// fetch stored minter & params
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)

// recalculate inflation rate
totalUNDSupply := keeper.GetTotalUndSupply(ctx)
totalLockedUND := keeper.GetTotalLockedUnd(ctx)
liquidUND := totalUNDSupply.Sub(totalLockedUND)

bondedRatio := k.BondedRatio(ctx)
minter.Inflation = minter.NextInflationRate(params, bondedRatio)
minter.AnnualProvisions = minter.NextAnnualProvisions(params, liquidUND.Amount)
k.SetMinter(ctx, minter)

// mint coins, update supply
mintedCoin := minter.BlockProvision(params)
mintedCoins := sdk.NewCoins(mintedCoin)

err := k.MintCoins(ctx, mintedCoins)
if err != nil {
panic(err)
}

// send the minted coins to the fee collector account
err = k.AddCollectedFees(ctx, mintedCoins)
if err != nil {
panic(err)
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()),
sdk.NewAttribute(types.AttributeKeyInflation, minter.Inflation.String()),
sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
}
55 changes: 55 additions & 0 deletions x/mint/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/unification-com/mainchain-cosmos/x/mint/internal/keeper
// ALIASGEN: github.com/unification-com/mainchain-cosmos/x/mint/internal/types
package mint

import (
"github.com/unification-com/mainchain-cosmos/x/mint/internal/keeper"
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types"
)

const (
ModuleName = types.ModuleName
DefaultParamspace = types.DefaultParamspace
StoreKey = types.StoreKey
QuerierRoute = types.QuerierRoute
QueryParameters = types.QueryParameters
QueryInflation = types.QueryInflation
QueryAnnualProvisions = types.QueryAnnualProvisions
)

var (
// functions aliases
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
NewMinter = types.NewMinter
InitialMinter = types.InitialMinter
DefaultInitialMinter = types.DefaultInitialMinter
ValidateMinter = types.ValidateMinter
ParamKeyTable = types.ParamKeyTable
NewParams = types.NewParams
DefaultParams = types.DefaultParams
ValidateParams = types.ValidateParams

// variable aliases
ModuleCdc = types.ModuleCdc
MinterKey = types.MinterKey
KeyMintDenom = types.KeyMintDenom
KeyInflationRateChange = types.KeyInflationRateChange
KeyInflationMax = types.KeyInflationMax
KeyInflationMin = types.KeyInflationMin
KeyGoalBonded = types.KeyGoalBonded
KeyBlocksPerYear = types.KeyBlocksPerYear
)

type (
Keeper = keeper.Keeper
GenesisState = types.GenesisState
Minter = types.Minter
Params = types.Params
)
112 changes: 112 additions & 0 deletions x/mint/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types"
)

// GetQueryCmd returns the cli query commands for the minting module.
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
mintingQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the minting module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

mintingQueryCmd.AddCommand(
client.GetCommands(
GetCmdQueryParams(cdc),
GetCmdQueryInflation(cdc),
GetCmdQueryAnnualProvisions(cdc),
)...,
)

return mintingQueryCmd
}

// GetCmdQueryParams implements a command to return the current minting
// parameters.
func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "Query the current minting parameters",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}

var params types.Params
if err := cdc.UnmarshalJSON(res, &params); err != nil {
return err
}

return cliCtx.PrintOutput(params)
},
}
}

// GetCmdQueryInflation implements a command to return the current minting
// inflation value.
func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "inflation",
Short: "Query the current minting inflation value",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryInflation)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}

var inflation sdk.Dec
if err := cdc.UnmarshalJSON(res, &inflation); err != nil {
return err
}

return cliCtx.PrintOutput(inflation)
},
}
}

// GetCmdQueryAnnualProvisions implements a command to return the current minting
// annual provisions value.
func GetCmdQueryAnnualProvisions(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "annual-provisions",
Short: "Query the current minting annual provisions value",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAnnualProvisions)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}

var inflation sdk.Dec
if err := cdc.UnmarshalJSON(res, &inflation); err != nil {
return err
}

return cliCtx.PrintOutput(inflation)
},
}
}
89 changes: 89 additions & 0 deletions x/mint/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package rest

import (
"fmt"
"net/http"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types"
)

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/minting/parameters",
queryParamsHandlerFn(cliCtx),
).Methods("GET")

r.HandleFunc(
"/minting/inflation",
queryInflationHandlerFn(cliCtx),
).Methods("GET")

r.HandleFunc(
"/minting/annual-provisions",
queryAnnualProvisionsHandlerFn(cliCtx),
).Methods("GET")
}

func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryInflationHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryInflation)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryAnnualProvisionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAnnualProvisions)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
12 changes: 12 additions & 0 deletions x/mint/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package rest

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
)

// RegisterRoutes registers minting module REST handlers on the provided router.
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
registerQueryRoutes(cliCtx, r)
}
18 changes: 18 additions & 0 deletions x/mint/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mint

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// InitGenesis new mint genesis
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
keeper.SetMinter(ctx, data.Minter)
keeper.SetParams(ctx, data.Params)
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
minter := keeper.GetMinter(ctx)
params := keeper.GetParams(ctx)
return NewGenesisState(minter, params)
}
20 changes: 20 additions & 0 deletions x/mint/internal/keeper/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package keeper_test

import (
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types"
)

// returns context and an app with updated mint keeper
func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) {
app := simapp.Setup(isCheckTx)

ctx := app.BaseApp.NewContext(isCheckTx, abci.Header{})
app.MintKeeper.SetParams(ctx, types.DefaultParams())
app.MintKeeper.SetMinter(ctx, types.DefaultInitialMinter())

return app, ctx
}
Loading

0 comments on commit 6d0231d

Please sign in to comment.