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 all 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
2 changes: 1 addition & 1 deletion app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
distrsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation"
"github.com/cosmos/cosmos-sdk/x/gov"
govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
paramsim "github.com/cosmos/cosmos-sdk/x/params/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
Expand All @@ -32,6 +31,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking"
stakingsim "github.com/cosmos/cosmos-sdk/x/staking/simulation"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/irisnet/irishub/modules/mint"
)

func init() {
Expand Down
18 changes: 7 additions & 11 deletions cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/irisnet/irishub/modules/mint"
)

func TestIrisCLIKeysAddMultisig(t *testing.T) {
Expand Down Expand Up @@ -420,12 +420,10 @@ func TestIrisCLIQueryRewards(t *testing.T) {
cdc := app.MakeCodec()

genesisState := f.GenesisState()
inflationMin := sdk.MustNewDecFromStr("10000.0")
var mintData mint.GenesisState
cdc.UnmarshalJSON(genesisState[mint.ModuleName], &mintData)
mintData.Minter.Inflation = inflationMin
mintData.Params.InflationMin = inflationMin
mintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
cdc.MustUnmarshalJSON(genesisState[mint.ModuleName], &mintData)
mintData.Minter = mint.DefaultMinter()
mintData.Params = mint.DefaultParams()
mintDataBz, err := cdc.MarshalJSON(mintData)
require.NoError(t, err)
genesisState[mint.ModuleName] = mintDataBz
Expand Down Expand Up @@ -680,12 +678,10 @@ func TestIrisCLISubmitCommunityPoolSpendProposal(t *testing.T) {
// create some inflation
cdc := app.MakeCodec()
genesisState := f.GenesisState()
inflationMin := sdk.MustNewDecFromStr("10000.0")
var mintData mint.GenesisState
cdc.UnmarshalJSON(genesisState[mint.ModuleName], &mintData)
mintData.Minter.Inflation = inflationMin
mintData.Params.InflationMin = inflationMin
mintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
cdc.MustUnmarshalJSON(genesisState[mint.ModuleName], &mintData)
mintData.Minter = mint.DefaultMinter()
mintData.Params = mint.DefaultParams()
mintDataBz, err := cdc.MarshalJSON(mintData)
require.NoError(t, err)
genesisState[mint.ModuleName] = mintDataBz
Expand Down
39 changes: 39 additions & 0 deletions cli_test/mint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package clitest

import (
"fmt"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/irisnet/irishub/app"
"github.com/irisnet/irishub/modules/mint"
"github.com/stretchr/testify/require"
"testing"
)

func TestMintGetParams(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

// start iris server
proc := f.GDStart()
defer proc.Stop(false)

params := f.QueryMintParams()
require.Equal(t, sdk.NewDecWithPrec(4, 2), params.Inflation)
require.Equal(t, sdk.DefaultBondDenom, params.MintDenom)

// Cleanup testing directories
f.Cleanup()
}

// QueryMintParams is iriscli query mint params
func (f *Fixtures) QueryMintParams() mint.Params {
cmd := fmt.Sprintf("%s query mint params %s", f.IriscliBinary, f.Flags())
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
cdc := app.MakeCodec()
var params mint.Params
err := cdc.UnmarshalJSON([]byte(res), &params)
require.NoError(f.T, err)
return params
}
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
29 changes: 2 additions & 27 deletions lcd_test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/irisnet/irishub/modules/mint"

tmcfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
Expand Down Expand Up @@ -255,15 +255,7 @@ func defaultGenesis(config *tmcfg.Config, nValidators int, initAddrs []sdk.AccAd

// mint genesis (none set within genesisState)
mintData := mint.DefaultGenesisState()
inflationMin := sdk.ZeroDec()
if minting {
inflationMin = sdk.MustNewDecFromStr("10000.0")
mintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
} else {
mintData.Params.InflationMax = inflationMin
}
mintData.Minter.Inflation = inflationMin
mintData.Params.InflationMin = inflationMin

mintDataBz := cdc.MustMarshalJSON(mintData)
genesisState[mint.ModuleName] = mintDataBz

Expand All @@ -275,23 +267,6 @@ func defaultGenesis(config *tmcfg.Config, nValidators int, initAddrs []sdk.AccAd
crisisDataBz = cdc.MustMarshalJSON(crisisData)
genesisState[crisis.ModuleName] = crisisDataBz

//// double check inflation is set according to the minting boolean flag
if minting {
if !(mintData.Params.InflationMax.Equal(sdk.MustNewDecFromStr("15000.0")) &&
mintData.Minter.Inflation.Equal(sdk.MustNewDecFromStr("10000.0")) &&
mintData.Params.InflationMin.Equal(sdk.MustNewDecFromStr("10000.0"))) {
err = errors.New("Mint parameters does not correspond to their defaults")
return
}
} else {
if !(mintData.Params.InflationMax.Equal(sdk.ZeroDec()) &&
mintData.Minter.Inflation.Equal(sdk.ZeroDec()) &&
mintData.Params.InflationMin.Equal(sdk.ZeroDec())) {
err = errors.New("Mint parameters not equal to decimal 0")
return
}
}

appState, err := codec.MarshalJSONIndent(cdc, genesisState)
if err != nil {
return
Expand Down
16 changes: 2 additions & 14 deletions lcd_test/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/irisnet/irishub/modules/mint"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -1072,23 +1072,11 @@ func TestMintingQueries(t *testing.T) {
require.NoError(t, err)
defer cleanup()

res, body := Request(t, port, "GET", "/minting/parameters", nil)
res, body := Request(t, port, "GET", "/mint/parameters", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var params mint.Params
require.NoError(t, cdc.UnmarshalJSON(extractResultFromResponse(t, []byte(body)), &params))

res, body = Request(t, port, "GET", "/minting/inflation", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var inflation sdk.Dec
require.NoError(t, cdc.UnmarshalJSON(extractResultFromResponse(t, []byte(body)), &inflation))

res, body = Request(t, port, "GET", "/minting/annual-provisions", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var annualProvisions sdk.Dec
require.NoError(t, cdc.UnmarshalJSON(extractResultFromResponse(t, []byte(body)), &annualProvisions))
}

func TestAccountBalanceQuery(t *testing.T) {
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
}
35 changes: 35 additions & 0 deletions modules/mint/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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
DefaultMinter = types.DefaultMinter

// variable aliases
ModuleCdc = types.ModuleCdc
)

type (
Keeper = keeper.Keeper
GenesisState = types.GenesisState
Minter = types.Minter
Params = types.Params
)
Loading