Skip to content

Commit

Permalink
Merge pull request #273 from CosmosContracts/dimi/inflation
Browse files Browse the repository at this point in the history
Update mint module
  • Loading branch information
faddat authored Sep 11, 2022
2 parents cef621e + 78d3b6a commit 9480914
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/superlinter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ jobs:
env:
VALIDATE_ALL_CODEBASE: false
VALIDATE_GO: false
IGNORE_GENERATED_FILES: true
FILTER_REGEX_EXCLUDE: .*.pb.go
DEFAULT_BRANCH: "main"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
// RegisterUpgradeHandlers returns upgrade handlers

func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
app.UpgradeKeeper.SetUpgradeHandler("MigrateTraces",
app.UpgradeKeeper.SetUpgradeHandler("v10",
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// transfer module consensus version has been bumped to 2
return app.mm.RunMigrations(ctx, cfg, fromVM)
Expand Down
24 changes: 18 additions & 6 deletions proto/juno/mint/mint.proto
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
syntax = "proto3";
package juno.mint;

option go_package = "github.com/CosmosContracts/juno/x/mint/types";
package juno.mint;

import "gogoproto/gogo.proto";

option go_package = "github.com/CosmosContracts/juno/x/mint/types";

// Minter represents the minting state.
message Minter {
// current annual inflation rate
string inflation = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string inflation = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
uint64 phase = 2;
uint64 start_phase_block = 3 [(gogoproto.moretags) = "yaml:\"start_phase_block\""];
uint64 start_phase_block = 3 [
(gogoproto.moretags) = "yaml:\"start_phase_block\""
];
// current annual expected provisions
string annual_provisions = 4 [
(gogoproto.moretags) = "yaml:\"annual_provisions\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string target_supply = 5 [
(gogoproto.moretags) = "yaml:\"target_supply\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// Params holds parameters for the mint module.
Expand All @@ -27,5 +37,7 @@ message Params {
// type of coin to mint
string mint_denom = 1;
// expected blocks per year
uint64 blocks_per_year = 2 [(gogoproto.moretags) = "yaml:\"blocks_per_year\""];
uint64 blocks_per_year = 2 [
(gogoproto.moretags) = "yaml:\"blocks_per_year\""
];
}
11 changes: 8 additions & 3 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
// fetch stored params
params := k.GetParams(ctx)
currentBlock := uint64(ctx.BlockHeight())
nextPhase := minter.NextPhase(params, currentBlock)

// fetch current total supply
totalSupply := k.TokenSupply(ctx, params.MintDenom)

// check if we need to change phase
nextPhase := minter.NextPhase(params, totalSupply)

if nextPhase != minter.Phase {
// store new inflation rate by phase
newInflation := minter.PhaseInflationRate(nextPhase)
totalSupply := k.TokenSupply(ctx, params.MintDenom)
minter.Inflation = newInflation
minter.Phase = nextPhase
minter.StartPhaseBlock = currentBlock
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply)
minter.TargetSupply = totalSupply.Add(minter.AnnualProvisions.TruncateInt())
k.SetMinter(ctx, minter)

// inflation phase end
Expand All @@ -43,7 +48,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
}

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

err := k.MintCoins(ctx, mintedCoins)
Expand Down
23 changes: 23 additions & 0 deletions x/mint/keeper/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
v2 "github.com/CosmosContracts/juno/v10/x/mint/migrations/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Migrator is a struct for handling in-place state migrations.
type Migrator struct {
keeper Keeper
}

func NewMigrator(k Keeper) Migrator {
return Migrator{
keeper: k,
}
}

// Migrate migrates the x/mint module state from the consensus version
// 1 to version 2
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.keeper.cdc)
}
38 changes: 38 additions & 0 deletions x/mint/migrations/v2/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v2

import (
"github.com/CosmosContracts/juno/v10/x/mint/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
ModuleName = "mint"
)

// Migrate migrates the x/mint module state from the consensus version 1 to
// version 2. Specifically, it take calculate target supply for the current phase
func Migrate(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
) error {

// Get minter
var minter types.Minter
b := store.Get(types.MinterKey)
if b == nil {
panic("stored minter should not have been nil")
}

cdc.MustUnmarshal(b, &minter)

// Calculate target supply
minter.TargetSupply = minter.AnnualProvisions.Add(minter.AnnualProvisions.Quo(minter.Inflation)).TruncateInt()

// Save new minter
bz := cdc.MustMarshal(&minter)
store.Set(types.MinterKey, bz)

return nil
}
8 changes: 7 additions & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

m := keeper.NewMigrator(am.keeper)

if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
}
}

// InitGenesis performs genesis initialization for the mint module. It returns
Expand All @@ -146,7 +152,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// BeginBlock returns the begin blocker for the mint module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
2 changes: 1 addition & 1 deletion x/mint/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestDecodeStore(t *testing.T) {
cdc := app.MakeEncodingConfig().Marshaler
dec := simulation.NewDecodeStore(cdc)

minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15), 1, 1)
minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15), 1, 1, sdk.NewInt(1))

kvPairs := kv.Pairs{
Pairs: []kv.Pair{
Expand Down
4 changes: 2 additions & 2 deletions x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func TestRandomizedGenState(t *testing.T) {

require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear)
require.Equal(t, "stake", mintGenesis.Params.MintDenom)
require.Equal(t, "0stake", mintGenesis.Minter.BlockProvision(mintGenesis.Params).String())
require.Equal(t, "0stake", mintGenesis.Minter.BlockProvision(mintGenesis.Params, sdk.NewInt(0)).String())
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.NextAnnualProvisions(mintGenesis.Params, sdk.OneInt()).String())
require.Equal(t, "0.400000000000000000", mintGenesis.Minter.PhaseInflationRate(1).String())
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.Inflation.String())
require.Equal(t, uint64(1), mintGenesis.Minter.NextPhase(mintGenesis.Params, 1))
require.Equal(t, uint64(1), mintGenesis.Minter.NextPhase(mintGenesis.Params, sdk.NewInt(1)))
require.Equal(t, uint64(0), mintGenesis.Minter.Phase)
require.Equal(t, "0.000000000000000000", mintGenesis.Minter.AnnualProvisions.String())
}
Expand Down
99 changes: 74 additions & 25 deletions x/mint/types/mint.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (

// NewMinter returns a new Minter object with the given inflation and annual
// provisions values.
func NewMinter(inflation, annualProvisions sdk.Dec, phase, startPhaseBlock uint64) Minter {
func NewMinter(inflation, annualProvisions sdk.Dec, phase, startPhaseBlock uint64, targetSupply sdk.Int) Minter {
return Minter{
Inflation: inflation,
AnnualProvisions: annualProvisions,
Phase: phase,
StartPhaseBlock: startPhaseBlock,
TargetSupply: targetSupply,
}
}

Expand All @@ -24,6 +25,7 @@ func InitialMinter(inflation sdk.Dec) Minter {
sdk.NewDec(0),
0,
0,
sdk.NewInt(0),
)
}

Expand Down Expand Up @@ -70,14 +72,13 @@ func (m Minter) PhaseInflationRate(phase uint64) sdk.Dec {
}

// NextPhase returns the new phase.
func (m Minter) NextPhase(params Params, currentBlock uint64) uint64 {
func (m Minter) NextPhase(params Params, currentSupply sdk.Int) uint64 {
nonePhase := m.Phase == 0
if nonePhase {
return 1
}

blockNewPhase := m.StartPhaseBlock + params.BlocksPerYear
if blockNewPhase > currentBlock {
if currentSupply.LT(m.TargetSupply) {
return m.Phase
}

Expand All @@ -92,7 +93,14 @@ func (m Minter) NextAnnualProvisions(_ Params, totalSupply sdk.Int) sdk.Dec {

// BlockProvision returns the provisions for a block based on the annual
// provisions rate.
func (m Minter) BlockProvision(params Params) sdk.Coin {
func (m Minter) BlockProvision(params Params, totalSupply sdk.Int) sdk.Coin {
provisionAmt := m.AnnualProvisions.QuoInt(sdk.NewInt(int64(params.BlocksPerYear)))

// Because of rounding, we might mint too many tokens in this phase, let's limit it
futureSupply := totalSupply.Add(provisionAmt.TruncateInt())
if futureSupply.GT(m.TargetSupply) {
return sdk.NewCoin(params.MintDenom, m.TargetSupply.Sub(totalSupply))
}

return sdk.NewCoin(params.MintDenom, provisionAmt.TruncateInt())
}
Loading

0 comments on commit 9480914

Please sign in to comment.