Skip to content
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

R4R: Added querier for auth module #3767

Merged
merged 7 commits into from
Mar 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added custom querier
ironman0x7b2 committed Feb 28, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit b42410ea8029e18d7c1cf92e10f07eb01390db2d
21 changes: 13 additions & 8 deletions x/auth/keeper.go
Original file line number Diff line number Diff line change
@@ -10,17 +10,22 @@ import (
"github.com/cosmos/cosmos-sdk/x/params"
)

var (
// AddressStoreKeyPrefix prefix for account-by-address store
AddressStoreKeyPrefix = []byte{0x01}

globalAccountNumberKey = []byte("globalAccountNumber")

const (
// StoreKey is string representation of the store key for auth
StoreKey = "acc"

// FeeStoreKey is a string representation of the store key for fees
FeeStoreKey = "fee"

// QuerierRoute is the querier route for acc
QuerierRoute = StoreKey
)

var (
// AddressStoreKeyPrefix prefix for account-by-address store
AddressStoreKeyPrefix = []byte{0x01}

globalAccountNumberKey = []byte("globalAccountNumber")
)

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

//-----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Params

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

//-----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Misc.

func (ak AccountKeeper) decodeAccount(bz []byte) (acc Account) {
54 changes: 54 additions & 0 deletions x/auth/querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package auth

import (
"fmt"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// query endpoints supported by the auth Querier
const (
QueryAccount = "account"
)

// creates a querier for auth REST endpoints
func NewQuerier(keeper AccountKeeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
switch path[0] {
case QueryAccount:
return queryAccount(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown auth query endpoint")
}
}
}

// defines the params for query: "custom/acc/account"
type QueryAccountParams struct {
Address sdk.AccAddress
}

func NewQueryAccountParams(addr sdk.AccAddress) QueryAccountParams {
return QueryAccountParams{
Address: addr,
}
}

func queryAccount(ctx sdk.Context, req abci.RequestQuery, keeper AccountKeeper) ([]byte, sdk.Error) {
var params QueryAccountParams
if err := keeper.cdc.UnmarshalJSON(req.Data, &params); err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}

account := keeper.GetAccount(ctx, params.Address)

bz, err := codec.MarshalJSONIndent(keeper.cdc, account)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}

return bz, nil
}