-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(x/auth): Fix system test #20531
Changes from 30 commits
214605b
aefee6a
495ab79
f761eb1
13cd0df
9c3170a
c32693e
af58812
3b33ae9
e6afd5e
521c566
c1a08da
dfebc64
d1244ae
94e9b2b
1d9cfcf
d4d9a11
ba123e6
c43114f
e5206f6
179641d
663ef59
610ef6f
7388cbd
3441aae
e68685a
51742cc
f60aa6b
49ef1e3
5f9818b
053ae26
eae8154
3ef3407
c9aa0f2
6cf6894
c22d05c
75d5bab
c2c2b1c
69d6738
1d1b745
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,12 @@ 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 | ||
sontrinh16 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err := app.syncAccountNumber(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sontrinh16 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure error handling is robust in upgrade handlers. The error handling in the upgrade handler is crucial, especially since it involves synchronization of critical data (account numbers). Consider adding more detailed error logging or recovery mechanisms here to aid in troubleshooting and ensure system stability during upgrades. |
||
return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) | ||
}, | ||
) | ||
|
@@ -50,3 +56,14 @@ func (app SimApp) RegisterUpgradeHandlers() { | |
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) | ||
} | ||
} | ||
|
||
func (app SimApp) syncAccountNumber(ctx context.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. godoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how come this is in upgrade instead of auth? that should be documented as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to auth |
||
currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) | ||
|
||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.syncAccountNumber(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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
Comment on lines
+54
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor the test to ensure it covers scenarios with multiple account numbers. The current test modifies and checks a single account number. Considering the changes in the accounts module to handle multiple account numbers, it would be beneficial to extend this test to cover scenarios where multiple account numbers are modified and verified. This would ensure more robust testing of the new logic introduced in the accounts module. |
||
} | ||
|
||
func TestImportAccountError(t *testing.T) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,10 @@ type AccountKeeper struct { | |
Schema collections.Schema | ||
Params collections.Item[types.Params] | ||
|
||
// only use for upgrade handler | ||
// | ||
// Deprecated: move to accounts module accountNumber | ||
accountNumber collections.Sequence | ||
sontrinh16 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper deprecation of The |
||
// Accounts key: AccAddr | value: AccountI | index: AccountsIndex | ||
Accounts *collections.IndexedMap[sdk.AccAddress, sdk.AccountI, AccountsIndexes] | ||
} | ||
|
@@ -135,6 +139,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() | ||
|
@@ -145,6 +150,22 @@ func NewAccountKeeper( | |
return ak | ||
} | ||
|
||
// 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) { | ||
sontrinh16 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarify the deprecation and removal process for The |
||
|
||
// GetAuthority returns the x/auth module's authority. | ||
func (ak AccountKeeper) GetAuthority() string { | ||
return ak.authority | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling in account number synchronization logic
The code snippet provided for synchronizing account numbers between the
auth
andaccounts
modules lacks comprehensive error handling. Specifically, the function returnsnil
instead of the actual error object when an error occurs, which could lead to unhandled errors and make debugging difficult.Committable suggestion
Tools
Markdownlint