Skip to content

Commit 7ac01bd

Browse files
authored
Merge PR #3767: Added querier for auth module
2 parents b47032d + f7df00f commit 7ac01bd

File tree

6 files changed

+134
-31
lines changed

6 files changed

+134
-31
lines changed

PENDING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ decoded automatically.
5555

5656
* [\#3653] Prompt user confirmation prior to signing and broadcasting a transaction.
5757
* [\#3670] CLI support for showing bech32 addresses in Ledger devices
58-
* [\#3711] Update `tx sign` to use `--from` instead of the deprecated `--name`
59-
CLI flag.
58+
* [\#3711] Update `tx sign` to use `--from` instead of the deprecated `--name` CLI flag.
6059
* [\#3730](https://github.com/cosmos/cosmos-sdk/issues/3730) Improve workflow for `gaiad gentx` with offline public keys, by outputting stdtx file that needs to be signed.
60+
* [\#3761](https://github.com/cosmos/cosmos-sdk/issues/3761) Querying account related information using custom querier in auth module
6161

6262
### Gaia
6363

client/context/query.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
6666
return nil, errors.New("account decoder required but not provided")
6767
}
6868

69-
res, err := ctx.QueryStore(auth.AddressStoreKey(address), ctx.AccountStore)
69+
res, err := ctx.queryAccount(address)
7070
if err != nil {
7171
return nil, err
72-
} else if len(res) == 0 {
73-
return nil, ErrInvalidAccount(address)
7472
}
7573

76-
account, err := ctx.AccDecoder(res)
77-
if err != nil {
74+
var account auth.Account
75+
if err := ctx.Codec.UnmarshalJSON(res, &account); err != nil {
7876
return nil, err
7977
}
8078

@@ -117,32 +115,33 @@ func (ctx CLIContext) GetAccountSequence(address []byte) (uint64, error) {
117115
// error is returned if it does not.
118116
func (ctx CLIContext) EnsureAccountExists() error {
119117
addr := ctx.GetFromAddress()
120-
accountBytes, err := ctx.QueryStore(auth.AddressStoreKey(addr), ctx.AccountStore)
121-
if err != nil {
122-
return err
123-
}
124-
125-
if len(accountBytes) == 0 {
126-
return ErrInvalidAccount(addr)
127-
}
128-
129-
return nil
118+
return ctx.EnsureAccountExistsFromAddr(addr)
130119
}
131120

132121
// EnsureAccountExistsFromAddr ensures that an account exists for a given
133122
// address. Instead of using the context's from name, a direct address is
134123
// given. An error is returned if it does not.
135124
func (ctx CLIContext) EnsureAccountExistsFromAddr(addr sdk.AccAddress) error {
136-
accountBytes, err := ctx.QueryStore(auth.AddressStoreKey(addr), ctx.AccountStore)
125+
_, err := ctx.queryAccount(addr)
126+
return err
127+
}
128+
129+
// queryAccount queries an account using custom query endpoint of auth module
130+
// returns an error if result is `null` otherwise account data
131+
func (ctx CLIContext) queryAccount(addr sdk.AccAddress) ([]byte, error) {
132+
bz, err := ctx.Codec.MarshalJSON(auth.NewQueryAccountParams(addr))
137133
if err != nil {
138-
return err
134+
return nil, err
139135
}
140136

141-
if len(accountBytes) == 0 {
142-
return ErrInvalidAccount(addr)
137+
route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, auth.QueryAccount)
138+
139+
res, err := ctx.QueryWithData(route, bz)
140+
if err != nil {
141+
return nil, err
143142
}
144143

145-
return nil
144+
return res, nil
146145
}
147146

148147
// query performs a query from a Tendermint node with the provided store name

cmd/gaia/app/app.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
157157
AddRoute(gov.RouterKey, gov.NewHandler(app.govKeeper))
158158

159159
app.QueryRouter().
160+
AddRoute(auth.QuerierRoute, auth.NewQuerier(app.accountKeeper)).
160161
AddRoute(distr.QuerierRoute, distr.NewQuerier(app.distrKeeper)).
161162
AddRoute(gov.QuerierRoute, gov.NewQuerier(app.govKeeper)).
162163
AddRoute(slashing.QuerierRoute, slashing.NewQuerier(app.slashingKeeper, app.cdc)).
@@ -324,7 +325,7 @@ func (app *GaiaApp) LoadHeight(height int64) error {
324325
return app.LoadVersion(height, app.keyMain)
325326
}
326327

327-
//______________________________________________________________________________________________
328+
// ______________________________________________________________________________________________
328329

329330
var _ sdk.StakingHooks = StakingHooks{}
330331

x/auth/keeper.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ import (
1010
"github.com/cosmos/cosmos-sdk/x/params"
1111
)
1212

13-
var (
14-
// AddressStoreKeyPrefix prefix for account-by-address store
15-
AddressStoreKeyPrefix = []byte{0x01}
16-
17-
globalAccountNumberKey = []byte("globalAccountNumber")
18-
13+
const (
1914
// StoreKey is string representation of the store key for auth
2015
StoreKey = "acc"
2116

2217
// FeeStoreKey is a string representation of the store key for fees
2318
FeeStoreKey = "fee"
19+
20+
// QuerierRoute is the querier route for acc
21+
QuerierRoute = StoreKey
22+
)
23+
24+
var (
25+
// AddressStoreKeyPrefix prefix for account-by-address store
26+
AddressStoreKeyPrefix = []byte{0x01}
27+
28+
globalAccountNumberKey = []byte("globalAccountNumber")
2429
)
2530

2631
// AccountKeeper encodes/decodes accounts using the go-amino (binary)
@@ -193,7 +198,7 @@ func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 {
193198
return accNumber
194199
}
195200

196-
//-----------------------------------------------------------------------------
201+
// -----------------------------------------------------------------------------
197202
// Params
198203

199204
// SetParams sets the auth module's parameters.
@@ -207,7 +212,7 @@ func (ak AccountKeeper) GetParams(ctx sdk.Context) (params Params) {
207212
return
208213
}
209214

210-
//-----------------------------------------------------------------------------
215+
// -----------------------------------------------------------------------------
211216
// Misc.
212217

213218
func (ak AccountKeeper) decodeAccount(bz []byte) (acc Account) {

x/auth/querier.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package auth
2+
3+
import (
4+
"fmt"
5+
6+
abci "github.com/tendermint/tendermint/abci/types"
7+
8+
"github.com/cosmos/cosmos-sdk/codec"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
)
11+
12+
// query endpoints supported by the auth Querier
13+
const (
14+
QueryAccount = "account"
15+
)
16+
17+
// creates a querier for auth REST endpoints
18+
func NewQuerier(keeper AccountKeeper) sdk.Querier {
19+
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
20+
switch path[0] {
21+
case QueryAccount:
22+
return queryAccount(ctx, req, keeper)
23+
default:
24+
return nil, sdk.ErrUnknownRequest("unknown auth query endpoint")
25+
}
26+
}
27+
}
28+
29+
// defines the params for query: "custom/acc/account"
30+
type QueryAccountParams struct {
31+
Address sdk.AccAddress
32+
}
33+
34+
func NewQueryAccountParams(addr sdk.AccAddress) QueryAccountParams {
35+
return QueryAccountParams{
36+
Address: addr,
37+
}
38+
}
39+
40+
func queryAccount(ctx sdk.Context, req abci.RequestQuery, keeper AccountKeeper) ([]byte, sdk.Error) {
41+
var params QueryAccountParams
42+
if err := keeper.cdc.UnmarshalJSON(req.Data, &params); err != nil {
43+
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
44+
}
45+
46+
account := keeper.GetAccount(ctx, params.Address)
47+
if account == nil {
48+
return nil, sdk.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", params.Address))
49+
}
50+
51+
bz, err := codec.MarshalJSONIndent(keeper.cdc, account)
52+
if err != nil {
53+
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
54+
}
55+
56+
return bz, nil
57+
}

x/auth/querier_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package auth
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
abci "github.com/tendermint/tendermint/abci/types"
9+
)
10+
11+
func Test_queryAccount(t *testing.T) {
12+
input := setupTestInput()
13+
req := abci.RequestQuery{
14+
Path: fmt.Sprintf("custom/%s/%s", QuerierRoute, QueryAccount),
15+
Data: []byte{},
16+
}
17+
18+
res, err := queryAccount(input.ctx, req, input.ak)
19+
require.NotNil(t, err)
20+
require.Nil(t, res)
21+
22+
req.Data = input.cdc.MustMarshalJSON(NewQueryAccountParams([]byte("")))
23+
res, err = queryAccount(input.ctx, req, input.ak)
24+
require.NotNil(t, err)
25+
require.Nil(t, res)
26+
27+
_, _, addr := keyPubAddr()
28+
req.Data = input.cdc.MustMarshalJSON(NewQueryAccountParams(addr))
29+
res, err = queryAccount(input.ctx, req, input.ak)
30+
require.NotNil(t, err)
31+
require.Nil(t, res)
32+
33+
input.ak.SetAccount(input.ctx, input.ak.NewAccountWithAddress(input.ctx, addr))
34+
res, err = queryAccount(input.ctx, req, input.ak)
35+
require.Nil(t, err)
36+
require.NotNil(t, res)
37+
38+
var account Account
39+
err2 := input.cdc.UnmarshalJSON(res, &account)
40+
require.Nil(t, err2)
41+
}

0 commit comments

Comments
 (0)