Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generate-only to fully operate offline #4306

Merged
merged 10 commits into from
May 8, 2019
1 change: 1 addition & 0 deletions .pending/breaking/sdk/4305-GenerateOrBroad
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4305 `GenerateOrBroadcastMsgs` no longer takes an `offline` parameter.
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4305-The---generate-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4305 The `--generate-only` CLI flag fully respects offline tx processing.
13 changes: 8 additions & 5 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,23 @@ 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 {
fmt.Printf("failed to get from fields: %v", err)
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()
Expand Down
2 changes: 1 addition & 1 deletion client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,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"
Expand Down
44 changes: 14 additions & 30 deletions client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,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)
}

Expand Down Expand Up @@ -141,29 +145,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
Expand Down Expand Up @@ -327,16 +321,6 @@ 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() {
txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/init/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ following delegation and commission default parameters:

if info.GetType() == kbkeys.TypeOffline || info.GetType() == kbkeys.TypeMulti {
fmt.Println("Offline key passed in. Use `gaiacli tx sign` command to sign:")
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}

// write the unsigned transaction to the buffer
w := bytes.NewBuffer([]byte{})
cliCtx = cliCtx.WithOutput(w)

if err = utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true); err != nil {
if err = utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion x/bank/client/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {

// build and sign the transaction, then broadcast to Tendermint
msg := bank.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/crisis/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func GetCmdInvariantBroken(cdc *codec.Codec) *cobra.Command {
senderAddr := cliCtx.GetFromAddress()
moduleName, route := args[0], args[1]
msg := crisis.NewMsgVerifyInvariant(senderAddr, moduleName, route)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ $ gaiacli tx distr withdraw-rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9
msgs = append(msgs, types.NewMsgWithdrawValidatorCommission(valAddr))
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, msgs, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, msgs)
},
}
cmd.Flags().Bool(flagComission, false, "also withdraw validator's commission")
Expand Down Expand Up @@ -98,7 +98,7 @@ $ gaiacli tx distr withdraw-all-rewards --from mykey
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, msgs, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, msgs)
},
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ $ gaiacli tx set-withdraw-addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p --f
}

msg := types.NewMsgSetWithdrawAddress(delAddr, withdrawAddr)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
Expand Down
6 changes: 3 additions & 3 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ $ gaiacli gov submit-proposal --title="Test Proposal" --description="My awesome
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

Expand Down Expand Up @@ -151,7 +151,7 @@ $ gaiacli tx gov deposit 1 10stake --from mykey
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ $ gaiacli tx gov vote 1 yes --from mykey
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/client/cli/ibctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func IBCTransferCmd(cdc *codec.Codec) *cobra.Command {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/params/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where proposal.json contains:
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/slashing/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $ gaiacli tx slashing unjail --from mykey
valAddr := cliCtx.GetFromAddress()

msg := slashing.NewMsgUnjail(sdk.ValAddress(valAddr))
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
10 changes: 5 additions & 5 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, true)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

Expand Down Expand Up @@ -102,7 +102,7 @@ func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command {
msg := staking.NewMsgEditValidator(sdk.ValAddress(valAddr), description, newRate, newMinSelfDelegation)

// build and sign the transaction, then broadcast to Tendermint
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

Expand Down Expand Up @@ -140,7 +140,7 @@ $ gaiacli tx staking delegate cosmosvaloper1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59
}

msg := staking.NewMsgDelegate(delAddr, valAddr, amount)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ $ gaiacli tx staking redelegate cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
}

msg := staking.NewMsgBeginRedelegate(delAddr, valSrcAddr, valDstAddr, amount)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ $ gaiacli tx staking unbond cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
}

msg := staking.NewMsgUndelegate(delAddr, valAddr, amount)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
Expand Down