2018-08-06 11:11:30 -07:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
"strings"
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-08-30 00:52:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-08-06 11:11:30 -07:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2018-09-14 11:41:21 -07:00
|
|
|
"github.com/tendermint/tendermint/lite"
|
|
|
|
tmliteErr "github.com/tendermint/tendermint/lite/errors"
|
2018-08-30 19:03:48 -07:00
|
|
|
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
|
2018-08-06 11:11:30 -07:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetNode returns an RPC client. If the context's client is not defined, an
|
|
|
|
// error is returned.
|
|
|
|
func (ctx CLIContext) GetNode() (rpcclient.Client, error) {
|
|
|
|
if ctx.Client == nil {
|
|
|
|
return nil, errors.New("no RPC client defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query performs a query for information about the connected node.
|
2018-08-22 04:38:55 -07:00
|
|
|
func (ctx CLIContext) Query(path string, data cmn.HexBytes) (res []byte, err error) {
|
|
|
|
return ctx.query(path, data)
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2018-08-04 22:56:48 -07:00
|
|
|
// Query information about the connected node with a data payload
|
|
|
|
func (ctx CLIContext) QueryWithData(path string, data []byte) (res []byte, err error) {
|
|
|
|
return ctx.query(path, data)
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// 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) {
|
|
|
|
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")
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Codec.MustUnmarshalBinary(resRaw, &res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount queries for an account given an address and a block height. An
|
|
|
|
// error is returned if the query or decoding fails.
|
|
|
|
func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
|
|
|
|
if ctx.AccDecoder == nil {
|
|
|
|
return nil, errors.New("account decoder required but not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := ctx.QueryStore(auth.AddressStoreKey(address), ctx.AccountStore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(res) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := ctx.AccDecoder(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFromAddress returns the from address from the context's name.
|
2018-09-25 13:48:38 -07:00
|
|
|
func (ctx CLIContext) GetFromAddress() (sdk.AccAddress, error) {
|
|
|
|
return ctx.fromAddress, nil
|
|
|
|
}
|
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.
|
|
|
|
func (ctx CLIContext) GetFromName() (string, error) {
|
|
|
|
return ctx.fromName, nil
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountNumber returns the next account number for the given account
|
|
|
|
// address.
|
|
|
|
func (ctx CLIContext) GetAccountNumber(address []byte) (int64, error) {
|
|
|
|
account, err := ctx.GetAccount(address)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.GetAccountNumber(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountSequence returns the sequence number for the given account
|
|
|
|
// address.
|
|
|
|
func (ctx CLIContext) GetAccountSequence(address []byte) (int64, error) {
|
|
|
|
account, err := ctx.GetAccount(address)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.GetSequence(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastTx broadcasts transaction bytes to a Tendermint node.
|
|
|
|
func (ctx CLIContext) BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
|
|
|
|
node, err := ctx.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := node.BroadcastTxCommit(tx)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.CheckTx.IsOK() {
|
|
|
|
return res, errors.Errorf("checkTx failed: (%d) %s",
|
|
|
|
res.CheckTx.Code,
|
|
|
|
res.CheckTx.Log)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.DeliverTx.IsOK() {
|
|
|
|
return res, errors.Errorf("deliverTx failed: (%d) %s",
|
|
|
|
res.DeliverTx.Code,
|
|
|
|
res.DeliverTx.Log)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node
|
|
|
|
// asynchronously.
|
|
|
|
func (ctx CLIContext) BroadcastTxAsync(tx []byte) (*ctypes.ResultBroadcastTx, error) {
|
|
|
|
node, err := ctx.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := node.BroadcastTxAsync(tx)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureAccountExists ensures that an account exists for a given context. An
|
|
|
|
// error is returned if it does not.
|
|
|
|
func (ctx CLIContext) EnsureAccountExists() error {
|
|
|
|
addr, err := ctx.GetFromAddress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountBytes, err := ctx.QueryStore(auth.AddressStoreKey(addr), ctx.AccountStore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(accountBytes) == 0 {
|
|
|
|
return ErrInvalidAccount(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureAccountExistsFromAddr ensures that an account exists for a given
|
|
|
|
// address. Instead of using the context's from name, a direct address is
|
|
|
|
// given. An error is returned if it does not.
|
|
|
|
func (ctx CLIContext) EnsureAccountExistsFromAddr(addr sdk.AccAddress) error {
|
|
|
|
accountBytes, err := ctx.QueryStore(auth.AddressStoreKey(addr), ctx.AccountStore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(accountBytes) == 0 {
|
|
|
|
return ErrInvalidAccount(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureBroadcastTx broadcasts a transactions either synchronously or
|
|
|
|
// asynchronously based on the context parameters. The result of the broadcast
|
|
|
|
// is parsed into an intermediate structure which is logged if the context has
|
|
|
|
// a logger defined.
|
|
|
|
func (ctx CLIContext) EnsureBroadcastTx(txBytes []byte) error {
|
|
|
|
if ctx.Async {
|
|
|
|
return ctx.ensureBroadcastTxAsync(txBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.ensureBroadcastTx(txBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx CLIContext) ensureBroadcastTxAsync(txBytes []byte) error {
|
|
|
|
res, err := ctx.BroadcastTxAsync(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.JSON {
|
|
|
|
type toJSON struct {
|
|
|
|
TxHash string
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Logger != nil {
|
|
|
|
resJSON := toJSON{res.Hash.String()}
|
|
|
|
bz, err := ctx.Codec.MarshalJSON(resJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Logger.Write(bz)
|
|
|
|
io.WriteString(ctx.Logger, "\n")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ctx.Logger != nil {
|
|
|
|
io.WriteString(ctx.Logger, fmt.Sprintf("Async tx sent (tx hash: %s)\n", res.Hash))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx CLIContext) ensureBroadcastTx(txBytes []byte) error {
|
|
|
|
res, err := ctx.BroadcastTx(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.JSON {
|
|
|
|
// since JSON is intended for automated scripts, always include
|
|
|
|
// response in JSON mode.
|
|
|
|
type toJSON struct {
|
|
|
|
Height int64
|
|
|
|
TxHash string
|
2018-09-08 01:33:55 -07:00
|
|
|
Response abci.ResponseDeliverTx
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Logger != nil {
|
2018-09-08 01:33:55 -07:00
|
|
|
resJSON := toJSON{res.Height, res.Hash.String(), res.DeliverTx}
|
2018-08-06 11:11:30 -07:00
|
|
|
bz, err := ctx.Codec.MarshalJSON(resJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Logger.Write(bz)
|
|
|
|
io.WriteString(ctx.Logger, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Logger != nil {
|
|
|
|
resStr := fmt.Sprintf("Committed at block %d (tx hash: %s)\n", res.Height, res.Hash.String())
|
|
|
|
|
|
|
|
if ctx.PrintResponse {
|
|
|
|
resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n",
|
|
|
|
res.Height, res.Hash.String(), res.DeliverTx,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(ctx.Logger, resStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// query performs a query from a Tendermint node with the provided store name
|
|
|
|
// and path.
|
2018-08-22 04:38:55 -07:00
|
|
|
func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, err error) {
|
2018-08-06 11:11:30 -07:00
|
|
|
node, err := ctx.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rpcclient.ABCIQueryOptions{
|
|
|
|
Height: ctx.Height,
|
|
|
|
Trusted: ctx.TrustNode,
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := node.ABCIQueryWithOptions(path, key, opts)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := result.Response
|
|
|
|
if !resp.IsOK() {
|
|
|
|
return res, errors.Errorf("query failed: (%d) %s", resp.Code, resp.Log)
|
|
|
|
}
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
// Data from trusted node or subspace query doesn't need verification.
|
2018-08-29 21:50:41 -07:00
|
|
|
if ctx.TrustNode || !isQueryStoreWithProof(path) {
|
|
|
|
return resp.Value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ctx.verifyProof(path, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
return resp.Value, nil
|
|
|
|
}
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
// Certify verifies the consensus proof at given height
|
|
|
|
func (ctx CLIContext) Certify(height int64) (lite.Commit, error) {
|
|
|
|
check, err := tmliteProxy.GetCertifiedCommit(height, ctx.Client, ctx.Certifier)
|
|
|
|
if tmliteErr.IsCommitNotFoundErr(err) {
|
|
|
|
return lite.Commit{}, ErrVerifyCommit(height)
|
|
|
|
} else if err != nil {
|
|
|
|
return lite.Commit{}, err
|
|
|
|
}
|
|
|
|
return check, nil
|
|
|
|
}
|
|
|
|
|
2018-08-29 21:50:41 -07:00
|
|
|
// verifyProof perform response proof verification
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-29 21:50:41 -07:00
|
|
|
func (ctx CLIContext) verifyProof(path string, resp abci.ResponseQuery) error {
|
|
|
|
|
|
|
|
if ctx.Certifier == nil {
|
2018-08-30 19:03:48 -07:00
|
|
|
return fmt.Errorf("missing valid certifier to verify data from untrusted node")
|
2018-08-29 21:50:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppHash for height H is in header H+1
|
2018-09-14 11:41:21 -07:00
|
|
|
commit, err := ctx.Certify(resp.Height + 1)
|
2018-08-29 21:50:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var multiStoreProof store.MultiStoreProof
|
2018-09-13 11:17:32 -07:00
|
|
|
cdc := codec.New()
|
2018-08-29 21:50:41 -07:00
|
|
|
err = cdc.UnmarshalBinary(resp.Proof, &multiStoreProof)
|
|
|
|
if err != nil {
|
2018-08-30 00:52:17 -07:00
|
|
|
return errors.Wrap(err, "failed to unmarshalBinary rangeProof")
|
2018-08-29 21:50:41 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 23:06:53 -07:00
|
|
|
// Verify the substore commit hash against trusted appHash
|
2018-09-14 11:41:21 -07:00
|
|
|
substoreCommitHash, err := store.VerifyMultiStoreCommitInfo(
|
|
|
|
multiStoreProof.StoreName, multiStoreProof.StoreInfos, commit.Header.AppHash)
|
2018-08-29 21:50:41 -07:00
|
|
|
if err != nil {
|
2018-08-30 00:52:17 -07:00
|
|
|
return errors.Wrap(err, "failed in verifying the proof against appHash")
|
2018-08-29 21:50:41 -07:00
|
|
|
}
|
2018-09-14 11:41:21 -07:00
|
|
|
|
2018-08-29 21:50:41 -07:00
|
|
|
err = store.VerifyRangeProof(resp.Key, resp.Value, substoreCommitHash, &multiStoreProof.RangeProof)
|
|
|
|
if err != nil {
|
2018-08-30 00:52:17 -07:00
|
|
|
return errors.Wrap(err, "failed in the range proof verification")
|
2018-08-29 21:50:41 -07:00
|
|
|
}
|
2018-09-14 11:41:21 -07:00
|
|
|
|
2018-08-29 21:50:41 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// 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) {
|
|
|
|
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>
|
|
|
|
// queryType can be app or store
|
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
|
|
|
|
}
|
|
|
|
paths := strings.SplitN(path[1:], "/", 3)
|
|
|
|
if len(paths) != 3 {
|
|
|
|
return false
|
|
|
|
}
|
2018-08-30 19:03:48 -07:00
|
|
|
|
2018-08-30 19:15:37 -07:00
|
|
|
if store.RequireProof("/" + paths[2]) {
|
2018-08-29 21:50:41 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|