Skip to content

Commit 9480914

Browse files
authored
Merge pull request CosmosContracts#273 from CosmosContracts/dimi/inflation
Update mint module
2 parents cef621e + 78d3b6a commit 9480914

File tree

12 files changed

+224
-62
lines changed

12 files changed

+224
-62
lines changed

.github/workflows/superlinter.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ jobs:
2020
env:
2121
VALIDATE_ALL_CODEBASE: false
2222
VALIDATE_GO: false
23+
IGNORE_GENERATED_FILES: true
24+
FILTER_REGEX_EXCLUDE: .*.pb.go
2325
DEFAULT_BRANCH: "main"
2426
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
840840
// RegisterUpgradeHandlers returns upgrade handlers
841841

842842
func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
843-
app.UpgradeKeeper.SetUpgradeHandler("MigrateTraces",
843+
app.UpgradeKeeper.SetUpgradeHandler("v10",
844844
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
845845
// transfer module consensus version has been bumped to 2
846846
return app.mm.RunMigrations(ctx, cfg, fromVM)

proto/juno/mint/mint.proto

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
syntax = "proto3";
2-
package juno.mint;
32

4-
option go_package = "github.com/CosmosContracts/juno/x/mint/types";
3+
package juno.mint;
54

65
import "gogoproto/gogo.proto";
76

7+
option go_package = "github.com/CosmosContracts/juno/x/mint/types";
8+
89
// Minter represents the minting state.
910
message Minter {
1011
// current annual inflation rate
11-
string inflation = 1
12-
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
12+
string inflation = 1 [
13+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
14+
(gogoproto.nullable) = false
15+
];
1316
uint64 phase = 2;
14-
uint64 start_phase_block = 3 [(gogoproto.moretags) = "yaml:\"start_phase_block\""];
17+
uint64 start_phase_block = 3 [
18+
(gogoproto.moretags) = "yaml:\"start_phase_block\""
19+
];
1520
// current annual expected provisions
1621
string annual_provisions = 4 [
1722
(gogoproto.moretags) = "yaml:\"annual_provisions\"",
1823
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
1924
(gogoproto.nullable) = false
2025
];
26+
string target_supply = 5 [
27+
(gogoproto.moretags) = "yaml:\"target_supply\"",
28+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
29+
(gogoproto.nullable) = false
30+
];
2131
}
2232

2333
// Params holds parameters for the mint module.
@@ -27,5 +37,7 @@ message Params {
2737
// type of coin to mint
2838
string mint_denom = 1;
2939
// expected blocks per year
30-
uint64 blocks_per_year = 2 [(gogoproto.moretags) = "yaml:\"blocks_per_year\""];
40+
uint64 blocks_per_year = 2 [
41+
(gogoproto.moretags) = "yaml:\"blocks_per_year\""
42+
];
3143
}

x/mint/abci.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
2424
// fetch stored params
2525
params := k.GetParams(ctx)
2626
currentBlock := uint64(ctx.BlockHeight())
27-
nextPhase := minter.NextPhase(params, currentBlock)
27+
28+
// fetch current total supply
29+
totalSupply := k.TokenSupply(ctx, params.MintDenom)
30+
31+
// check if we need to change phase
32+
nextPhase := minter.NextPhase(params, totalSupply)
2833

2934
if nextPhase != minter.Phase {
3035
// store new inflation rate by phase
3136
newInflation := minter.PhaseInflationRate(nextPhase)
32-
totalSupply := k.TokenSupply(ctx, params.MintDenom)
3337
minter.Inflation = newInflation
3438
minter.Phase = nextPhase
3539
minter.StartPhaseBlock = currentBlock
3640
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply)
41+
minter.TargetSupply = totalSupply.Add(minter.AnnualProvisions.TruncateInt())
3742
k.SetMinter(ctx, minter)
3843

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

4550
// mint coins, update supply
46-
mintedCoin := minter.BlockProvision(params)
51+
mintedCoin := minter.BlockProvision(params, totalSupply)
4752
mintedCoins := sdk.NewCoins(mintedCoin)
4853

4954
err := k.MintCoins(ctx, mintedCoins)

x/mint/keeper/migrator.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package keeper
2+
3+
import (
4+
v2 "github.com/CosmosContracts/juno/v10/x/mint/migrations/v2"
5+
sdk "github.com/cosmos/cosmos-sdk/types"
6+
)
7+
8+
// Migrator is a struct for handling in-place state migrations.
9+
type Migrator struct {
10+
keeper Keeper
11+
}
12+
13+
func NewMigrator(k Keeper) Migrator {
14+
return Migrator{
15+
keeper: k,
16+
}
17+
}
18+
19+
// Migrate migrates the x/mint module state from the consensus version
20+
// 1 to version 2
21+
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
22+
return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.keeper.cdc)
23+
}

x/mint/migrations/v2/migrate.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package v2
2+
3+
import (
4+
"github.com/CosmosContracts/juno/v10/x/mint/types"
5+
"github.com/cosmos/cosmos-sdk/codec"
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
)
8+
9+
const (
10+
ModuleName = "mint"
11+
)
12+
13+
// Migrate migrates the x/mint module state from the consensus version 1 to
14+
// version 2. Specifically, it take calculate target supply for the current phase
15+
func Migrate(
16+
ctx sdk.Context,
17+
store sdk.KVStore,
18+
cdc codec.BinaryCodec,
19+
) error {
20+
21+
// Get minter
22+
var minter types.Minter
23+
b := store.Get(types.MinterKey)
24+
if b == nil {
25+
panic("stored minter should not have been nil")
26+
}
27+
28+
cdc.MustUnmarshal(b, &minter)
29+
30+
// Calculate target supply
31+
minter.TargetSupply = minter.AnnualProvisions.Add(minter.AnnualProvisions.Quo(minter.Inflation)).TruncateInt()
32+
33+
// Save new minter
34+
bz := cdc.MustMarshal(&minter)
35+
store.Set(types.MinterKey, bz)
36+
37+
return nil
38+
}

x/mint/module.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
126126
// module-specific gRPC queries.
127127
func (am AppModule) RegisterServices(cfg module.Configurator) {
128128
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
129+
130+
m := keeper.NewMigrator(am.keeper)
131+
132+
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
133+
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
134+
}
129135
}
130136

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

148154
// ConsensusVersion implements AppModule/ConsensusVersion.
149-
func (AppModule) ConsensusVersion() uint64 { return 1 }
155+
func (AppModule) ConsensusVersion() uint64 { return 2 }
150156

151157
// BeginBlock returns the begin blocker for the mint module.
152158
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {

x/mint/simulation/decoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestDecodeStore(t *testing.T) {
1717
cdc := app.MakeEncodingConfig().Marshaler
1818
dec := simulation.NewDecodeStore(cdc)
1919

20-
minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15), 1, 1)
20+
minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15), 1, 1, sdk.NewInt(1))
2121

2222
kvPairs := kv.Pairs{
2323
Pairs: []kv.Pair{

x/mint/simulation/genesis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func TestRandomizedGenState(t *testing.T) {
4242

4343
require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear)
4444
require.Equal(t, "stake", mintGenesis.Params.MintDenom)
45-
require.Equal(t, "0stake", mintGenesis.Minter.BlockProvision(mintGenesis.Params).String())
45+
require.Equal(t, "0stake", mintGenesis.Minter.BlockProvision(mintGenesis.Params, sdk.NewInt(0)).String())
4646
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.NextAnnualProvisions(mintGenesis.Params, sdk.OneInt()).String())
4747
require.Equal(t, "0.400000000000000000", mintGenesis.Minter.PhaseInflationRate(1).String())
4848
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.Inflation.String())
49-
require.Equal(t, uint64(1), mintGenesis.Minter.NextPhase(mintGenesis.Params, 1))
49+
require.Equal(t, uint64(1), mintGenesis.Minter.NextPhase(mintGenesis.Params, sdk.NewInt(1)))
5050
require.Equal(t, uint64(0), mintGenesis.Minter.Phase)
5151
require.Equal(t, "0.000000000000000000", mintGenesis.Minter.AnnualProvisions.String())
5252
}

x/mint/types/mint.pb.go

Lines changed: 74 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)