Skip to content

Commit

Permalink
mint: Babylon inflation module (#211)
Browse files Browse the repository at this point in the history
Resolves babylonlabs-io/pm#92

This PR provides the implementation of the inflation module. 

- The module shares the same interfaces as `x/mint`, and serves as a
drop-in replacement of Cosmos SDK's `x/mint`.
- The module implements a different inflation function. Currently, it
employs Celestia's
[algorithm](https://docs.celestia.org/learn/staking-governance-supply)
where the inflation rate decreases gradually w.r.t. the block timestamp.
It contains all necessary dependencies for implementing any inflation
function.
- The implementation is adapted from
[Celestia](https://github.com/celestiaorg/celestia-app/tree/main/x/mint)
and [Cosmos SDK](https://github.com/cosmos/cosmos-sdk/tree/main/x/mint).

Steps for implementation:

- [x] copy https://github.com/celestiaorg/celestia-app/tree/main/x/mint
to `x/mint`
- [x] fix all dependencies
- [x] bump module to Cosmos SDK v0.50
- [x] replace Cosmos x/mint with the new x/mint
- [x] fix unit tests
- [x] fix e2e tests
- [x] fix software upgrade e2e
- [x] changelog
- [x] write ADR
- [x] implement the design
- [x] properly initialise the new mint module in upgrade handler
- [x] fix doc
  • Loading branch information
SebastianElvis authored Oct 30, 2024
1 parent c9a55a5 commit a98cfd9
Show file tree
Hide file tree
Showing 35 changed files with 4,171 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### State Machine Breaking

* [#211](https://github.com/babylonlabs-io/babylon/pull/211) Babylon inflation module

### Improvements

* [#235](https://github.com/babylonlabs-io/babylon/pull/235) Change default values
Expand Down
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/babylonlabs-io/babylon/x/mint"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
abci "github.com/cometbft/cometbft/abci/types"
cmtos "github.com/cometbft/cometbft/libs/os"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down Expand Up @@ -67,8 +69,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/mint"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand Down Expand Up @@ -285,7 +285,7 @@ func NewBabylonApp(
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
Expand Down
4 changes: 2 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
mintkeeper "github.com/babylonlabs-io/babylon/x/mint/keeper"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
Expand All @@ -35,8 +37,6 @@ import (
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/params"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand Down
2 changes: 1 addition & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/math"
pruningtypes "cosmossdk.io/store/pruning/types"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/ed25519"
tmjson "github.com/cometbft/cometbft/libs/json"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/mempool"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"

Expand Down
55 changes: 51 additions & 4 deletions app/upgrades/v1/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"

"github.com/babylonlabs-io/babylon/app/keepers"
appparams "github.com/babylonlabs-io/babylon/app/params"
Expand All @@ -31,6 +33,8 @@ import (
btcstktypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
finalitykeeper "github.com/babylonlabs-io/babylon/x/finality/keeper"
finalitytypes "github.com/babylonlabs-io/babylon/x/finality/types"
mintkeeper "github.com/babylonlabs-io/babylon/x/mint/keeper"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
)

const (
Expand Down Expand Up @@ -60,16 +64,42 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand
return nil, err
}

// Re-initialise the mint module as we have replaced Cosmos SDK's
// mint module with our own one.
err = upgradeMint(
ctx,
keepers.EncCfg.Codec,
&keepers.MintKeeper,
&keepers.AccountKeeper,
keepers.StakingKeeper,
)
if err != nil {
return nil, err
}

err = upgradeParameters(
ctx, keepers.EncCfg.Codec,
&keepers.BTCStakingKeeper, &keepers.FinalityKeeper, &keepers.WasmKeeper,
upgradeDataStr.BtcStakingParamStr, upgradeDataStr.FinalityParamStr, upgradeDataStr.CosmWasmParamStr,
ctx,
keepers.EncCfg.Codec,
&keepers.BTCStakingKeeper,
&keepers.FinalityKeeper,
&keepers.WasmKeeper,
upgradeDataStr.BtcStakingParamStr,
upgradeDataStr.FinalityParamStr,
upgradeDataStr.CosmWasmParamStr,
)
if err != nil {
return nil, err
}

if err := upgradeLaunch(ctx, keepers.EncCfg, &keepers.BTCLightClientKeeper, keepers.BankKeeper, upgradeDataStr.NewBtcHeadersStr, upgradeDataStr.TokensDistributionStr); err != nil {
err = upgradeLaunch(
ctx,
keepers.EncCfg,
&keepers.BTCLightClientKeeper,
keepers.BankKeeper,
upgradeDataStr.NewBtcHeadersStr,
upgradeDataStr.TokensDistributionStr,
)
if err != nil {
return nil, err
}

Expand All @@ -78,6 +108,23 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand
}
}

func upgradeMint(
ctx sdk.Context,
cdc codec.Codec,
k *mintkeeper.Keeper,
ak *accountkeeper.AccountKeeper,
stk *stakingkeeper.Keeper,
) error {
bondedDenom, err := stk.BondDenom(ctx)
if err != nil {
return err
}
k.InitGenesis(ctx, ak, &minttypes.GenesisState{
BondDenom: bondedDenom,
})
return nil
}

func upgradeParameters(
ctx sdk.Context,
cdc codec.Codec,
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v1/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"github.com/babylonlabs-io/babylon/app/upgrades"
"github.com/babylonlabs-io/babylon/test/e2e/util"
"github.com/babylonlabs-io/babylon/testutil/datagen"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/stretchr/testify/suite"

"github.com/babylonlabs-io/babylon/app"
Expand Down
15 changes: 3 additions & 12 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

sdkmath "cosmossdk.io/math"

minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
comettypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -22,7 +24,6 @@ import (
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibctypes "github.com/cosmos/ibc-go/v8/modules/core/types"
Expand Down Expand Up @@ -200,7 +201,7 @@ func PrepareGenesis(

// mint module genesis
mintGenState := minttypes.DefaultGenesisState()
mintGenState.Params = genesisParams.MintParams
mintGenState.BondDenom = genesisParams.StakingParams.BondDenom
genesisState[minttypes.ModuleName] = cdc.MustMarshalJSON(mintGenState)

// distribution module genesis
Expand Down Expand Up @@ -250,7 +251,6 @@ type GenesisParams struct {
NativeCoinMetadatas []banktypes.Metadata

StakingParams stakingtypes.Params
MintParams minttypes.Params
DistributionParams distributiontypes.Params
GovParams govv1.Params

Expand Down Expand Up @@ -337,15 +337,6 @@ func TestnetGenesisParams(
genParams.StakingParams.MaxValidators = maxActiveValidators
genParams.StakingParams.BondDenom = genParams.NativeCoinMetadatas[0].Base

genParams.MintParams = minttypes.DefaultParams()
genParams.MintParams.MintDenom = genParams.NativeCoinMetadatas[0].Base
genParams.MintParams.BlocksPerYear = blocksPerYear
// This should always work as inflation rate is already a float64
genParams.MintParams.InflationRateChange = sdkmath.LegacyMustNewDecFromStr(fmt.Sprintf("%f", inflationRateChange))
genParams.MintParams.InflationMin = sdkmath.LegacyMustNewDecFromStr(fmt.Sprintf("%f", inflationMin))
genParams.MintParams.InflationMax = sdkmath.LegacyMustNewDecFromStr(fmt.Sprintf("%f", inflationMax))
genParams.MintParams.GoalBonded = sdkmath.LegacyMustNewDecFromStr(fmt.Sprintf("%f", goalBonded))

genParams.GovParams = govv1.DefaultParams()
genParams.GovParams.MinDeposit = sdk.NewCoins(sdk.NewCoin(
genParams.NativeCoinMetadatas[0].Base,
Expand Down
12 changes: 12 additions & 0 deletions proto/babylon/mint/v1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";
package babylon.mint.v1;

option go_package = "github.com/babylonlabs-io/babylon/x/mint/types";

// GenesisState defines the mint module's genesis state.
message GenesisState {
reserved 1; // 1 was previously used for the `Minter` field.

// BondDenom is the denomination of the token that should be minted.
string bond_denom = 2;
}
40 changes: 40 additions & 0 deletions proto/babylon/mint/v1/mint.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";
package babylon.mint.v1;

option go_package = "github.com/babylonlabs-io/babylon/x/mint/types";

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/timestamp.proto";

// Minter represents the mint state.
message Minter {
// InflationRate is the rate at which new tokens should be minted for the
// current year. For example if InflationRate=0.1, then 10% of the total
// supply will be minted over the course of the year.
string inflation_rate = 1 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
// AnnualProvisions is the total number of tokens to be minted in the current
// year due to inflation.
string annual_provisions = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];

// PreviousBlockTime is the timestamp of the previous block.
google.protobuf.Timestamp previous_block_time = 4
[ (gogoproto.stdtime) = true ];

// BondDenom is the denomination of the token that should be minted.
string bond_denom = 5;
}

// GenesisTime contains the timestamp of the genesis block.
message GenesisTime {
// GenesisTime is the timestamp of the genesis block.
google.protobuf.Timestamp genesis_time = 1 [ (gogoproto.stdtime) = true ];
}
68 changes: 68 additions & 0 deletions proto/babylon/mint/v1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
syntax = "proto3";
package babylon.mint.v1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "babylon/mint/v1/mint.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/babylonlabs-io/babylon/x/mint/types";

// Query defines the gRPC querier service.
service Query {
// InflationRate returns the current inflation rate.
rpc InflationRate(QueryInflationRateRequest)
returns (QueryInflationRateResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/inflation_rate";
}

// AnnualProvisions returns the current annual provisions.
rpc AnnualProvisions(QueryAnnualProvisionsRequest)
returns (QueryAnnualProvisionsResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/annual_provisions";
}

// GenesisTime returns the genesis time.
rpc GenesisTime(QueryGenesisTimeRequest) returns (QueryGenesisTimeResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/genesis_time";
}
}

// QueryInflationRateRequest is the request type for the Query/InflationRate RPC
// method.
message QueryInflationRateRequest {}

// QueryInflationRateResponse is the response type for the Query/InflationRate
// RPC method.
message QueryInflationRateResponse {
// InflationRate is the current inflation rate.
bytes inflation_rate = 1 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

// QueryAnnualProvisionsRequest is the request type for the
// Query/AnnualProvisions RPC method.
message QueryAnnualProvisionsRequest {}

// QueryAnnualProvisionsResponse is the response type for the
// Query/AnnualProvisions RPC method.
message QueryAnnualProvisionsResponse {
// AnnualProvisions is the current annual provisions.
bytes annual_provisions = 1 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

// QueryGenesisTimeRequest is the request type for the Query/GenesisTime RPC
// method.
message QueryGenesisTimeRequest {}

// QueryGenesisTimeResponse is the response type for the Query/GenesisTime RPC
// method.
message QueryGenesisTimeResponse {
// GenesisTime is the timestamp associated with the first block.
google.protobuf.Timestamp genesis_time = 1 [ (gogoproto.stdtime) = true ];
}
4 changes: 2 additions & 2 deletions test/e2e/initialization/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

sdkmath "cosmossdk.io/math"

minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/server"
Expand All @@ -19,7 +20,6 @@ import (
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
staketypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gogoproto/proto"

Expand Down Expand Up @@ -316,7 +316,7 @@ func updateGovGenesis(votingPeriod, expeditedVotingPeriod time.Duration) func(go
}

func updateMintGenesis(mintGenState *minttypes.GenesisState) {
mintGenState.Params.MintDenom = BabylonDenom
mintGenState.BondDenom = BabylonDenom
}

func updateStakeGenesis(stakeGenState *staketypes.GenesisState) {
Expand Down
2 changes: 1 addition & 1 deletion wasmbinding/test/custom_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"cosmossdk.io/math"
"github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/ed25519"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/app"
Expand Down
Loading

0 comments on commit a98cfd9

Please sign in to comment.