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!: use KVStoreService in x/auth #15520

Merged
merged 23 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
54 changes: 40 additions & 14 deletions x/auth/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,32 @@ func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc sdk.AccountI) sdk.Accoun

// HasAccount implements AccountKeeperI.
func (ak AccountKeeper) HasAccount(ctx sdk.Context, addr sdk.AccAddress) bool {
store := ctx.KVStore(ak.storeKey)
return store.Has(types.AddressStoreKey(addr))
store := ak.storeSvc.OpenKVStore(ctx)
has, err := store.Has(types.AddressStoreKey(addr))
if err != nil {
panic(err)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
return has
}

// HasAccountAddressByID checks account address exists by id.
func (ak AccountKeeper) HasAccountAddressByID(ctx sdk.Context, id uint64) bool {
store := ctx.KVStore(ak.storeKey)
return store.Has(types.AccountNumberStoreKey(id))
store := ak.storeSvc.OpenKVStore(ctx)
has, err := store.Has(types.AccountNumberStoreKey(id))
if err != nil {
panic(err)
}
return has
}

// GetAccount implements AccountKeeperI.
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) sdk.AccountI {
store := ctx.KVStore(ak.storeKey)
bz := store.Get(types.AddressStoreKey(addr))
store := ak.storeSvc.OpenKVStore(ctx)
bz, err := store.Get(types.AddressStoreKey(addr))
if err != nil {
panic(err)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}

if bz == nil {
return nil
}
Expand All @@ -52,8 +64,12 @@ func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) sdk.Acc

// GetAccountAddressById returns account address by id.
func (ak AccountKeeper) GetAccountAddressByID(ctx sdk.Context, id uint64) string {
store := ctx.KVStore(ak.storeKey)
bz := store.Get(types.AccountNumberStoreKey(id))
store := ak.storeSvc.OpenKVStore(ctx)
bz, err := store.Get(types.AccountNumberStoreKey(id))
if err != nil {
panic(err)
}

if bz == nil {
return ""
}
Expand All @@ -73,7 +89,7 @@ func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []sdk.AccountI
// SetAccount implements AccountKeeperI.
func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc sdk.AccountI) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.storeKey)
store := ak.storeSvc.OpenKVStore(ctx)

bz, err := ak.MarshalAccount(acc)
if err != nil {
Expand All @@ -88,16 +104,26 @@ func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc sdk.AccountI) {
// NOTE: this will cause supply invariant violation if called
func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc sdk.AccountI) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.storeKey)
store.Delete(types.AddressStoreKey(addr))
store.Delete(types.AccountNumberStoreKey(acc.GetAccountNumber()))
store := ak.storeSvc.OpenKVStore(ctx)
err := store.Delete(types.AddressStoreKey(addr))
if err != nil {
panic(err)
}

err = store.Delete(types.AccountNumberStoreKey(acc.GetAccountNumber()))
if err != nil {
panic(err)
}
}

// IterateAccounts iterates over all the stored accounts and performs a callback function.
// Stops iteration when callback returns true.
func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account sdk.AccountI) (stop bool)) {
store := ctx.KVStore(ak.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)
store := ak.storeSvc.OpenKVStore(ctx)
iterator, err := store.Iterator(types.AddressStoreKeyPrefix, storetypes.PrefixEndBytes(types.AddressStoreKeyPrefix))
if err != nil {
panic(err)
}

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down
14 changes: 12 additions & 2 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"cosmossdk.io/log"
gogotypes "github.com/cosmos/gogoproto/types"

corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -56,6 +58,7 @@ type AccountKeeperI interface {
// AccountKeeper encodes/decodes accounts using the go-amino (binary)
// encoding/decoding library.
type AccountKeeper struct {
storeSvc corestore.KVStoreService
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
permAddrs map[string]types.PermissionsForAddress
Expand Down Expand Up @@ -88,7 +91,10 @@ func NewAccountKeeper(

bech32Codec := NewBech32Codec(bech32Prefix)

storeSvc := runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey))

return AccountKeeper{
storeSvc: storeSvc,
storeKey: storeKey,
proto: proto,
cdc: cdc,
Expand Down Expand Up @@ -138,9 +144,13 @@ func (ak AccountKeeper) GetSequence(ctx sdk.Context, addr sdk.AccAddress) (uint6
// If the global account number is not set, it initializes it with value 0.
func (ak AccountKeeper) NextAccountNumber(ctx sdk.Context) uint64 {
var accNumber uint64
store := ctx.KVStore(ak.storeKey)
store := ak.storeSvc.OpenKVStore(ctx)

bz := store.Get(types.GlobalAccountNumberKey)
bz, err := store.Get(types.GlobalAccountNumberKey)
if err != nil {
// panics only on nil key, which should not be possible
panic(err)
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
}
if bz == nil {
// initialize the account numbers
accNumber = 0
Expand Down
9 changes: 4 additions & 5 deletions x/auth/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
// Migrate2to3 migrates from consensus version 2 to version 3. Specifically, for each account
// we index the account's ID to their address.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
return v3.MigrateStore(ctx, m.keeper.storeSvc, m.keeper.cdc)
}

// Migrate3to4 migrates the x/auth module state from the consensus version 3 to
// version 4. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/auth
// module state.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc)
return v4.Migrate(ctx, m.keeper.storeSvc, m.legacySubspace, m.keeper.cdc)
}

// V45_SetAccount implements V45_SetAccount
Expand All @@ -65,13 +65,12 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
// NOTE: This is used for testing purposes only.
func (m Migrator) V45_SetAccount(ctx sdk.Context, acc sdk.AccountI) error { //nolint:revive
addr := acc.GetAddress()
store := ctx.KVStore(m.keeper.storeKey)
store := m.keeper.storeSvc.OpenKVStore(ctx)

bz, err := m.keeper.MarshalAccount(acc)
if err != nil {
return err
}

store.Set(types.AddressStoreKey(addr), bz)
return nil
return store.Set(types.AddressStoreKey(addr), bz)
}
14 changes: 8 additions & 6 deletions x/auth/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ func (ak AccountKeeper) SetParams(ctx sdk.Context, params types.Params) error {
return err
}

store := ctx.KVStore(ak.storeKey)
store := ak.storeSvc.OpenKVStore(ctx)
bz := ak.cdc.MustMarshal(&params)
store.Set(types.ParamsKey, bz)

return nil
return store.Set(types.ParamsKey, bz)
}

// GetParams gets the auth module's parameters.
func (ak AccountKeeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(ak.storeKey)
bz := store.Get(types.ParamsKey)
store := ak.storeSvc.OpenKVStore(ctx)
bz, err := store.Get(types.ParamsKey)
if err != nil {
panic(err)
}

if bz == nil {
return params
}
Expand Down
5 changes: 3 additions & 2 deletions x/auth/migrations/v2/store_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/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
tKey := storetypes.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)
storeSvc := runtime.NewKVStoreService(storeKey)

var (
accountKeeper keeper.AccountKeeper
Expand All @@ -64,7 +65,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
require.NoError(t, err)

legacySubspace := newMockSubspace(authtypes.DefaultParams())
require.NoError(t, v4.Migrate(ctx, store, legacySubspace, cdc))
require.NoError(t, v4.Migrate(ctx, storeSvc, legacySubspace, cdc))

ctx = app.BaseApp.NewContext(false, cmtproto.Header{Time: time.Now()})
stakingKeeper.SetParams(ctx, stakingtypes.DefaultParams())
Expand Down
12 changes: 8 additions & 4 deletions x/auth/migrations/v3/store.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package v3

import (
corestore "cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

func mapAccountAddressToAccountID(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)
func mapAccountAddressToAccountID(ctx sdk.Context, storeSvc corestore.KVStoreService, cdc codec.BinaryCodec) error {
store := storeSvc.OpenKVStore(ctx)
iterator, err := store.Iterator(types.AddressStoreKeyPrefix, storetypes.PrefixEndBytes(types.AddressStoreKeyPrefix))
if err != nil {
return err
}

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -27,6 +31,6 @@ func mapAccountAddressToAccountID(ctx sdk.Context, storeKey storetypes.StoreKey,
// MigrateStore performs in-place store migrations from v0.45 to v0.46. The
// migration includes:
// - Add an Account number as an index to get the account address
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
func MigrateStore(ctx sdk.Context, storeKey corestore.KVStoreService, cdc codec.BinaryCodec) error {
return mapAccountAddressToAccountID(ctx, storeKey, cdc)
}
5 changes: 3 additions & 2 deletions x/auth/migrations/v3/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
tKey := storetypes.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)
storeSvc := runtime.NewKVStoreService(storeKey)

var accountKeeper keeper.AccountKeeper

Expand All @@ -55,7 +56,7 @@ func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) {
require.NoError(t, err)

legacySubspace := newMockSubspace(authtypes.DefaultParams())
require.NoError(t, v4.Migrate(ctx, store, legacySubspace, cdc))
require.NoError(t, v4.Migrate(ctx, storeSvc, legacySubspace, cdc))

// new base account
senderPrivKey := secp256k1.GenPrivKey()
Expand Down
6 changes: 4 additions & 2 deletions x/auth/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v4

import (
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 @@ -15,7 +15,9 @@ var ParamsKey = []byte{0x00}
// version 4. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/auth
// module state.
func Migrate(ctx sdk.Context, store storetypes.KVStore, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
func Migrate(ctx sdk.Context, storeSvc storetypes.KVStoreService, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
store := storeSvc.OpenKVStore(ctx)

var currParams types.Params
legacySubspace.GetParamSet(ctx, &currParams)

Expand Down
8 changes: 5 additions & 3 deletions x/auth/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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 @@ -36,13 +37,14 @@ func TestMigrate(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
tKey := storetypes.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)
storeSvc := runtime.NewKVStoreService(storeKey)

legacySubspace := newMockSubspace(types.DefaultParams())
require.NoError(t, v4.Migrate(ctx, store, legacySubspace, cdc))
require.NoError(t, v4.Migrate(ctx, storeSvc, legacySubspace, cdc))

var res types.Params
bz := store.Get(v4.ParamsKey)
bz, err := storeSvc.OpenKVStore(ctx).Get(v4.ParamsKey)
require.NoError(t, err)
require.NoError(t, cdc.Unmarshal(bz, &res))
require.Equal(t, legacySubspace.ps, res)
}
5 changes: 3 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"cosmossdk.io/depinject"

"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/types/address"

modulev1 "cosmossdk.io/api/cosmos/auth/module/v1"

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

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -214,7 +215,7 @@ type AuthInputs struct {
depinject.In

Config *modulev1.Module
Key *store.KVStoreKey
Key *storetypes.KVStoreKey
Cdc codec.Codec

RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"`
Expand Down