diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 2a52b7179f..20faf06cd0 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -498,10 +498,9 @@ func validateBasicTxMsgs(msgs []sdk.Msg) sdk.Error { func (app *BaseApp) getContextForAnte(mode runTxMode, txBytes []byte) (ctx sdk.Context) { // Get the context ctx = getState(app, mode).ctx.WithTxBytes(txBytes) - if mode != runTxModeDeliver { - return + if mode == runTxModeDeliver { + ctx = ctx.WithSigningValidators(app.signedValidators) } - ctx = ctx.WithSigningValidators(app.signedValidators) return } @@ -568,10 +567,10 @@ func getState(app *BaseApp, mode runTxMode) *state { } func (app *BaseApp) initializeContext(ctx sdk.Context, mode runTxMode) sdk.Context { - if mode != runTxModeSimulate { - return ctx + if mode == runTxModeSimulate { + ctx = ctx.WithMultiStore(getState(app, runTxModeSimulate).CacheMultiStore()) } - return ctx.WithMultiStore(getState(app, runTxModeSimulate).CacheMultiStore()) + return ctx } // runTx processes a transaction. The transactions is proccessed via an diff --git a/client/flags.go b/client/flags.go index 15cc9effa4..7337e97411 100644 --- a/client/flags.go +++ b/client/flags.go @@ -4,7 +4,7 @@ import "github.com/spf13/cobra" // nolint const ( - DefaultGasAdjustment = 0 + DefaultGasAdjustment = 1.2 FlagUseLedger = "ledger" FlagChainID = "chain-id" @@ -53,7 +53,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command { c.Flags().String(FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") c.Flags().Int64(FlagGas, 0, "gas limit to set per-transaction; set to 0 to calculate required gas automatically") - c.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; defaults to an internal value") + c.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation") c.Flags().Bool(FlagAsync, false, "broadcast transactions asynchronously") c.Flags().Bool(FlagJson, false, "return output in json format") c.Flags().Bool(FlagPrintResponse, true, "return tx response (only works with async = false)") diff --git a/client/utils/utils.go b/client/utils/utils.go index da49ff5e03..fb5d619887 100644 --- a/client/utils/utils.go +++ b/client/utils/utils.go @@ -59,9 +59,11 @@ func SendTx(txCtx authctx.TxContext, cliCtx context.CLIContext, msgs []sdk.Msg) return err } - txCtx, err = enrichCtxWithGasIfGasAuto(txCtx, cliCtx, passphrase, msgs) - if err != nil { - return err + if cliCtx.Gas == 0 { + txCtx, err = EnrichCtxWithGas(txCtx, cliCtx, cliCtx.FromAddressName, passphrase, msgs) + if err != nil { + return err + } } // build and sign the transaction @@ -73,20 +75,19 @@ func SendTx(txCtx authctx.TxContext, cliCtx context.CLIContext, msgs []sdk.Msg) return cliCtx.EnsureBroadcastTx(txBytes) } -func enrichCtxWithGasIfGasAuto(txCtx authctx.TxContext, cliCtx context.CLIContext, passphrase string, msgs []sdk.Msg) (authctx.TxContext, error) { - if cliCtx.Gas == 0 { - txBytes, err := BuildAndSignTxWithZeroGas(txCtx, cliCtx.FromAddressName, passphrase, msgs) - if err != nil { - return txCtx, err - } - estimate, adjusted, err := CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) - if err != nil { - return txCtx, err - } - fmt.Fprintf(os.Stderr, "gas: [estimated = %v] [adjusted = %v]\n", estimate, adjusted) - return txCtx.WithGas(adjusted), nil +// EnrichCtxWithGas calculates the gas estimate that would be consumed by the +// transaction and set the transaction's respective value accordingly. +func EnrichCtxWithGas(txCtx authctx.TxContext, cliCtx context.CLIContext, name, passphrase string, msgs []sdk.Msg) (authctx.TxContext, error) { + txBytes, err := BuildAndSignTxWithZeroGas(txCtx, name, passphrase, msgs) + if err != nil { + return txCtx, err } - return txCtx, nil + estimate, adjusted, err := CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) + if err != nil { + return txCtx, err + } + fmt.Fprintf(os.Stderr, "gas: [estimated = %v] [adjusted = %v]\n", estimate, adjusted) + return txCtx.WithGas(adjusted), nil } // BuildAndSignTxWithZeroGas builds transactions with GasWanted set to 0. diff --git a/x/bank/client/rest/sendtx.go b/x/bank/client/rest/sendtx.go index 14bff9a49b..c7baa96910 100644 --- a/x/bank/client/rest/sendtx.go +++ b/x/bank/client/rest/sendtx.go @@ -86,9 +86,9 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo } if m.Gas == 0 { - newCtx, httperr, err := enrichContextWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, msg) + newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, []sdk.Msg{msg}) if err != nil { - utils.WriteErrorResponse(&w, httperr, err.Error()) + utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error()) return } txCtx = newCtx @@ -115,15 +115,3 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo w.Write(output) } } - -func enrichContextWithGas(txCtx authctx.TxContext, cliCtx context.CLIContext, name, password string, msg sdk.Msg) (authctx.TxContext, int, error) { - txBytes, err := utils.BuildAndSignTxWithZeroGas(txCtx, name, password, []sdk.Msg{msg}) - if err != nil { - return txCtx, http.StatusInternalServerError, err - } - _, adjusted, err := utils.CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) - if err != nil { - return txCtx, http.StatusUnauthorized, err - } - return txCtx.WithGas(adjusted), http.StatusOK, nil -} diff --git a/x/gov/client/rest/util.go b/x/gov/client/rest/util.go index e204028310..f98f7bfa59 100644 --- a/x/gov/client/rest/util.go +++ b/x/gov/client/rest/util.go @@ -77,9 +77,9 @@ func signAndBuild(w http.ResponseWriter, cliCtx context.CLIContext, baseReq base } if baseReq.Gas == 0 { - newCtx, httperr, err := enrichContextWithGas(txCtx, cliCtx, baseReq.Name, baseReq.Password, msg) + newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, baseReq.Name, baseReq.Password, []sdk.Msg{msg}) if err != nil { - utils.WriteErrorResponse(&w, httperr, err.Error()) + utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error()) return } txCtx = newCtx @@ -116,15 +116,3 @@ func parseInt64OrReturnBadRequest(s string, w http.ResponseWriter) (n int64, ok } return n, true } - -func enrichContextWithGas(txCtx authctx.TxContext, cliCtx context.CLIContext, name, password string, msg sdk.Msg) (authctx.TxContext, int, error) { - txBytes, err := utils.BuildAndSignTxWithZeroGas(txCtx, name, password, []sdk.Msg{msg}) - if err != nil { - return txCtx, http.StatusInternalServerError, err - } - _, adjusted, err := utils.CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) - if err != nil { - return txCtx, http.StatusUnauthorized, err - } - return txCtx.WithGas(adjusted), http.StatusOK, nil -} diff --git a/x/ibc/client/rest/transfer.go b/x/ibc/client/rest/transfer.go index cf0f24b312..765208b056 100644 --- a/x/ibc/client/rest/transfer.go +++ b/x/ibc/client/rest/transfer.go @@ -77,9 +77,9 @@ func TransferRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.C } if m.Gas == 0 { - newCtx, httperr, err := enrichContextWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, msg) + newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, []sdk.Msg{msg}) if err != nil { - utils.WriteErrorResponse(&w, httperr, err.Error()) + utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error()) return } txCtx = newCtx @@ -106,15 +106,3 @@ func TransferRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.C w.Write(output) } } - -func enrichContextWithGas(txCtx authctx.TxContext, cliCtx context.CLIContext, name, password string, msg sdk.Msg) (authctx.TxContext, int, error) { - txBytes, err := utils.BuildAndSignTxWithZeroGas(txCtx, name, password, []sdk.Msg{msg}) - if err != nil { - return txCtx, http.StatusInternalServerError, err - } - _, adjusted, err := utils.CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) - if err != nil { - return txCtx, http.StatusUnauthorized, err - } - return txCtx.WithGas(adjusted), http.StatusOK, nil -} diff --git a/x/slashing/client/rest/tx.go b/x/slashing/client/rest/tx.go index 414b05cfda..26412ebc00 100644 --- a/x/slashing/client/rest/tx.go +++ b/x/slashing/client/rest/tx.go @@ -78,9 +78,9 @@ func unjailRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLI msg := slashing.NewMsgUnjail(validatorAddr) if m.Gas == 0 { - newCtx, httperr, err := enrichContextWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, msg) + newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, []sdk.Msg{msg}) if err != nil { - utils.WriteErrorResponse(&w, httperr, err.Error()) + utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error()) return } txCtx = newCtx @@ -107,15 +107,3 @@ func unjailRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLI w.Write(output) } } - -func enrichContextWithGas(txCtx authctx.TxContext, cliCtx context.CLIContext, name, password string, msg sdk.Msg) (authctx.TxContext, int, error) { - txBytes, err := utils.BuildAndSignTxWithZeroGas(txCtx, name, password, []sdk.Msg{msg}) - if err != nil { - return txCtx, http.StatusInternalServerError, err - } - _, adjusted, err := utils.CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) - if err != nil { - return txCtx, http.StatusUnauthorized, err - } - return txCtx.WithGas(adjusted), http.StatusOK, nil -} diff --git a/x/stake/client/rest/tx.go b/x/stake/client/rest/tx.go index 729f578957..3d7d419a3a 100644 --- a/x/stake/client/rest/tx.go +++ b/x/stake/client/rest/tx.go @@ -276,9 +276,9 @@ func delegationsRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx contex m.Sequence++ if m.Gas == 0 { - newCtx, httperr, err := enrichContextWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, msg) + newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, []sdk.Msg{msg}) if err != nil { - utils.WriteErrorResponse(&w, httperr, err.Error()) + utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error()) return } txCtx = newCtx @@ -316,15 +316,3 @@ func delegationsRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx contex w.Write(output) } } - -func enrichContextWithGas(txCtx authcliCtx.TxContext, cliCtx context.CLIContext, name, password string, msg sdk.Msg) (authcliCtx.TxContext, int, error) { - txBytes, err := utils.BuildAndSignTxWithZeroGas(txCtx, name, password, []sdk.Msg{msg}) - if err != nil { - return txCtx, http.StatusInternalServerError, err - } - _, adjusted, err := utils.CalculateGas(cliCtx.Query, cliCtx.Codec, txBytes, cliCtx.GasAdjustment) - if err != nil { - return txCtx, http.StatusUnauthorized, err - } - return txCtx.WithGas(adjusted), http.StatusOK, nil -}