Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Refactor mint module #2102

Merged
merged 19 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"github.com/cosmos/cosmos-sdk/x/evidence"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/irisnet/irishub/modules/mint"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -156,7 +156,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
stakingKeeper := staking.NewKeeper(
app.cdc, keys[staking.StoreKey], app.supplyKeeper, stakingSubspace, staking.DefaultCodespace,
)
app.mintKeeper = mint.NewKeeper(app.cdc, keys[mint.StoreKey], mintSubspace, &stakingKeeper, app.supplyKeeper, auth.FeeCollectorName)
app.mintKeeper = mint.NewKeeper(app.cdc, keys[mint.StoreKey], mintSubspace, app.supplyKeeper, auth.FeeCollectorName)
app.distrKeeper = distr.NewKeeper(app.cdc, keys[distr.StoreKey], distrSubspace, &stakingKeeper,
app.supplyKeeper, distr.DefaultCodespace, auth.FeeCollectorName, app.ModuleAccountAddrs())
app.slashingKeeper = slashing.NewKeeper(
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ require (
github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect
github.com/cosmos/cosmos-sdk v0.34.4-0.20191108144056-d81d46192a0c
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect
github.com/go-kit/kit v0.9.0
github.com/golang/mock v1.3.1 // indirect
github.com/gorilla/mux v1.7.3
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/otiai10/copy v1.0.2
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.1.0 // indirect
github.com/prometheus/client_golang v1.1.0
github.com/rakyll/statik v0.1.6
github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 // indirect
github.com/snikch/goodman v0.0.0-20171125024755-10e37e294daa
Expand Down
44 changes: 26 additions & 18 deletions modules/mint/abci.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
package mint

import (
"github.com/irisnet/irishub/app/v1/mint/tags"
sdk "github.com/irisnet/irishub/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/irisnet/irishub/modules/mint/internal/types"
)

// Called every block, process inflation on the first block of every hour
func BeginBlocker(ctx sdk.Context, k Keeper) sdk.Tags {
func BeginBlocker(ctx sdk.Context, k Keeper) {
ctx = ctx.WithLogger(ctx.Logger().With("handler", "beginBlock").With("module", "iris/mint"))
logger := ctx.Logger()
// Get block BFT time and block height
blockTime := ctx.BlockHeader().Time
blockHeight := ctx.BlockHeader().Height
minter := k.GetMinter(ctx)
if blockHeight <= 1 { // don't inflate token in the first block
if ctx.BlockHeight() <= 1 { // don't inflate token in the first block
minter.LastUpdate = blockTime
k.SetMinter(ctx, minter)
return nil
return
}

// Calculate block mint amount
params := k.GetParamSet(ctx)
logger.Info("Mint parameters", "inflation_rate", params.Inflation.String())
annualProvisions := minter.NextAnnualProvisions(params)
logger.Info("Calculate annual provisions", "annual_provisions", annualProvisions.String())
mintedCoin := minter.BlockProvision(annualProvisions)
logger.Info("Mint parameters", "inflation_rate", params.Inflation.String(), "mint_denom", params.MintDenom)

mintedCoin := minter.BlockProvision(params)
logger.Info("Mint result", "block_provisions", mintedCoin.String(), "time", blockTime.String())

// Increase loosen token and add minted coin to feeCollector
k.bk.IncreaseLoosenToken(ctx, sdk.Coins{mintedCoin})
k.fk.AddCollectedFees(ctx, sdk.Coins{mintedCoin})
mintedCoins := sdk.NewCoins(mintedCoin)
// mint coins to submodule account
if err := k.MintCoins(ctx, mintedCoins); err != nil {
panic(err)
}

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

// Update last block BFT time
lastInflationTime := minter.LastUpdate
minter.LastUpdate = blockTime
k.SetMinter(ctx, minter)
// Add tags
return sdk.NewTags(
tags.LastInflationTime, []byte(lastInflationTime.String()),
tags.InflationTime, []byte(blockTime.String()),
tags.MintCoin, []byte(mintedCoin.String()),

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyLastInflationTime, lastInflationTime.String()),
sdk.NewAttribute(types.AttributeKeyInflationTime, blockTime.String()),
sdk.NewAttribute(types.AttributeKeyMintCoin, mintedCoin.Amount.String()),
),
)
}
41 changes: 41 additions & 0 deletions modules/mint/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mint_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/irisnet/irishub/modules/mint"
"github.com/irisnet/irishub/modules/mint/internal/types"
"github.com/irisnet/irishub/simapp"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestBeginBlocker(t *testing.T) {
app, ctx := createTestApp(true)

mint.BeginBlocker(ctx, app.MintKeeper)
minter := app.MintKeeper.GetMinter(ctx)
param := app.MintKeeper.GetParamSet(ctx)
mintCoins := minter.BlockProvision(param)

acc1 := app.SupplyKeeper.GetModuleAccount(ctx, "fee_collector")
require.Equal(t, acc1.GetCoins(), sdk.NewCoins(mintCoins))
}

// 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{Height: 2})
app.MintKeeper.SetParamSet(ctx, types.Params{
Inflation: sdk.NewDecWithPrec(4, 2),
MintDenom: "iris",
})
app.MintKeeper.SetMinter(ctx, types.DefaultMinter())
app.SupplyKeeper.SetSupply(ctx, supply.Supply{})
app.DistrKeeper.SetFeePool(ctx, distribution.InitialFeePool())
return app, ctx
}
34 changes: 34 additions & 0 deletions modules/mint/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mint

import (
"github.com/irisnet/irishub/modules/mint/internal/keeper"
"github.com/irisnet/irishub/modules/mint/internal/types"
)

const (
ModuleName = types.ModuleName
DefaultParamspace = types.DefaultParamspace
StoreKey = types.StoreKey
QuerierRoute = types.QuerierRoute
)

var (
// functions aliases
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
RegisterCodec = types.RegisterCodec
DefaultParams = types.DefaultParams

// variable aliases
ModuleCdc = types.ModuleCdc
)

type (
Keeper = keeper.Keeper
GenesisState = types.GenesisState
Minter = types.Minter
Params = types.Params
)
57 changes: 57 additions & 0 deletions modules/mint/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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"
"github.com/irisnet/irishub/modules/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),
)...,
)

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)
},
}
}
38 changes: 38 additions & 0 deletions modules/mint/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package rest

import (
"fmt"
"net/http"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/irisnet/irishub/modules/mint/internal/types"
)

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/mint/parameters",
queryParamsHandlerFn(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)
}
}
12 changes: 12 additions & 0 deletions modules/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)
}
10 changes: 0 additions & 10 deletions modules/mint/expected_keepers.go

This file was deleted.

47 changes: 3 additions & 44 deletions modules/mint/genesis.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,18 @@
package mint

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

// GenesisState - all distribution state that must be provided at genesis
type GenesisState struct {
Minter Minter `json:"minter"` // minter object
Params Params `json:"params"` // inflation params
}

func NewGenesisState(minter Minter, params Params) GenesisState {
return GenesisState{
Minter: minter,
Params: params,
}
}

// get raw genesis raw message for testing
func DefaultGenesisState() GenesisState {
return GenesisState{
Minter: InitialMinter(),
Params: DefaultParams(),
}
}

// new mint genesis
// InitGenesis new mint genesis
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
if err := ValidateGenesis(data); err != nil {
panic(err.Error())
}

keeper.SetMinter(ctx, data.Minter)
keeper.SetParamSet(ctx, data.Params)
}

// ExportGenesis returns a GenesisState for a given context and keeper. The
// GenesisState will contain the pool, and validator/delegator distribution info's
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {

minter := keeper.GetMinter(ctx)
params := keeper.GetParamSet(ctx)
return NewGenesisState(minter, params)
}

// ValidateGenesis validates the provided staking genesis state to ensure the
// expected invariants holds. (i.e. params in correct bounds, no duplicate validators)
func ValidateGenesis(data GenesisState) error {
err := validateParams(data.Params)
if err != nil {
return err
}
err = validateMinter(data.Minter)
if err != nil {
return err
}
return nil
}
20 changes: 20 additions & 0 deletions modules/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"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/irisnet/irishub/modules/mint/internal/types"
"github.com/irisnet/irishub/simapp"
)

// 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.SetParamSet(ctx, types.DefaultParams())
app.MintKeeper.SetMinter(ctx, types.DefaultMinter())

return app, ctx
}
Loading