From 214605bec151f1145fe9b3c8e21432f7e84cf725 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Fri, 31 May 2024 18:33:42 +0700 Subject: [PATCH 01/28] change initial version to 0 --- store/v2/commitment/store.go | 2 +- store/v2/root/store.go | 13 +++++++++---- store/v2/root/store_test.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index e9cf2c0c6375..8aa527d5ecac 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -202,7 +202,7 @@ func (c *CommitStore) Commit(version uint64) (*proof.CommitInfo, error) { // will be larger than the RMS's metadata, when the block is replayed, we // should avoid committing that iavl store again. var commitID proof.CommitID - if tree.GetLatestVersion() >= version { + if tree.GetLatestVersion() >= version && version > 0 { commitID.Version = version commitID.Hash = tree.Hash() } else { diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 8c77cd0d7570..43d0c9e6bf48 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -72,7 +72,7 @@ func New( ) (store.RootStore, error) { return &Store{ logger: logger.With("module", "root_store"), - initialVersion: 1, + initialVersion: 0, stateStorage: ss, stateCommitment: sc, pruningManager: pm, @@ -389,7 +389,7 @@ func (s *Store) writeSC(cs *corestore.Changeset) error { } var previousHeight, version uint64 - if s.lastCommitInfo.GetVersion() == 0 && s.initialVersion > 1 { + if s.lastCommitInfo.GetVersion() == 0 && s.initialVersion > 0 { // This case means that no commit has been made in the store, we // start from initialVersion. version = s.initialVersion @@ -399,11 +399,16 @@ func (s *Store) writeSC(cs *corestore.Changeset) error { // 1. There was already a previous commit in the store, in which case we // increment the version from there. // 2. There was no previous commit, and initial version was not set, in which - // case we start at version 1. + // case we start at version 0. previousHeight = s.lastCommitInfo.GetVersion() - version = previousHeight + 1 + if previousHeight == 0 { + version = previousHeight + } else { + version = previousHeight + 1 + } } + fmt.Println(version) s.lastCommitInfo = s.stateCommitment.WorkingCommitInfo(version) return nil diff --git a/store/v2/root/store_test.go b/store/v2/root/store_test.go index a0766c249561..46e867a7d17a 100644 --- a/store/v2/root/store_test.go +++ b/store/v2/root/store_test.go @@ -251,7 +251,7 @@ func (s *RootStoreTestSuite) TestCommit() { // ensure latest version is updated lv, err = s.rootStore.GetLatestVersion() s.Require().NoError(err) - s.Require().Equal(uint64(1), lv) + s.Require().Equal(uint64(0), lv) // perform reads on the updated root store _, ro, err := s.rootStore.StateLatest() From aefee6a441ad889d53877aa99b6b157d4d345cc6 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Sun, 2 Jun 2024 03:55:55 +0700 Subject: [PATCH 02/28] Revert "change initial version to 0" This reverts commit 214605bec151f1145fe9b3c8e21432f7e84cf725. --- store/v2/commitment/store.go | 2 +- store/v2/root/store.go | 13 ++++--------- store/v2/root/store_test.go | 2 +- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index 8aa527d5ecac..e9cf2c0c6375 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -202,7 +202,7 @@ func (c *CommitStore) Commit(version uint64) (*proof.CommitInfo, error) { // will be larger than the RMS's metadata, when the block is replayed, we // should avoid committing that iavl store again. var commitID proof.CommitID - if tree.GetLatestVersion() >= version && version > 0 { + if tree.GetLatestVersion() >= version { commitID.Version = version commitID.Hash = tree.Hash() } else { diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 43d0c9e6bf48..8c77cd0d7570 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -72,7 +72,7 @@ func New( ) (store.RootStore, error) { return &Store{ logger: logger.With("module", "root_store"), - initialVersion: 0, + initialVersion: 1, stateStorage: ss, stateCommitment: sc, pruningManager: pm, @@ -389,7 +389,7 @@ func (s *Store) writeSC(cs *corestore.Changeset) error { } var previousHeight, version uint64 - if s.lastCommitInfo.GetVersion() == 0 && s.initialVersion > 0 { + if s.lastCommitInfo.GetVersion() == 0 && s.initialVersion > 1 { // This case means that no commit has been made in the store, we // start from initialVersion. version = s.initialVersion @@ -399,16 +399,11 @@ func (s *Store) writeSC(cs *corestore.Changeset) error { // 1. There was already a previous commit in the store, in which case we // increment the version from there. // 2. There was no previous commit, and initial version was not set, in which - // case we start at version 0. + // case we start at version 1. previousHeight = s.lastCommitInfo.GetVersion() - if previousHeight == 0 { - version = previousHeight - } else { - version = previousHeight + 1 - } + version = previousHeight + 1 } - fmt.Println(version) s.lastCommitInfo = s.stateCommitment.WorkingCommitInfo(version) return nil diff --git a/store/v2/root/store_test.go b/store/v2/root/store_test.go index 46e867a7d17a..a0766c249561 100644 --- a/store/v2/root/store_test.go +++ b/store/v2/root/store_test.go @@ -251,7 +251,7 @@ func (s *RootStoreTestSuite) TestCommit() { // ensure latest version is updated lv, err = s.rootStore.GetLatestVersion() s.Require().NoError(err) - s.Require().Equal(uint64(0), lv) + s.Require().Equal(uint64(1), lv) // perform reads on the updated root store _, ro, err := s.rootStore.StateLatest() From 9c3170aa2d62af793b4b3175d5a18b02d70bc23b Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 3 Jun 2024 23:58:52 +0700 Subject: [PATCH 03/28] fix system test --- x/auth/keeper/keeper.go | 7 ++++--- x/auth/keeper/migrations.go | 12 +++--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 7c86fb093390..81257ca02f35 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -99,9 +99,9 @@ type AccountKeeper struct { authority string // State - Schema collections.Schema - Params collections.Item[types.Params] - + Schema collections.Schema + Params collections.Item[types.Params] + AccountNumber collections.Sequence // Accounts key: AccAddr | value: AccountI | index: AccountsIndex Accounts *collections.IndexedMap[sdk.AccAddress, sdk.AccountI, AccountsIndexes] } @@ -135,6 +135,7 @@ 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() diff --git a/x/auth/keeper/migrations.go b/x/auth/keeper/migrations.go index 5b3418b99e65..3e8e17010fc4 100644 --- a/x/auth/keeper/migrations.go +++ b/x/auth/keeper/migrations.go @@ -3,7 +3,6 @@ package keeper import ( "context" - "cosmossdk.io/collections" v5 "cosmossdk.io/x/auth/migrations/v5" "cosmossdk.io/x/auth/types" @@ -13,16 +12,11 @@ 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 { - sb := collections.NewSchemaBuilder(keeper.Environment.KVStoreService) - accNumSeq := collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number") - - return Migrator{keeper: keeper, accNum: accNumSeq} + return Migrator{keeper: keeper} } // Migrate1to2 migrates from version 1 to 2. @@ -48,13 +42,13 @@ 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.accNum) + return v5.Migrate(ctx, m.keeper.KVStoreService, m.keeper.AccountNumber) } // 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) + currentAccNum, err := m.keeper.AccountNumber.Peek(ctx) if err != nil { return err } From af588123f60eb0385982734bb5f855d84d60b534 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 4 Jun 2024 15:06:48 +0700 Subject: [PATCH 04/28] move acc num sync to upgarde handler --- simapp/upgrades.go | 10 ++++++++++ tests/systemtests/upgrade_test.go | 2 +- x/auth/module.go | 8 ++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 01fe745d6e88..3a77d90e4a49 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -25,6 +25,16 @@ func (app SimApp) RegisterUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler( UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { + currentAccNum, err := app.AuthKeeper.AccountNumber.Peek(ctx) + if err != nil { + return nil, err + } + + err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) + if err != nil { + return nil, err + } + return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) }, ) diff --git a/tests/systemtests/upgrade_test.go b/tests/systemtests/upgrade_test.go index d1b093175165..f17de972310d 100644 --- a/tests/systemtests/upgrade_test.go +++ b/tests/systemtests/upgrade_test.go @@ -1,4 +1,4 @@ -//go:build system_test && linux +//go:build system_test package systemtests diff --git a/x/auth/module.go b/x/auth/module.go index 29a46e3e9fec..5c9d25ff6d61 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 = 6 + ConsensusVersion = 5 GovModuleName = "gov" ) @@ -109,9 +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) - } + // 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 } From 3b33ae9b109d85b17425f0d5d7c927182da3862a Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 5 Jun 2024 02:40:24 +0700 Subject: [PATCH 05/28] revert main logic for now --- x/auth/keeper/account.go | 7 +------ x/auth/keeper/genesis.go | 5 +---- x/auth/keeper/keeper.go | 4 +--- x/auth/keeper/keeper_test.go | 6 ++---- x/auth/keeper/migrations.go | 11 ----------- x/auth/migrations/v6/migrate.go | 12 ------------ x/auth/module.go | 3 --- x/group/migrations/v2/migrate_test.go | 6 +++--- 8 files changed, 8 insertions(+), 46 deletions(-) delete mode 100644 x/auth/migrations/v6/migrate.go diff --git a/x/auth/keeper/account.go b/x/auth/keeper/account.go index d19b15d99b83..3e21fd9a8594 100644 --- a/x/auth/keeper/account.go +++ b/x/auth/keeper/account.go @@ -22,12 +22,7 @@ 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 { - accNum, err := ak.AccountsModKeeper.NextAccountNumber(ctx) - if err != nil { - panic(err) - } - - if err := acc.SetAccountNumber(accNum); err != nil { + if err := acc.SetAccountNumber(ak.NextAccountNumber(ctx)); err != nil { panic(err) } diff --git a/x/auth/keeper/genesis.go b/x/auth/keeper/genesis.go index 7bed1523912c..8bb2cd2f5ed8 100644 --- a/x/auth/keeper/genesis.go +++ b/x/auth/keeper/genesis.go @@ -29,10 +29,7 @@ func (ak AccountKeeper) InitGenesis(ctx context.Context, data types.GenesisState for _, acc := range accounts { accNum := acc.GetAccountNumber() for lastAccNum == nil || *lastAccNum < accNum { - n, err := ak.AccountsModKeeper.NextAccountNumber(ctx) - if err != nil { - return err - } + n := ak.NextAccountNumber(ctx) lastAccNum = &n } ak.SetAccount(ctx, acc) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 81257ca02f35..a01cf546baa9 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -183,10 +183,8 @@ 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.AccountsModKeeper.NextAccountNumber(ctx) + n, err := ak.AccountNumber.Next(ctx) if err != nil { panic(err) } diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index e1cb88ed9415..2ed33f464b13 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -209,8 +209,7 @@ 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, err := suite.accountKeeper.AccountsModKeeper.NextAccountNumber(ctx) - suite.Require().NoError(err) + nextNum := suite.accountKeeper.NextAccountNumber(ctx) suite.Require().Equal(7, int(nextNum)) suite.SetupTest() // reset @@ -245,8 +244,7 @@ func (suite *KeeperTestSuite) TestInitGenesis() { feeCollector = suite.accountKeeper.GetModuleAccount(ctx, "fee_collector") suite.Require().Equal(1, int(feeCollector.GetAccountNumber())) - nextNum, err = suite.accountKeeper.AccountsModKeeper.NextAccountNumber(ctx) - suite.Require().NoError(err) + nextNum = suite.accountKeeper.NextAccountNumber(ctx) // 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 3e8e17010fc4..e3e563a9f232 100644 --- a/x/auth/keeper/migrations.go +++ b/x/auth/keeper/migrations.go @@ -45,17 +45,6 @@ func (m Migrator) Migrate4To5(ctx context.Context) error { return v5.Migrate(ctx, m.keeper.KVStoreService, m.keeper.AccountNumber) } -// 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.keeper.AccountNumber.Peek(ctx) - if err != nil { - return err - } - - return m.keeper.AccountsModKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) -} - // V45SetAccount implements V45_SetAccount // set the account without map to accAddr to accNumber. // diff --git a/x/auth/migrations/v6/migrate.go b/x/auth/migrations/v6/migrate.go deleted file mode 100644 index 889de0e9c9e4..000000000000 --- a/x/auth/migrations/v6/migrate.go +++ /dev/null @@ -1,12 +0,0 @@ -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 5c9d25ff6d61..a2ceba435aea 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -109,9 +109,6 @@ 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/group/migrations/v2/migrate_test.go b/x/group/migrations/v2/migrate_test.go index b3177f4ae197..10b7138e4d7b 100644 --- a/x/group/migrations/v2/migrate_test.go +++ b/x/group/migrations/v2/migrate_test.go @@ -116,7 +116,7 @@ func createOldPolicyAccount(t *testing.T, ctx sdk.Context, storeKey storetypes.S ctrl := gomock.NewController(t) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) // mock account number - accNum := uint64(0) + // accNum := uint64(0) accountKeeper := authkeeper.NewAccountKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), log.NewNopLogger()), cdc, authtypes.ProtoBaseAccount, acctsModKeeper, nil, addressCodec, sdk.Bech32MainPrefix, authorityStrAddr) @@ -126,8 +126,8 @@ func createOldPolicyAccount(t *testing.T, ctx sdk.Context, storeKey storetypes.S if err != nil { return nil, nil, err } - acctsModKeeper.EXPECT().NextAccountNumber(ctx).Return(accNum, nil) - accNum++ + // acctsModKeeper.EXPECT().NextAccountNumber(ctx).Return(accNum, nil) + // accNum++ acc := accountKeeper.NewAccount(ctx, &authtypes.ModuleAccount{ BaseAccount: &authtypes.BaseAccount{ From 521c566cb08d384851a4309dee9019f2e3e9ceae Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 5 Jun 2024 02:43:35 +0700 Subject: [PATCH 06/28] minor --- tests/systemtests/upgrade_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/systemtests/upgrade_test.go b/tests/systemtests/upgrade_test.go index f17de972310d..d1b093175165 100644 --- a/tests/systemtests/upgrade_test.go +++ b/tests/systemtests/upgrade_test.go @@ -1,4 +1,4 @@ -//go:build system_test +//go:build system_test && linux package systemtests From c1a08da973981fc400f69bddcdcffd0c7d7b5aa3 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 5 Jun 2024 06:07:46 +0700 Subject: [PATCH 07/28] fix accounts init genesis problem and fix system and sim test --- client/v2/go.mod | 1 + simapp/upgrades.go | 1 + tests/systemtests/upgrade_test.go | 2 +- x/accounts/genesis.go | 11 ++++++----- x/accounts/go.mod | 1 + x/auth/keeper/account.go | 7 ++++++- x/auth/keeper/genesis.go | 5 ++++- x/auth/keeper/keeper.go | 2 +- x/auth/keeper/keeper_test.go | 6 ++++-- x/authz/go.mod | 1 + x/bank/go.mod | 1 + x/bank/go.sum | 2 -- x/circuit/go.mod | 1 + x/consensus/go.mod | 1 + x/distribution/go.mod | 1 + x/evidence/go.mod | 1 + x/feegrant/go.mod | 1 + x/gov/go.mod | 1 + x/group/go.mod | 1 + x/group/migrations/v2/migrate_test.go | 6 +++--- x/mint/go.mod | 1 + x/nft/go.mod | 1 + x/params/go.mod | 1 + x/protocolpool/go.mod | 1 + x/slashing/go.mod | 1 + x/staking/go.mod | 1 + x/upgrade/go.mod | 1 + 27 files changed, 44 insertions(+), 16 deletions(-) diff --git a/client/v2/go.mod b/client/v2/go.mod index 5eeec158c37e..223af34746e1 100644 --- a/client/v2/go.mod +++ b/client/v2/go.mod @@ -190,4 +190,5 @@ replace ( cosmossdk.io/x/protocolpool => ./../../x/protocolpool cosmossdk.io/x/slashing => ./../../x/slashing cosmossdk.io/x/staking => ./../../x/staking + cosmossdk.io/x/tx => ./../../x/tx ) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 3a77d90e4a49..2e1fbab34176 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -25,6 +25,7 @@ func (app SimApp) RegisterUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler( UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { + // sync accounts and auth module account number currentAccNum, err := app.AuthKeeper.AccountNumber.Peek(ctx) if err != nil { return nil, err diff --git a/tests/systemtests/upgrade_test.go b/tests/systemtests/upgrade_test.go index d1b093175165..f17de972310d 100644 --- a/tests/systemtests/upgrade_test.go +++ b/tests/systemtests/upgrade_test.go @@ -1,4 +1,4 @@ -//go:build system_test && linux +//go:build system_test package systemtests diff --git a/x/accounts/genesis.go b/x/accounts/genesis.go index bd0f9edc0b4d..1b6c56c63efb 100644 --- a/x/accounts/genesis.go +++ b/x/accounts/genesis.go @@ -64,13 +64,14 @@ func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string, } func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) error { - err := k.AccountNumber.Set(ctx, genState.AccountNumber) - if err != nil { - return err - } - // import accounts for _, acc := range genState.Accounts { + // increase the account number + _, err := k.AccountNumber.Next(ctx) + if err != nil { + return fmt.Errorf("%w: %s", err, acc.Address) + } + err = k.importAccount(ctx, acc) if err != nil { return fmt.Errorf("%w: %s", err, acc.Address) diff --git a/x/accounts/go.mod b/x/accounts/go.mod index 8300ce3a1737..690d830381db 100644 --- a/x/accounts/go.mod +++ b/x/accounts/go.mod @@ -186,4 +186,5 @@ replace ( cosmossdk.io/x/mint => ../mint cosmossdk.io/x/slashing => ../slashing cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) 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/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 a01cf546baa9..d391f8b7f542 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -184,7 +184,7 @@ 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. 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 2ed33f464b13..e1cb88ed9415 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -209,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 @@ -244,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/authz/go.mod b/x/authz/go.mod index 7c6be4e383cf..5c04bb398f27 100644 --- a/x/authz/go.mod +++ b/x/authz/go.mod @@ -186,4 +186,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/bank/go.mod b/x/bank/go.mod index c3d9da713039..92c218ca60c0 100644 --- a/x/bank/go.mod +++ b/x/bank/go.mod @@ -184,4 +184,5 @@ replace ( cosmossdk.io/x/auth => ../auth cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/bank/go.sum b/x/bank/go.sum index dafdb3a811a4..2e2bd97339f4 100644 --- a/x/bank/go.sum +++ b/x/bank/go.sum @@ -12,8 +12,6 @@ cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc h1:R9O9d75e0qZYUsVV0zzi+ cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc/go.mod h1:amTTatOUV3u1PsKmNb87z6/galCxrRbz9kRdJkL0DyU= cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5 h1:eb0kcGyaYHSS0do7+MIWg7UKlskSH01biRNENbm/zDA= cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5/go.mod h1:drzY4oVisyWvSgpsM7ccQ7IX3efMuVIvd9Eij1Gm/6o= -cosmossdk.io/x/tx v0.13.3 h1:Ha4mNaHmxBc6RMun9aKuqul8yHiL78EKJQ8g23Zf73g= -cosmossdk.io/x/tx v0.13.3/go.mod h1:I8xaHv0rhUdIvIdptKIqzYy27+n2+zBVaxO6fscFhys= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= diff --git a/x/circuit/go.mod b/x/circuit/go.mod index d2c8cb892bb8..56e5e56048c2 100644 --- a/x/circuit/go.mod +++ b/x/circuit/go.mod @@ -180,4 +180,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/consensus/go.mod b/x/consensus/go.mod index 933f842ab9dd..f6668c9b0528 100644 --- a/x/consensus/go.mod +++ b/x/consensus/go.mod @@ -178,4 +178,5 @@ replace ( cosmossdk.io/x/auth => ../auth cosmossdk.io/x/bank => ../bank cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/distribution/go.mod b/x/distribution/go.mod index 7f2d28244444..ddeb328d17c2 100644 --- a/x/distribution/go.mod +++ b/x/distribution/go.mod @@ -185,4 +185,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/evidence/go.mod b/x/evidence/go.mod index 4ccaa57c8e0f..6a2117a47b86 100644 --- a/x/evidence/go.mod +++ b/x/evidence/go.mod @@ -180,4 +180,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/feegrant/go.mod b/x/feegrant/go.mod index 94a3a9b1d22d..20dcb3c6a14b 100644 --- a/x/feegrant/go.mod +++ b/x/feegrant/go.mod @@ -190,4 +190,5 @@ replace ( cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/gov => ../gov cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/gov/go.mod b/x/gov/go.mod index 47fe589c04d4..723b632c5499 100644 --- a/x/gov/go.mod +++ b/x/gov/go.mod @@ -187,4 +187,5 @@ replace ( cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/protocolpool => ../protocolpool cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/group/go.mod b/x/group/go.mod index 7086c7a95072..be6202e55949 100644 --- a/x/group/go.mod +++ b/x/group/go.mod @@ -194,4 +194,5 @@ replace ( cosmossdk.io/x/protocolpool => ../protocolpool cosmossdk.io/x/slashing => ../slashing cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/group/migrations/v2/migrate_test.go b/x/group/migrations/v2/migrate_test.go index 10b7138e4d7b..b3177f4ae197 100644 --- a/x/group/migrations/v2/migrate_test.go +++ b/x/group/migrations/v2/migrate_test.go @@ -116,7 +116,7 @@ func createOldPolicyAccount(t *testing.T, ctx sdk.Context, storeKey storetypes.S ctrl := gomock.NewController(t) acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl) // mock account number - // accNum := uint64(0) + accNum := uint64(0) accountKeeper := authkeeper.NewAccountKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), log.NewNopLogger()), cdc, authtypes.ProtoBaseAccount, acctsModKeeper, nil, addressCodec, sdk.Bech32MainPrefix, authorityStrAddr) @@ -126,8 +126,8 @@ func createOldPolicyAccount(t *testing.T, ctx sdk.Context, storeKey storetypes.S if err != nil { return nil, nil, err } - // acctsModKeeper.EXPECT().NextAccountNumber(ctx).Return(accNum, nil) - // accNum++ + acctsModKeeper.EXPECT().NextAccountNumber(ctx).Return(accNum, nil) + accNum++ acc := accountKeeper.NewAccount(ctx, &authtypes.ModuleAccount{ BaseAccount: &authtypes.BaseAccount{ diff --git a/x/mint/go.mod b/x/mint/go.mod index 06e22e5b56da..fa5f453905d6 100644 --- a/x/mint/go.mod +++ b/x/mint/go.mod @@ -187,4 +187,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/nft/go.mod b/x/nft/go.mod index fa0c734c3c15..f2c8431f94bf 100644 --- a/x/nft/go.mod +++ b/x/nft/go.mod @@ -180,4 +180,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/params/go.mod b/x/params/go.mod index b8cf3fc59894..366f9b239712 100644 --- a/x/params/go.mod +++ b/x/params/go.mod @@ -186,4 +186,5 @@ replace ( cosmossdk.io/x/protocolpool => ../protocolpool cosmossdk.io/x/slashing => ../slashing cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/protocolpool/go.mod b/x/protocolpool/go.mod index d288e56fc7ee..2ecb77d205a9 100644 --- a/x/protocolpool/go.mod +++ b/x/protocolpool/go.mod @@ -180,4 +180,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/slashing/go.mod b/x/slashing/go.mod index c161c3e47228..02f563b6437f 100644 --- a/x/slashing/go.mod +++ b/x/slashing/go.mod @@ -182,4 +182,5 @@ replace ( cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/staking/go.mod b/x/staking/go.mod index 5eab98c5c9be..691247b7e06d 100644 --- a/x/staking/go.mod +++ b/x/staking/go.mod @@ -188,4 +188,5 @@ replace ( cosmossdk.io/x/auth => ../auth cosmossdk.io/x/bank => ../bank cosmossdk.io/x/consensus => ../consensus + cosmossdk.io/x/tx => ../tx ) diff --git a/x/upgrade/go.mod b/x/upgrade/go.mod index 267d46b14b50..22702a705096 100644 --- a/x/upgrade/go.mod +++ b/x/upgrade/go.mod @@ -213,4 +213,5 @@ replace ( cosmossdk.io/x/consensus => ../consensus cosmossdk.io/x/gov => ../gov cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) From dfebc64f752ed1a3e6213700c30d0c2f4e6508fa Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 5 Jun 2024 06:20:29 +0700 Subject: [PATCH 08/28] go mod tidy --- x/consensus/go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/consensus/go.sum b/x/consensus/go.sum index d59da404e424..c610bbc870d2 100644 --- a/x/consensus/go.sum +++ b/x/consensus/go.sum @@ -14,8 +14,6 @@ cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc h1:R9O9d75e0qZYUsVV0zzi+ cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc/go.mod h1:amTTatOUV3u1PsKmNb87z6/galCxrRbz9kRdJkL0DyU= cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5 h1:eb0kcGyaYHSS0do7+MIWg7UKlskSH01biRNENbm/zDA= cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5/go.mod h1:drzY4oVisyWvSgpsM7ccQ7IX3efMuVIvd9Eij1Gm/6o= -cosmossdk.io/x/tx v0.13.3 h1:Ha4mNaHmxBc6RMun9aKuqul8yHiL78EKJQ8g23Zf73g= -cosmossdk.io/x/tx v0.13.3/go.mod h1:I8xaHv0rhUdIvIdptKIqzYy27+n2+zBVaxO6fscFhys= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= From d1244ae40ac8ac5d4f258866c4757ee335e519e7 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 5 Jun 2024 06:27:52 +0700 Subject: [PATCH 09/28] minor --- x/auth/keeper/keeper.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index d391f8b7f542..81257ca02f35 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -183,6 +183,8 @@ 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.AccountsModKeeper.NextAccountNumber(ctx) if err != nil { From 1d9cfcf8fa947a03f916206e814fe49f138b115b Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 5 Jun 2024 23:07:43 +0700 Subject: [PATCH 10/28] address comments --- simapp/upgrades.go | 2 +- tests/systemtests/upgrade_test.go | 2 +- x/accounts/genesis.go | 20 ++++++++++++++------ x/auth/keeper/keeper.go | 15 +++++++++++---- x/auth/keeper/migrations.go | 2 +- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 2e1fbab34176..be5750dd4ddb 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -26,7 +26,7 @@ func (app SimApp) RegisterUpgradeHandlers() { UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { // sync accounts and auth module account number - currentAccNum, err := app.AuthKeeper.AccountNumber.Peek(ctx) + currentAccNum, err := app.AuthKeeper.GetAccountNumber(ctx) if err != nil { return nil, err } diff --git a/tests/systemtests/upgrade_test.go b/tests/systemtests/upgrade_test.go index f17de972310d..d1b093175165 100644 --- a/tests/systemtests/upgrade_test.go +++ b/tests/systemtests/upgrade_test.go @@ -1,4 +1,4 @@ -//go:build system_test +//go:build system_test && linux package systemtests diff --git a/x/accounts/genesis.go b/x/accounts/genesis.go index 1b6c56c63efb..5f9ad3ee954e 100644 --- a/x/accounts/genesis.go +++ b/x/accounts/genesis.go @@ -64,20 +64,28 @@ func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string, } func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) error { + var largestNum *uint64 + var err error // import accounts for _, acc := range genState.Accounts { - // increase the account number - _, err := k.AccountNumber.Next(ctx) + err = k.importAccount(ctx, acc) if err != nil { return fmt.Errorf("%w: %s", err, acc.Address) } - err = k.importAccount(ctx, acc) - if err != nil { - return fmt.Errorf("%w: %s", err, acc.Address) + accNum := acc.AccountNumber + + if largestNum == nil || *largestNum < accNum { + largestNum = &accNum } } - return nil + + if largestNum != nil { + // set the account number to the highest account number to avoid duplicate account number + err = k.AccountNumber.Set(ctx, *largestNum) + } + + return err } func (k Keeper) importAccount(ctx context.Context, acc *v1.GenesisAccount) error { diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 81257ca02f35..75384cd37cb7 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -99,9 +99,11 @@ 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] + + // only use for upgrade handler + accountNumber collections.Sequence // Accounts key: AccAddr | value: AccountI | index: AccountsIndex Accounts *collections.IndexedMap[sdk.AccAddress, sdk.AccountI, AccountsIndexes] } @@ -135,7 +137,7 @@ 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"), + 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() @@ -146,6 +148,11 @@ func NewAccountKeeper( return ak } +// GetAccountNumber returns the x/auth module's current account number. +func (ak AccountKeeper) GetAccountNumber(ctx context.Context) (uint64, error) { + return ak.accountNumber.Peek(ctx) +} + // GetAuthority returns the x/auth module's authority. func (ak AccountKeeper) GetAuthority() string { return ak.authority diff --git a/x/auth/keeper/migrations.go b/x/auth/keeper/migrations.go index e3e563a9f232..c858e7e9551a 100644 --- a/x/auth/keeper/migrations.go +++ b/x/auth/keeper/migrations.go @@ -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.KVStoreService, m.keeper.AccountNumber) + return v5.Migrate(ctx, m.keeper.KVStoreService, m.keeper.accountNumber) } // V45SetAccount implements V45_SetAccount From c43114f27a16a40d190ff2b0d0895fe91a8eca0c Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 03:35:09 +0700 Subject: [PATCH 11/28] make deprecate --- x/auth/keeper/keeper.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 75384cd37cb7..282b1108e0eb 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -103,6 +103,8 @@ type AccountKeeper struct { Params collections.Item[types.Params] // only use for upgrade handler + // + // Deprecated: move to accounts module accountNumber accountNumber collections.Sequence // Accounts key: AccAddr | value: AccountI | index: AccountsIndex Accounts *collections.IndexedMap[sdk.AccAddress, sdk.AccountI, AccountsIndexes] From e5206f6f6f5b5753d550a03f74ebd8f80211bd19 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 16:20:52 +0700 Subject: [PATCH 12/28] add test case --- x/accounts/genesis_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/x/accounts/genesis_test.go b/x/accounts/genesis_test.go index 357fb8eb6628..ecd44a91d00d 100644 --- a/x/accounts/genesis_test.go +++ b/x/accounts/genesis_test.go @@ -51,6 +51,23 @@ func TestGenesis(t *testing.T) { resp, err = k.Query(ctx, addr2, &types.DoubleValue{}) require.NoError(t, err) require.Equal(t, &types.UInt64Value{Value: 20}, resp) + + // reset state + k, ctx = newKeeper(t, func(deps implementation.Dependencies) (string, implementation.Account, error) { + acc, err := NewTestAccount(deps) + return testAccountType, acc, err + }) + + // modify the accounts account number + state.Accounts[0].AccountNumber = 99 + + err = k.ImportState(ctx, state) + require.NoError(t, err) + + currentAccNum, err := k.AccountNumber.Peek(ctx) + require.NoError(t, err) + // AccountNumber should be set to the highest account number in the genesis state + require.Equal(t, uint64(99), currentAccNum) } func TestImportAccountError(t *testing.T) { From 179641d5eb81da919c6c5d36f6dd8018745fd4b2 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 18:02:15 +0700 Subject: [PATCH 13/28] address comments --- simapp/upgrades.go | 2 +- x/auth/keeper/keeper.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index be5750dd4ddb..7ed9a2488d75 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -26,7 +26,7 @@ func (app SimApp) RegisterUpgradeHandlers() { UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { // sync accounts and auth module account number - currentAccNum, err := app.AuthKeeper.GetAccountNumber(ctx) + currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) if err != nil { return nil, err } diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 282b1108e0eb..6f2adc895bb3 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -150,9 +150,20 @@ func NewAccountKeeper( return ak } -// GetAccountNumber returns the x/auth module's current account number. -func (ak AccountKeeper) GetAccountNumber(ctx context.Context) (uint64, error) { - return ak.accountNumber.Peek(ctx) +// RemoveLegacyAccountNumber is used for migration purpose only. It deletes the sequence in the DB +// and returns the last value used on success. +// Deprecated +func (ak AccountKeeper) RemoveLegacyAccountNumber(ctx context.Context) (uint64, error) { + accNum, err := ak.accountNumber.Peek(ctx) + if err != nil { + return 0, err + } + + // Delete DB entry for legacy account number + store := ak.KVStoreService.OpenKVStore(ctx) + err = store.Delete(types.GlobalAccountNumberKey.Bytes()) + + return accNum, err } // GetAuthority returns the x/auth module's authority. From 663ef59f69122c11badfc831c92f25849d3c3110 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 18:49:00 +0700 Subject: [PATCH 14/28] update upgrade doc --- UPGRADING.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/UPGRADING.md b/UPGRADING.md index 48e2db4164c4..d24e8af2078e 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -252,10 +252,28 @@ Most of Cosmos SDK modules have migrated to [collections](https://docs.cosmos.ne Many functions have been removed due to this changes as the API can be smaller thanks to collections. For modules that have migrated, verify you are checking against `collections.ErrNotFound` when applicable. +#### `x/accounts` + +Accounts's AccountNumber will be use as a global account number tracking replacing Auth legacy AccountNumber. Need to set accounts's AccountNumber with auth's AccountNumber value in upgrade handler: + +```go +currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) +if err != nil { + return nil, err +} + +err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) +if err != nil { + return nil, err +} +``` + #### `x/auth` Auth was spun out into its own `go.mod`. To import it use `cosmossdk.io/x/auth` +Auth accountNumber is deprecated, use + #### `x/authz` Authz was spun out into its own `go.mod`. To import it use `cosmossdk.io/x/authz` From 610ef6f5650aa9add06a7b53db25edf16730f45a Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 18:53:58 +0700 Subject: [PATCH 15/28] minor --- UPGRADING.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/UPGRADING.md b/UPGRADING.md index d24e8af2078e..a9870dd09c58 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -272,8 +272,6 @@ if err != nil { Auth was spun out into its own `go.mod`. To import it use `cosmossdk.io/x/auth` -Auth accountNumber is deprecated, use - #### `x/authz` Authz was spun out into its own `go.mod`. To import it use `cosmossdk.io/x/authz` From 7388cbd35509ace58a9e56d143d05fd7745fab1a Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 20:44:05 +0700 Subject: [PATCH 16/28] add test for upgrade logic --- simapp/upgrades.go | 19 ++++++++++----- simapp/upgrades_test.go | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 simapp/upgrades_test.go diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 7ed9a2488d75..c2a54037f1f6 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -26,12 +26,7 @@ func (app SimApp) RegisterUpgradeHandlers() { UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { // sync accounts and auth module account number - currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) - if err != nil { - return nil, err - } - - err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) + err := app.syncAccoutnNumber(ctx) if err != nil { return nil, err } @@ -61,3 +56,15 @@ func (app SimApp) RegisterUpgradeHandlers() { app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) } } + +func (app SimApp) syncAccoutnNumber(ctx context.Context) error { + // sync accounts and auth module account number + currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) + if err != nil { + return err + } + + err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) + + return err +} diff --git a/simapp/upgrades_test.go b/simapp/upgrades_test.go new file mode 100644 index 000000000000..2e9b03587e31 --- /dev/null +++ b/simapp/upgrades_test.go @@ -0,0 +1,51 @@ +package simapp + +import ( + "testing" + + "cosmossdk.io/collections" + authtypes "cosmossdk.io/x/auth/types" + cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" + "github.com/stretchr/testify/require" +) + +// TestSyncAccountNumber tests if accounts module account number is set correctly with the value get from auth. +// Also check if the store entry for auth GlobalAccountNumberKey is successfully deleted. +func TestSyncAccountNumber(t *testing.T) { + app := Setup(t, true) + ctx := app.NewUncachedContext(true, cmtproto.Header{}) + + bytesKey := authtypes.GlobalAccountNumberKey + store := app.AuthKeeper.KVStoreService.OpenKVStore(ctx) + + // initially there is no value set yet + v, err := store.Get(bytesKey) + require.NoError(t, err) + require.Nil(t, v) + + // set value for legacy account number + v, err = collections.Uint64Value.Encode(10) + require.NoError(t, err) + err = store.Set(bytesKey, v) + require.NoError(t, err) + + // make sure value are updated + v, err = store.Get(bytesKey) + require.NoError(t, err) + require.NotEmpty(t, v) + num, err := collections.Uint64Value.Decode(v) + require.NoError(t, err) + require.Equal(t, uint64(10), num) + + app.syncAccoutnNumber(ctx) + + // make sure the DB entry for this key is deleted + v, err = store.Get(bytesKey) + require.NoError(t, err) + require.Nil(t, v) + + // check if accounts's account number is updated + currentNum, err := app.AccountsKeeper.AccountNumber.Peek(ctx) + require.NoError(t, err) + require.Equal(t, uint64(10), currentNum) +} From 3441aae78c24cc08b14072d2c2deac6a6bdf77b6 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 20:48:08 +0700 Subject: [PATCH 17/28] minor --- UPGRADING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADING.md b/UPGRADING.md index a9870dd09c58..fb3634195230 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -254,7 +254,7 @@ For modules that have migrated, verify you are checking against `collections.Err #### `x/accounts` -Accounts's AccountNumber will be use as a global account number tracking replacing Auth legacy AccountNumber. Need to set accounts's AccountNumber with auth's AccountNumber value in upgrade handler: +Accounts's AccountNumber will be used as a global account number tracking replacing Auth legacy AccountNumber. Must set accounts's AccountNumber with auth's AccountNumber value in upgrade handler: ```go currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) From e68685a5c71f5773e905de0d213b153970a6f5da Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 20:53:43 +0700 Subject: [PATCH 18/28] duplicate comment --- simapp/upgrades.go | 1 - 1 file changed, 1 deletion(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index c2a54037f1f6..1c0a17770550 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -58,7 +58,6 @@ func (app SimApp) RegisterUpgradeHandlers() { } func (app SimApp) syncAccoutnNumber(ctx context.Context) error { - // sync accounts and auth module account number currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) if err != nil { return err From f60aa6bd721493001100f9eae9df5042816e34b7 Mon Sep 17 00:00:00 2001 From: son trinh Date: Mon, 10 Jun 2024 20:58:42 +0700 Subject: [PATCH 19/28] Update simapp/upgrades_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- simapp/upgrades_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/upgrades_test.go b/simapp/upgrades_test.go index 2e9b03587e31..c569ef25f24a 100644 --- a/simapp/upgrades_test.go +++ b/simapp/upgrades_test.go @@ -37,7 +37,7 @@ func TestSyncAccountNumber(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(10), num) - app.syncAccoutnNumber(ctx) + app.syncAccountNumber(ctx) // make sure the DB entry for this key is deleted v, err = store.Get(bytesKey) From 49ef1e39ba20ae6de5806815c07249bed8d7f04f Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 10 Jun 2024 21:01:23 +0700 Subject: [PATCH 20/28] fix simapp test --- simapp/upgrades.go | 4 ++-- simapp/upgrades_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 1c0a17770550..820848d3decf 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -26,7 +26,7 @@ func (app SimApp) RegisterUpgradeHandlers() { UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { // sync accounts and auth module account number - err := app.syncAccoutnNumber(ctx) + err := app.syncAccountNumber(ctx) if err != nil { return nil, err } @@ -57,7 +57,7 @@ func (app SimApp) RegisterUpgradeHandlers() { } } -func (app SimApp) syncAccoutnNumber(ctx context.Context) error { +func (app SimApp) syncAccountNumber(ctx context.Context) error { currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) if err != nil { return err diff --git a/simapp/upgrades_test.go b/simapp/upgrades_test.go index 2e9b03587e31..c569ef25f24a 100644 --- a/simapp/upgrades_test.go +++ b/simapp/upgrades_test.go @@ -37,7 +37,7 @@ func TestSyncAccountNumber(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(10), num) - app.syncAccoutnNumber(ctx) + app.syncAccountNumber(ctx) // make sure the DB entry for this key is deleted v, err = store.Get(bytesKey) From 053ae2687c8ea1b82942456567a3bf705f9372a6 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 11 Jun 2024 16:59:01 +0700 Subject: [PATCH 21/28] address comment --- UPGRADING.md | 9 ++------- simapp/upgrades.go | 13 +------------ simapp/upgrades_test.go | 2 +- x/auth/keeper/keeper.go | 15 +++++++++++++++ 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/UPGRADING.md b/UPGRADING.md index fb3634195230..2709df650c97 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -254,15 +254,10 @@ For modules that have migrated, verify you are checking against `collections.Err #### `x/accounts` -Accounts's AccountNumber will be used as a global account number tracking replacing Auth legacy AccountNumber. Must set accounts's AccountNumber with auth's AccountNumber value in upgrade handler: +Accounts's AccountNumber will be used as a global account number tracking replacing Auth legacy AccountNumber. Must set accounts's AccountNumber with auth's AccountNumber value in upgrade handler. This is done through auth keeper MigrateAccountNumber function. ```go -currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) -if err != nil { - return nil, err -} - -err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) +err := app.AuthKeeper.MigrateAccountNumber(ctx) if err != nil { return nil, err } diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 820848d3decf..38206fb8360c 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -26,7 +26,7 @@ func (app SimApp) RegisterUpgradeHandlers() { UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { // sync accounts and auth module account number - err := app.syncAccountNumber(ctx) + err := app.AuthKeeper.MigrateAccountNumberUnsafe(ctx) if err != nil { return nil, err } @@ -56,14 +56,3 @@ func (app SimApp) RegisterUpgradeHandlers() { app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) } } - -func (app SimApp) syncAccountNumber(ctx context.Context) error { - currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) - if err != nil { - return err - } - - err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) - - return err -} diff --git a/simapp/upgrades_test.go b/simapp/upgrades_test.go index c569ef25f24a..3b34120d791d 100644 --- a/simapp/upgrades_test.go +++ b/simapp/upgrades_test.go @@ -37,7 +37,7 @@ func TestSyncAccountNumber(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(10), num) - app.syncAccountNumber(ctx) + app.AuthKeeper.MigrateAccountNumberUnsafe(ctx) // make sure the DB entry for this key is deleted v, err = store.Get(bytesKey) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 6f2adc895bb3..c70f6c7d2c3d 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -338,3 +338,18 @@ func (ak AccountKeeper) NonAtomicMsgsExec(ctx context.Context, signer sdk.AccAdd return msgResponses, nil } + +// MigrateAccountNumberUnsafe migrates auth's account number to accounts's account number +// and delete store entry for auth legacy GlobalAccountNumberKey. +// +// Should only use in an upgrade handler for migrate process. +func (ak AccountKeeper) MigrateAccountNumberUnsafe(ctx context.Context) error { + currentAccNum, err := ak.RemoveLegacyAccountNumber(ctx) + if err != nil { + return err + } + + err = ak.AccountsModKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) + + return err +} From eae815481d8bf9514e25eeee0f1d87cc7480d09c Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Tue, 11 Jun 2024 17:20:43 +0700 Subject: [PATCH 22/28] changelog, add detail err log --- CHANGELOG.md | 1 + x/auth/keeper/keeper.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21784defaa04..82449cabf0dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -199,6 +199,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i * (simapp) [#19146](https://github.com/cosmos/cosmos-sdk/pull/19146) Replace `--v` CLI option with `--validator-count`/`-n`. * (module) [#19370](https://github.com/cosmos/cosmos-sdk/pull/19370) Deprecate `module.Configurator`, use `appmodule.HasMigrations` and `appmodule.HasServices` instead from Core API. +* (x/auth) [#20531](https://github.com/cosmos/cosmos-sdk/pull/20531) Deprecate auth keeper `NextAccountNumber`, use `keeper.AccountsModKeeper.NextAccountNumber` instead. ## [v0.50.7](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.7) - 2024-06-04 diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index c70f6c7d2c3d..ff30374e0f8d 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -342,11 +342,11 @@ func (ak AccountKeeper) NonAtomicMsgsExec(ctx context.Context, signer sdk.AccAdd // MigrateAccountNumberUnsafe migrates auth's account number to accounts's account number // and delete store entry for auth legacy GlobalAccountNumberKey. // -// Should only use in an upgrade handler for migrate process. +// Should only use in an upgrade handler for migrating account number. func (ak AccountKeeper) MigrateAccountNumberUnsafe(ctx context.Context) error { currentAccNum, err := ak.RemoveLegacyAccountNumber(ctx) if err != nil { - return err + return fmt.Errorf("failed to migrate account number: %w", err) } err = ak.AccountsModKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) From 3ef34073cb47cb74dfb4bc09284c0704099b7e3c Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 12 Jun 2024 01:22:07 +0700 Subject: [PATCH 23/28] address comments --- simapp/upgrades.go | 3 ++- simapp/upgrades_test.go | 4 +++- x/auth/keeper/keeper.go | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 38206fb8360c..9433b94e37b4 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -6,6 +6,7 @@ import ( "cosmossdk.io/core/appmodule" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/accounts" + authkeeper "cosmossdk.io/x/auth/keeper" epochstypes "cosmossdk.io/x/epochs/types" protocolpooltypes "cosmossdk.io/x/protocolpool/types" upgradetypes "cosmossdk.io/x/upgrade/types" @@ -26,7 +27,7 @@ func (app SimApp) RegisterUpgradeHandlers() { UpgradeName, func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) { // sync accounts and auth module account number - err := app.AuthKeeper.MigrateAccountNumberUnsafe(ctx) + err := authkeeper.MigrateAccountNumberUnsafe(ctx, &app.AuthKeeper) if err != nil { return nil, err } diff --git a/simapp/upgrades_test.go b/simapp/upgrades_test.go index 3b34120d791d..fb9e6da46607 100644 --- a/simapp/upgrades_test.go +++ b/simapp/upgrades_test.go @@ -4,6 +4,7 @@ import ( "testing" "cosmossdk.io/collections" + authkeeper "cosmossdk.io/x/auth/keeper" authtypes "cosmossdk.io/x/auth/types" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" "github.com/stretchr/testify/require" @@ -37,7 +38,8 @@ func TestSyncAccountNumber(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(10), num) - app.AuthKeeper.MigrateAccountNumberUnsafe(ctx) + err = authkeeper.MigrateAccountNumberUnsafe(ctx, &app.AuthKeeper) + require.NoError(t, err) // make sure the DB entry for this key is deleted v, err = store.Get(bytesKey) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index ff30374e0f8d..00240e8f9a65 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -150,10 +150,10 @@ func NewAccountKeeper( return ak } -// RemoveLegacyAccountNumber is used for migration purpose only. It deletes the sequence in the DB +// removeLegacyAccountNumberUnsafe is used for migration purpose only. It deletes the sequence in the DB // and returns the last value used on success. // Deprecated -func (ak AccountKeeper) RemoveLegacyAccountNumber(ctx context.Context) (uint64, error) { +func (ak AccountKeeper) removeLegacyAccountNumberUnsafe(ctx context.Context) (uint64, error) { accNum, err := ak.accountNumber.Peek(ctx) if err != nil { return 0, err @@ -343,8 +343,8 @@ func (ak AccountKeeper) NonAtomicMsgsExec(ctx context.Context, signer sdk.AccAdd // and delete store entry for auth legacy GlobalAccountNumberKey. // // Should only use in an upgrade handler for migrating account number. -func (ak AccountKeeper) MigrateAccountNumberUnsafe(ctx context.Context) error { - currentAccNum, err := ak.RemoveLegacyAccountNumber(ctx) +func MigrateAccountNumberUnsafe(ctx context.Context, ak *AccountKeeper) error { + currentAccNum, err := ak.removeLegacyAccountNumberUnsafe(ctx) if err != nil { return fmt.Errorf("failed to migrate account number: %w", err) } From c9aa0f21bf2055be81d6c534d93f73ec457293f0 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 12 Jun 2024 01:50:52 +0700 Subject: [PATCH 24/28] lint --- orm/encoding/ormfield/duration.go | 12 ++++++------ orm/encoding/ormfield/timestamp.go | 12 ++++++------ simapp/upgrades_test.go | 5 +++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/orm/encoding/ormfield/duration.go b/orm/encoding/ormfield/duration.go index 572b6e21e612..7d7f25b5c7e6 100644 --- a/orm/encoding/ormfield/duration.go +++ b/orm/encoding/ormfield/duration.go @@ -16,12 +16,12 @@ const ( // DurationCodec encodes google.protobuf.Duration values with the following // encoding: -// - nil is encoded as []byte{0xFF} -// - seconds (which can range from -315,576,000,000 to +315,576,000,000) is encoded as 5 fixed bytes -// - nanos (which can range from 0 to 999,999,999 or -999,999,999 to 0 if seconds is negative) are encoded such -// that 999,999,999 is always added to nanos. This ensures that the encoded nanos are always >= 0. Additionally, -// by adding 999,999,999 to both positive and negative nanos, we guarantee that the lexicographical order is -// preserved when comparing the encoded values of two Durations: +// - nil is encoded as []byte{0xFF} +// - seconds (which can range from -315,576,000,000 to +315,576,000,000) is encoded as 5 fixed bytes +// - nanos (which can range from 0 to 999,999,999 or -999,999,999 to 0 if seconds is negative) are encoded such +// that 999,999,999 is always added to nanos. This ensures that the encoded nanos are always >= 0. Additionally, +// by adding 999,999,999 to both positive and negative nanos, we guarantee that the lexicographical order is +// preserved when comparing the encoded values of two Durations: // - []byte{0xBB, 0x9A, 0xC9, 0xFF} for zero nanos // - 4 fixed bytes with the bit mask 0x80 applied to the first byte, with negative nanos scaled so that -999,999,999 // is encoded as 0 and -1 is encoded as 999,999,998 diff --git a/orm/encoding/ormfield/timestamp.go b/orm/encoding/ormfield/timestamp.go index 4788f390848a..d643a1c1d026 100644 --- a/orm/encoding/ormfield/timestamp.go +++ b/orm/encoding/ormfield/timestamp.go @@ -9,12 +9,12 @@ import ( // TimestampCodec encodes google.protobuf.Timestamp values with the following // encoding: -// - nil is encoded as []byte{0xFF} -// - seconds (which can range from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z) is encoded as 5 fixed bytes -// - nanos (which can range from 0 to 999,999,999 or -999,999,999 to 0 if seconds is negative) are encoded such -// that 999,999,999 is always added to nanos. This ensures that the encoded nanos are always >= 0. Additionally, -// by adding 999,999,999 to both positive and negative nanos, we guarantee that the lexicographical order is -// preserved when comparing the encoded values of two Timestamps. +// - nil is encoded as []byte{0xFF} +// - seconds (which can range from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z) is encoded as 5 fixed bytes +// - nanos (which can range from 0 to 999,999,999 or -999,999,999 to 0 if seconds is negative) are encoded such +// that 999,999,999 is always added to nanos. This ensures that the encoded nanos are always >= 0. Additionally, +// by adding 999,999,999 to both positive and negative nanos, we guarantee that the lexicographical order is +// preserved when comparing the encoded values of two Timestamps. // // When iterating over timestamp indexes, nil values will always be ordered last. // diff --git a/simapp/upgrades_test.go b/simapp/upgrades_test.go index fb9e6da46607..b70ee15c7863 100644 --- a/simapp/upgrades_test.go +++ b/simapp/upgrades_test.go @@ -3,11 +3,12 @@ package simapp import ( "testing" + cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" + "github.com/stretchr/testify/require" + "cosmossdk.io/collections" authkeeper "cosmossdk.io/x/auth/keeper" authtypes "cosmossdk.io/x/auth/types" - cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" - "github.com/stretchr/testify/require" ) // TestSyncAccountNumber tests if accounts module account number is set correctly with the value get from auth. From 6cf68941171472d2791105f7fc3330f71d4d27c0 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 12 Jun 2024 01:53:21 +0700 Subject: [PATCH 25/28] fix upgrading doc --- UPGRADING.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/UPGRADING.md b/UPGRADING.md index 2709df650c97..e99c120b0519 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -257,7 +257,9 @@ For modules that have migrated, verify you are checking against `collections.Err Accounts's AccountNumber will be used as a global account number tracking replacing Auth legacy AccountNumber. Must set accounts's AccountNumber with auth's AccountNumber value in upgrade handler. This is done through auth keeper MigrateAccountNumber function. ```go -err := app.AuthKeeper.MigrateAccountNumber(ctx) +import authkeeper "cosmossdk.io/x/auth/keeper" +... +err := authkeeper.MigrateAccountNumberUnsafe(ctx, &app.AuthKeeper) if err != nil { return nil, err } From 75d5bab2d6ca8ecbff6e74bd4e0cbe60a3cb9f3e Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 12 Jun 2024 13:19:37 +0700 Subject: [PATCH 26/28] more lint --- crypto/codec/amino.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/codec/amino.go b/crypto/codec/amino.go index 2c83723a6d48..894f11604b41 100644 --- a/crypto/codec/amino.go +++ b/crypto/codec/amino.go @@ -3,7 +3,7 @@ package codec import ( "github.com/cometbft/cometbft/crypto/sr25519" - "cosmossdk.io/core/legacy" + "cosmossdk.io/core/legacy" //nolint:staticcheck "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" From 69d673838563dc07504d7dda0791e72c85fcad2e Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 12 Jun 2024 13:21:28 +0700 Subject: [PATCH 27/28] lint --- crypto/codec/amino.go | 1 + crypto/codec/cmt.go | 1 + 2 files changed, 2 insertions(+) diff --git a/crypto/codec/amino.go b/crypto/codec/amino.go index 648637da276b..373ef14621f8 100644 --- a/crypto/codec/amino.go +++ b/crypto/codec/amino.go @@ -4,6 +4,7 @@ import ( "github.com/cometbft/cometbft/crypto/sr25519" "cosmossdk.io/core/legacy" + bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" diff --git a/crypto/codec/cmt.go b/crypto/codec/cmt.go index 9f6eccfdf88d..9617f94bf1a3 100644 --- a/crypto/codec/cmt.go +++ b/crypto/codec/cmt.go @@ -6,6 +6,7 @@ import ( "github.com/cometbft/cometbft/crypto/encoding" "cosmossdk.io/errors" + bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" From 1d1b7459feb92b673d882dd2c4e5340be24c8975 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Wed, 12 Jun 2024 13:37:56 +0700 Subject: [PATCH 28/28] lint more --- x/genutil/client/cli/init.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/x/genutil/client/cli/init.go b/x/genutil/client/cli/init.go index 19c3d148c48f..866a472778e0 100644 --- a/x/genutil/client/cli/init.go +++ b/x/genutil/client/cli/init.go @@ -9,10 +9,14 @@ import ( "os" "path/filepath" - errorsmod "cosmossdk.io/errors" - "cosmossdk.io/math/unsafe" cfg "github.com/cometbft/cometbft/config" cmttypes "github.com/cometbft/cometbft/types" + "github.com/cosmos/go-bip39" + "github.com/spf13/cobra" + + errorsmod "cosmossdk.io/errors" + "cosmossdk.io/math/unsafe" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/input" @@ -21,8 +25,6 @@ import ( "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/genutil/types" - "github.com/cosmos/go-bip39" - "github.com/spf13/cobra" ) const (