2018-02-23 01:49:30 -08:00
package client
2018-09-11 17:31:30 -07:00
import (
"fmt"
"strconv"
"github.com/spf13/cobra"
2018-09-25 14:36:42 -07:00
"github.com/spf13/viper"
2018-09-11 17:31:30 -07:00
)
2018-02-23 01:49:30 -08:00
2018-02-28 15:26:39 -08:00
// nolint
2018-02-23 01:49:30 -08:00
const (
2018-08-31 10:04:42 -07:00
// DefaultGasAdjustment is applied to gas estimates to avoid tx
// execution failures due to state changes that might
// occur between the tx simulation and the actual run.
2018-08-31 10:04:11 -07:00
DefaultGasAdjustment = 1.0
2018-08-31 10:04:42 -07:00
DefaultGasLimit = 200000
2018-09-11 17:31:30 -07:00
GasFlagSimulate = "simulate"
2018-08-22 04:38:55 -07:00
2018-11-16 14:21:36 -08:00
FlagUseLedger = "ledger"
FlagChainID = "chain-id"
FlagNode = "node"
FlagHeight = "height"
FlagGas = "gas"
FlagGasAdjustment = "gas-adjustment"
FlagTrustNode = "trust-node"
FlagFrom = "from"
FlagName = "name"
FlagAccountNumber = "account-number"
FlagSequence = "sequence"
FlagMemo = "memo"
FlagFee = "fee"
FlagAsync = "async"
FlagJson = "json"
FlagPrintResponse = "print-response"
FlagDryRun = "dry-run"
FlagGenerateOnly = "generate-only"
FlagIndentResponse = "indent"
FlagListenAddr = "laddr"
FlagCORS = "cors"
FlagMaxOpenConnections = "max-open"
FlagInsecure = "insecure"
FlagSSLHosts = "ssl-hosts"
FlagSSLCertFile = "ssl-certfile"
FlagSSLKeyFile = "ssl-keyfile"
2018-02-23 01:49:30 -08:00
)
2018-02-23 02:25:25 -08:00
// LineBreak can be included in a command list to provide a blank line
// to help with readability
2018-09-11 17:31:30 -07:00
var (
LineBreak = & cobra . Command { Run : func ( * cobra . Command , [ ] string ) { } }
GasFlagVar = GasSetting { Gas : DefaultGasLimit }
)
2018-02-23 02:25:25 -08:00
2018-02-23 01:49:30 -08:00
// GetCommands adds common flags to query commands
func GetCommands ( cmds ... * cobra . Command ) [ ] * cobra . Command {
for _ , c := range cmds {
2018-10-04 04:00:24 -07:00
c . Flags ( ) . Bool ( FlagIndentResponse , false , "Add indent to JSON response" )
2018-09-14 11:41:21 -07:00
c . Flags ( ) . Bool ( FlagTrustNode , false , "Trust connected full node (don't verify proofs for responses)" )
2018-06-28 17:54:47 -07:00
c . Flags ( ) . Bool ( FlagUseLedger , false , "Use a connected Ledger device" )
2018-02-23 01:49:30 -08:00
c . Flags ( ) . String ( FlagChainID , "" , "Chain ID of tendermint node" )
2018-06-13 15:13:51 -07:00
c . Flags ( ) . String ( FlagNode , "tcp://localhost:26657" , "<host>:<port> to tendermint rpc interface for this chain" )
2018-02-23 01:49:30 -08:00
c . Flags ( ) . Int64 ( FlagHeight , 0 , "block height to query, omit to get most recent provable block" )
2018-09-25 14:36:42 -07:00
viper . BindPFlag ( FlagTrustNode , c . Flags ( ) . Lookup ( FlagTrustNode ) )
viper . BindPFlag ( FlagUseLedger , c . Flags ( ) . Lookup ( FlagUseLedger ) )
viper . BindPFlag ( FlagChainID , c . Flags ( ) . Lookup ( FlagChainID ) )
viper . BindPFlag ( FlagNode , c . Flags ( ) . Lookup ( FlagNode ) )
2018-02-23 01:49:30 -08:00
}
return cmds
}
// PostCommands adds common flags for commands to post tx
func PostCommands ( cmds ... * cobra . Command ) [ ] * cobra . Command {
for _ , c := range cmds {
2018-10-04 04:00:24 -07:00
c . Flags ( ) . Bool ( FlagIndentResponse , false , "Add indent to JSON response" )
2018-09-25 13:48:38 -07:00
c . Flags ( ) . String ( FlagFrom , "" , "Name or address of private key with which to sign" )
2018-11-26 03:29:21 -08:00
c . Flags ( ) . Uint64 ( FlagAccountNumber , 0 , "AccountNumber number to sign the tx" )
c . Flags ( ) . Uint64 ( FlagSequence , 0 , "Sequence number to sign the tx" )
2018-06-20 12:27:36 -07:00
c . Flags ( ) . String ( FlagMemo , "" , "Memo to send along with transaction" )
2018-03-03 11:07:50 -08:00
c . Flags ( ) . String ( FlagFee , "" , "Fee to pay along with transaction" )
2018-02-23 01:49:30 -08:00
c . Flags ( ) . String ( FlagChainID , "" , "Chain ID of tendermint node" )
2018-06-13 15:13:51 -07:00
c . Flags ( ) . String ( FlagNode , "tcp://localhost:26657" , "<host>:<port> to tendermint rpc interface for this chain" )
2018-06-28 17:54:47 -07:00
c . Flags ( ) . Bool ( FlagUseLedger , false , "Use a connected Ledger device" )
2018-08-31 10:04:11 -07:00
c . Flags ( ) . Float64 ( FlagGasAdjustment , DefaultGasAdjustment , "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored " )
2018-07-05 19:15:35 -07:00
c . Flags ( ) . Bool ( FlagAsync , false , "broadcast transactions asynchronously" )
2018-07-05 20:42:41 -07:00
c . Flags ( ) . Bool ( FlagJson , false , "return output in json format" )
2018-08-10 23:42:57 -07:00
c . Flags ( ) . Bool ( FlagPrintResponse , true , "return tx response (only works with async = false)" )
2018-09-14 11:41:21 -07:00
c . Flags ( ) . Bool ( FlagTrustNode , true , "Trust connected full node (don't verify proofs for responses)" )
2018-08-31 10:04:11 -07:00
c . Flags ( ) . Bool ( FlagDryRun , false , "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it" )
2018-09-02 11:20:14 -07:00
c . Flags ( ) . Bool ( FlagGenerateOnly , false , "build an unsigned transaction and write it to STDOUT" )
2018-09-11 17:31:30 -07:00
// --gas can accept integers and "simulate"
c . Flags ( ) . Var ( & GasFlagVar , "gas" , fmt . Sprintf (
"gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)" , GasFlagSimulate , DefaultGasLimit ) )
2018-09-25 14:36:42 -07:00
viper . BindPFlag ( FlagTrustNode , c . Flags ( ) . Lookup ( FlagTrustNode ) )
viper . BindPFlag ( FlagUseLedger , c . Flags ( ) . Lookup ( FlagUseLedger ) )
viper . BindPFlag ( FlagChainID , c . Flags ( ) . Lookup ( FlagChainID ) )
viper . BindPFlag ( FlagNode , c . Flags ( ) . Lookup ( FlagNode ) )
2018-02-23 01:49:30 -08:00
}
return cmds
}
2018-09-11 17:31:30 -07:00
2018-11-16 14:21:36 -08:00
// RegisterRestServerFlags registers the flags required for rest server
func RegisterRestServerFlags ( cmd * cobra . Command ) * cobra . Command {
cmd . Flags ( ) . String ( FlagListenAddr , "tcp://localhost:1317" , "The address for the server to listen on" )
cmd . Flags ( ) . Bool ( FlagInsecure , false , "Do not set up SSL/TLS layer" )
cmd . Flags ( ) . String ( FlagSSLHosts , "" , "Comma-separated hostnames and IPs to generate a certificate for" )
cmd . Flags ( ) . String ( FlagSSLCertFile , "" , "Path to a SSL certificate file. If not supplied, a self-signed certificate will be generated." )
cmd . Flags ( ) . String ( FlagSSLKeyFile , "" , "Path to a key file; ignored if a certificate file is not supplied." )
cmd . Flags ( ) . String ( FlagCORS , "" , "Set the domains that can make CORS requests (* for all)" )
cmd . Flags ( ) . String ( FlagChainID , "" , "Chain ID of Tendermint node" )
cmd . Flags ( ) . String ( FlagNode , "tcp://localhost:26657" , "Address of the node to connect to" )
cmd . Flags ( ) . Int ( FlagMaxOpenConnections , 1000 , "The number of maximum open connections" )
cmd . Flags ( ) . Bool ( FlagTrustNode , false , "Trust connected full node (don't verify proofs for responses)" )
cmd . Flags ( ) . Bool ( FlagIndentResponse , false , "Add indent to JSON response" )
viper . BindPFlag ( FlagTrustNode , cmd . Flags ( ) . Lookup ( FlagTrustNode ) )
viper . BindPFlag ( FlagChainID , cmd . Flags ( ) . Lookup ( FlagChainID ) )
viper . BindPFlag ( FlagNode , cmd . Flags ( ) . Lookup ( FlagNode ) )
return cmd
}
2018-09-11 17:31:30 -07:00
// Gas flag parsing functions
// GasSetting encapsulates the possible values passed through the --gas flag.
type GasSetting struct {
Simulate bool
2018-11-19 09:13:45 -08:00
Gas uint64
2018-09-11 17:31:30 -07:00
}
// Type returns the flag's value type.
func ( v * GasSetting ) Type ( ) string { return "string" }
// Set parses and sets the value of the --gas flag.
func ( v * GasSetting ) Set ( s string ) ( err error ) {
v . Simulate , v . Gas , err = ReadGasFlag ( s )
return
}
func ( v * GasSetting ) String ( ) string {
if v . Simulate {
return GasFlagSimulate
}
2018-11-19 09:13:45 -08:00
return strconv . FormatUint ( v . Gas , 10 )
2018-09-11 17:31:30 -07:00
}
// ParseGasFlag parses the value of the --gas flag.
2018-11-19 09:13:45 -08:00
func ReadGasFlag ( s string ) ( simulate bool , gas uint64 , err error ) {
2018-09-11 17:31:30 -07:00
switch s {
case "" :
gas = DefaultGasLimit
case GasFlagSimulate :
simulate = true
default :
2018-11-19 09:13:45 -08:00
gas , err = strconv . ParseUint ( s , 10 , 64 )
2018-09-11 17:31:30 -07:00
if err != nil {
err = fmt . Errorf ( "gas must be either integer or %q" , GasFlagSimulate )
return
}
}
return
}