Skip to content

Commit 95f3d32

Browse files
colin-axneralexanderbez
authored andcommitted
Merge PR #4536: Return account queries with height
1 parent 8c89023 commit 95f3d32

File tree

20 files changed

+125
-104
lines changed

20 files changed

+125
-104
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#4536 cli context queries return query height and accounts are returned with query height

client/context/query.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,34 @@ func (ctx CLIContext) GetNode() (rpcclient.Client, error) {
2929
return ctx.Client, nil
3030
}
3131

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

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

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

48-
// QuerySubspace performs a query from a Tendermint node with the provided
49-
// store name and subspace.
50-
func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, err error) {
51-
resRaw, err := ctx.queryStore(subspace, storeName, "subspace")
53+
// QuerySubspace performs a query to a Tendermint node with the provided
54+
// store name and subspace. It returns key value pair and height of the query
55+
// upon success or an error if the query fails.
56+
func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, height int64, err error) {
57+
resRaw, height, err := ctx.queryStore(subspace, storeName, "subspace")
5258
if err != nil {
53-
return res, err
59+
return res, height, err
5460
}
5561

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

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

137-
res, err := ctx.QueryWithData(route, bz)
143+
res, _, err := ctx.query(route, bz)
138144
if err != nil {
139145
return nil, err
140146
}
141147

142148
return res, nil
143149
}
144150

145-
// query performs a query from a Tendermint node with the provided store name
146-
// and path.
147-
func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, err error) {
151+
// query performs a query to a Tendermint node with the provided store name
152+
// and path. It returns the result and height of the query upon success
153+
// or an error if the query fails.
154+
func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, height int64, err error) {
148155
node, err := ctx.GetNode()
149156
if err != nil {
150-
return res, err
157+
return res, height, err
151158
}
152159

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

158165
result, err := node.ABCIQueryWithOptions(path, key, opts)
159166
if err != nil {
160-
return res, err
167+
return res, height, err
161168
}
162169

163170
resp := result.Response
164171
if !resp.IsOK() {
165-
return res, errors.New(resp.Log)
172+
return res, height, errors.New(resp.Log)
166173
}
167174

168175
// data from trusted node or subspace query doesn't need verification
169176
if ctx.TrustNode || !isQueryStoreWithProof(path) {
170-
return resp.Value, nil
177+
return resp.Value, resp.Height, nil
171178
}
172179

173180
err = ctx.verifyProof(path, resp)
174181
if err != nil {
175-
return nil, err
182+
return res, height, err
176183
}
177184

178-
return resp.Value, nil
185+
return resp.Value, resp.Height, nil
179186
}
180187

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

234-
// queryStore performs a query from a Tendermint node with the provided a store
235-
// name and path.
236-
func (ctx CLIContext) queryStore(key cmn.HexBytes, storeName, endPath string) ([]byte, error) {
241+
// queryStore performs a query to a Tendermint node with the provided a store
242+
// name and path. It returns the result and height of the query upon success
243+
// or an error if the query fails.
244+
func (ctx CLIContext) queryStore(key cmn.HexBytes, storeName, endPath string) ([]byte, int64, error) {
237245
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
238246
return ctx.query(path, key)
239247
}

client/rpc/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func CLIVersionRequestHandler(w http.ResponseWriter, r *http.Request) {
3535
// connected node version REST handler endpoint
3636
func NodeVersionRequestHandler(cliCtx context.CLIContext) http.HandlerFunc {
3737
return func(w http.ResponseWriter, r *http.Request) {
38-
version, err := cliCtx.Query("/app/version", nil)
38+
version, _, err := cliCtx.Query("/app/version", nil)
3939
if err != nil {
4040
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
4141
return

client/utils/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ func EnrichWithGas(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs [
127127

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

133133
// run a simulation (via /app/simulate query) to
134134
// estimate gas and update TxBuilder accordingly
135-
rawRes, err := queryFunc("/app/simulate", txBytes)
135+
rawRes, _, err := queryFunc("/app/simulate", txBytes)
136136
if err != nil {
137137
return
138138
}

client/utils/utils_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ func TestParseQueryResponse(t *testing.T) {
3636

3737
func TestCalculateGas(t *testing.T) {
3838
cdc := makeCodec()
39-
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, common.HexBytes) ([]byte, error) {
40-
return func(string, common.HexBytes) ([]byte, error) {
39+
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, common.HexBytes) ([]byte, int64, error) {
40+
return func(string, common.HexBytes) ([]byte, int64, error) {
4141
if wantErr {
42-
return nil, errors.New("")
42+
return nil, 0, errors.New("")
4343
}
44-
return cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: gasUsed}), nil
44+
return cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: gasUsed}), 0, nil
4545
}
4646
}
4747
type args struct {

x/auth/client/rest/query.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212
"github.com/cosmos/cosmos-sdk/x/auth/types"
1313
)
1414

15+
// AccountWithHeight wraps the embedded Account
16+
// with the height it was queried at
17+
type AccountWithHeight struct {
18+
types.Account
19+
Height int64 `json:"height"`
20+
}
21+
1522
// register REST routes
1623
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) {
1724
r.HandleFunc(
@@ -45,7 +52,7 @@ func QueryAccountRequestHandlerFn(
4552
return
4653
}
4754

48-
res, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
55+
res, height, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
4956
if err != nil {
5057
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
5158
return
@@ -64,7 +71,7 @@ func QueryAccountRequestHandlerFn(
6471
return
6572
}
6673

67-
rest.PostProcessResponse(w, cliCtx, account)
74+
rest.PostProcessResponse(w, cliCtx, AccountWithHeight{account, height})
6875
}
6976
}
7077

@@ -89,7 +96,7 @@ func QueryBalancesRequestHandlerFn(
8996
return
9097
}
9198

92-
res, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
99+
res, _, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
93100
if err != nil {
94101
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
95102
return

x/distribution/client/cli/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ $ %s query distr validator-outstanding-rewards cosmosvaloper1lwjmdnks33xwnmfayc6
8686
return err
8787
}
8888

89-
resp, err := cliCtx.QueryWithData(
89+
resp, _, err := cliCtx.QueryWithData(
9090
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorOutstandingRewards),
9191
bz,
9292
)
@@ -178,7 +178,7 @@ $ %s query distr slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 0
178178
return err
179179
}
180180

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

255-
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
255+
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
256256
if err != nil {
257257
return err
258258
}

x/distribution/client/common/common.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ import (
1212
func QueryParams(cliCtx context.CLIContext, queryRoute string) (PrettyParams, error) {
1313
route := fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamCommunityTax)
1414

15-
retCommunityTax, err := cliCtx.QueryWithData(route, []byte{})
15+
retCommunityTax, _, err := cliCtx.QueryWithData(route, []byte{})
1616
if err != nil {
1717
return PrettyParams{}, err
1818
}
1919

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

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

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

50-
return cliCtx.QueryWithData(
50+
res, _, err := cliCtx.QueryWithData(
5151
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards),
5252
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
5353
)
54+
return res, err
5455
}
5556

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

68-
return cliCtx.QueryWithData(
69+
res, _, err := cliCtx.QueryWithData(
6970
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegationRewards),
7071
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)),
7172
)
73+
return res, err
7274
}
7375

7476
// QueryDelegatorValidators returns delegator's list of validators
7577
// it submitted delegations to.
7678
func QueryDelegatorValidators(cliCtx context.CLIContext, queryRoute string, delegatorAddr sdk.AccAddress) ([]byte, error) {
77-
return cliCtx.QueryWithData(
79+
res, _, err := cliCtx.QueryWithData(
7880
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorValidators),
7981
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
8082
)
83+
return res, err
8184
}
8285

8386
// QueryValidatorCommission returns a validator's commission.
8487
func QueryValidatorCommission(cliCtx context.CLIContext, queryRoute string, validatorAddr sdk.ValAddress) ([]byte, error) {
85-
return cliCtx.QueryWithData(
88+
res, _, err := cliCtx.QueryWithData(
8689
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorCommission),
8790
cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
8891
)
92+
return res, err
8993
}
9094

9195
// WithdrawAllDelegatorRewards builds a multi-message slice to be used

x/distribution/client/rest/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func delegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext, queryRoute stri
115115
}
116116

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

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

264264
bin := cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr))
265-
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin)
265+
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin)
266266
if err != nil {
267267
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
268268
return

0 commit comments

Comments
 (0)