2020-01-24 08:40:56 -08:00
|
|
|
package client
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
import (
|
2019-06-22 02:24:59 -07:00
|
|
|
"bufio"
|
2018-09-07 03:59:16 -07:00
|
|
|
"bytes"
|
2018-08-22 04:38:55 -07:00
|
|
|
"fmt"
|
2020-06-08 08:19:29 -07:00
|
|
|
"io"
|
2019-02-26 03:34:27 -08:00
|
|
|
"io/ioutil"
|
2018-08-22 04:38:55 -07:00
|
|
|
"os"
|
2020-04-03 07:59:46 -07:00
|
|
|
"strings"
|
2018-08-22 04:38:55 -07:00
|
|
|
|
2020-04-03 07:59:46 -07:00
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
2019-03-19 09:52:43 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-07-29 15:33:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-08-06 11:11:30 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-15 14:55:02 -07:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-06-05 16:26:17 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-08-06 11:11:30 -07:00
|
|
|
)
|
|
|
|
|
2020-02-18 04:50:13 -08:00
|
|
|
// Codec defines the x/auth account codec to be used for use with the
|
|
|
|
// AccountRetriever. The application must be sure to set this to their respective
|
|
|
|
// codec that implements the Codec interface and must be the same codec that
|
|
|
|
// passed to the x/auth module.
|
|
|
|
//
|
|
|
|
// TODO:/XXX: Using a package-level global isn't ideal and we should consider
|
|
|
|
// refactoring the module manager to allow passing in the correct module codec.
|
2020-05-20 12:21:00 -07:00
|
|
|
var Codec codec.Marshaler
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2019-02-04 15:35:12 -08:00
|
|
|
// GasEstimateResponse defines a response definition for tx gas estimation.
|
|
|
|
type GasEstimateResponse struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
GasEstimate uint64 `json:"gas_estimate" yaml:"gas_estimate"`
|
2019-02-04 15:35:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gr GasEstimateResponse) String() string {
|
|
|
|
return fmt.Sprintf("gas estimate: %d", gr.GasEstimate)
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// EnrichWithGas calculates the gas estimate that would be consumed by the
|
2018-08-31 10:04:11 -07:00
|
|
|
// transaction and set the transaction's respective value accordingly.
|
2020-06-01 05:46:03 -07:00
|
|
|
func EnrichWithGas(txBldr authtypes.TxBuilder, clientCtx client.Context, msgs []sdk.Msg) (authtypes.TxBuilder, error) {
|
|
|
|
_, adjusted, err := simulateMsgs(txBldr, clientCtx, msgs)
|
2018-08-24 06:57:45 -07:00
|
|
|
if err != nil {
|
2018-09-07 10:15:49 -07:00
|
|
|
return txBldr, err
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2018-09-07 10:15:49 -07:00
|
|
|
return txBldr.WithGas(adjusted), nil
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 01:48:02 -07:00
|
|
|
// CalculateGas simulates the execution of a transaction and returns
|
2020-03-19 18:42:34 -07:00
|
|
|
// the simulation response obtained by the query and the adjusted gas amount.
|
2019-06-15 05:34:11 -07:00
|
|
|
func CalculateGas(
|
|
|
|
queryFunc func(string, []byte) ([]byte, int64, error), cdc *codec.Codec,
|
|
|
|
txBytes []byte, adjustment float64,
|
2020-03-19 18:40:07 -07:00
|
|
|
) (sdk.SimulationResponse, uint64, error) {
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2018-08-22 04:38:55 -07:00
|
|
|
// run a simulation (via /app/simulate query) to
|
2018-09-07 06:36:21 -07:00
|
|
|
// estimate gas and update TxBuilder accordingly
|
2019-06-13 06:54:17 -07:00
|
|
|
rawRes, _, err := queryFunc("/app/simulate", txBytes)
|
2018-08-22 04:38:55 -07:00
|
|
|
if err != nil {
|
2020-03-19 18:40:07 -07:00
|
|
|
return sdk.SimulationResponse{}, 0, err
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2020-04-03 07:59:46 -07:00
|
|
|
simRes, err := parseQueryResponse(rawRes)
|
2018-08-22 04:38:55 -07:00
|
|
|
if err != nil {
|
2020-03-19 18:40:07 -07:00
|
|
|
return sdk.SimulationResponse{}, 0, err
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2020-03-19 18:40:07 -07:00
|
|
|
adjusted := adjustGasEstimate(simRes.GasUsed, adjustment)
|
|
|
|
return simRes, adjusted, nil
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
|
|
|
|
2018-09-02 11:20:14 -07:00
|
|
|
// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
|
2020-07-29 15:33:42 -07:00
|
|
|
func PrintUnsignedStdTx(txBldr tx.Factory, clientCtx client.Context, msgs []sdk.Msg) error {
|
|
|
|
err := tx.GenerateOrBroadcastTxWithFactory(clientCtx, txBldr, msgs...)
|
|
|
|
return err
|
2018-09-02 11:20:14 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
// SignTx appends a signature to a transaction. If appendSig
|
2018-09-07 03:59:16 -07:00
|
|
|
// is false, it replaces the signatures already attached with the new signature.
|
2018-10-19 11:00:27 -07:00
|
|
|
// Don't perform online validation or lookups if offline is true.
|
2020-07-29 15:33:42 -07:00
|
|
|
func SignTx(txFactory tx.Factory, clientCtx client.Context, name string, stdTx client.TxBuilder, offline bool) error {
|
|
|
|
info, err := txFactory.Keybase().Key(name)
|
2018-09-07 03:59:16 -07:00
|
|
|
if err != nil {
|
2020-07-29 15:33:42 -07:00
|
|
|
return err
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
2020-07-29 15:33:42 -07:00
|
|
|
addr := sdk.AccAddress(info.GetPubKey().Address())
|
|
|
|
if !isTxSigner(addr, stdTx.GetTx().GetSigners()) {
|
|
|
|
return fmt.Errorf("%s: %s", sdkerrors.ErrorInvalidSigner, name)
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
2019-01-16 09:30:57 -08:00
|
|
|
if !offline {
|
2020-07-29 15:33:42 -07:00
|
|
|
txFactory, err = populateAccountFromState(txFactory, clientCtx, addr)
|
2018-09-07 03:59:16 -07:00
|
|
|
if err != nil {
|
2020-07-29 15:33:42 -07:00
|
|
|
return err
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
return tx.Sign(txFactory, name, stdTx)
|
2019-01-16 09:30:57 -08:00
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
// SignTxWithSignerAddress attaches a signature to a transaction.
|
2019-01-16 09:30:57 -08:00
|
|
|
// Don't perform online validation or lookups if offline is true, else
|
|
|
|
// populate account and sequence numbers from a foreign account.
|
2020-07-29 15:33:42 -07:00
|
|
|
func SignTxWithSignerAddress(txFactory tx.Factory, clientCtx client.Context, addr sdk.AccAddress,
|
|
|
|
name string, txBuilder client.TxBuilder, offline bool) (err error) {
|
2019-01-16 09:30:57 -08:00
|
|
|
|
|
|
|
// check whether the address is a signer
|
2020-07-29 15:33:42 -07:00
|
|
|
if !isTxSigner(addr, txBuilder.GetTx().GetSigners()) {
|
|
|
|
return fmt.Errorf("%s: %s", sdkerrors.ErrorInvalidSigner, name)
|
2019-01-16 09:30:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !offline {
|
2020-07-29 15:33:42 -07:00
|
|
|
txFactory, err = populateAccountFromState(txFactory, clientCtx, addr)
|
2018-09-07 03:59:16 -07:00
|
|
|
if err != nil {
|
2020-07-29 15:33:42 -07:00
|
|
|
return err
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
return tx.Sign(txFactory, name, txBuilder)
|
2019-01-16 09:30:57 -08:00
|
|
|
}
|
|
|
|
|
2019-02-26 03:34:27 -08:00
|
|
|
// Read and decode a StdTx from the given filename. Can pass "-" to read from stdin.
|
2020-06-18 13:29:41 -07:00
|
|
|
func ReadTxFromFile(ctx client.Context, filename string) (tx sdk.Tx, err error) {
|
2019-02-26 03:34:27 -08:00
|
|
|
var bytes []byte
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2019-02-26 03:34:27 -08:00
|
|
|
if filename == "-" {
|
|
|
|
bytes, err = ioutil.ReadAll(os.Stdin)
|
|
|
|
} else {
|
|
|
|
bytes, err = ioutil.ReadFile(filename)
|
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2019-02-26 03:34:27 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
return ctx.TxConfig.TxJSONDecoder()(bytes)
|
2019-02-26 03:34:27 -08:00
|
|
|
}
|
|
|
|
|
2020-06-08 08:19:29 -07:00
|
|
|
// NewBatchScanner returns a new BatchScanner to read newline-delimited StdTx transactions from r.
|
2020-07-29 15:33:42 -07:00
|
|
|
func NewBatchScanner(cfg client.TxConfig, r io.Reader) *BatchScanner {
|
|
|
|
return &BatchScanner{Scanner: bufio.NewScanner(r), cfg: cfg}
|
2020-06-08 08:19:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BatchScanner provides a convenient interface for reading batch data such as a file
|
|
|
|
// of newline-delimited JSON encoded StdTx.
|
|
|
|
type BatchScanner struct {
|
|
|
|
*bufio.Scanner
|
2020-07-29 15:33:42 -07:00
|
|
|
theTx sdk.Tx
|
|
|
|
cfg client.TxConfig
|
2020-06-08 08:19:29 -07:00
|
|
|
unmarshalErr error
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
// Tx returns the most recent Tx unmarshalled by a call to Scan.
|
|
|
|
func (bs BatchScanner) Tx() sdk.Tx { return bs.theTx }
|
2020-06-08 08:19:29 -07:00
|
|
|
|
|
|
|
// UnmarshalErr returns the first unmarshalling error that was encountered by the scanner.
|
|
|
|
func (bs BatchScanner) UnmarshalErr() error { return bs.unmarshalErr }
|
|
|
|
|
|
|
|
// Scan advances the Scanner to the next line.
|
|
|
|
func (bs *BatchScanner) Scan() bool {
|
|
|
|
if !bs.Scanner.Scan() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
tx, err := bs.cfg.TxJSONDecoder()(bs.Bytes())
|
|
|
|
bs.theTx = tx
|
|
|
|
if err != nil && bs.unmarshalErr == nil {
|
2020-06-08 08:19:29 -07:00
|
|
|
bs.unmarshalErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:36:10 -07:00
|
|
|
func populateAccountFromState(
|
2020-07-29 15:33:42 -07:00
|
|
|
txBldr tx.Factory, clientCtx client.Context, addr sdk.AccAddress,
|
|
|
|
) (tx.Factory, error) {
|
2019-03-26 07:36:10 -07:00
|
|
|
|
2020-06-18 13:29:41 -07:00
|
|
|
num, seq, err := clientCtx.AccountRetriever.GetAccountNumberSequence(clientCtx, addr)
|
2019-03-26 07:36:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return txBldr, err
|
2019-01-16 09:30:57 -08:00
|
|
|
}
|
|
|
|
|
2019-06-19 05:24:11 -07:00
|
|
|
return txBldr.WithAccountNumber(num).WithSequence(seq), nil
|
2018-09-07 03:59:16 -07:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:29:42 -08:00
|
|
|
// GetTxEncoder return tx encoder from global sdk configuration if ones is defined.
|
|
|
|
// Otherwise returns encoder with default logic.
|
|
|
|
func GetTxEncoder(cdc *codec.Codec) (encoder sdk.TxEncoder) {
|
|
|
|
encoder = sdk.GetConfig().GetTxEncoder()
|
|
|
|
if encoder == nil {
|
2019-06-05 16:26:17 -07:00
|
|
|
encoder = authtypes.DefaultTxEncoder(cdc)
|
2018-12-12 13:29:42 -08:00
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
|
|
|
return encoder
|
2018-12-12 13:29:42 -08:00
|
|
|
}
|
|
|
|
|
2020-03-19 18:42:34 -07:00
|
|
|
// simulateMsgs simulates the transaction and returns the simulation response and
|
|
|
|
// the adjusted gas value.
|
2020-06-01 05:46:03 -07:00
|
|
|
func simulateMsgs(txBldr authtypes.TxBuilder, clientCtx client.Context, msgs []sdk.Msg) (sdk.SimulationResponse, uint64, error) {
|
2019-02-01 17:04:13 -08:00
|
|
|
txBytes, err := txBldr.BuildTxForSim(msgs)
|
2018-09-11 17:31:30 -07:00
|
|
|
if err != nil {
|
2020-03-19 18:40:07 -07:00
|
|
|
return sdk.SimulationResponse{}, 0, err
|
2018-09-11 17:31:30 -07:00
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
return CalculateGas(clientCtx.QueryWithData, clientCtx.Codec, txBytes, txBldr.GasAdjustment())
|
2018-09-11 17:31:30 -07:00
|
|
|
}
|
|
|
|
|
2018-11-19 09:13:45 -08:00
|
|
|
func adjustGasEstimate(estimate uint64, adjustment float64) uint64 {
|
|
|
|
return uint64(adjustment * float64(estimate))
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
|
|
|
|
2020-04-03 07:59:46 -07:00
|
|
|
func parseQueryResponse(bz []byte) (sdk.SimulationResponse, error) {
|
2020-03-19 18:40:07 -07:00
|
|
|
var simRes sdk.SimulationResponse
|
2020-04-03 07:59:46 -07:00
|
|
|
if err := jsonpb.Unmarshal(strings.NewReader(string(bz)), &simRes); err != nil {
|
2020-03-19 18:40:07 -07:00
|
|
|
return sdk.SimulationResponse{}, err
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2020-03-19 18:40:07 -07:00
|
|
|
return simRes, nil
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2018-08-31 10:04:11 -07:00
|
|
|
|
2018-09-07 03:59:16 -07:00
|
|
|
func isTxSigner(user sdk.AccAddress, signers []sdk.AccAddress) bool {
|
|
|
|
for _, s := range signers {
|
|
|
|
if bytes.Equal(user.Bytes(), s.Bytes()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 11:22:47 -08:00
|
|
|
|
2018-09-07 03:59:16 -07:00
|
|
|
return false
|
|
|
|
}
|