Skip to content

Commit

Permalink
Merge PR #4536: Return account queries with height
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored and alexanderbez committed Jun 13, 2019
1 parent 8c89023 commit 95f3d32
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 104 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4536-cli-context-que
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4536 cli context queries return query height and accounts are returned with query height
58 changes: 33 additions & 25 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,34 @@ func (ctx CLIContext) GetNode() (rpcclient.Client, error) {
return ctx.Client, nil
}

// Query performs a query for information about the connected node.
func (ctx CLIContext) Query(path string, data cmn.HexBytes) (res []byte, err error) {
// Query performs a query to a Tendermint node with the provided path.
// It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx CLIContext) Query(path string, data cmn.HexBytes) ([]byte, int64, error) {
return ctx.query(path, data)
}

// Query information about the connected node with a data payload
func (ctx CLIContext) QueryWithData(path string, data []byte) (res []byte, err error) {
// QueryWithData performs a query to a Tendermint node with the provided path
// and a data payload. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx CLIContext) QueryWithData(path string, data []byte) ([]byte, int64, error) {
return ctx.query(path, data)
}

// QueryStore performs a query from a Tendermint node with the provided key and
// store name.
func (ctx CLIContext) QueryStore(key cmn.HexBytes, storeName string) (res []byte, err error) {
// QueryStore performs a query to a Tendermint node with the provided key and
// store name. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx CLIContext) QueryStore(key cmn.HexBytes, storeName string) ([]byte, int64, error) {
return ctx.queryStore(key, storeName, "key")
}

// QuerySubspace performs a query from a Tendermint node with the provided
// store name and subspace.
func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, err error) {
resRaw, err := ctx.queryStore(subspace, storeName, "subspace")
// QuerySubspace performs a query to a Tendermint node with the provided
// store name and subspace. It returns key value pair and height of the query
// upon success or an error if the query fails.
func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, height int64, err error) {
resRaw, height, err := ctx.queryStore(subspace, storeName, "subspace")
if err != nil {
return res, err
return res, height, err
}

ctx.Codec.MustUnmarshalBinaryLengthPrefixed(resRaw, &res)
Expand Down Expand Up @@ -134,20 +140,21 @@ func (ctx CLIContext) queryAccount(addr sdk.AccAddress) ([]byte, error) {

route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, authtypes.QueryAccount)

res, err := ctx.QueryWithData(route, bz)
res, _, err := ctx.query(route, bz)
if err != nil {
return nil, err
}

return res, nil
}

// query performs a query from a Tendermint node with the provided store name
// and path.
func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, err error) {
// query performs a query to a Tendermint node with the provided store name
// and path. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, height int64, err error) {
node, err := ctx.GetNode()
if err != nil {
return res, err
return res, height, err
}

opts := rpcclient.ABCIQueryOptions{
Expand All @@ -157,25 +164,25 @@ func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, err erro

result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
return res, err
return res, height, err
}

resp := result.Response
if !resp.IsOK() {
return res, errors.New(resp.Log)
return res, height, errors.New(resp.Log)
}

// data from trusted node or subspace query doesn't need verification
if ctx.TrustNode || !isQueryStoreWithProof(path) {
return resp.Value, nil
return resp.Value, resp.Height, nil
}

err = ctx.verifyProof(path, resp)
if err != nil {
return nil, err
return res, height, err
}

return resp.Value, nil
return resp.Value, resp.Height, nil
}

// Verify verifies the consensus proof at given height.
Expand Down Expand Up @@ -231,9 +238,10 @@ func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) err
return nil
}

// queryStore performs a query from a Tendermint node with the provided a store
// name and path.
func (ctx CLIContext) queryStore(key cmn.HexBytes, storeName, endPath string) ([]byte, error) {
// queryStore performs a query to a Tendermint node with the provided a store
// name and path. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx CLIContext) queryStore(key cmn.HexBytes, storeName, endPath string) ([]byte, int64, error) {
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
return ctx.query(path, key)
}
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CLIVersionRequestHandler(w http.ResponseWriter, r *http.Request) {
// connected node version REST handler endpoint
func NodeVersionRequestHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
version, err := cliCtx.Query("/app/version", nil)
version, _, err := cliCtx.Query("/app/version", nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
4 changes: 2 additions & 2 deletions client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ func EnrichWithGas(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs [

// CalculateGas simulates the execution of a transaction and returns
// both the estimate obtained by the query and the adjusted amount.
func CalculateGas(queryFunc func(string, common.HexBytes) ([]byte, error),
func CalculateGas(queryFunc func(string, common.HexBytes) ([]byte, int64, error),
cdc *codec.Codec, txBytes []byte, adjustment float64) (estimate, adjusted uint64, err error) {

// run a simulation (via /app/simulate query) to
// estimate gas and update TxBuilder accordingly
rawRes, err := queryFunc("/app/simulate", txBytes)
rawRes, _, err := queryFunc("/app/simulate", txBytes)
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions client/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestParseQueryResponse(t *testing.T) {

func TestCalculateGas(t *testing.T) {
cdc := makeCodec()
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, common.HexBytes) ([]byte, error) {
return func(string, common.HexBytes) ([]byte, error) {
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, common.HexBytes) ([]byte, int64, error) {
return func(string, common.HexBytes) ([]byte, int64, error) {
if wantErr {
return nil, errors.New("")
return nil, 0, errors.New("")
}
return cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: gasUsed}), nil
return cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: gasUsed}), 0, nil
}
}
type args struct {
Expand Down
13 changes: 10 additions & 3 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

// AccountWithHeight wraps the embedded Account
// with the height it was queried at
type AccountWithHeight struct {
types.Account
Height int64 `json:"height"`
}

// register REST routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) {
r.HandleFunc(
Expand Down Expand Up @@ -45,7 +52,7 @@ func QueryAccountRequestHandlerFn(
return
}

res, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
res, height, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -64,7 +71,7 @@ func QueryAccountRequestHandlerFn(
return
}

rest.PostProcessResponse(w, cliCtx, account)
rest.PostProcessResponse(w, cliCtx, AccountWithHeight{account, height})
}
}

Expand All @@ -89,7 +96,7 @@ func QueryBalancesRequestHandlerFn(
return
}

res, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
res, _, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ $ %s query distr validator-outstanding-rewards cosmosvaloper1lwjmdnks33xwnmfayc6
return err
}

resp, err := cliCtx.QueryWithData(
resp, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorOutstandingRewards),
bz,
)
Expand Down Expand Up @@ -178,7 +178,7 @@ $ %s query distr slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 0
return err
}

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", queryRoute), bz)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", queryRoute), bz)
if err != nil {
return err
}
Expand Down Expand Up @@ -252,7 +252,7 @@ $ %s query distr community-pool
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
if err != nil {
return err
}
Expand Down
20 changes: 12 additions & 8 deletions x/distribution/client/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import (
func QueryParams(cliCtx context.CLIContext, queryRoute string) (PrettyParams, error) {
route := fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamCommunityTax)

retCommunityTax, err := cliCtx.QueryWithData(route, []byte{})
retCommunityTax, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamBaseProposerReward)
retBaseProposerReward, err := cliCtx.QueryWithData(route, []byte{})
retBaseProposerReward, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamBonusProposerReward)
retBonusProposerReward, err := cliCtx.QueryWithData(route, []byte{})
retBonusProposerReward, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamWithdrawAddrEnabled)
retWithdrawAddrEnabled, err := cliCtx.QueryWithData(route, []byte{})
retWithdrawAddrEnabled, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}
Expand All @@ -47,10 +47,11 @@ func QueryDelegatorTotalRewards(cliCtx context.CLIContext, queryRoute, delAddr s
return nil, err
}

return cliCtx.QueryWithData(
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards),
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
)
return res, err
}

// QueryDelegationRewards queries a delegation rewards.
Expand All @@ -65,27 +66,30 @@ func QueryDelegationRewards(cliCtx context.CLIContext, queryRoute, delAddr, valA
return nil, err
}

return cliCtx.QueryWithData(
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegationRewards),
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)),
)
return res, err
}

// QueryDelegatorValidators returns delegator's list of validators
// it submitted delegations to.
func QueryDelegatorValidators(cliCtx context.CLIContext, queryRoute string, delegatorAddr sdk.AccAddress) ([]byte, error) {
return cliCtx.QueryWithData(
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorValidators),
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
)
return res, err
}

// QueryValidatorCommission returns a validator's commission.
func QueryValidatorCommission(cliCtx context.CLIContext, queryRoute string, validatorAddr sdk.ValAddress) ([]byte, error) {
return cliCtx.QueryWithData(
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorCommission),
cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
)
return res, err
}

// WithdrawAllDelegatorRewards builds a multi-message slice to be used
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func delegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext, queryRoute stri
}

bz := cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr))
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraw_addr", queryRoute), bz)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraw_addr", queryRoute), bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -232,7 +232,7 @@ func communityPoolHandler(cliCtx context.CLIContext, queryRoute string) http.Han
return
}

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -262,7 +262,7 @@ func outstandingRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) h
}

bin := cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr))
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
Loading

0 comments on commit 95f3d32

Please sign in to comment.