2020-06-01 05:46:03 -07:00
|
|
|
package client
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-28 01:44:04 -07:00
|
|
|
"strings"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-08-30 00:52:17 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-01-16 13:46:51 -08:00
|
|
|
tmbytes "github.com/tendermint/tendermint/libs/bytes"
|
2018-08-06 11:11:30 -07:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
2019-02-08 13:45:41 -08:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/rootmulti"
|
2019-05-28 01:44:04 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-06 11:11:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetNode returns an RPC client. If the context's client is not defined, an
|
|
|
|
// error is returned.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) GetNode() (rpcclient.Client, error) {
|
2018-08-06 11:11:30 -07:00
|
|
|
if ctx.Client == nil {
|
2020-03-18 18:49:33 -07:00
|
|
|
return nil, errors.New("no RPC client is defined in offline mode")
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Client, nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
// Query performs a query to a Tendermint node with the provided path.
|
2019-06-14 02:52:28 -07:00
|
|
|
// It returns the result and height of the query upon success or an error if
|
|
|
|
// the query fails.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) Query(path string) ([]byte, int64, error) {
|
2019-06-14 02:52:28 -07:00
|
|
|
return ctx.query(path, nil)
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
// 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.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) QueryWithData(path string, data []byte) ([]byte, int64, error) {
|
2018-08-04 22:56:48 -07:00
|
|
|
return ctx.query(path, data)
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
// 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.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) QueryStore(key tmbytes.HexBytes, storeName string) ([]byte, int64, error) {
|
2018-08-06 11:11:30 -07:00
|
|
|
return ctx.queryStore(key, storeName, "key")
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:13:42 -08:00
|
|
|
// QueryABCI performs a query to a Tendermint node with the provide RequestQuery.
|
|
|
|
// It returns the ResultQuery obtained from the query.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) QueryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) {
|
2020-01-30 07:13:42 -08:00
|
|
|
return ctx.queryABCI(req)
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
// 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.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) QuerySubspace(subspace []byte, storeName string) (res []sdk.KVPair, height int64, err error) {
|
2019-06-13 06:54:17 -07:00
|
|
|
resRaw, height, err := ctx.queryStore(subspace, storeName, "subspace")
|
2018-08-06 11:11:30 -07:00
|
|
|
if err != nil {
|
2019-06-13 06:54:17 -07:00
|
|
|
return res, height, err
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2020-03-13 12:58:43 -07:00
|
|
|
ctx.Codec.MustUnmarshalBinaryBare(resRaw, &res)
|
2018-08-06 11:11:30 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFromAddress returns the from address from the context's name.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) GetFromAddress() sdk.AccAddress {
|
2019-01-29 11:22:47 -08:00
|
|
|
return ctx.FromAddress
|
2018-09-25 13:48:38 -07:00
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-09-25 13:48:38 -07:00
|
|
|
// GetFromName returns the key name for the current context.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) GetFromName() string {
|
2019-01-29 11:22:47 -08:00
|
|
|
return ctx.FromName
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) queryABCI(req abci.RequestQuery) (abci.ResponseQuery, error) {
|
2018-08-06 11:11:30 -07:00
|
|
|
node, err := ctx.GetNode()
|
|
|
|
if err != nil {
|
2020-01-30 07:13:42 -08:00
|
|
|
return abci.ResponseQuery{}, err
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := rpcclient.ABCIQueryOptions{
|
2018-11-04 18:28:38 -08:00
|
|
|
Height: ctx.Height,
|
2020-07-22 23:44:18 -07:00
|
|
|
Prove: req.Prove,
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:13:42 -08:00
|
|
|
result, err := node.ABCIQueryWithOptions(req.Path, req.Data, opts)
|
2018-08-06 11:11:30 -07:00
|
|
|
if err != nil {
|
2020-01-30 07:13:42 -08:00
|
|
|
return abci.ResponseQuery{}, err
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:13:42 -08:00
|
|
|
if !result.Response.IsOK() {
|
|
|
|
return abci.ResponseQuery{}, errors.New(result.Response.Log)
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
// data from trusted node or subspace query doesn't need verification
|
2020-06-16 08:11:02 -07:00
|
|
|
if !opts.Prove || !isQueryStoreWithProof(req.Path) {
|
2020-01-30 07:13:42 -08:00
|
|
|
return result.Response, nil
|
2018-08-29 21:50:41 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:13:42 -08:00
|
|
|
return result.Response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-07-22 23:44:18 -07:00
|
|
|
// or an error if the query fails.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) query(path string, key tmbytes.HexBytes) ([]byte, int64, error) {
|
2020-01-30 07:13:42 -08:00
|
|
|
resp, err := ctx.queryABCI(abci.RequestQuery{
|
|
|
|
Path: path,
|
|
|
|
Data: key,
|
|
|
|
})
|
2018-08-29 21:50:41 -07:00
|
|
|
if err != nil {
|
2020-01-30 07:13:42 -08:00
|
|
|
return nil, 0, err
|
2018-08-29 21:50:41 -07:00
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
return resp.Value, resp.Height, nil
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
// 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.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) queryStore(key tmbytes.HexBytes, storeName, endPath string) ([]byte, int64, error) {
|
2018-08-06 11:11:30 -07:00
|
|
|
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
|
|
|
|
return ctx.query(path, key)
|
|
|
|
}
|
2018-08-29 21:50:41 -07:00
|
|
|
|
|
|
|
// isQueryStoreWithProof expects a format like /<queryType>/<storeName>/<subpath>
|
2018-11-04 19:36:35 -08:00
|
|
|
// queryType must be "store" and subpath must be "key" to require a proof.
|
2018-08-30 00:52:17 -07:00
|
|
|
func isQueryStoreWithProof(path string) bool {
|
2018-08-29 21:50:41 -07:00
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
return false
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-08-29 21:50:41 -07:00
|
|
|
paths := strings.SplitN(path[1:], "/", 3)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
switch {
|
|
|
|
case len(paths) != 3:
|
2018-08-29 21:50:41 -07:00
|
|
|
return false
|
2018-11-04 19:36:35 -08:00
|
|
|
case paths[0] != "store":
|
|
|
|
return false
|
2019-02-01 17:03:09 -08:00
|
|
|
case rootmulti.RequireProof("/" + paths[2]):
|
2018-08-29 21:50:41 -07:00
|
|
|
return true
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-08-29 21:50:41 -07:00
|
|
|
return false
|
|
|
|
}
|