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

Update mint module #273

Merged
merged 14 commits into from
Sep 11, 2022
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\""
];
}
16 changes: 13 additions & 3 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,39 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
return
}

logger := k.Logger(ctx)

// 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 warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

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

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

logger.Debug("New Block", "Height", ctx.BlockHeight(), "Current Total Supply", totalSupply, "Current Phase", minter.Phase)
Fixed Show fixed Hide fixed
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())

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
k.SetMinter(ctx, minter)

logger.Info("New inflation phase started!", "Current Supply", totalSupply, "Target Supply", minter.TargetSupply, "Annual Provisions", minter.AnnualProvisions, "Height", ctx.BlockHeight())

// inflation phase end
if minter.Inflation.Equal(sdk.ZeroDec()) {
return
}
}

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

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
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. Specifically, it take calculate target supply for the current phase
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.

Loading