-
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
feat(auth): support accounts from auth #21688
Merged
Merged
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
feb7761
all works
testinginprod 0c57db3
add sum
testinginprod d1b7b07
add autogenerated file
testinginprod 5d16275
enhance docs
testinginprod ca46072
tidyness
testinginprod 6b8aeb0
add one more autogen file
testinginprod faf5244
checkpoint
testinginprod f187c3f
lint happier
testinginprod 4f69ab3
try again
testinginprod 4909371
cleanup
testinginprod 7849641
Merge branch 'main' into tip/auth/get_from_accounts
testinginprod 024f180
checkpoint
testinginprod 928d2e0
checkpoint
testinginprod d6019b9
Merge branch 'main' into tip/auth/get_from_accounts
testinginprod 6b7bf1e
checkpoint
testinginprod 6da1b2b
lint gods
testinginprod a6d4490
update CHANGELOG.md
testinginprod 69bf78a
add CHANGELOG.md
testinginprod d0a9a67
add to labeler
testinginprod cbbea0e
add to go.work
testinginprod 76ce317
add to dependabot.yml
testinginprod 1517f99
add to github CI
testinginprod e942a8c
add sonar
testinginprod 5832eef
add genesis header
testinginprod 4ac3aa8
PR review comments
testinginprod 9280c12
fix leftover test
testinginprod ee91b4f
lints
testinginprod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
272 changes: 135 additions & 137 deletions
272
api/cosmos/accounts/defaults/multisig/v1/multisig.pulsar.go
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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
157 changes: 157 additions & 0 deletions
157
tests/integration/auth/keeper/accounts_retro_compatibility_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,157 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
gogotypes "github.com/cosmos/gogoproto/types" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"cosmossdk.io/x/accounts/accountstd" | ||
basev1 "cosmossdk.io/x/accounts/defaults/base/v1" | ||
|
||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
var _ accountstd.Interface = mockRetroCompatAccount{} | ||
|
||
type mockRetroCompatAccount struct { | ||
retroCompat *authtypes.QueryLegacyAccountResponse | ||
address []byte | ||
} | ||
|
||
func (m mockRetroCompatAccount) RegisterInitHandler(builder *accountstd.InitBuilder) { | ||
accountstd.RegisterInitHandler(builder, func(ctx context.Context, req *gogotypes.Empty) (*gogotypes.Empty, error) { | ||
return &gogotypes.Empty{}, nil | ||
}) | ||
} | ||
|
||
func (m mockRetroCompatAccount) RegisterExecuteHandlers(_ *accountstd.ExecuteBuilder) {} | ||
|
||
func (m mockRetroCompatAccount) RegisterQueryHandlers(builder *accountstd.QueryBuilder) { | ||
if m.retroCompat == nil { | ||
return | ||
} | ||
accountstd.RegisterQueryHandler(builder, func(ctx context.Context, req *authtypes.QueryLegacyAccount) (*authtypes.QueryLegacyAccountResponse, error) { | ||
return m.retroCompat, nil | ||
}) | ||
} | ||
|
||
func TestAuthToAccountsGRPCCompat(t *testing.T) { | ||
valid := &mockRetroCompatAccount{ | ||
retroCompat: &authtypes.QueryLegacyAccountResponse{ | ||
Account: &codectypes.Any{}, | ||
Info: &authtypes.BaseAccount{ | ||
Address: "test", | ||
PubKey: nil, | ||
AccountNumber: 10, | ||
Sequence: 20, | ||
}, | ||
}, | ||
} | ||
|
||
noInfo := &mockRetroCompatAccount{ | ||
retroCompat: &authtypes.QueryLegacyAccountResponse{ | ||
Account: &codectypes.Any{}, | ||
}, | ||
} | ||
noImplement := &mockRetroCompatAccount{ | ||
retroCompat: nil, | ||
} | ||
|
||
accs := map[string]accountstd.Interface{ | ||
"valid": valid, | ||
"no_info": noInfo, | ||
"no_implement": noImplement, | ||
} | ||
|
||
f := initFixture(t, accs) | ||
|
||
// init three accounts | ||
for n, a := range accs { | ||
_, addr, err := f.accountsKeeper.Init(f.ctx, n, []byte("me"), &gogotypes.Empty{}, nil) | ||
require.NoError(t, err) | ||
a.(*mockRetroCompatAccount).address = addr | ||
} | ||
|
||
qs := authkeeper.NewQueryServer(f.authKeeper) | ||
|
||
t.Run("account supports info and account query", func(t *testing.T) { | ||
infoResp, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{ | ||
Address: f.mustAddr(valid.address), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, infoResp.Info, valid.retroCompat.Info) | ||
|
||
accountResp, err := qs.Account(f.ctx, &authtypes.QueryAccountRequest{ | ||
Address: f.mustAddr(noInfo.address), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, accountResp.Account, valid.retroCompat.Account) | ||
}) | ||
|
||
t.Run("account only supports account query, not info", func(t *testing.T) { | ||
_, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{ | ||
Address: f.mustAddr(noInfo.address), | ||
}) | ||
require.Error(t, err) | ||
require.Equal(t, status.Code(err), codes.NotFound) | ||
|
||
resp, err := qs.Account(f.ctx, &authtypes.QueryAccountRequest{ | ||
Address: f.mustAddr(noInfo.address), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, resp.Account, valid.retroCompat.Account) | ||
}) | ||
|
||
t.Run("account does not support any retro compat", func(t *testing.T) { | ||
_, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{ | ||
Address: f.mustAddr(noImplement.address), | ||
}) | ||
require.Error(t, err) | ||
require.Equal(t, status.Code(err), codes.NotFound) | ||
|
||
_, err = qs.Account(f.ctx, &authtypes.QueryAccountRequest{ | ||
Address: f.mustAddr(noImplement.address), | ||
}) | ||
|
||
require.Error(t, err) | ||
require.Equal(t, status.Code(err), codes.NotFound) | ||
}) | ||
} | ||
|
||
func TestAccountsBaseAccountRetroCompat(t *testing.T) { | ||
f := initFixture(t, nil) | ||
// init a base acc | ||
anyPk, err := codectypes.NewAnyWithValue(secp256k1.GenPrivKey().PubKey()) | ||
require.NoError(t, err) | ||
|
||
// we init two accounts to have account num not be zero. | ||
_, _, err = f.accountsKeeper.Init(f.ctx, "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil) | ||
require.NoError(t, err) | ||
|
||
_, addr, err := f.accountsKeeper.Init(f.ctx, "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil) | ||
require.NoError(t, err) | ||
|
||
// try to query it via auth | ||
qs := authkeeper.NewQueryServer(f.authKeeper) | ||
|
||
r, err := qs.Account(f.ctx, &authtypes.QueryAccountRequest{ | ||
Address: f.mustAddr(addr), | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, r.Account) | ||
|
||
info, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{ | ||
Address: f.mustAddr(addr), | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, info.Info) | ||
require.Equal(t, info.Info.PubKey, anyPk) | ||
require.Equal(t, info.Info.AccountNumber, uint64(1)) | ||
} |
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,143 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
|
||
"cosmossdk.io/core/appmodule" | ||
"cosmossdk.io/log" | ||
storetypes "cosmossdk.io/store/types" | ||
"cosmossdk.io/x/accounts" | ||
"cosmossdk.io/x/accounts/accountstd" | ||
baseaccount "cosmossdk.io/x/accounts/defaults/base" | ||
accountsv1 "cosmossdk.io/x/accounts/v1" | ||
"cosmossdk.io/x/bank" | ||
bankkeeper "cosmossdk.io/x/bank/keeper" | ||
banktypes "cosmossdk.io/x/bank/types" | ||
minttypes "cosmossdk.io/x/mint/types" | ||
"cosmossdk.io/x/tx/signing" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address" | ||
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" | ||
"github.com/cosmos/cosmos-sdk/runtime" | ||
"github.com/cosmos/cosmos-sdk/testutil/integration" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
type fixture struct { | ||
app *integration.App | ||
|
||
cdc codec.Codec | ||
ctx sdk.Context | ||
|
||
authKeeper authkeeper.AccountKeeper | ||
accountsKeeper accounts.Keeper | ||
bankKeeper bankkeeper.Keeper | ||
} | ||
|
||
func (f fixture) mustAddr(address []byte) string { | ||
s, _ := f.authKeeper.AddressCodec().BytesToString(address) | ||
return s | ||
} | ||
|
||
func initFixture(t *testing.T, extraAccs map[string]accountstd.Interface) *fixture { | ||
t.Helper() | ||
keys := storetypes.NewKVStoreKeys( | ||
authtypes.StoreKey, banktypes.StoreKey, accounts.StoreKey, | ||
) | ||
encodingCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, auth.AppModule{}, bank.AppModule{}, accounts.AppModule{}) | ||
cdc := encodingCfg.Codec | ||
|
||
logger := log.NewTestLogger(t) | ||
cms := integration.CreateMultiStore(keys, logger) | ||
|
||
newCtx := sdk.NewContext(cms, true, logger) | ||
|
||
router := baseapp.NewMsgServiceRouter() | ||
queryRouter := baseapp.NewGRPCQueryRouter() | ||
|
||
handler := directHandler{} | ||
account := baseaccount.NewAccount("base", signing.NewHandlerMap(handler), baseaccount.WithSecp256K1PubKey()) | ||
|
||
var accs []accountstd.AccountCreatorFunc | ||
for name, acc := range extraAccs { | ||
f := accountstd.AddAccount(name, func(_ accountstd.Dependencies) (accountstd.Interface, error) { | ||
return acc, nil | ||
}) | ||
accs = append(accs, f) | ||
} | ||
accountsKeeper, err := accounts.NewKeeper( | ||
cdc, | ||
runtime.NewEnvironment(runtime.NewKVStoreService(keys[accounts.StoreKey]), log.NewNopLogger(), runtime.EnvWithQueryRouterService(queryRouter), runtime.EnvWithMsgRouterService(router)), | ||
addresscodec.NewBech32Codec("cosmos"), | ||
cdc.InterfaceRegistry(), | ||
append(accs, account)..., | ||
) | ||
assert.NilError(t, err) | ||
accountsv1.RegisterQueryServer(queryRouter, accounts.NewQueryServer(accountsKeeper)) | ||
|
||
authority := authtypes.NewModuleAddress("gov") | ||
|
||
authKeeper := authkeeper.NewAccountKeeper( | ||
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()), | ||
cdc, | ||
authtypes.ProtoBaseAccount, | ||
accountsKeeper, | ||
map[string][]string{minttypes.ModuleName: {authtypes.Minter}}, | ||
addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), | ||
sdk.Bech32MainPrefix, | ||
authority.String(), | ||
) | ||
|
||
blockedAddresses := map[string]bool{ | ||
authKeeper.GetAuthority(): false, | ||
} | ||
bankKeeper := bankkeeper.NewBaseKeeper( | ||
runtime.NewEnvironment(runtime.NewKVStoreService(keys[banktypes.StoreKey]), log.NewNopLogger()), | ||
cdc, | ||
authKeeper, | ||
blockedAddresses, | ||
authority.String(), | ||
) | ||
|
||
params := banktypes.DefaultParams() | ||
assert.NilError(t, bankKeeper.SetParams(newCtx, params)) | ||
|
||
accountsModule := accounts.NewAppModule(cdc, accountsKeeper) | ||
authModule := auth.NewAppModule(cdc, authKeeper, accountsKeeper, authsims.RandomGenesisAccounts, nil) | ||
bankModule := bank.NewAppModule(cdc, bankKeeper, authKeeper) | ||
|
||
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, | ||
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(), | ||
encodingCfg.InterfaceRegistry.SigningContext().ValidatorAddressCodec(), | ||
map[string]appmodule.AppModule{ | ||
accounts.ModuleName: accountsModule, | ||
authtypes.ModuleName: authModule, | ||
banktypes.ModuleName: bankModule, | ||
}, router, queryRouter) | ||
|
||
authtypes.RegisterInterfaces(cdc.InterfaceRegistry()) | ||
banktypes.RegisterInterfaces(cdc.InterfaceRegistry()) | ||
|
||
authtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), authkeeper.NewMsgServerImpl(authKeeper)) | ||
authtypes.RegisterQueryServer(integrationApp.QueryHelper(), authkeeper.NewQueryServer(authKeeper)) | ||
|
||
banktypes.RegisterMsgServer(router, bankkeeper.NewMsgServerImpl(bankKeeper)) | ||
|
||
return &fixture{ | ||
app: integrationApp, | ||
cdc: cdc, | ||
ctx: newCtx, | ||
accountsKeeper: accountsKeeper, | ||
authKeeper: authKeeper, | ||
bankKeeper: bankKeeper, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Handle the error returned by
BytesToString
.The
mustAddr
method currently discards the error returned byBytesToString
. Since the method name implies that it will panic on failure, it should handle the error appropriately. Consider the following suggestions:require.NoError(t, err)
to fail the test if an error occurs.