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

refactor(x/mint)!: use KVStoreService and context.Context #16179

Merged
merged 15 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mint

import (
"context"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -10,12 +11,19 @@ import (
)

// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculationFn) error {
func BeginBlocker(ctx context.Context, k keeper.Keeper, ic types.InflationCalculationFn) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

// fetch stored minter & params
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)
minter, err := k.GetMinter(ctx)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call
if err != nil {
return err
}

params, err := k.GetParams(ctx)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call
if err != nil {
return err
}

// recalculate inflation rate
totalStakingSupply := k.StakingTokenSupply(ctx)
Expand All @@ -28,7 +36,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio
mintedCoin := minter.BlockProvision(params)
mintedCoins := sdk.NewCoins(mintedCoin)

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

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call
if err != nil {
return err
}
Expand All @@ -43,7 +51,8 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio
defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens")
}

ctx.EventManager().EmitEvent(
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()),
Expand Down
12 changes: 10 additions & 2 deletions x/mint/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data *

// ExportGenesis returns a GenesisState for a given context and keeper.
func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
minter := keeper.GetMinter(ctx)
params := keeper.GetParams(ctx)
minter, err := keeper.GetMinter(ctx)
if err != nil {
panic(err)
}

params, err := keeper.GetParams(ctx)
if err != nil {
panic(err)
}

return types.NewGenesisState(minter, params)
}
9 changes: 6 additions & 3 deletions x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *GenesisTestSuite) SetupTest() {
accountKeeper.EXPECT().GetModuleAddress(minterAcc.Name).Return(minterAcc.GetAddress())
accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc)

s.keeper = keeper.NewKeeper(s.cdc, key, stakingKeeper, accountKeeper, bankKeeper, "", "")
s.keeper = keeper.NewKeeper(s.cdc, runtime.NewKVStoreService(key), stakingKeeper, accountKeeper, bankKeeper, "", "")
}

func (s *GenesisTestSuite) TestImportExportGenesis() {
Expand All @@ -71,13 +72,15 @@ func (s *GenesisTestSuite) TestImportExportGenesis() {

s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState)

minter := s.keeper.GetMinter(s.sdkCtx)
minter, err := s.keeper.GetMinter(s.sdkCtx)
s.Require().Equal(genesisState.Minter, minter)
s.Require().NoError(err)

invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test"))
s.Require().Panics(func() { s.keeper.GetMinter(invalidCtx.Ctx) }, "stored minter should not have been nil")
params := s.keeper.GetParams(s.sdkCtx)
params, err := s.keeper.GetParams(s.sdkCtx)
s.Require().Equal(genesisState.Params, params)
s.Require().NoError(err)

genesisState2 := s.keeper.ExportGenesis(s.sdkCtx)
s.Require().Equal(genesisState, genesisState2)
Expand Down
15 changes: 12 additions & 3 deletions x/mint/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,32 @@ var _ types.QueryServer = Keeper{}
// Params returns params of the mint module.
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
params, err := k.GetParams(ctx)
if err != nil {
return nil, err
}

return &types.QueryParamsResponse{Params: params}, nil
}

// Inflation returns minter.Inflation of the mint module.
func (k Keeper) Inflation(c context.Context, _ *types.QueryInflationRequest) (*types.QueryInflationResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
minter := k.GetMinter(ctx)
minter, err := k.GetMinter(ctx)
if err != nil {
return nil, err
}

return &types.QueryInflationResponse{Inflation: minter.Inflation}, nil
}

// AnnualProvisions returns minter.AnnualProvisions of the mint module.
func (k Keeper) AnnualProvisions(c context.Context, _ *types.QueryAnnualProvisionsRequest) (*types.QueryAnnualProvisionsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
minter := k.GetMinter(ctx)
minter, err := k.GetMinter(ctx)
if err != nil {
return nil, err
}

return &types.QueryAnnualProvisionsResponse{AnnualProvisions: minter.AnnualProvisions}, nil
}
14 changes: 10 additions & 4 deletions x/mint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand All @@ -32,6 +33,7 @@ type MintTestSuite struct {
func (suite *MintTestSuite) SetupTest() {
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx

Expand All @@ -45,7 +47,7 @@ func (suite *MintTestSuite) SetupTest() {

suite.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
key,
storeService,
stakingKeeper,
accountKeeper,
bankKeeper,
Expand All @@ -66,15 +68,19 @@ func (suite *MintTestSuite) SetupTest() {
func (suite *MintTestSuite) TestGRPCParams() {
params, err := suite.queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(params.Params, suite.mintKeeper.GetParams(suite.ctx))
kparams, err := suite.mintKeeper.GetParams(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(params.Params, kparams)

inflation, err := suite.queryClient.Inflation(gocontext.Background(), &types.QueryInflationRequest{})
suite.Require().NoError(err)
suite.Require().Equal(inflation.Inflation, suite.mintKeeper.GetMinter(suite.ctx).Inflation)
minter, err := suite.mintKeeper.GetMinter(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(inflation.Inflation, minter.Inflation)

annualProvisions, err := suite.queryClient.AnnualProvisions(gocontext.Background(), &types.QueryAnnualProvisionsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(annualProvisions.AnnualProvisions, suite.mintKeeper.GetMinter(suite.ctx).AnnualProvisions)
suite.Require().Equal(annualProvisions.AnnualProvisions, minter.AnnualProvisions)
}

func TestMintTestSuite(t *testing.T) {
Expand Down
85 changes: 51 additions & 34 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package keeper

import (
"context"
"fmt"

"cosmossdk.io/log"
"cosmossdk.io/math"

storetypes "cosmossdk.io/store/types"
storetypes "cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -16,7 +17,7 @@ import (
// Keeper of the mint store
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
storeService storetypes.KVStoreService
stakingKeeper types.StakingKeeper
bankKeeper types.BankKeeper
feeCollectorName string
Expand All @@ -29,7 +30,7 @@ type Keeper struct {
// NewKeeper creates a new mint Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
storeService storetypes.KVStoreService,
sk types.StakingKeeper,
ak types.AccountKeeper,
bk types.BankKeeper,
Expand All @@ -43,7 +44,7 @@ func NewKeeper(

return Keeper{
cdc: cdc,
storeKey: key,
storeService: storeService,
stakingKeeper: sk,
bankKeeper: bk,
feeCollectorName: feeCollectorName,
Expand All @@ -57,65 +58,81 @@ func (k Keeper) GetAuthority() string {
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
func (k Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return sdkCtx.Logger().With("module", "x/"+types.ModuleName)
}

// GetMinter returns the minter.
func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.MinterKey)
func (k Keeper) GetMinter(ctx context.Context) (types.Minter, error) {
var minter types.Minter
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.MinterKey)
if err != nil {
return minter, err
}

if bz == nil {
panic("stored minter should not have been nil")
return minter, fmt.Errorf("stored minter should not have been nil")
}

k.cdc.MustUnmarshal(bz, &minter)
return
err = k.cdc.Unmarshal(bz, &minter)
return minter, err
}

// SetMinter sets the minter.
func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&minter)
store.Set(types.MinterKey, bz)
func (k Keeper) SetMinter(ctx context.Context, minter types.Minter) error {
store := k.storeService.OpenKVStore(ctx)
bz, err := k.cdc.Marshal(&minter)
if err != nil {
return err
}
return store.Set(types.MinterKey, bz)
}

// SetParams sets the x/mint module parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set(types.ParamsKey, bz)

return nil
func (k Keeper) SetParams(ctx context.Context, params types.Params) error {
store := k.storeService.OpenKVStore(ctx)
bz, err := k.cdc.Marshal(&params)
if err != nil {
return err
}
return store.Set(types.ParamsKey, bz)
}

// GetParams returns the current x/mint module parameters.
func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
func (k Keeper) GetParams(ctx context.Context) (p types.Params, err error) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.ParamsKey)
if err != nil {
return p, err
}

if bz == nil {
return p
return p, nil
}

k.cdc.MustUnmarshal(bz, &p)
return p
err = k.cdc.Unmarshal(bz, &p)
return p, err
}

// StakingTokenSupply implements an alias call to the underlying staking keeper's
// StakingTokenSupply to be used in BeginBlocker.
func (k Keeper) StakingTokenSupply(ctx sdk.Context) math.Int {
return k.stakingKeeper.StakingTokenSupply(ctx)
func (k Keeper) StakingTokenSupply(ctx context.Context) math.Int {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return k.stakingKeeper.StakingTokenSupply(sdkCtx)
}

// BondedRatio implements an alias call to the underlying staking keeper's
// BondedRatio to be used in BeginBlocker.
func (k Keeper) BondedRatio(ctx sdk.Context) math.LegacyDec {
return k.stakingKeeper.BondedRatio(ctx)
func (k Keeper) BondedRatio(ctx context.Context) math.LegacyDec {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return k.stakingKeeper.BondedRatio(sdkCtx)
}

// 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 {
func (k Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error {
if newCoins.Empty() {
// skip as no coins need to be minted
return nil
Expand All @@ -126,6 +143,6 @@ func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {

// AddCollectedFees implements an alias call to the underlying supply keeper's
// AddCollectedFees to be used in BeginBlocker.
func (k Keeper) AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error {
func (k Keeper) AddCollectedFees(ctx context.Context, fees sdk.Coins) error {
return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.feeCollectorName, fees)
}
12 changes: 8 additions & 4 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand Down Expand Up @@ -37,6 +38,7 @@ func TestKeeperTestSuite(t *testing.T) {
func (s *IntegrationTestSuite) SetupTest() {
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
s.ctx = testCtx.Ctx

Expand All @@ -50,7 +52,7 @@ func (s *IntegrationTestSuite) SetupTest() {

s.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
key,
storeService,
stakingKeeper,
accountKeeper,
bankKeeper,
Expand Down Expand Up @@ -106,16 +108,18 @@ func (s *IntegrationTestSuite) TestParams() {
tc := tc

s.Run(tc.name, func() {
expected := s.mintKeeper.GetParams(s.ctx)
err := s.mintKeeper.SetParams(s.ctx, tc.input)
expected, err := s.mintKeeper.GetParams(s.ctx)
s.Require().NoError(err)
err = s.mintKeeper.SetParams(s.ctx, tc.input)
if tc.expectErr {
s.Require().Error(err)
} else {
expected = tc.input
s.Require().NoError(err)
}

p := s.mintKeeper.GetParams(s.ctx)
p, err := s.mintKeeper.GetParams(s.ctx)
s.Require().NoError(err)
s.Require().Equal(expected, p)
})
}
Expand Down
Loading