forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR cosmos#3767: Added querier for auth module
- Loading branch information
Showing
6 changed files
with
134 additions
and
31 deletions.
There are no files selected for viewing
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
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,57 @@ | ||
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, ¶ms); err != nil { | ||
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err)) | ||
} | ||
|
||
account := keeper.GetAccount(ctx, params.Address) | ||
if account == nil { | ||
return nil, sdk.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", 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 | ||
} |
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,41 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
func Test_queryAccount(t *testing.T) { | ||
input := setupTestInput() | ||
req := abci.RequestQuery{ | ||
Path: fmt.Sprintf("custom/%s/%s", QuerierRoute, QueryAccount), | ||
Data: []byte{}, | ||
} | ||
|
||
res, err := queryAccount(input.ctx, req, input.ak) | ||
require.NotNil(t, err) | ||
require.Nil(t, res) | ||
|
||
req.Data = input.cdc.MustMarshalJSON(NewQueryAccountParams([]byte(""))) | ||
res, err = queryAccount(input.ctx, req, input.ak) | ||
require.NotNil(t, err) | ||
require.Nil(t, res) | ||
|
||
_, _, addr := keyPubAddr() | ||
req.Data = input.cdc.MustMarshalJSON(NewQueryAccountParams(addr)) | ||
res, err = queryAccount(input.ctx, req, input.ak) | ||
require.NotNil(t, err) | ||
require.Nil(t, res) | ||
|
||
input.ak.SetAccount(input.ctx, input.ak.NewAccountWithAddress(input.ctx, addr)) | ||
res, err = queryAccount(input.ctx, req, input.ak) | ||
require.Nil(t, err) | ||
require.NotNil(t, res) | ||
|
||
var account Account | ||
err2 := input.cdc.UnmarshalJSON(res, &account) | ||
require.Nil(t, err2) | ||
} |