-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Return account queries with height #4536
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
Merged
alexanderbez
merged 4 commits into
cosmos:master
from
colin-axner:colin/account_height_queries
Jun 13, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
#4536 cli context queries return query height and accounts are returned with query height |
This file contains hidden or 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 |
---|---|---|
|
@@ -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) | ||
|
@@ -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{ | ||
|
@@ -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. | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
path := fmt.Sprintf("/store/%s/%s", storeName, endPath) | ||
return ctx.query(path, key) | ||
} | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
type AccountWithHeight struct { | ||||||
types.Account | ||||||
Height int64 `json:"height"` | ||||||
} | ||||||
|
||||||
// register REST routes | ||||||
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) { | ||||||
r.HandleFunc( | ||||||
|
@@ -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 | ||||||
|
@@ -64,7 +71,7 @@ func QueryAccountRequestHandlerFn( | |||||
return | ||||||
} | ||||||
|
||||||
rest.PostProcessResponse(w, cliCtx, account) | ||||||
rest.PostProcessResponse(w, cliCtx, AccountWithHeight{account, height}) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -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 | ||||||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, looks like
QueryWithData
is identical toQuery
! How about we simply removeQueryWithData
?