Skip to content

Commit

Permalink
refactor(auth): migrate to use env (#19476)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Feb 19, 2024
1 parent 72eae6d commit c883624
Show file tree
Hide file tree
Showing 23 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func NewSimApp(
}
app.AccountsKeeper = accountsKeeper

app.AuthKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, addressCodec, sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String())
app.AuthKeeper = authkeeper.NewAccountKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), logger), appCodec, authtypes.ProtoBaseAccount, maccPerms, addressCodec, sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String())

app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func initFixture(t *testing.T) *fixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func initFixture(tb testing.TB) *fixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func Example() {
newCtx := sdk.NewContext(cms, true, logger)

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
encodingCfg.Codec,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
map[string][]string{minttypes.ModuleName: {authtypes.Minter}},
addresscodec.NewBech32Codec("cosmos"),
Expand Down Expand Up @@ -129,8 +129,8 @@ func Example_oneModule() {
newCtx := sdk.NewContext(cms, true, logger)

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
encodingCfg.Codec,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
map[string][]string{minttypes.ModuleName: {authtypes.Minter}},
addresscodec.NewBech32Codec("cosmos"),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func initFixture(tb testing.TB) *fixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func initFixture(tb testing.TB) *fixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func initFixture(tb testing.TB) *fixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
}

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix),
Expand Down
1 change: 1 addition & 0 deletions x/auth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#17985](https://github.com/cosmos/cosmos-sdk/pull/17985) Remove `StdTxConfig`
* [#19161](https://github.com/cosmos/cosmos-sdk/pull/19161) Remove `simulate` from `SetGasMeter`
* [#19363](https://github.com/cosmos/cosmos-sdk/pull/19363) Remove `IterateAccounts` and `GetAllAccounts` methods from the AccountKeeper interface and Keeper.
* [#19290](https://github.com/cosmos/cosmos-sdk/issues/19290) Pass `appmodule.Environment` to NewKeeper instead of passing individual services.

### Consensus Breaking Changes

Expand Down
3 changes: 2 additions & 1 deletion x/auth/ante/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// ref: https://github.com/cosmos/cosmos-sdk/issues/14647
_ "cosmossdk.io/api/cosmos/bank/v1beta1"
_ "cosmossdk.io/api/cosmos/crypto/secp256k1"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth"
"cosmossdk.io/x/auth/ante"
Expand Down Expand Up @@ -78,7 +79,7 @@ func SetupTestSuite(t *testing.T, isCheckTx bool) *AnteTestSuite {
}

suite.accountKeeper = keeper.NewAccountKeeper(
suite.encCfg.Codec, runtime.NewKVStoreService(key), types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"),
runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()), suite.encCfg.Codec, types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"),
sdk.Bech32MainPrefix, types.NewModuleAddress("gov").String(),
)
suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName)
Expand Down
9 changes: 4 additions & 5 deletions x/auth/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
modulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
"cosmossdk.io/x/auth/keeper"
Expand All @@ -29,9 +28,9 @@ func init() {
type ModuleInputs struct {
depinject.In

Config *modulev1.Module
StoreService store.KVStoreService
Cdc codec.Codec
Config *modulev1.Module
Environment appmodule.Environment
Cdc codec.Codec

AddressCodec address.Codec
RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"`
Expand Down Expand Up @@ -70,7 +69,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
panic(err)
}

k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, auth)
k := keeper.NewAccountKeeper(in.Environment, in.Cdc, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, auth)
m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn)

return ModuleOutputs{AccountKeeper: k, Module: m}
Expand Down
14 changes: 8 additions & 6 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/stretchr/testify/suite"
"pgregory.net/rapid"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/header"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth"
authcodec "cosmossdk.io/x/auth/codec"
Expand All @@ -32,7 +33,7 @@ type DeterministicTestSuite struct {
accountNumberLanes uint64

key *storetypes.KVStoreKey
storeService corestore.KVStoreService
environment appmodule.Environment
ctx sdk.Context
queryClient types.QueryClient
accountKeeper keeper.AccountKeeper
Expand All @@ -56,6 +57,7 @@ func (suite *DeterministicTestSuite) SetupTest() {
suite.Require()
key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
env := runtime.NewEnvironment(storeService, log.NewNopLogger())
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx.WithHeaderInfo(header.Info{})

Expand All @@ -69,8 +71,8 @@ func (suite *DeterministicTestSuite) SetupTest() {
}

suite.accountKeeper = keeper.NewAccountKeeper(
env,
suite.encCfg.Codec,
storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
Expand All @@ -83,7 +85,7 @@ func (suite *DeterministicTestSuite) SetupTest() {
suite.queryClient = types.NewQueryClient(queryHelper)

suite.key = key
suite.storeService = storeService
suite.environment = env
suite.maccPerms = maccPerms
suite.accountNumberLanes = 1
}
Expand Down Expand Up @@ -289,8 +291,8 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccounts() {
}

ak := keeper.NewAccountKeeper(
suite.environment,
suite.encCfg.Codec,
suite.storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
Expand Down Expand Up @@ -336,8 +338,8 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccountByName() {
maccPerms[mName] = mPerms

ak := keeper.NewAccountKeeper(
suite.environment,
suite.encCfg.Codec,
suite.storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
Expand Down
12 changes: 6 additions & 6 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/collections/indexes"
"cosmossdk.io/core/address"
"cosmossdk.io/core/store"
"cosmossdk.io/core/appmodule"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/x/auth/types"
Expand Down Expand Up @@ -82,7 +82,7 @@ func (a AccountsIndexes) IndexesList() []collections.Index[sdk.AccAddress, sdk.A
type AccountKeeper struct {
addressCodec address.Codec

storeService store.KVStoreService
Environment appmodule.Environment
cdc codec.BinaryCodec
permAddrs map[string]types.PermissionsForAddress
bech32Prefix string
Expand Down Expand Up @@ -111,20 +111,20 @@ var _ AccountKeeperI = &AccountKeeper{}
// and don't have to fit into any predefined structure. This auth module does not use account permissions internally, though other modules
// may use auth.Keeper to access the accounts permissions map.
func NewAccountKeeper(
cdc codec.BinaryCodec, storeService store.KVStoreService, proto func() sdk.AccountI,
env appmodule.Environment, cdc codec.BinaryCodec, proto func() sdk.AccountI,
maccPerms map[string][]string, ac address.Codec, bech32Prefix, authority string,
) AccountKeeper {
permAddrs := make(map[string]types.PermissionsForAddress)
for name, perms := range maccPerms {
permAddrs[name] = types.NewPermissionsForAddress(name, perms)
}

sb := collections.NewSchemaBuilder(storeService)
sb := collections.NewSchemaBuilder(env.KVStoreService)

ak := AccountKeeper{
addressCodec: ac,
bech32Prefix: bech32Prefix,
storeService: storeService,
Environment: env,
proto: proto,
cdc: cdc,
permAddrs: permAddrs,
Expand Down Expand Up @@ -154,7 +154,7 @@ func (ak AccountKeeper) AddressCodec() address.Codec {

// Logger returns a module-specific logger.
func (ak AccountKeeper) Logger(ctx context.Context) log.Logger {
return sdk.UnwrapSDKContext(ctx).Logger().With("module", "x/"+types.ModuleName)
return ak.Environment.Logger.With("module", "x/"+types.ModuleName)
}

// GetPubKey Returns the PubKey of the account at address
Expand Down
4 changes: 3 additions & 1 deletion x/auth/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"

"cosmossdk.io/core/header"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth"
authcodec "cosmossdk.io/x/auth/codec"
Expand Down Expand Up @@ -49,6 +50,7 @@ func (suite *KeeperTestSuite) SetupTest() {

key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
env := runtime.NewEnvironment(storeService, log.NewNopLogger())
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx.WithHeaderInfo(header.Info{})

Expand All @@ -62,8 +64,8 @@ func (suite *KeeperTestSuite) SetupTest() {
}

suite.accountKeeper = keeper.NewAccountKeeper(
env,
suite.encCfg.Codec,
storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
Expand Down
4 changes: 2 additions & 2 deletions x/auth/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m Migrator) Migrate3to4(ctx context.Context) error {
// It migrates the GlobalAccountNumber from being a protobuf defined value to a
// big-endian encoded uint64, it also migrates it to use a more canonical prefix.
func (m Migrator) Migrate4To5(ctx context.Context) error {
return v5.Migrate(ctx, m.keeper.storeService, m.keeper.AccountNumber)
return v5.Migrate(ctx, m.keeper.Environment.KVStoreService, m.keeper.AccountNumber)
}

// V45_SetAccount implements V45_SetAccount
Expand All @@ -51,7 +51,7 @@ func (m Migrator) Migrate4To5(ctx context.Context) error {
// NOTE: This is used for testing purposes only.
func (m Migrator) V45SetAccount(ctx context.Context, acc sdk.AccountI) error {
addr := acc.GetAddress()
store := m.keeper.storeService.OpenKVStore(ctx)
store := m.keeper.Environment.KVStoreService.OpenKVStore(ctx)

bz, err := m.keeper.Accounts.ValueCodec().Encode(acc)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions x/auth/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"

"cosmossdk.io/x/auth/types"

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

var _ types.MsgServer = msgServer{}
Expand All @@ -22,7 +20,7 @@ func NewMsgServerImpl(ak AccountKeeper) types.MsgServer {
}
}

func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
func (ms msgServer) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.ak.authority != msg.Authority {
return nil, fmt.Errorf(
"expected authority account as only signer for proposal message; invalid authority; expected %s, got %s",
Expand All @@ -33,7 +31,6 @@ func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdatePara
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := ms.ak.Params.Set(ctx, msg.Params); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions x/auth/vesting/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ func FuzzMsgServerCreateVestingAccount(f *testing.F) {
}

key := storetypes.NewKVStoreKey(authtypes.StoreKey)
storeService := runtime.NewKVStoreService(key)

env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger())
maccPerms := map[string][]string{}

encCfg := moduletestutil.MakeTestEncodingConfig()
accountKeeper := authkeeper.NewAccountKeeper(
env,
encCfg.Codec,
storeService,
authtypes.ProtoBaseAccount,
maccPerms,
address.NewBech32Codec("cosmos"),
Expand All @@ -103,6 +102,7 @@ func FuzzMsgServerCreateVestingAccount(f *testing.F) {
return
}

storeService := runtime.NewKVStoreService(key)
ctrl := gomock.NewController(t)
authKeeper := banktestutil.NewMockAccountKeeper(ctrl)
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
Expand Down
3 changes: 1 addition & 2 deletions x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func (s msgServer) CreateVestingAccount(ctx context.Context, msg *types.MsgCreat
} else {
start := msg.StartTime
if msg.StartTime == 0 {
sdkctx := sdk.UnwrapSDKContext(ctx)
start = sdkctx.HeaderInfo().Time.Unix()
start = s.AccountKeeper.Environment.HeaderService.GetHeaderInfo(ctx).Time.Unix()
}
vestingAccount = types.NewContinuousVestingAccountRaw(baseVestingAccount, start)
}
Expand Down
Loading

0 comments on commit c883624

Please sign in to comment.