Skip to content

Commit

Permalink
feat!: add mint pool address for mint module
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsam committed May 21, 2022
1 parent 0a2b6d2 commit bf4478f
Show file tree
Hide file tree
Showing 21 changed files with 420 additions and 80 deletions.
24 changes: 24 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import (
dbm "github.com/tendermint/tm-db"

farmingparams "github.com/cosmosquad-labs/squad/app/params"
"github.com/cosmosquad-labs/squad/app/upgrades/mainnet/v2.0.0"
"github.com/cosmosquad-labs/squad/x/claim"
claimkeeper "github.com/cosmosquad-labs/squad/x/claim/keeper"
claimtypes "github.com/cosmosquad-labs/squad/x/claim/types"
Expand Down Expand Up @@ -695,6 +696,9 @@ func NewApp(
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)

app.SetUpgradeStoreLoaders()
app.SetUpgradeHandlers(app.mm, app.configurator)

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(fmt.Sprintf("failed to load latest version: %s", err))
Expand Down Expand Up @@ -867,3 +871,23 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino

return paramsKeeper
}

func (app *App) SetUpgradeStoreLoaders() {
// common logics for set upgrades
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}

// mainnet upgrade state loaders
if upgradeInfo.Name == v2_0_0.UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &v2_0_0.StoreUpgrades))
}
}

func (app *App) SetUpgradeHandlers(mm *module.Manager, configurator module.Configurator) {
// mainnet upgrade handlers
app.UpgradeKeeper.SetUpgradeHandler(
v2_0_0.UpgradeName, v2_0_0.UpgradeHandler(mm, configurator, app.BudgetKeeper))
}
43 changes: 22 additions & 21 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/cosmos/ibc-go/v2/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v2/modules/core"
"github.com/cosmosquad-labs/squad/x/mint"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/tendermint/budget/x/budget"
Expand All @@ -36,9 +35,11 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmosquad-labs/squad/x/claim"
"github.com/cosmosquad-labs/squad/x/farming"
"github.com/cosmosquad-labs/squad/x/liquidity"
"github.com/cosmosquad-labs/squad/x/liquidstaking"
"github.com/cosmosquad-labs/squad/x/mint"
)

func TestSimAppExportAndBlockedAddrs(t *testing.T) {
Expand Down Expand Up @@ -228,27 +229,27 @@ func TestInitGenesisOnMigration(t *testing.T) {
// the VersionMap to simulate upgrading with a new module.
_, err := app.mm.RunMigrations(ctx, app.configurator,
module.VersionMap{
"bank": bank.AppModule{}.ConsensusVersion(),
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authzmodule.AppModule{}.ConsensusVersion(),
"staking": staking.AppModule{}.ConsensusVersion(),
"mint": mint.AppModule{}.ConsensusVersion(),
"distribution": distribution.AppModule{}.ConsensusVersion(),
"slashing": slashing.AppModule{}.ConsensusVersion(),
"gov": gov.AppModule{}.ConsensusVersion(),
"params": params.AppModule{}.ConsensusVersion(),
"upgrade": upgrade.AppModule{}.ConsensusVersion(),
"vesting": vesting.AppModule{}.ConsensusVersion(),
"feegrant": feegrantmodule.AppModule{}.ConsensusVersion(),
"evidence": evidence.AppModule{}.ConsensusVersion(),
"crisis": crisis.AppModule{}.ConsensusVersion(),
"genutil": genutil.AppModule{}.ConsensusVersion(),
"capability": capability.AppModule{}.ConsensusVersion(),
"budget": budget.AppModule{}.ConsensusVersion(),
"farming": farming.AppModule{}.ConsensusVersion(),
// TODO: add liquidity module
//"liquidity": liquidity.AppModule{}.ConsensusVersion(),
"bank": bank.AppModule{}.ConsensusVersion(),
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authzmodule.AppModule{}.ConsensusVersion(),
"staking": staking.AppModule{}.ConsensusVersion(),
"mint": mint.AppModule{}.ConsensusVersion(),
"distribution": distribution.AppModule{}.ConsensusVersion(),
"slashing": slashing.AppModule{}.ConsensusVersion(),
"gov": gov.AppModule{}.ConsensusVersion(),
"params": params.AppModule{}.ConsensusVersion(),
"upgrade": upgrade.AppModule{}.ConsensusVersion(),
"vesting": vesting.AppModule{}.ConsensusVersion(),
"feegrant": feegrantmodule.AppModule{}.ConsensusVersion(),
"evidence": evidence.AppModule{}.ConsensusVersion(),
"crisis": crisis.AppModule{}.ConsensusVersion(),
"genutil": genutil.AppModule{}.ConsensusVersion(),
"capability": capability.AppModule{}.ConsensusVersion(),
"budget": budget.AppModule{}.ConsensusVersion(),
"farming": farming.AppModule{}.ConsensusVersion(),
"liquidity": liquidity.AppModule{}.ConsensusVersion(),
"liquidstaking": liquidstaking.AppModule{}.ConsensusVersion(),
"claim": claim.AppModule{}.ConsensusVersion(),
"ibc": ibc.AppModule{}.ConsensusVersion(),
"transfer": transfer.AppModule{}.ConsensusVersion(),
},
Expand Down
26 changes: 26 additions & 0 deletions app/upgrades/mainnet/v2.0.0/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v2_0_0

import (
store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
budgetkeeper "github.com/tendermint/budget/x/budget/keeper"
)

const UpgradeName = "v2.0.0"

func UpgradeHandler(mm *module.Manager, configurator module.Configurator, budgetKeeper budgetkeeper.Keeper) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
newVM, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return newVM, err
}

// add budget migration code related mint pool

return newVM, err
}
}

var StoreUpgrades store.StoreUpgrades
2 changes: 1 addition & 1 deletion client/docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Squad REST and gRPC Gateway docs",
"description": "A REST interface for state queries, transactions",
"version": "1.1.0"
"version": "1.2.0"
},
"apis": [
{
Expand Down
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

35 changes: 34 additions & 1 deletion client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ swagger: '2.0'
info:
title: Squad REST and gRPC Gateway docs
description: 'A REST interface for state queries, transactions'
version: 1.1.0
version: 1.2.0
paths:
/node_info:
get:
Expand Down Expand Up @@ -26350,6 +26350,17 @@ paths:
mint_denom:
type: string
title: mint_denom defines denomination of coin to be minted
mint_pool_address:
type: string
title: >-
mint_pool_address defines the address where inflation will
be minted, The default is FeeCollector,

but if it set FeeCollector, it could mix the minted
inflation with the collected tx fee,

Therefore, it is recommended to specify a separate address
depending on the chain
block_time_threshold:
type: string
title: >-
Expand Down Expand Up @@ -46129,6 +46140,17 @@ definitions:
mint_denom:
type: string
title: mint_denom defines denomination of coin to be minted
mint_pool_address:
type: string
title: >-
mint_pool_address defines the address where inflation will be minted,
The default is FeeCollector,

but if it set FeeCollector, it could mix the minted inflation with the
collected tx fee,

Therefore, it is recommended to specify a separate address depending
on the chain
block_time_threshold:
type: string
title: >-
Expand Down Expand Up @@ -46171,6 +46193,17 @@ definitions:
mint_denom:
type: string
title: mint_denom defines denomination of coin to be minted
mint_pool_address:
type: string
title: >-
mint_pool_address defines the address where inflation will be
minted, The default is FeeCollector,

but if it set FeeCollector, it could mix the minted inflation with
the collected tx fee,

Therefore, it is recommended to specify a separate address
depending on the chain
block_time_threshold:
type: string
title: >-
Expand Down
9 changes: 7 additions & 2 deletions proto/squad/mint/v1beta1/mint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ message Params {
// mint_denom defines denomination of coin to be minted
string mint_denom = 1;

// mint_pool_address defines the address where inflation will be minted, The default is FeeCollector,
// but if it set FeeCollector, it could mix the minted inflation with the collected tx fee,
// Therefore, it is recommended to specify a separate address depending on the chain
string mint_pool_address = 2;

// block_time_threshold defines block time threshold to prevent from any inflationary manipulation attacks
// it is used for maximum block duration when calculating block inflation
google.protobuf.Duration block_time_threshold = 2 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
google.protobuf.Duration block_time_threshold = 3 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];

// inflation_schedules defines a list of inflation schedules
repeated InflationSchedule inflation_schedules = 3 [(gogoproto.nullable) = false];
repeated InflationSchedule inflation_schedules = 4 [(gogoproto.nullable) = false];
}

// InflationSchedule defines the start and end time of the inflation period, and the amount of inflation during that
Expand Down
4 changes: 2 additions & 2 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
panic(err)
}

// send the minted coins to the fee collector account
err = k.AddInflationToFeeCollector(ctx, mintedCoins)
// send the minted coins to the mint pool
err = k.SendInflationToMintPool(ctx, mintedCoins)
if err != nil {
panic(err)
}
Expand Down
46 changes: 46 additions & 0 deletions x/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,52 @@ func TestConstantInflation(t *testing.T) {
require.True(t, advanceHeight().IsZero())
}

func TestChangeMintPool(t *testing.T) {
app := chain.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

app.InitChain(
abcitypes.RequestInitChain{
AppStateBytes: []byte("{}"),
ChainId: "test-chain-id",
},
)

blockTime := 5 * time.Second
params := app.MintKeeper.GetParams(ctx)
require.EqualValues(t, params.MintPoolAddress, types.DefaultMintPoolAddress.String())
require.EqualValues(t, params.MintPoolAddress, app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String())

advanceHeight := func(mintPool sdk.AccAddress) sdk.Int {
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1).WithBlockTime(ctx.BlockTime().Add(blockTime))
beforeBalance := app.BankKeeper.GetBalance(ctx, mintPool, sdk.DefaultBondDenom)
mint.BeginBlocker(ctx, app.MintKeeper)
afterBalance := app.BankKeeper.GetBalance(ctx, mintPool, sdk.DefaultBondDenom)
mintedAmt := afterBalance.Sub(beforeBalance)
require.False(t, mintedAmt.IsNegative())
return mintedAmt.Amount
}

ctx = ctx.WithBlockHeight(0).WithBlockTime(utils.ParseTime("2022-01-01T00:00:00Z"))

// skip first block inflation, not set LastBlockTime
require.EqualValues(t, advanceHeight(types.DefaultMintPoolAddress), sdk.NewInt(0))

require.EqualValues(t, advanceHeight(types.DefaultMintPoolAddress), sdk.NewInt(47564687))
require.EqualValues(t, advanceHeight(types.DefaultMintPoolAddress), sdk.NewInt(47564687))
require.EqualValues(t, advanceHeight(types.MintModuleAcc).String(), sdk.NewInt(0).String())
require.EqualValues(t, advanceHeight(types.MintModuleAcc).String(), sdk.NewInt(0).String())

// change mint pool address to mint module account from fee collector
params.MintPoolAddress = types.MintModuleAcc.String()
app.MintKeeper.SetParams(ctx, params)

require.EqualValues(t, advanceHeight(types.DefaultMintPoolAddress).String(), sdk.NewInt(0).String())
require.EqualValues(t, advanceHeight(types.DefaultMintPoolAddress).String(), sdk.NewInt(0).String())
require.EqualValues(t, advanceHeight(types.MintModuleAcc), sdk.NewInt(47564687))
require.EqualValues(t, advanceHeight(types.MintModuleAcc), sdk.NewInt(47564687))
}

func (s *ModuleTestSuite) TestDefaultGenesis() {
genState := *types.DefaultGenesisState()

Expand Down
5 changes: 3 additions & 2 deletions x/mint/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
{
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`{"mint_denom":"stake","block_time_threshold":"10s","inflation_schedules":[{"start_time":"2022-01-01T00:00:00Z","end_time":"2023-01-01T00:00:00Z","amount":"300000000000000"},{"start_time":"2023-01-01T00:00:00Z","end_time":"2024-01-01T00:00:00Z","amount":"200000000000000"}]}`,
`{"mint_denom":"stake","mint_pool_address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","block_time_threshold":"10s","inflation_schedules":[{"start_time":"2022-01-01T00:00:00Z","end_time":"2023-01-01T00:00:00Z","amount":"300000000000000"},{"start_time":"2023-01-01T00:00:00Z","end_time":"2024-01-01T00:00:00Z","amount":"200000000000000"}]}`,
},
{
"text output",
Expand All @@ -80,7 +80,8 @@ inflation_schedules:
- amount: "200000000000000"
end_time: "2024-01-01T00:00:00Z"
start_time: "2023-01-01T00:00:00Z"
mint_denom: stake`,
mint_denom: stake
mint_pool_address: cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta`,
},
}

Expand Down
23 changes: 19 additions & 4 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
feeCollectorName string
}
Expand All @@ -36,6 +37,7 @@ func NewKeeper(
cdc: cdc,
storeKey: key,
paramSpace: paramSpace,
accountKeeper: ak,
bankKeeper: bk,
feeCollectorName: feeCollectorName,
}
Expand Down Expand Up @@ -68,14 +70,27 @@ func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
return k.bankKeeper.MintCoins(ctx, types.ModuleName, newCoins)
}

// AddInflationToFeeCollector implements an alias call to the underlying supply keeper's
// AddInflationToFeeCollector to be used in BeginBlocker.
func (k Keeper) AddInflationToFeeCollector(ctx sdk.Context, fees sdk.Coins) error {
return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.feeCollectorName, fees)
// SendInflationToMintPool sends inflation to params.MintPoolAddress, It to be used in BeginBlocker.
func (k Keeper) SendInflationToMintPool(ctx sdk.Context, inflation sdk.Coins) error {
return k.bankKeeper.SendCoins(ctx,
k.accountKeeper.GetModuleAddress(types.ModuleName),
k.GetMintPoolAddress(ctx),
inflation)
}

// GetInflationSchedules return inflation schedules set on app
func (k Keeper) GetInflationSchedules(ctx sdk.Context) (res []types.InflationSchedule) {
k.paramSpace.Get(ctx, types.KeyInflationSchedules, &res)
return
}

// GetMintPoolAddress return mint pool address on app
func (k Keeper) GetMintPoolAddress(ctx sdk.Context) (acc sdk.AccAddress) {
var addr string
k.paramSpace.Get(ctx, types.KeyMintPoolAddress, &addr)
acc, err := sdk.AccAddressFromBech32(addr)
if err != nil {
panic(err)
}
return
}
22 changes: 22 additions & 0 deletions x/mint/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/cosmosquad-labs/squad/x/mint/legacy/v2"
)

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

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.paramSpace)
}
Loading

0 comments on commit bf4478f

Please sign in to comment.