Merge PR #4306: Allow generate-only to fully operate offline

This commit is contained in:
Alexander Bezobchuk
2019-05-08 16:06:05 -04:00
committed by GitHub
parent 2d93efd0d5
commit 85ffce5f58
17 changed files with 63 additions and 86 deletions
+8 -5
View File
@@ -62,13 +62,9 @@ type CLIContext struct {
// command line using Viper. It takes a key name or address and populates the FromName and
// FromAddress field accordingly.
func NewCLIContextWithFrom(from string) CLIContext {
var nodeURI string
var rpc rpcclient.Client
nodeURI := viper.GetString(client.FlagNode)
if nodeURI != "" {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
}
genOnly := viper.GetBool(client.FlagGenerateOnly)
fromAddress, fromName, err := GetFromFields(from, genOnly)
if err != nil {
@@ -76,6 +72,13 @@ func NewCLIContextWithFrom(from string) CLIContext {
os.Exit(1)
}
if !genOnly {
nodeURI = viper.GetString(client.FlagNode)
if nodeURI != "" {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
}
}
// We need to use a single verifier for all contexts
if verifier == nil || verifierHome != viper.GetString(cli.HomeFlag) {
verifier = createVerifier()
+1 -1
View File
@@ -95,7 +95,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
c.Flags().Bool(FlagPrintResponse, true, "return tx response (only works with async = false)")
c.Flags().Bool(FlagTrustNode, true, "Trust connected full node (don't verify proofs for responses)")
c.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it")
c.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible)")
c.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible and the node operates offline)")
c.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation")
// --gas can accept integers and "simulate"
+21 -32
View File
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
@@ -30,11 +31,15 @@ func (gr GasEstimateResponse) String() string {
return fmt.Sprintf("gas estimate: %d", gr.GasEstimate)
}
// GenerateOrBroadcastMsgs respects CLI flags and outputs a message
func GenerateOrBroadcastMsgs(cliCtx context.CLIContext, txBldr authtxb.TxBuilder, msgs []sdk.Msg, offline bool) error {
// GenerateOrBroadcastMsgs creates a StdTx given a series of messages. If
// the provided context has generate-only enabled, the tx will only be printed
// to STDOUT in a fully offline manner. Otherwise, the tx will be signed and
// broadcasted.
func GenerateOrBroadcastMsgs(cliCtx context.CLIContext, txBldr authtxb.TxBuilder, msgs []sdk.Msg) error {
if cliCtx.GenerateOnly {
return PrintUnsignedStdTx(txBldr, cliCtx, msgs, offline)
return PrintUnsignedStdTx(txBldr, cliCtx, msgs)
}
return CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
}
@@ -141,29 +146,19 @@ func CalculateGas(queryFunc func(string, common.HexBytes) ([]byte, error),
}
// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
// Don't perform online validation or lookups if offline is true.
func PrintUnsignedStdTx(
txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg, offline bool,
) (err error) {
var stdTx auth.StdTx
if offline {
stdTx, err = buildUnsignedStdTxOffline(txBldr, cliCtx, msgs)
} else {
stdTx, err = buildUnsignedStdTx(txBldr, cliCtx, msgs)
}
func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) error {
stdTx, err := buildUnsignedStdTxOffline(txBldr, cliCtx, msgs)
if err != nil {
return
return err
}
json, err := cliCtx.Codec.MarshalJSON(stdTx)
if err == nil {
fmt.Fprintf(cliCtx.Output, "%s\n", json)
if err != nil {
return err
}
return
_, _ = fmt.Fprintf(cliCtx.Output, "%s\n", json)
return nil
}
// SignStdTx appends a signature to a StdTx and returns a copy of it. If appendSig
@@ -327,21 +322,15 @@ func PrepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (auth
return txBldr, nil
}
// buildUnsignedStdTx builds a StdTx as per the parameters passed in the
// contexts. Gas is automatically estimated if gas wanted is set to 0.
func buildUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (stdTx auth.StdTx, err error) {
txBldr, err = PrepareTxBuilder(txBldr, cliCtx)
if err != nil {
return
}
return buildUnsignedStdTxOffline(txBldr, cliCtx, msgs)
}
func buildUnsignedStdTxOffline(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (stdTx auth.StdTx, err error) {
if txBldr.SimulateAndExecute() {
if cliCtx.GenerateOnly {
return stdTx, errors.New("cannot estimate gas with generate-only")
}
txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs)
if err != nil {
return
return stdTx, err
}
fmt.Fprintf(os.Stderr, "estimated gas = %v\n", txBldr.Gas())
@@ -349,7 +338,7 @@ func buildUnsignedStdTxOffline(txBldr authtxb.TxBuilder, cliCtx context.CLIConte
stdSignMsg, err := txBldr.BuildSignMsg(msgs)
if err != nil {
return
return stdTx, nil
}
return auth.NewStdTx(stdSignMsg.Msgs, stdSignMsg.Fee, nil, stdSignMsg.Memo), nil