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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (x/mint) [#16179](https://github.com/cosmos/cosmos-sdk/issues/16179) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error`.
* (x/crisis) [#16216](https://github.com/cosmos/cosmos-sdk/issues/16216) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error` instead of panicking.
* (x/gov) [#15988](https://github.com/cosmos/cosmos-sdk/issues/15988) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error` (instead of panicking or returning a `found bool`). Iterators callback functions now return an error instead of a `bool`.
* (x/auth) [#15985](https://github.com/cosmos/cosmos-sdk/pull/15985) The `AccountKeeper` does not expose the `QueryServer` and `MsgServer` APIs anymore.
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The following modules `NewKeeper` function now take a `KVStoreService` instead o
* `x/evidence`
* `x/feegrant`
* `x/gov`
* `x/mint`
* `x/nft`

User manually wiring their chain need to use the `runtime.NewKVStoreService` method to create a `KVStoreService` from a `StoreKey`:
Expand All @@ -99,6 +100,7 @@ The following modules' `Keeper` methods now take in a `context.Context` instead

* `x/authz`
* `x/bank`
* `x/mint`
* `x/crisis`
* `x/distribution`
* `x/evidence`
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func NewSimApp(
app.StakingKeeper = stakingkeeper.NewKeeper(
appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.MintKeeper = mintkeeper.NewKeeper(appCodec, keys[minttypes.StoreKey], app.StakingKeeper, app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String())
app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), app.StakingKeeper, app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String())

app.DistrKeeper = distrkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[distrtypes.StoreKey]), app.AccountKeeper, app.BankKeeper, app.StakingKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String())

Expand Down
8 changes: 6 additions & 2 deletions testutil/integration/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Example() {

// here bankkeeper and staking keeper is nil because we are not testing them
// subspace is nil because we don't test params (which is legacy anyway)
mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, keys[minttypes.StoreKey], nil, accountKeeper, nil, authtypes.FeeCollectorName, authority)
mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), nil, accountKeeper, nil, authtypes.FeeCollectorName, authority)
mintModule := mint.NewAppModule(encodingCfg.Codec, mintKeeper, accountKeeper, nil, nil)

// create the application and register all the modules from the previous step
Expand Down Expand Up @@ -98,7 +98,11 @@ func Example() {
sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context())

// we should also check the state of the application
got := mintKeeper.GetParams(sdkCtx)
got, err := mintKeeper.GetParams(sdkCtx)
if err != nil {
panic(err)
}

if diff := cmp.Diff(got, params); diff != "" {
panic(diff)
}
Expand Down
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)
}
13 changes: 9 additions & 4 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,17 @@ 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)
_, err = s.keeper.GetMinter(invalidCtx.Ctx)
s.Require().EqualError(err, "stored minter should not have been nil")

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
Loading