Skip to content

Commit

Permalink
refactor(x/mint): unexport keeper methods (#2417)
Browse files Browse the repository at this point in the history
* refactor(x/mint): unexport keeper methods

* changelog

* Update x/mint/keeper/keeper.go

Co-authored-by: Adam Tucker <[email protected]>

* Update x/mint/keeper/keeper.go

Co-authored-by: Adam Tucker <[email protected]>

* Update x/mint/keeper/keeper.go

Co-authored-by: Adam Tucker <[email protected]>

* Update x/mint/keeper/keeper.go

Co-authored-by: Adam Tucker <[email protected]>

Co-authored-by: Adam Tucker <[email protected]>
(cherry picked from commit 4176b28)

# Conflicts:
#	CHANGELOG.md
#	app/apptesting/test_suite.go
#	x/mint/keeper/genesis.go
#	x/mint/keeper/hooks.go
#	x/mint/keeper/keeper.go
#	x/mint/keeper/keeper_test.go
  • Loading branch information
p0mvn authored and mergify[bot] committed Aug 16, 2022
1 parent 50d49dd commit 4b4613c
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 61 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ This release contains minor CLI bug fixes.
* [#1671](https://github.com/osmosis-labs/osmosis/pull/1671) Add hourly epochs to `x/epochs` DefaultGenesis.
* [#1665](https://github.com/osmosis-labs/osmosis/pull/1665) Delete app/App interface, instead use simapp.App
* [#1630](https://github.com/osmosis-labs/osmosis/pull/1630) Delete the v043_temp module, now that we're on an updated SDK version.
<<<<<<< HEAD
=======
* [#1667](https://github.com/osmosis-labs/osmosis/pull/1673) Move wasm-bindings code out of app package into its own root level package.
* [#2013](https://github.com/osmosis-labs/osmosis/pull/2013) Make `SetParams`, `SetPool`, `SetTotalLiquidity`, and `SetDenomLiquidity` GAMM APIs private
* [#1857](https://github.com/osmosis-labs/osmosis/pull/1857) x/mint rename GetLastHalvenEpochNum to GetLastReductionEpochNum
* [#2394](https://github.com/osmosis-labs/osmosis/pull/2394) Remove unused interface methods from expected keepers of each module
* [#2390](https://github.com/osmosis-labs/osmosis/pull/2390) x/mint remove unused mintCoins parameter from AfterDistributeMintedCoin
* [#2417](https://github.com/osmosis-labs/osmosis/pull/2417) x/mint unexport keeper `SetLastReductionEpochNum`, `getLastReductionEpochNum`, `CreateDeveloperVestingModuleAccount`, and `MintCoins`

### Features

* [#2387](https://github.com/osmosis-labs/osmosis/pull/2387) Upgrade to IBC v3.2.0, which allows for sending/receiving IBC tokens with slashes.
* [#2057](https://github.com/osmosis-labs/osmosis/pull/2057) Reduce block times to about three seconds
* [#1312] Stableswap: Createpool logic
* [#1230] Stableswap CFMM equations
* [#1429] solver for multi-asset CFMM
* [#1539] Superfluid: Combine superfluid and staking query on querying delegation by delegator
* [#2223] Tokenfactory: Add SetMetadata functionality
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
### Bug Fixes

Expand Down
10 changes: 10 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
gammtypes "github.com/osmosis-labs/osmosis/v11/x/gamm/types"
lockupkeeper "github.com/osmosis-labs/osmosis/v11/x/lockup/keeper"
lockuptypes "github.com/osmosis-labs/osmosis/v11/x/lockup/types"
minttypes "github.com/osmosis-labs/osmosis/v11/x/mint/types"
)

type KeeperTestHelper struct {
Expand Down Expand Up @@ -77,6 +78,15 @@ func (s *KeeperTestHelper) FundAcc(acc sdk.AccAddress, amounts sdk.Coins) {
s.Require().NoError(err)
}

<<<<<<< HEAD
=======
func (s *KeeperTestHelper) MintCoins(coins sdk.Coins) {
err := s.App.BankKeeper.MintCoins(s.Ctx, minttypes.ModuleName, coins)
s.Require().NoError(err)
}

// SetupValidator sets up a validator and returns the ValAddress.
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
func (s *KeeperTestHelper) SetupValidator(bondStatus stakingtypes.BondStatus) sdk.ValAddress {
valPub := secp256k1.GenPrivKey().PubKey()
valAddr := sdk.ValAddress(valPub.Address())
Expand Down
16 changes: 16 additions & 0 deletions x/mint/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
GetProportions = getProportions
)

func (k Keeper) CreateDeveloperVestingModuleAccount(ctx sdk.Context, amount sdk.Coin) error {
return k.createDeveloperVestingModuleAccount(ctx, amount)
}

func (k Keeper) DistributeToModule(ctx sdk.Context, recipientModule string, mintedCoin sdk.Coin, proportion sdk.Dec) (sdk.Int, error) {
return k.distributeToModule(ctx, recipientModule, mintedCoin, proportion)
}
Expand All @@ -28,6 +32,18 @@ func (k Keeper) DistributeDeveloperRewards(ctx sdk.Context, totalMintedCoin sdk.
return k.distributeDeveloperRewards(ctx, totalMintedCoin, developerRewardsProportion, developerRewardsReceivers)
}

func (k Keeper) GetLastReductionEpochNum(ctx sdk.Context) int64 {
return k.getLastReductionEpochNum(ctx)
}

func (k Keeper) SetLastReductionEpochNum(ctx sdk.Context, epochNum int64) {
k.setLastReductionEpochNum(ctx, epochNum)
}

func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
return k.mintCoins(ctx, newCoins)
}

// Set the mint hooks. This is used for testing purposes only.
func (k *Keeper) SetMintHooksUnsafe(h types.MintHooks) *Keeper {
k.hooks = h
Expand Down
10 changes: 9 additions & 1 deletion x/mint/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
if !k.accountKeeper.HasAccount(ctx, k.accountKeeper.GetModuleAddress(types.DeveloperVestingModuleAcctName)) {
totalDeveloperVestingCoins := sdk.NewCoin(data.Params.MintDenom, sdk.NewInt(developerVestingAmount))

if err := k.CreateDeveloperVestingModuleAccount(ctx, totalDeveloperVestingCoins); err != nil {
if err := k.createDeveloperVestingModuleAccount(ctx, totalDeveloperVestingCoins); err != nil {
panic(err)
}

k.bankKeeper.AddSupplyOffset(ctx, data.Params.MintDenom, sdk.NewInt(developerVestingAmount).Neg())
}

<<<<<<< HEAD
k.SetLastHalvenEpochNum(ctx, data.HalvenStartedEpoch)
=======
k.setLastReductionEpochNum(ctx, data.ReductionStartedEpoch)
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
}

// ExportGenesis returns a GenesisState for a given context and keeper.
Expand All @@ -46,6 +50,10 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params.WeightedDeveloperRewardsReceivers = make([]types.WeightedAddress, 0)
}

<<<<<<< HEAD
lastHalvenEpoch := k.GetLastHalvenEpochNum(ctx)
=======
lastHalvenEpoch := k.getLastReductionEpochNum(ctx)
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
return types.NewGenesisState(minter, params, lastHalvenEpoch)
}
14 changes: 13 additions & 1 deletion x/mint/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
if epochNumber < params.MintingRewardsDistributionStartEpoch {
return
} else if epochNumber == params.MintingRewardsDistributionStartEpoch {
<<<<<<< HEAD
k.SetLastHalvenEpochNum(ctx, epochNumber)
=======
k.setLastReductionEpochNum(ctx, epochNumber)
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
}
// fetch stored minter & params
minter := k.GetMinter(ctx)
Expand All @@ -40,19 +44,27 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
// This avoids issues with measuring in block numbers, as epochs have fixed intervals, with very
// low variance at the relevant sizes. As a result, it is safe to store the epoch number
// of the last reduction to be later retrieved for comparison.
<<<<<<< HEAD
if epochNumber >= params.ReductionPeriodInEpochs+k.GetLastHalvenEpochNum(ctx) {
// Reduce the reward per reduction period
minter.EpochProvisions = minter.NextEpochProvisions(params)
k.SetMinter(ctx, minter)
k.SetLastHalvenEpochNum(ctx, epochNumber)
=======
if epochNumber >= params.ReductionPeriodInEpochs+k.getLastReductionEpochNum(ctx) {
// Reduce the reward per reduction period
minter.EpochProvisions = minter.NextEpochProvisions(params)
k.SetMinter(ctx, minter)
k.setLastReductionEpochNum(ctx, epochNumber)
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
}

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

// We over-allocate by the developer vesting portion, and burn this later
err := k.MintCoins(ctx, mintedCoins)
err := k.mintCoins(ctx, mintedCoins)
if err != nil {
panic(err)
}
Expand Down
105 changes: 60 additions & 45 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,6 @@ func (k Keeper) SetInitialSupplyOffsetDuringMigration(ctx sdk.Context) error {
return nil
}

// CreateDeveloperVestingModuleAccount creates the developer vesting module account
// and mints amount of tokens to it.
// Should only be called during the initial genesis creation, never again. Returns nil on success.
// Returns error in the following cases:
// - amount is nil or zero.
// - if ctx has block height greater than 0.
// - developer vesting module account is already created prior to calling this method.
func (k Keeper) CreateDeveloperVestingModuleAccount(ctx sdk.Context, amount sdk.Coin) error {
if amount.IsNil() || amount.Amount.IsZero() {
return sdkerrors.Wrap(types.ErrAmountNilOrZero, "amount cannot be nil or zero")
}
if k.accountKeeper.HasAccount(ctx, k.accountKeeper.GetModuleAddress(types.DeveloperVestingModuleAcctName)) {
return sdkerrors.Wrapf(types.ErrModuleAccountAlreadyExist, "%s vesting module account already exist", types.DeveloperVestingModuleAcctName)
}

moduleAcc := authtypes.NewEmptyModuleAccount(
types.DeveloperVestingModuleAcctName, authtypes.Minter)
k.accountKeeper.SetModuleAccount(ctx, moduleAcc)

err := k.bankKeeper.MintCoins(ctx, types.DeveloperVestingModuleAcctName, sdk.NewCoins(amount))
if err != nil {
return err
}
return nil
}

// _____________________________________________________________________

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
Expand All @@ -136,6 +108,7 @@ func (k *Keeper) SetHooks(h types.MintHooks) *Keeper {
return k
}

<<<<<<< HEAD
// GetLastHalvenEpochNum returns last halven epoch number.
func (k Keeper) GetLastHalvenEpochNum(ctx sdk.Context) int64 {
store := ctx.KVStore(k.storeKey)
Expand All @@ -154,6 +127,9 @@ func (k Keeper) SetLastHalvenEpochNum(ctx sdk.Context, epochNum int64) {
}

// get the minter.
=======
// GetMinter gets the minter.
>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.MinterKey)
Expand All @@ -165,15 +141,13 @@ func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {
return
}

// set the minter.
// SetMinter sets the minter.
func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&minter)
store.Set(types.MinterKey, b)
}

// _____________________________________________________________________

// GetParams returns the total set of minting parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, &params)
Expand All @@ -185,20 +159,7 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

// _____________________________________________________________________

// MintCoins implements an alias call to the underlying supply keeper's
// MintCoins to be used in BeginBlocker.
func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
if newCoins.Empty() {
// skip as no coins need to be minted
return nil
}

return k.bankKeeper.MintCoins(ctx, types.ModuleName, newCoins)
}

// DistributeMintedCoins implements distribution of minted coins from mint to external modules.
// DistributeMintedCoin implements distribution of a minted coin from mint to external modules.
func (k Keeper) DistributeMintedCoin(ctx sdk.Context, mintedCoin sdk.Coin) error {
params := k.GetParams(ctx)
proportions := params.DistributionProportions
Expand Down Expand Up @@ -234,6 +195,34 @@ func (k Keeper) DistributeMintedCoin(ctx sdk.Context, mintedCoin sdk.Coin) error
return err
}

// getLastReductionEpochNum returns last reduction epoch number.
func (k Keeper) getLastReductionEpochNum(ctx sdk.Context) int64 {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.LastReductionEpochKey)
if b == nil {
return 0
}

return int64(sdk.BigEndianToUint64(b))
}

// setLastReductionEpochNum set last reduction epoch number.
func (k Keeper) setLastReductionEpochNum(ctx sdk.Context, epochNum int64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.LastReductionEpochKey, sdk.Uint64ToBigEndian(uint64(epochNum)))
}

// mintCoins implements an alias call to the underlying bank keeper's
// MintCoins to be used in BeginBlocker.
func (k Keeper) mintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
if newCoins.Empty() {
// skip as no coins need to be minted
return nil
}

return k.bankKeeper.MintCoins(ctx, types.ModuleName, newCoins)
}

// distributeToModule distributes mintedCoin multiplied by proportion to the recepientModule account.
func (k Keeper) distributeToModule(ctx sdk.Context, recipientModule string, mintedCoin sdk.Coin, proportion sdk.Dec) (sdk.Int, error) {
distributionCoin, err := getProportions(mintedCoin, proportion)
Expand Down Expand Up @@ -338,3 +327,29 @@ func getProportions(mintedCoin sdk.Coin, ratio sdk.Dec) (sdk.Coin, error) {
}
return sdk.NewCoin(mintedCoin.Denom, mintedCoin.Amount.ToDec().Mul(ratio).TruncateInt()), nil
}

// createDeveloperVestingModuleAccount creates the developer vesting module account
// and mints amount of tokens to it.
// Should only be called during the initial genesis creation, never again. Returns nil on success.
// Returns error in the following cases:
// - amount is nil or zero.
// - if ctx has block height greater than 0.
// - developer vesting module account is already created prior to calling this method.
func (k Keeper) createDeveloperVestingModuleAccount(ctx sdk.Context, amount sdk.Coin) error {
if amount.IsNil() || amount.Amount.IsZero() {
return sdkerrors.Wrap(types.ErrAmountNilOrZero, "amount cannot be nil or zero")
}
if k.accountKeeper.HasAccount(ctx, k.accountKeeper.GetModuleAddress(types.DeveloperVestingModuleAcctName)) {
return sdkerrors.Wrapf(types.ErrModuleAccountAlreadyExist, "%s vesting module account already exist", types.DeveloperVestingModuleAcctName)
}

moduleAcc := authtypes.NewEmptyModuleAccount(
types.DeveloperVestingModuleAcctName, authtypes.Minter)
k.accountKeeper.SetModuleAccount(ctx, moduleAcc)

err := k.bankKeeper.MintCoins(ctx, types.DeveloperVestingModuleAcctName, sdk.NewCoins(amount))
if err != nil {
return err
}
return nil
}
15 changes: 11 additions & 4 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,10 @@ func (suite *KeeperTestSuite) TestDistributeMintedCoin() {
}

// mints coins so supply exists on chain
err := mintKeeper.MintCoins(ctx, sdk.NewCoins(tc.mintCoin))
suite.Require().NoError(err)
suite.MintCoins(sdk.NewCoins(tc.mintCoin))

// System under test.
err = mintKeeper.DistributeMintedCoin(ctx, tc.mintCoin)
err := mintKeeper.DistributeMintedCoin(ctx, tc.mintCoin)
suite.Require().NoError(err)

// validate that AfterDistributeMintedCoin hook was called once.
Expand Down Expand Up @@ -336,6 +335,14 @@ func (suite *KeeperTestSuite) TestSetInitialSupplyOffsetDuringMigration() {
bankKeeper := suite.App.BankKeeper
mintKeeper := suite.App.MintKeeper

<<<<<<< HEAD
=======
// in order to ensure the offset is correctly calculated, we need to mint the supply + 1
// this is because a negative supply offset will always return zero
// by setting this to the supply + 1, we ensure we are correctly calculating the offset by keeping it delta positive
suite.MintCoins(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(keeper.DeveloperVestingAmount+1))))

>>>>>>> 4176b287 (refactor(x/mint): unexport keeper methods (#2417))
supplyWithOffsetBefore := bankKeeper.GetSupplyWithOffset(ctx, sdk.DefaultBondDenom)
supplyOffsetBefore := bankKeeper.GetSupplyOffset(ctx, sdk.DefaultBondDenom)

Expand Down Expand Up @@ -436,7 +443,7 @@ func (suite *KeeperTestSuite) TestDistributeToModule() {
ctx := suite.Ctx

// Setup.
suite.Require().NoError(mintKeeper.MintCoins(ctx, sdk.NewCoins(tc.preMintCoin)))
suite.MintCoins(sdk.NewCoins(tc.preMintCoin))

// TODO: Should not be truncated. Remove truncation after rounding errors are addressed and resolved.
// Ref: https://github.com/osmosis-labs/osmosis/issues/1917
Expand Down
Loading

0 comments on commit 4b4613c

Please sign in to comment.