Skip to content

Commit

Permalink
R4R: Add offline option to command line (#136)
Browse files Browse the repository at this point in the history
* Add offline option to develop

* Add missed generate-only and fix import

* improve import order
  • Loading branch information
HaoyangLiu authored May 28, 2019
1 parent e4293f3 commit beb50b1
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 26 deletions.
5 changes: 5 additions & 0 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
cmn "github.com/tendermint/tendermint/libs/common"
Expand All @@ -13,6 +14,7 @@ import (
rpcclient "github.com/tendermint/tendermint/rpc/client"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
Expand Down Expand Up @@ -113,6 +115,9 @@ func (ctx CLIContext) GetAccountSequence(address []byte) (int64, error) {
// EnsureAccountExists ensures that an account exists for a given context. An
// error is returned if it does not.
func (ctx CLIContext) EnsureAccountExists() error {
if viper.GetBool(client.FlagOffline) {
return nil
}
addr, err := ctx.GetFromAddress()
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
FlagJson = "json"
FlagPrintResponse = "print-response"
FlagDryRun = "dry-run"
FlagOffline = "offline"
FlagGenerateOnly = "generate-only"
FlagIndentResponse = "indent"
)
Expand Down Expand Up @@ -66,6 +67,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 perform a simulation of a transaction, but don't broadcast it")
c.Flags().Bool(FlagOffline, false, "Offline mode. Do not query blockchain data")
c.Flags().Bool(FlagGenerateOnly, false, "build an unsigned transaction and write it to STDOUT")
viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
Expand Down
12 changes: 8 additions & 4 deletions client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"os"

"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -56,7 +59,7 @@ func CompleteAndBroadcastTxCli(txBldr authtxb.TxBuilder, cliCtx context.CLIConte
func simulateMsgs(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name, passphrase string, msgs []sdk.Msg) error {
txBytes, err := txBldr.BuildAndSign(name, passphrase, msgs)
if err != nil {
return err
return err
}

// run a simulation (via /app/simulate query)
Expand Down Expand Up @@ -88,8 +91,9 @@ func printTxResult(result sdk.Result) {

// 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) {
func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (err error) {
var stdTx auth.StdTx
offline := viper.GetBool(client.FlagOffline)
if offline {
stdTx, err = buildUnsignedStdTxOffline(txBldr, msgs)
} else {
Expand Down Expand Up @@ -169,7 +173,7 @@ func prepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (auth

// TODO: (ref #1903) Allow for user supplied account number without
// automatically doing a manual lookup.
if txBldr.AccountNumber == 0 {
if txBldr.AccountNumber == 0 && !viper.GetBool(client.FlagOffline) {
accNum, err := cliCtx.GetAccountNumber(from)
if err != nil {
return txBldr, err
Expand All @@ -179,7 +183,7 @@ func prepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (auth

// TODO: (ref #1903) Allow for user supplied account sequence without
// automatically doing a manual lookup.
if txBldr.Sequence == 0 {
if txBldr.Sequence == 0 && !viper.GetBool(client.FlagOffline) {
accSeq, err := cliCtx.GetAccountSequence(from)
if err != nil {
return txBldr, err
Expand Down
1 change: 0 additions & 1 deletion x/auth/client/cli/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ recommended to set such parameters manually.`,
cmd.Flags().String(client.FlagName, "", "Name of private key with which to sign")
cmd.Flags().Bool(flagAppend, true, "Append the signature to the existing ones. If disabled, old signatures would be overwritten")
cmd.Flags().Bool(flagPrintSigs, false, "Print the addresses that must sign the transaction and those who have already signed it, then exit")
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
return cmd
}

Expand Down
23 changes: 13 additions & 10 deletions x/bank/client/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

const (
flagTo = "to"
flagAmount = "amount"
flagTo = "to"
flagAmount = "amount"
flagOffline = "offline"
)

// SendTxCmd will create a send tx and sign it with the given key.
Expand Down Expand Up @@ -53,20 +54,22 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {
return err
}

account, err := cliCtx.GetAccount(from)
if err != nil {
return err
}
if !viper.GetBool(flagOffline) {
account, err := cliCtx.GetAccount(from)
if err != nil {
return err
}

// ensure account has enough coins
if !account.GetCoins().IsGTE(coins) {
return errors.Errorf("Address %s doesn't have enough coins to pay for this transaction.", from)
// ensure account has enough coins
if !account.GetCoins().IsGTE(coins) {
return errors.Errorf("Address %s doesn't have enough coins to pay for this transaction.", from)
}
}

// build and sign the transaction, then broadcast to Tendermint
msg := client.CreateMsg(from, to, coins)
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}

return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down
14 changes: 11 additions & 3 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ $ CLI gov submit-proposal --title="Test Proposal" --description="My awesome prop
}

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

// Build and sign the transaction, then broadcast to Tendermint
Expand Down Expand Up @@ -217,7 +217,7 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
}

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

// Build and sign the transaction, then broadcast to a Tendermint
Expand Down Expand Up @@ -263,7 +263,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command {
}

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

fmt.Printf("Vote[Voter:%s,ProposalID:%d,Option:%s]",
Expand Down Expand Up @@ -652,6 +652,10 @@ func GetCmdSubmitListProposal(cdc *codec.Codec) *cobra.Command {
return err
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}

cliCtx.PrintResponse = true
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
},
Expand Down Expand Up @@ -744,6 +748,10 @@ func GetCmdSubmitDelistProposal(cdc *codec.Codec) *cobra.Command {
return err
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}

cliCtx.PrintResponse = true
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []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 @@ -42,7 +42,7 @@ func IBCTransferCmd(cdc *codec.Codec) *cobra.Command {
return err
}
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}

return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []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 @@ -31,7 +31,7 @@ func GetCmdUnjail(cdc *codec.Codec) *cobra.Command {

msg := slashing.NewMsgUnjail(sdk.ValAddress(valAddr))
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
},
Expand Down
1 change: 1 addition & 0 deletions x/stake/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
FlagCommissionMaxChangeRate = "commission-max-change-rate"

FlagGenesisFormat = "genesis-format"
FlagOffline = "offline"
FlagNodeID = "node-id"
FlagIP = "ip"

Expand Down
14 changes: 8 additions & 6 deletions x/stake/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command {
}

if viper.GetBool(FlagGenesisFormat) || cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true)
//Enable offline mode
viper.Set(FlagOffline, true)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}

// build and sign the transaction, then broadcast to Tendermint
Expand Down Expand Up @@ -217,7 +219,7 @@ func GetCmdRemoveValidator(cdc *codec.Codec) *cobra.Command {
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}
// build and sign the transaction, then broadcast to Tendermint
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down Expand Up @@ -271,7 +273,7 @@ func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command {
msg := stake.NewMsgEditValidator(sdk.ValAddress(valAddr), description, newRate)

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

// build and sign the transaction, then broadcast to Tendermint
Expand Down Expand Up @@ -314,7 +316,7 @@ func GetCmdDelegate(cdc *codec.Codec) *cobra.Command {
msg := stake.NewMsgDelegate(delAddr, valAddr, amount)

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}
// build and sign the transaction, then broadcast to Tendermint
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down Expand Up @@ -384,7 +386,7 @@ func GetCmdBeginRedelegate(storeName string, cdc *codec.Codec) *cobra.Command {
msg := stake.NewMsgBeginRedelegate(delAddr, valSrcAddr, valDstAddr, sharesAmount)

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}
// build and sign the transaction, then broadcast to Tendermint
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down Expand Up @@ -447,7 +449,7 @@ func GetCmdBeginUnbonding(storeName string, cdc *codec.Codec) *cobra.Command {
msg := stake.NewMsgBeginUnbonding(delAddr, valAddr, sharesAmount)

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}
// build and sign the transaction, then broadcast to Tendermint
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down

0 comments on commit beb50b1

Please sign in to comment.