-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): allow BaseAccounts to be migrated to x/accounts (#21820)
Co-authored-by: Akhil Kumar P <[email protected]> (cherry picked from commit ebbaa0e) # Conflicts: # UPGRADING.md # api/cosmos/accounts/defaults/base/v1/base.pulsar.go # api/cosmos/auth/v1beta1/accounts.pulsar.go # api/cosmos/auth/v1beta1/tx.pulsar.go # api/cosmos/auth/v1beta1/tx_grpc.pb.go
- Loading branch information
1 parent
70e80ca
commit 0b362ad
Showing
18 changed files
with
10,795 additions
and
59 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
3,686 changes: 3,686 additions & 0 deletions
3,686
api/cosmos/accounts/defaults/base/v1/base.pulsar.go
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/integration/auth/keeper/migrate_x_accounts_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
basev1 "cosmossdk.io/x/accounts/defaults/base/v1" | ||
|
||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
func TestMigrateToAccounts(t *testing.T) { | ||
f := initFixture(t, nil) | ||
|
||
// create a module account | ||
modAcc := &authtypes.ModuleAccount{ | ||
BaseAccount: &authtypes.BaseAccount{ | ||
Address: f.mustAddr([]byte("cookies")), | ||
PubKey: nil, | ||
AccountNumber: 0, | ||
Sequence: 0, | ||
}, | ||
Name: "cookies", | ||
Permissions: nil, | ||
} | ||
updatedMod := f.authKeeper.NewAccount(f.ctx, modAcc) | ||
f.authKeeper.SetAccount(f.ctx, updatedMod) | ||
|
||
// create account | ||
msgSrv := authkeeper.NewMsgServerImpl(f.authKeeper) | ||
privKey := secp256k1.GenPrivKey() | ||
addr := sdk.AccAddress(privKey.PubKey().Address()) | ||
|
||
acc := f.authKeeper.NewAccountWithAddress(f.ctx, addr) | ||
require.NoError(t, acc.SetPubKey(privKey.PubKey())) | ||
f.authKeeper.SetAccount(f.ctx, acc) | ||
|
||
t.Run("account does not exist", func(t *testing.T) { | ||
resp, err := msgSrv.MigrateAccount(f.ctx, &authtypes.MsgMigrateAccount{ | ||
Signer: f.mustAddr([]byte("notexist")), | ||
AccountType: "base", | ||
AccountInitMsg: nil, | ||
}) | ||
require.Nil(t, resp) | ||
require.ErrorIs(t, err, sdkerrors.ErrUnknownAddress) | ||
}) | ||
|
||
t.Run("invalid account type", func(t *testing.T) { | ||
resp, err := msgSrv.MigrateAccount(f.ctx, &authtypes.MsgMigrateAccount{ | ||
Signer: f.mustAddr(updatedMod.GetAddress()), | ||
AccountType: "base", | ||
AccountInitMsg: nil, | ||
}) | ||
require.Nil(t, resp) | ||
require.ErrorContains(t, err, "only BaseAccount can be migrated") | ||
}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
pk, err := codectypes.NewAnyWithValue(privKey.PubKey()) | ||
require.NoError(t, err) | ||
|
||
migrateMsg := &basev1.MsgInit{ | ||
PubKey: pk, | ||
InitSequence: 100, | ||
} | ||
|
||
initMsgAny, err := codectypes.NewAnyWithValue(migrateMsg) | ||
require.NoError(t, err) | ||
|
||
resp, err := msgSrv.MigrateAccount(f.ctx, &authtypes.MsgMigrateAccount{ | ||
Signer: f.mustAddr(addr), | ||
AccountType: "base", | ||
AccountInitMsg: initMsgAny, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// check response semantics. | ||
require.Equal(t, resp.InitResponse.TypeUrl, "/cosmos.accounts.defaults.base.v1.MsgInitResponse") | ||
require.NotNil(t, resp.InitResponse.Value) | ||
|
||
// check the account was removed from x/auth and added to x/accounts | ||
require.Nil(t, f.authKeeper.GetAccount(f.ctx, addr)) | ||
require.True(t, f.accountsKeeper.IsAccountsModuleAccount(f.ctx, addr)) | ||
|
||
// check the init information is correctly propagated. | ||
seq, err := f.accountsKeeper.Query(f.ctx, addr, &basev1.QuerySequence{}) | ||
require.NoError(t, err) | ||
require.Equal(t, migrateMsg.InitSequence, seq.(*basev1.QuerySequenceResponse).Sequence) | ||
|
||
pkResp, err := f.accountsKeeper.Query(f.ctx, addr, &basev1.QueryPubKey{}) | ||
require.NoError(t, err) | ||
require.Equal(t, migrateMsg.PubKey, pkResp.(*basev1.QueryPubKeyResponse).PubKey) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.