2020-03-13 10:59:14 -07:00
|
|
|
package tx
|
2020-03-12 12:35:22 -07:00
|
|
|
|
|
|
|
import (
|
2020-03-24 13:36:12 -07:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-03-25 11:23:34 -07:00
|
|
|
"net/http"
|
2020-03-24 13:36:12 -07:00
|
|
|
"os"
|
2020-03-26 09:50:39 -07:00
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
"github.com/spf13/pflag"
|
2020-03-24 13:36:12 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-03-24 13:51:59 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-03-24 13:36:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
2020-09-21 09:48:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2020-11-09 08:01:43 -08:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2020-03-12 12:35:22 -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"
|
2020-03-25 11:23:34 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-09-10 11:26:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
2020-06-16 12:57:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
2020-12-18 06:55:25 -08:00
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2020-03-12 12:35:22 -07:00
|
|
|
)
|
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
// GenerateOrBroadcastTxCLI will either generate and print and unsigned transaction
|
|
|
|
// or sign it and broadcast it returning an error upon failure.
|
|
|
|
func GenerateOrBroadcastTxCLI(clientCtx client.Context, flagSet *pflag.FlagSet, msgs ...sdk.Msg) error {
|
|
|
|
txf := NewFactoryCLI(clientCtx, flagSet)
|
|
|
|
return GenerateOrBroadcastTxWithFactory(clientCtx, txf, msgs...)
|
|
|
|
}
|
|
|
|
|
2020-05-21 14:29:34 -07:00
|
|
|
// GenerateOrBroadcastTxWithFactory will either generate and print and unsigned transaction
|
|
|
|
// or sign it and broadcast it returning an error upon failure.
|
2020-06-01 05:46:03 -07:00
|
|
|
func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
|
|
|
|
if clientCtx.GenerateOnly {
|
|
|
|
return GenerateTx(clientCtx, txf, msgs...)
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
return BroadcastTx(clientCtx, txf, msgs...)
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateTx will generate an unsigned transaction and print it to the writer
|
|
|
|
// specified by ctx.Output. If simulation was requested, the gas will be
|
|
|
|
// simulated and also printed to the same writer before the transaction is
|
|
|
|
// printed.
|
2020-06-01 05:46:03 -07:00
|
|
|
func GenerateTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
|
2020-03-24 13:36:12 -07:00
|
|
|
if txf.SimulateAndExecute() {
|
2020-06-01 05:46:03 -07:00
|
|
|
if clientCtx.Offline {
|
2020-03-24 13:36:12 -07:00
|
|
|
return errors.New("cannot estimate gas in offline mode")
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
txf = txf.WithGas(adjusted)
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s\n", GasEstimateResponse{GasEstimate: txf.Gas()})
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := BuildUnsignedTx(txf, msgs...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
json, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientCtx.PrintString(fmt.Sprintf("%s\n", json))
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastTx attempts to generate, sign and broadcast a transaction with the
|
|
|
|
// given set of messages. It will also simulate gas requirements if necessary.
|
|
|
|
// It will return an error upon failure.
|
2020-06-01 05:46:03 -07:00
|
|
|
func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
|
|
|
|
txf, err := PrepareFactory(clientCtx, txf)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
if txf.SimulateAndExecute() || clientCtx.Simulate {
|
|
|
|
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
txf = txf.WithGas(adjusted)
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s\n", GasEstimateResponse{GasEstimate: txf.Gas()})
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
if clientCtx.Simulate {
|
2020-03-24 13:36:12 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := BuildUnsignedTx(txf, msgs...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
if !clientCtx.SkipConfirm {
|
2020-08-21 08:43:12 -07:00
|
|
|
out, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx())
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s\n\n", out)
|
|
|
|
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
2020-04-08 02:38:28 -07:00
|
|
|
ok, err := input.GetConfirmation("confirm transaction before signing and broadcasting", buf, os.Stderr)
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil || !ok {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s\n", "cancelled transaction")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add fee grant module (#8061)
* Add docs
* Add BasicFeeAllowance implementation
* Add expiration structs and complete basic fee
* Add delegation messages, add validation logic
* Add keeper and helper structs
* Add alias and handler to top level
* Add delegation module
* Add basic querier
* Add types tests
* Add types tests
* More internal test coverage
* Solid internal test coverage
* Expose Querier to top level module
* Add FeeAccount to auth/types, like StdTx, SignDoc
* Fix all tests in x/auth
* All tests pass
* Appease the Golang Linter
* Add fee-account command line flag
* Start on DelegatedDeductFeeDecorator
* Cleanup the Decorator
* Wire up delegation module in simapp
* add basic test for decorator (no delegation)
* Table tests for deduct fees
* Table tests over all conditions of delegated fee decorator
* Build full ante handler stack and test it
* Start genesis
* Implement Genesis
* Rename package delegation to subkeys
* Clarify antes test cases, handle empty account w/o fees
* Allow paying delegated fees with no account
* Pull mempool into delegated ante, for control on StdFee
* Use custom DelegatedTx, DelegatedFee for subkeys
* Revert all changes to x/auth.StdTx
* Appease scopelint
* Register DelegatedTx with codec
* Address PR comments
* Remove unnecessary DelegatedMempoolFeeDecorator
* Cleaned up errors in querier
* Clean up message sign bytes
* Minor PR comments
* Replace GetAllFees... with Iterator variants
* PrepareForExport adjusts grant expiration height
* Panic on de/serialization error in keeper
* Move custom ante handler chain to tests, update docs
* More cleanup
* More doc cleanup
* Renamed subkeys module to fee_grant
* Rename subkeys/delegation to fee grant in all strings
* Modify Msg and Keeper methods to use Grant not Delegate
* Add PeriodicFeeAllowance
* Update aliases
* Cover all accept cases for PeriodicFeeAllowance
* Et tu scopelint?
* Update docs as requested
* Remove error return from GetFeeGrant
* Code cleanup as requested by PR
* Updated all errors to use new sdk/errors package
* Use test suite for keeper tests
* Clean up alias.go file
* Define expected interfaces in exported, rather than importing from account
* Remove dependency on auth/ante
* Improve godoc, Logger
* Cleaned up ExpiresAt
* Improve error reporting with UseGrantedFee
* Enforce period limit subset of basic limit
* Add events
* Rename fee_grant to feegrant
* Ensure KeeperTestSuite actually runs
* Move types/tx to types
* Update alias file, include ante
* I do need nolint in alias.go
* Properly emit events in the handler. Use cosmos-sdk in amino types
* Update godoc
* Linting...
* Update errors
* Update pkg doc and fix ante-handler order
* Merge PR #5782: Migrate x/feegrant to proto
* fix errors
* proto changes
* proto changes
* fix errors
* fix errors
* genesis state changed to proto
* fix keeper tests
* fix test
* fixed tests
* fix tests
* updated expected keepers
* updated ante tests
* lint
* deleted alias.go
* tx updated to proto tx
* remove explicit signmode
* tests
* Added `cli/query.go`
* Added tx.go in cli
* updated `module.go`
* resolve errors in tx.go
* Add fee payer gentx func
* updated tx
* fixed error
* WIP: cli tests
* fix query error
* fix tests
* Unused types and funcs
* fix tests
* rename helper func to create tx
* remove unused
* update tx cfg
* fix cli tests
* added simulations
* Add `decoder.go`
* fix build fail
* added init genesis code
* update tx.go
* fixed LGTM alert
* modified cli
* remove gogoproto extensions
* change acc address type to string
* lint
* fix simulations
* Add gen simulations
* remove legacy querier
* remove legacy code
* add grpc queries tests
* fix simulations
* update module.go
* lint
* register feegrant NewSimulationManager
* fix sims
* fix sims
* add genesis test
* add periodic grant
* updated cmd
* changed times
* updated flags
* removed days as period clock
* added condition for period and exp
* add periodic fee cli tests
* udpated tests
* fix lint
* fix tests
* fix sims
* renaming to `fee_grant`
* review changes
* fix test
* add condition for duplicate grants
* fix tests
* add `genTxWithFeeGranter` in tests
* fix simulation
* one of changes & test fixes
* fix test
* fix lint
* changed package name `feegrant` to `fee_grant`
* review comments
* review changes
* review change
* review changes
* added fee-account in flags
* address review changes
* read fee granter from cli
* updated create account with mnemonic
* Address review comments
* move `simapp/ante` file to `feegrant/ante`
* update keeper logic to create account
* update docs
* fix tests
* update `serviceMsgClientConn` from `msgservice`
* review changes
* add test case for using more fees than allowed
* eliminate panic checks from keeper
* fix lint
* change store keys string to bytes
* fix tests
* review changes
* review changes
* udpate docs
* make spend limit optional
* fix tests
* fix tests
* review changes
* add norace tag
* proto-docs
* add docs
Co-authored-by: Ethan Frey <ethanfrey@users.noreply.github.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>
Co-authored-by: aleem1413 <aleem@vitwit.com>
Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>
Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2021-01-29 11:54:51 -08:00
|
|
|
tx.SetFeeGranter(clientCtx.GetFeeGranterAddress())
|
2020-12-14 13:44:15 -08:00
|
|
|
err = Sign(txf, clientCtx.GetFromName(), tx, true)
|
2020-06-16 12:57:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
txBytes, err := clientCtx.TxConfig.TxEncoder()(tx.GetTx())
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// broadcast to a Tendermint node
|
2020-06-01 05:46:03 -07:00
|
|
|
res, err := clientCtx.BroadcastTx(txBytes)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:17:40 -08:00
|
|
|
return clientCtx.PrintProto(res)
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WriteGeneratedTxResponse writes a generated unsigned transaction to the
|
|
|
|
// provided http.ResponseWriter. It will simulate gas costs if requested by the
|
|
|
|
// BaseReq. Upon any error, the error will be written to the http.ResponseWriter.
|
2020-07-30 07:45:18 -07:00
|
|
|
// Note that this function returns the legacy StdTx Amino JSON format for compatibility
|
|
|
|
// with legacy clients.
|
2020-03-25 11:23:34 -07:00
|
|
|
func WriteGeneratedTxResponse(
|
2020-06-01 05:46:03 -07:00
|
|
|
ctx client.Context, w http.ResponseWriter, br rest.BaseReq, msgs ...sdk.Msg,
|
2020-03-25 11:23:34 -07:00
|
|
|
) {
|
|
|
|
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-11 01:13:46 -07:00
|
|
|
gasSetting, err := flags.ParseGasSetting(br.Gas)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txf := Factory{fees: br.Fees, gasPrices: br.GasPrices}.
|
|
|
|
WithAccountNumber(br.AccountNumber).
|
|
|
|
WithSequence(br.Sequence).
|
2020-07-11 01:13:46 -07:00
|
|
|
WithGas(gasSetting.Gas).
|
2020-03-25 11:23:34 -07:00
|
|
|
WithGasAdjustment(gasAdj).
|
|
|
|
WithMemo(br.Memo).
|
|
|
|
WithChainID(br.ChainID).
|
2020-05-21 14:29:34 -07:00
|
|
|
WithSimulateAndExecute(br.Simulate).
|
2020-08-07 16:32:22 -07:00
|
|
|
WithTxConfig(ctx.TxConfig).
|
|
|
|
WithTimeoutHeight(br.TimeoutHeight)
|
2020-03-25 11:23:34 -07:00
|
|
|
|
2020-07-11 01:13:46 -07:00
|
|
|
if br.Simulate || gasSetting.Simulate {
|
2020-03-25 11:23:34 -07:00
|
|
|
if gasAdj < 0 {
|
2020-04-15 14:55:02 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, sdkerrors.ErrorInvalidGasAdjustment.Error())
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txf = txf.WithGas(adjusted)
|
|
|
|
|
|
|
|
if br.Simulate {
|
2020-08-10 12:41:21 -07:00
|
|
|
rest.WriteSimulationResponse(w, ctx.LegacyAmino, txf.Gas())
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := BuildUnsignedTx(txf, msgs...)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
stdTx, err := ConvertTxToStdTx(ctx.LegacyAmino, tx.GetTx())
|
2020-07-30 07:45:18 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
output, err := ctx.LegacyAmino.MarshalJSON(stdTx)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write(output)
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:36:12 -07:00
|
|
|
// BuildUnsignedTx builds a transaction to be signed given a set of messages. The
|
|
|
|
// transaction is initially created via the provided factory's generator. Once
|
|
|
|
// created, the fee, memo, and messages are set.
|
2020-06-01 05:46:03 -07:00
|
|
|
func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (client.TxBuilder, error) {
|
2020-03-24 13:36:12 -07:00
|
|
|
if txf.chainID == "" {
|
|
|
|
return nil, fmt.Errorf("chain ID required but not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
fees := txf.fees
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2020-03-24 13:36:12 -07:00
|
|
|
if !txf.gasPrices.IsZero() {
|
|
|
|
if !fees.IsZero() {
|
|
|
|
return nil, errors.New("cannot provide both fees and gas prices")
|
|
|
|
}
|
|
|
|
|
|
|
|
glDec := sdk.NewDec(int64(txf.gas))
|
|
|
|
|
|
|
|
// Derive the fees based on the provided gas prices, where
|
|
|
|
// fee = ceil(gasPrice * gasLimit).
|
|
|
|
fees = make(sdk.Coins, len(txf.gasPrices))
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2020-03-24 13:36:12 -07:00
|
|
|
for i, gp := range txf.gasPrices {
|
|
|
|
fee := gp.Amount.Mul(glDec)
|
|
|
|
fees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
tx := txf.txConfig.NewTxBuilder()
|
2020-03-24 13:36:12 -07:00
|
|
|
|
2020-04-06 07:24:40 -07:00
|
|
|
if err := tx.SetMsgs(msgs...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-22 08:02:36 -07:00
|
|
|
|
2020-06-16 12:57:37 -07:00
|
|
|
tx.SetMemo(txf.memo)
|
|
|
|
tx.SetFeeAmount(fees)
|
|
|
|
tx.SetGasLimit(txf.gas)
|
2020-08-07 16:32:22 -07:00
|
|
|
tx.SetTimeoutHeight(txf.TimeoutHeight())
|
2020-04-06 07:24:40 -07:00
|
|
|
|
2020-03-24 13:36:12 -07:00
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildSimTx creates an unsigned tx with an empty single signature and returns
|
|
|
|
// the encoded transaction or an error if the unsigned transaction cannot be
|
|
|
|
// built.
|
|
|
|
func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {
|
2020-09-01 13:59:48 -07:00
|
|
|
txb, err := BuildUnsignedTx(txf, msgs...)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an empty signature literal as the ante handler will populate with a
|
|
|
|
// sentinel pubkey.
|
2020-09-01 13:59:48 -07:00
|
|
|
sig := signing.SignatureV2{
|
2020-09-21 09:48:28 -07:00
|
|
|
PubKey: &secp256k1.PubKey{},
|
2020-09-01 13:59:48 -07:00
|
|
|
Data: &signing.SingleSignatureData{
|
2020-09-01 15:44:07 -07:00
|
|
|
SignMode: txf.signMode,
|
2020-09-01 13:59:48 -07:00
|
|
|
},
|
|
|
|
Sequence: txf.Sequence(),
|
|
|
|
}
|
|
|
|
if err := txb.SetSignatures(sig); err != nil {
|
2020-04-15 14:55:02 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-24 13:36:12 -07:00
|
|
|
|
2020-12-18 06:55:25 -08:00
|
|
|
protoProvider, ok := txb.(authtx.ProtoTxProvider)
|
2020-09-10 11:26:47 -07:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot simulate amino tx")
|
|
|
|
}
|
2020-12-18 06:55:25 -08:00
|
|
|
simReq := tx.SimulateRequest{Tx: protoProvider.GetProtoTx()}
|
2020-09-01 13:59:48 -07:00
|
|
|
|
|
|
|
return simReq.Marshal()
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateGas simulates the execution of a transaction and returns the
|
|
|
|
// simulation response obtained by the query and the adjusted gas amount.
|
|
|
|
func CalculateGas(
|
2020-03-25 11:23:34 -07:00
|
|
|
queryFunc func(string, []byte) ([]byte, int64, error), txf Factory, msgs ...sdk.Msg,
|
2020-10-30 05:32:02 -07:00
|
|
|
) (tx.SimulateResponse, uint64, error) {
|
2020-03-25 11:23:34 -07:00
|
|
|
txBytes, err := BuildSimTx(txf, msgs...)
|
|
|
|
if err != nil {
|
2020-10-30 05:32:02 -07:00
|
|
|
return tx.SimulateResponse{}, 0, err
|
2020-03-25 11:23:34 -07:00
|
|
|
}
|
|
|
|
|
2020-10-30 05:32:02 -07:00
|
|
|
// TODO This should use the generated tx service Client.
|
|
|
|
// https://github.com/cosmos/cosmos-sdk/issues/7726
|
|
|
|
bz, _, err := queryFunc("/cosmos.tx.v1beta1.Service/Simulate", txBytes)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
2020-10-30 05:32:02 -07:00
|
|
|
return tx.SimulateResponse{}, 0, err
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
2020-10-30 05:32:02 -07:00
|
|
|
var simRes tx.SimulateResponse
|
2020-09-01 13:59:48 -07:00
|
|
|
|
|
|
|
if err := simRes.Unmarshal(bz); err != nil {
|
2020-10-30 05:32:02 -07:00
|
|
|
return tx.SimulateResponse{}, 0, err
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
2020-09-01 13:59:48 -07:00
|
|
|
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareFactory ensures the account defined by ctx.GetFromAddress() exists and
|
|
|
|
// if the account number and/or the account sequence number are zero (not set),
|
|
|
|
// they will be queried for and set on the provided Factory. A new Factory with
|
|
|
|
// the updated fields will be returned.
|
2020-06-01 05:46:03 -07:00
|
|
|
func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
|
|
|
|
from := clientCtx.GetFromAddress()
|
2020-03-24 13:36:12 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
if err := txf.accountRetriever.EnsureExists(clientCtx, from); err != nil {
|
2020-03-24 13:36:12 -07:00
|
|
|
return txf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
initNum, initSeq := txf.accountNumber, txf.sequence
|
|
|
|
if initNum == 0 || initSeq == 0 {
|
2020-06-01 05:46:03 -07:00
|
|
|
num, seq, err := txf.accountRetriever.GetAccountNumberSequence(clientCtx, from)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return txf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if initNum == 0 {
|
|
|
|
txf = txf.WithAccountNumber(num)
|
|
|
|
}
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2020-03-24 13:36:12 -07:00
|
|
|
if initSeq == 0 {
|
|
|
|
txf = txf.WithSequence(seq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return txf, nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
// SignWithPrivKey signs a given tx with the given private key, and returns the
|
|
|
|
// corresponding SignatureV2 if the signing is successful.
|
|
|
|
func SignWithPrivKey(
|
2020-07-22 08:02:36 -07:00
|
|
|
signMode signing.SignMode, signerData authsigning.SignerData,
|
2020-11-09 08:01:43 -08:00
|
|
|
txBuilder client.TxBuilder, priv cryptotypes.PrivKey, txConfig client.TxConfig,
|
2020-08-21 07:20:47 -07:00
|
|
|
accSeq uint64,
|
2020-07-20 05:30:12 -07:00
|
|
|
) (signing.SignatureV2, error) {
|
|
|
|
var sigV2 signing.SignatureV2
|
|
|
|
|
2020-07-30 06:22:31 -07:00
|
|
|
// Generate the bytes to be signed.
|
|
|
|
signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
|
2020-07-20 05:30:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return sigV2, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign those bytes
|
|
|
|
signature, err := priv.Sign(signBytes)
|
|
|
|
if err != nil {
|
|
|
|
return sigV2, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the SignatureV2 struct
|
|
|
|
sigData := signing.SingleSignatureData{
|
|
|
|
SignMode: signMode,
|
|
|
|
Signature: signature,
|
|
|
|
}
|
2020-07-22 08:02:36 -07:00
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
sigV2 = signing.SignatureV2{
|
2020-08-21 07:20:47 -07:00
|
|
|
PubKey: priv.PubKey(),
|
|
|
|
Data: &sigData,
|
|
|
|
Sequence: accSeq,
|
2020-07-20 05:30:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return sigV2, nil
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:44:15 -08:00
|
|
|
func checkMultipleSigners(mode signing.SignMode, tx authsigning.Tx) error {
|
|
|
|
if mode == signing.SignMode_SIGN_MODE_DIRECT &&
|
|
|
|
len(tx.GetSigners()) > 1 {
|
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrNotSupported, "Signing in DIRECT mode is only supported for transactions with one signer only")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs a given tx with a named key. The bytes signed over are canconical.
|
|
|
|
// The resulting signature will be added to the transaction builder overwriting the previous
|
|
|
|
// ones if overwrite=true (otherwise, the signature will be appended).
|
|
|
|
// Signing a transaction with mutltiple signers in the DIRECT mode is not supprted and will
|
|
|
|
// return an error.
|
2020-07-22 08:02:36 -07:00
|
|
|
// An error is returned upon failure.
|
2020-12-14 13:44:15 -08:00
|
|
|
func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig bool) error {
|
2020-03-24 13:36:12 -07:00
|
|
|
if txf.keybase == nil {
|
2020-06-16 12:57:37 -07:00
|
|
|
return errors.New("keybase must be set prior to signing a transaction")
|
|
|
|
}
|
|
|
|
|
|
|
|
signMode := txf.signMode
|
|
|
|
if signMode == signing.SignMode_SIGN_MODE_UNSPECIFIED {
|
|
|
|
// use the SignModeHandler's default mode if unspecified
|
2020-07-20 05:30:12 -07:00
|
|
|
signMode = txf.txConfig.SignModeHandler().DefaultMode()
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
2020-12-14 13:44:15 -08:00
|
|
|
if err := checkMultipleSigners(signMode, txBuilder.GetTx()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-24 13:36:12 -07:00
|
|
|
|
2020-06-16 12:57:37 -07:00
|
|
|
key, err := txf.keybase.Key(name)
|
2020-03-24 13:36:12 -07:00
|
|
|
if err != nil {
|
2020-06-16 12:57:37 -07:00
|
|
|
return err
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
2020-06-16 12:57:37 -07:00
|
|
|
pubKey := key.GetPubKey()
|
2020-07-20 05:30:12 -07:00
|
|
|
signerData := authsigning.SignerData{
|
2020-08-21 07:20:47 -07:00
|
|
|
ChainID: txf.chainID,
|
|
|
|
AccountNumber: txf.accountNumber,
|
|
|
|
Sequence: txf.sequence,
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:20:47 -07:00
|
|
|
// For SIGN_MODE_DIRECT, calling SetSignatures calls setSignerInfos on
|
2020-07-30 06:22:31 -07:00
|
|
|
// TxBuilder under the hood, and SignerInfos is needed to generated the
|
|
|
|
// sign bytes. This is the reason for setting SetSignatures here, with a
|
|
|
|
// nil signature.
|
|
|
|
//
|
|
|
|
// Note: this line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it
|
|
|
|
// also doesn't affect its generated sign bytes, so for code's simplicity
|
|
|
|
// sake, we put it here.
|
|
|
|
sigData := signing.SingleSignatureData{
|
|
|
|
SignMode: signMode,
|
|
|
|
Signature: nil,
|
|
|
|
}
|
|
|
|
sig := signing.SignatureV2{
|
2020-08-21 07:20:47 -07:00
|
|
|
PubKey: pubKey,
|
|
|
|
Data: &sigData,
|
|
|
|
Sequence: txf.Sequence(),
|
2020-07-30 06:22:31 -07:00
|
|
|
}
|
2020-12-14 13:44:15 -08:00
|
|
|
var prevSignatures []signing.SignatureV2
|
|
|
|
if !overwriteSig {
|
|
|
|
prevSignatures, err = txBuilder.GetTx().GetSignaturesV2()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 06:22:31 -07:00
|
|
|
if err := txBuilder.SetSignatures(sig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the bytes to be signed.
|
2020-12-14 13:44:15 -08:00
|
|
|
bytesToSign, err := txf.txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
|
2020-06-16 12:57:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-15 14:55:02 -07:00
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
// Sign those bytes
|
2020-12-14 13:44:15 -08:00
|
|
|
sigBytes, _, err := txf.keybase.Sign(name, bytesToSign)
|
2020-06-16 12:57:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-15 14:55:02 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
// Construct the SignatureV2 struct
|
2020-07-30 06:22:31 -07:00
|
|
|
sigData = signing.SingleSignatureData{
|
2020-07-20 05:30:12 -07:00
|
|
|
SignMode: signMode,
|
|
|
|
Signature: sigBytes,
|
|
|
|
}
|
2020-07-30 06:22:31 -07:00
|
|
|
sig = signing.SignatureV2{
|
2020-08-21 07:20:47 -07:00
|
|
|
PubKey: pubKey,
|
|
|
|
Data: &sigData,
|
|
|
|
Sequence: txf.Sequence(),
|
2020-04-15 14:55:02 -07:00
|
|
|
}
|
|
|
|
|
2020-12-14 13:44:15 -08:00
|
|
|
if overwriteSig {
|
|
|
|
return txBuilder.SetSignatures(sig)
|
|
|
|
}
|
|
|
|
prevSignatures = append(prevSignatures, sig)
|
|
|
|
return txBuilder.SetSignatures(prevSignatures...)
|
2020-03-24 13:36:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GasEstimateResponse defines a response definition for tx gas estimation.
|
|
|
|
type GasEstimateResponse struct {
|
|
|
|
GasEstimate uint64 `json:"gas_estimate" yaml:"gas_estimate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gr GasEstimateResponse) String() string {
|
|
|
|
return fmt.Sprintf("gas estimate: %d", gr.GasEstimate)
|
|
|
|
}
|