Merge PR #4536: Return account queries with height
This commit is contained in:
committed by
Alexander Bezobchuk
parent
8c89023e9f
commit
95f3d32250
+33
-25
@@ -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,7 +140,7 @@ 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
|
||||
}
|
||||
@@ -142,12 +148,13 @@ func (ctx CLIContext) queryAccount(addr sdk.AccAddress) ([]byte, error) {
|
||||
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) {
|
||||
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
|
||||
return ctx.query(path, key)
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user