From 74d89bf506d7bac6b5422756735f4d203fc2d3fa Mon Sep 17 00:00:00 2001 From: son trinh Date: Mon, 3 Jun 2024 20:40:03 +0700 Subject: [PATCH] refactor(x/accounts)!: accounts and auth module use the same account number tracking (#20405) --- .../bank/keeper/deterministic_test.go | 7 +++++ .../distribution/keeper/msg_server_test.go | 7 +++++ .../evidence/keeper/infraction_test.go | 6 ++++ tests/integration/example/example_test.go | 13 +++++++++ tests/integration/gov/keeper/keeper_test.go | 7 +++++ .../slashing/keeper/keeper_test.go | 9 +++++- .../integration/staking/keeper/common_test.go | 9 +++++- .../staking/keeper/deterministic_test.go | 7 +++++ x/accounts/keeper.go | 24 +++++++++++++++ x/auth/ante/testutil_test.go | 7 +++++ x/auth/keeper/account.go | 7 ++++- x/auth/keeper/deterministic_test.go | 7 +++++ x/auth/keeper/genesis.go | 5 +++- x/auth/keeper/keeper.go | 13 +++++---- x/auth/keeper/keeper_test.go | 13 +++++++-- x/auth/keeper/migrations.go | 21 ++++++++++++-- x/auth/migrations/v6/migrate.go | 12 ++++++++ x/auth/module.go | 5 +++- x/auth/testutil/expected_keepers_mocks.go | 29 +++++++++++++++++++ x/auth/types/expected_keepers.go | 5 ++++ .../testutil/expected_keepers_mocks.go | 29 +++++++++++++++++++ .../migrations/v4/migrate_funds_test.go | 8 +++++ x/group/migrations/v2/migrate_test.go | 12 ++++++-- 23 files changed, 245 insertions(+), 17 deletions(-) create mode 100644 x/auth/migrations/v6/migrate.go diff --git a/tests/integration/bank/keeper/deterministic_test.go b/tests/integration/bank/keeper/deterministic_test.go index bcc02b3a9abb..130503b3dc41 100644 --- a/tests/integration/bank/keeper/deterministic_test.go +++ b/tests/integration/bank/keeper/deterministic_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "testing" "github.com/golang/mock/gomock" @@ -84,6 +85,12 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { // gomock initializations ctrl := gomock.NewController(t) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index c9181c7035a7..4bf6d47e6c77 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "fmt" "testing" @@ -95,6 +96,12 @@ func initFixture(t *testing.T) *fixture { // gomock initializations ctrl := gomock.NewController(t) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index 5ff02d0c1ffe..0be69083614b 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -107,6 +107,12 @@ func initFixture(tb testing.TB) *fixture { // gomock initializations ctrl := gomock.NewController(tb) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) maccPerms := map[string][]string{ pooltypes.ModuleName: {}, diff --git a/tests/integration/example/example_test.go b/tests/integration/example/example_test.go index 063250603650..92fdbbeebb07 100644 --- a/tests/integration/example/example_test.go +++ b/tests/integration/example/example_test.go @@ -1,6 +1,7 @@ package integration_test import ( + "context" "fmt" "io" "testing" @@ -49,6 +50,12 @@ func Example() { // gomock initializations ctrl := gomock.NewController(&testing.T{}) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), @@ -147,6 +154,12 @@ func Example_oneModule() { // gomock initializations ctrl := gomock.NewController(&testing.T{}) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index dfd89f42f193..d98e86c06dd0 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "testing" "github.com/golang/mock/gomock" @@ -77,6 +78,12 @@ func initFixture(tb testing.TB) *fixture { // gomock initializations ctrl := gomock.NewController(tb) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index d5eefc731517..2e925d49c4e7 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "testing" "time" @@ -81,8 +82,14 @@ func initFixture(tb testing.TB) *fixture { queryRouter := baseapp.NewGRPCQueryRouter() // gomock initializations - ctrl := gomock.NewController(&testing.T{}) + ctrl := gomock.NewController(tb) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithQueryRouterService(queryRouter), runtime.EnvWithMsgRouterService(msgRouter)), diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index b9075c8ad954..c9fd09258d37 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "math/big" "testing" @@ -134,7 +135,7 @@ func initFixture(tb testing.TB) *fixture { } // gomock initializations - ctrl := gomock.NewController(&testing.T{}) + ctrl := gomock.NewController(tb) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) accountKeeper := authkeeper.NewAccountKeeper( @@ -187,6 +188,12 @@ func initFixture(tb testing.TB) *fixture { // set default staking params assert.NilError(tb, stakingKeeper.Params.Set(sdkCtx, types.DefaultParams())) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) f := fixture{ app: integrationApp, diff --git a/tests/integration/staking/keeper/deterministic_test.go b/tests/integration/staking/keeper/deterministic_test.go index 4da0b8f8c2b2..5b5ff3232b68 100644 --- a/tests/integration/staking/keeper/deterministic_test.go +++ b/tests/integration/staking/keeper/deterministic_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "testing" "time" @@ -92,6 +93,12 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { // gomock initializations ctrl := gomock.NewController(t) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currentNum := accNum + accNum++ + return currentNum, nil + }) accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), diff --git a/x/accounts/keeper.go b/x/accounts/keeper.go index 887138626695..a58c32604065 100644 --- a/x/accounts/keeper.go +++ b/x/accounts/keeper.go @@ -107,6 +107,30 @@ func (k Keeper) IsAccountsModuleAccount( return hasAcc } +func (k Keeper) NextAccountNumber( + ctx context.Context, +) (accNum uint64, err error) { + accNum, err = k.AccountNumber.Next(ctx) + if err != nil { + return 0, err + } + + return accNum, nil +} + +// InitAccountNumberSeqUnsafe use to set accounts account number tracking. +// Only use for account number migration. +func (k Keeper) InitAccountNumberSeqUnsafe(ctx context.Context, accNum uint64) error { + currentNum, err := k.AccountNumber.Peek(ctx) + if err != nil { + return err + } + if currentNum > accNum { + return fmt.Errorf("cannot set number lower than current account number got %v while current account number is %v", accNum, currentNum) + } + return k.AccountNumber.Set(ctx, accNum) +} + // Init creates a new account of the given type. func (k Keeper) Init( ctx context.Context, diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index c2916d4a2906..8dff25c7713f 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -1,6 +1,7 @@ package ante_test import ( + "context" "testing" abci "github.com/cometbft/cometbft/abci/types" @@ -79,6 +80,12 @@ func SetupTestSuite(t *testing.T, isCheckTx bool) *AnteTestSuite { suite.ctx = testCtx.Ctx.WithIsCheckTx(isCheckTx).WithBlockHeight(1) suite.encCfg = moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, auth.AppModule{}) + accNum := uint64(0) + suite.acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currNum := accNum + accNum++ + return currNum, nil + }) maccPerms := map[string][]string{ "fee_collector": nil, "mint": {"minter"}, diff --git a/x/auth/keeper/account.go b/x/auth/keeper/account.go index 3e21fd9a8594..d19b15d99b83 100644 --- a/x/auth/keeper/account.go +++ b/x/auth/keeper/account.go @@ -22,7 +22,12 @@ func (ak AccountKeeper) NewAccountWithAddress(ctx context.Context, addr sdk.AccA // NewAccount sets the next account number to a given account interface func (ak AccountKeeper) NewAccount(ctx context.Context, acc sdk.AccountI) sdk.AccountI { - if err := acc.SetAccountNumber(ak.NextAccountNumber(ctx)); err != nil { + accNum, err := ak.AccountsModKeeper.NextAccountNumber(ctx) + if err != nil { + panic(err) + } + + if err := acc.SetAccountNumber(accNum); err != nil { panic(err) } diff --git a/x/auth/keeper/deterministic_test.go b/x/auth/keeper/deterministic_test.go index 43835c072241..75057368fb9d 100644 --- a/x/auth/keeper/deterministic_test.go +++ b/x/auth/keeper/deterministic_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "encoding/hex" "sort" "sync/atomic" @@ -69,6 +70,12 @@ func (suite *DeterministicTestSuite) SetupTest() { ctrl := gomock.NewController(suite.T()) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) suite.acctsModKeeper = acctsModKeeper + accNum := uint64(0) + suite.acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currNum := accNum + accNum++ + return currNum, nil + }) maccPerms := map[string][]string{ "fee_collector": nil, diff --git a/x/auth/keeper/genesis.go b/x/auth/keeper/genesis.go index 8bb2cd2f5ed8..7bed1523912c 100644 --- a/x/auth/keeper/genesis.go +++ b/x/auth/keeper/genesis.go @@ -29,7 +29,10 @@ func (ak AccountKeeper) InitGenesis(ctx context.Context, data types.GenesisState for _, acc := range accounts { accNum := acc.GetAccountNumber() for lastAccNum == nil || *lastAccNum < accNum { - n := ak.NextAccountNumber(ctx) + n, err := ak.AccountsModKeeper.NextAccountNumber(ctx) + if err != nil { + return err + } lastAccNum = &n } ak.SetAccount(ctx, acc) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 076509cdf35f..7c86fb093390 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -46,6 +46,8 @@ type AccountKeeperI interface { GetSequence(context.Context, sdk.AccAddress) (uint64, error) // Fetch the next account number, and increment the internal counter. + // + // Deprecated: keep this to avoid breaking api NextAccountNumber(context.Context) uint64 // GetModulePermissions fetches per-module account permissions @@ -97,9 +99,9 @@ type AccountKeeper struct { authority string // State - Schema collections.Schema - Params collections.Item[types.Params] - AccountNumber collections.Sequence + Schema collections.Schema + Params collections.Item[types.Params] + // Accounts key: AccAddr | value: AccountI | index: AccountsIndex Accounts *collections.IndexedMap[sdk.AccAddress, sdk.AccountI, AccountsIndexes] } @@ -133,7 +135,6 @@ func NewAccountKeeper( permAddrs: permAddrs, authority: authority, Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), - AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"), Accounts: collections.NewIndexedMap(sb, types.AddressStoreKeyPrefix, "accounts", sdk.AccAddressKey, codec.CollInterfaceValue[sdk.AccountI](cdc), NewAccountIndexes(sb)), } schema, err := sb.Build() @@ -181,8 +182,10 @@ func (ak AccountKeeper) GetSequence(ctx context.Context, addr sdk.AccAddress) (u // NextAccountNumber returns and increments the global account number counter. // If the global account number is not set, it initializes it with value 0. +// +// Deprecated: NextAccountNumber is deprecated func (ak AccountKeeper) NextAccountNumber(ctx context.Context) uint64 { - n, err := ak.AccountNumber.Next(ctx) + n, err := ak.AccountsModKeeper.NextAccountNumber(ctx) if err != nil { panic(err) } diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index 399b54311493..e1cb88ed9415 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "testing" "github.com/golang/mock/gomock" @@ -62,6 +63,12 @@ func (suite *KeeperTestSuite) SetupTest() { ctrl := gomock.NewController(suite.T()) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) suite.acctsModKeeper = acctsModKeeper + accNum := uint64(0) + suite.acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currNum := accNum + accNum++ + return currNum, nil + }) maccPerms := map[string][]string{ "fee_collector": nil, @@ -202,7 +209,8 @@ func (suite *KeeperTestSuite) TestInitGenesis() { suite.Require().Equal(6, int(feeCollector.GetAccountNumber())) // The 3rd account has account number 5, but because the FeeCollector account gets initialized last, the next should be 7. - nextNum := suite.accountKeeper.NextAccountNumber(ctx) + nextNum, err := suite.accountKeeper.AccountsModKeeper.NextAccountNumber(ctx) + suite.Require().NoError(err) suite.Require().Equal(7, int(nextNum)) suite.SetupTest() // reset @@ -237,7 +245,8 @@ func (suite *KeeperTestSuite) TestInitGenesis() { feeCollector = suite.accountKeeper.GetModuleAccount(ctx, "fee_collector") suite.Require().Equal(1, int(feeCollector.GetAccountNumber())) - nextNum = suite.accountKeeper.NextAccountNumber(ctx) + nextNum, err = suite.accountKeeper.AccountsModKeeper.NextAccountNumber(ctx) + suite.Require().NoError(err) // we expect nextNum to be 2 because we initialize fee_collector as account number 1 suite.Require().Equal(2, int(nextNum)) } diff --git a/x/auth/keeper/migrations.go b/x/auth/keeper/migrations.go index e3e563a9f232..5b3418b99e65 100644 --- a/x/auth/keeper/migrations.go +++ b/x/auth/keeper/migrations.go @@ -3,6 +3,7 @@ package keeper import ( "context" + "cosmossdk.io/collections" v5 "cosmossdk.io/x/auth/migrations/v5" "cosmossdk.io/x/auth/types" @@ -12,11 +13,16 @@ import ( // Migrator is a struct for handling in-place store migrations. type Migrator struct { keeper AccountKeeper + // accNum is use in v4 to v5 and v5 to v6 migration + accNum collections.Sequence } // NewMigrator returns a new Migrator. func NewMigrator(keeper AccountKeeper) Migrator { - return Migrator{keeper: keeper} + sb := collections.NewSchemaBuilder(keeper.Environment.KVStoreService) + accNumSeq := collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number") + + return Migrator{keeper: keeper, accNum: accNumSeq} } // Migrate1to2 migrates from version 1 to 2. @@ -42,7 +48,18 @@ 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.KVStoreService, m.keeper.AccountNumber) + return v5.Migrate(ctx, m.keeper.KVStoreService, m.accNum) +} + +// Migrate5To6 migrates the x/auth module state from the consensus version 5 to 6. +// It migrates the GlobalAccountNumber from x/auth to x/accounts . +func (m Migrator) Migrate5To6(ctx context.Context) error { + currentAccNum, err := m.accNum.Peek(ctx) + if err != nil { + return err + } + + return m.keeper.AccountsModKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) } // V45SetAccount implements V45_SetAccount diff --git a/x/auth/migrations/v6/migrate.go b/x/auth/migrations/v6/migrate.go new file mode 100644 index 000000000000..889de0e9c9e4 --- /dev/null +++ b/x/auth/migrations/v6/migrate.go @@ -0,0 +1,12 @@ +package v6 + +import ( + "context" +) + +type migrateAccNumFunc = func(ctx context.Context) error + +// Migrate account number from x/auth account number to x/accounts account number +func Migrate(ctx context.Context, f migrateAccNumFunc) error { + return f(ctx) +} diff --git a/x/auth/module.go b/x/auth/module.go index a2ceba435aea..29a46e3e9fec 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -23,7 +23,7 @@ import ( // ConsensusVersion defines the current x/auth module consensus version. const ( - ConsensusVersion = 5 + ConsensusVersion = 6 GovModuleName = "gov" ) @@ -109,6 +109,9 @@ func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { if err := mr.Register(types.ModuleName, 4, m.Migrate4To5); err != nil { return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %w", types.ModuleName, err) } + if err := mr.Register(types.ModuleName, 5, m.Migrate5To6); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 5 to 6: %w", types.ModuleName, err) + } return nil } diff --git a/x/auth/testutil/expected_keepers_mocks.go b/x/auth/testutil/expected_keepers_mocks.go index 8d4b1f2891be..a53fb9b61b85 100644 --- a/x/auth/testutil/expected_keepers_mocks.go +++ b/x/auth/testutil/expected_keepers_mocks.go @@ -106,6 +106,20 @@ func (m *MockAccountsModKeeper) EXPECT() *MockAccountsModKeeperMockRecorder { return m.recorder } +// InitAccountNumberSeqUnsafe mocks base method. +func (m *MockAccountsModKeeper) InitAccountNumberSeqUnsafe(ctx context.Context, currentAccNum uint64) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InitAccountNumberSeqUnsafe", ctx, currentAccNum) + ret0, _ := ret[0].(error) + return ret0 +} + +// InitAccountNumberSeqUnsafe indicates an expected call of InitAccountNumberSeqUnsafe. +func (mr *MockAccountsModKeeperMockRecorder) InitAccountNumberSeqUnsafe(ctx, currentAccNum interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitAccountNumberSeqUnsafe", reflect.TypeOf((*MockAccountsModKeeper)(nil).InitAccountNumberSeqUnsafe), ctx, currentAccNum) +} + // IsAccountsModuleAccount mocks base method. func (m *MockAccountsModKeeper) IsAccountsModuleAccount(ctx context.Context, accountAddr []byte) bool { m.ctrl.T.Helper() @@ -120,6 +134,21 @@ func (mr *MockAccountsModKeeperMockRecorder) IsAccountsModuleAccount(ctx, accoun return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsAccountsModuleAccount", reflect.TypeOf((*MockAccountsModKeeper)(nil).IsAccountsModuleAccount), ctx, accountAddr) } +// NextAccountNumber mocks base method. +func (m *MockAccountsModKeeper) NextAccountNumber(ctx context.Context) (uint64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NextAccountNumber", ctx) + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// NextAccountNumber indicates an expected call of NextAccountNumber. +func (mr *MockAccountsModKeeperMockRecorder) NextAccountNumber(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NextAccountNumber", reflect.TypeOf((*MockAccountsModKeeper)(nil).NextAccountNumber), ctx) +} + // SendModuleMessageUntyped mocks base method. func (m *MockAccountsModKeeper) SendModuleMessageUntyped(ctx context.Context, sender []byte, msg protoiface.MessageV1) (protoiface.MessageV1, error) { m.ctrl.T.Helper() diff --git a/x/auth/types/expected_keepers.go b/x/auth/types/expected_keepers.go index 10f3cc4f3331..401988a7d00d 100644 --- a/x/auth/types/expected_keepers.go +++ b/x/auth/types/expected_keepers.go @@ -19,4 +19,9 @@ type BankKeeper interface { type AccountsModKeeper interface { SendModuleMessageUntyped(ctx context.Context, sender []byte, msg protoiface.MessageV1) (protoiface.MessageV1, error) IsAccountsModuleAccount(ctx context.Context, accountAddr []byte) bool + NextAccountNumber(ctx context.Context) (accNum uint64, err error) + + // InitAccountNumberSeqUnsafe is use to set accounts module account number with value + // of auth module current account number + InitAccountNumberSeqUnsafe(ctx context.Context, currentAccNum uint64) error } diff --git a/x/auth/vesting/testutil/expected_keepers_mocks.go b/x/auth/vesting/testutil/expected_keepers_mocks.go index e7ff4c4d66ec..3446770eceae 100644 --- a/x/auth/vesting/testutil/expected_keepers_mocks.go +++ b/x/auth/vesting/testutil/expected_keepers_mocks.go @@ -106,6 +106,20 @@ func (m *MockAccountsModKeeper) EXPECT() *MockAccountsModKeeperMockRecorder { return m.recorder } +// InitAccountNumberSeqUnsafe mocks base method. +func (m *MockAccountsModKeeper) InitAccountNumberSeqUnsafe(ctx context.Context, currentAccNum uint64) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InitAccountNumberSeqUnsafe", ctx, currentAccNum) + ret0, _ := ret[0].(error) + return ret0 +} + +// InitAccountNumberSeqUnsafe indicates an expected call of InitAccountNumberSeqUnsafe. +func (mr *MockAccountsModKeeperMockRecorder) InitAccountNumberSeqUnsafe(ctx, currentAccNum interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitAccountNumberSeqUnsafe", reflect.TypeOf((*MockAccountsModKeeper)(nil).InitAccountNumberSeqUnsafe), ctx, currentAccNum) +} + // IsAccountsModuleAccount mocks base method. func (m *MockAccountsModKeeper) IsAccountsModuleAccount(ctx context.Context, accountAddr []byte) bool { m.ctrl.T.Helper() @@ -120,6 +134,21 @@ func (mr *MockAccountsModKeeperMockRecorder) IsAccountsModuleAccount(ctx, accoun return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsAccountsModuleAccount", reflect.TypeOf((*MockAccountsModKeeper)(nil).IsAccountsModuleAccount), ctx, accountAddr) } +// NextAccountNumber mocks base method. +func (m *MockAccountsModKeeper) NextAccountNumber(ctx context.Context) (uint64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NextAccountNumber", ctx) + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// NextAccountNumber indicates an expected call of NextAccountNumber. +func (mr *MockAccountsModKeeperMockRecorder) NextAccountNumber(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NextAccountNumber", reflect.TypeOf((*MockAccountsModKeeper)(nil).NextAccountNumber), ctx) +} + // SendModuleMessageUntyped mocks base method. func (m *MockAccountsModKeeper) SendModuleMessageUntyped(ctx context.Context, sender []byte, msg protoiface.MessageV1) (protoiface.MessageV1, error) { m.ctrl.T.Helper() diff --git a/x/distribution/migrations/v4/migrate_funds_test.go b/x/distribution/migrations/v4/migrate_funds_test.go index 470fc3cd07cc..d8b61c6cb1e3 100644 --- a/x/distribution/migrations/v4/migrate_funds_test.go +++ b/x/distribution/migrations/v4/migrate_funds_test.go @@ -1,6 +1,7 @@ package v4_test import ( + "context" "testing" "github.com/golang/mock/gomock" @@ -53,6 +54,13 @@ func TestFundsMigration(t *testing.T) { stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) poolKeeper := distrtestutil.NewMockPoolKeeper(ctrl) + accNum := uint64(0) + acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) { + currNum := accNum + accNum++ + return currNum, nil + }) + // create account keeper accountKeeper := authkeeper.NewAccountKeeper( runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), diff --git a/x/group/migrations/v2/migrate_test.go b/x/group/migrations/v2/migrate_test.go index 44bea162d2f4..b3177f4ae197 100644 --- a/x/group/migrations/v2/migrate_test.go +++ b/x/group/migrations/v2/migrate_test.go @@ -52,7 +52,7 @@ func TestMigrate(t *testing.T) { tKey := storetypes.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) - oldAccs, accountKeeper, err := createOldPolicyAccount(ctx, storeKey, cdc, policies) + oldAccs, accountKeeper, err := createOldPolicyAccount(t, ctx, storeKey, cdc, policies) require.NoError(t, err) groupPolicyTable, groupPolicySeq, err := createGroupPolicies(ctx, storeService, cdc, policies, codectestutil.CodecOptions{}.GetAddressCodec()) require.NoError(t, err) @@ -105,15 +105,18 @@ func createGroupPolicies(ctx sdk.Context, storeService corestore.KVStoreService, } // createOldPolicyAccount re-creates the group policy account using a module account -func createOldPolicyAccount(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec, policies []sdk.AccAddress) ([]*authtypes.ModuleAccount, group.AccountKeeper, error) { +func createOldPolicyAccount(t *testing.T, ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec, policies []sdk.AccAddress) ([]*authtypes.ModuleAccount, group.AccountKeeper, error) { + t.Helper() addressCodec := addresscodec.NewBech32Codec(sdk.Bech32MainPrefix) authorityStrAddr, err := addressCodec.BytesToString(authorityAddr) if err != nil { return nil, nil, err } // gomock initializations - ctrl := gomock.NewController(&testing.T{}) + ctrl := gomock.NewController(t) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) + // mock account number + accNum := uint64(0) accountKeeper := authkeeper.NewAccountKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), log.NewNopLogger()), cdc, authtypes.ProtoBaseAccount, acctsModKeeper, nil, addressCodec, sdk.Bech32MainPrefix, authorityStrAddr) @@ -123,6 +126,9 @@ func createOldPolicyAccount(ctx sdk.Context, storeKey storetypes.StoreKey, cdc c if err != nil { return nil, nil, err } + acctsModKeeper.EXPECT().NextAccountNumber(ctx).Return(accNum, nil) + accNum++ + acc := accountKeeper.NewAccount(ctx, &authtypes.ModuleAccount{ BaseAccount: &authtypes.BaseAccount{ Address: policyStrAddr,