-
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 all 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 |
---|---|---|
|
@@ -252,6 +252,19 @@ 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 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. | ||
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. I would add that this should be done in the ugprade handler of an app when wanting using x/accounts. Can we have this as a diff of upgrade.go like we did for protocolpool? |
||
|
||
```go | ||
import authkeeper "cosmossdk.io/x/auth/keeper" | ||
... | ||
err := authkeeper.MigrateAccountNumberUnsafe(ctx, &app.AuthKeeper) | ||
if err != nil { | ||
return nil, err | ||
} | ||
Comment on lines
+255
to
+265
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. Consider handling the error from The current implementation returns if err != nil {
return nil, fmt.Errorf("failed to migrate account number: %w", err)
} ToolsMarkdownlint
|
||
``` | ||
|
||
#### `x/auth` | ||
|
||
Auth was spun out into its own `go.mod`. To import it use `cosmossdk.io/x/auth` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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" | ||
) | ||
|
||
// 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) | ||
|
||
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) | ||
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 | ||
} | ||
|
||
// 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) removeLegacyAccountNumberUnsafe(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. | ||
func (ak AccountKeeper) GetAuthority() string { | ||
return ak.authority | ||
|
@@ -317,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 migrating account number. | ||
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) | ||
} | ||
|
||
err = ak.AccountsModKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum) | ||
|
||
return err | ||
} |
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.
nit, to move to x/auth/changelog