flags.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package utils
  2. import (
  3. "crypto/ecdsa"
  4. "os"
  5. "path"
  6. "runtime"
  7. "github.com/codegangsta/cli"
  8. "github.com/ethereum/go-ethereum/accounts"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/core"
  11. "github.com/ethereum/go-ethereum/crypto"
  12. "github.com/ethereum/go-ethereum/eth"
  13. "github.com/ethereum/go-ethereum/ethdb"
  14. "github.com/ethereum/go-ethereum/event"
  15. "github.com/ethereum/go-ethereum/logger"
  16. "github.com/ethereum/go-ethereum/logger/glog"
  17. "github.com/ethereum/go-ethereum/p2p/nat"
  18. "github.com/ethereum/go-ethereum/rpc"
  19. "github.com/ethereum/go-ethereum/xeth"
  20. )
  21. func init() {
  22. cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
  23. VERSION:
  24. {{.Version}}
  25. COMMANDS:
  26. {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  27. {{end}}{{if .Flags}}
  28. GLOBAL OPTIONS:
  29. {{range .Flags}}{{.}}
  30. {{end}}{{end}}
  31. `
  32. cli.CommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...]
  33. {{if .Description}}{{.Description}}
  34. {{end}}{{if .Subcommands}}
  35. SUBCOMMANDS:
  36. {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  37. {{end}}{{end}}{{if .Flags}}
  38. OPTIONS:
  39. {{range .Flags}}{{.}}
  40. {{end}}{{end}}
  41. `
  42. }
  43. // NewApp creates an app with sane defaults.
  44. func NewApp(version, usage string) *cli.App {
  45. app := cli.NewApp()
  46. app.Name = path.Base(os.Args[0])
  47. app.Author = ""
  48. //app.Authors = nil
  49. app.Email = ""
  50. app.Version = version
  51. app.Usage = usage
  52. return app
  53. }
  54. // These are all the command line flags we support.
  55. // If you add to this list, please remember to include the
  56. // flag in the appropriate command definition.
  57. //
  58. // The flags are defined here so their names and help texts
  59. // are the same for all commands.
  60. var (
  61. // General settings
  62. DataDirFlag = DirectoryFlag{
  63. Name: "datadir",
  64. Usage: "Data directory to be used",
  65. Value: DirectoryString{common.DefaultDataDir()},
  66. }
  67. ProtocolVersionFlag = cli.IntFlag{
  68. Name: "protocolversion",
  69. Usage: "ETH protocol version",
  70. Value: eth.ProtocolVersion,
  71. }
  72. NetworkIdFlag = cli.IntFlag{
  73. Name: "networkid",
  74. Usage: "Network Id",
  75. Value: eth.NetworkId,
  76. }
  77. // miner settings
  78. MinerThreadsFlag = cli.IntFlag{
  79. Name: "minerthreads",
  80. Usage: "Number of miner threads",
  81. Value: runtime.NumCPU(),
  82. }
  83. MiningEnabledFlag = cli.BoolFlag{
  84. Name: "mine",
  85. Usage: "Enable mining",
  86. }
  87. EtherbaseFlag = cli.StringFlag{
  88. Name: "etherbase",
  89. Usage: "public address for block mining rewards. By default the address of your primary account is used",
  90. Value: "primary",
  91. }
  92. UnlockedAccountFlag = cli.StringFlag{
  93. Name: "unlock",
  94. Usage: "unlock the account given until this program exits (prompts for password). '--unlock primary' unlocks the primary account",
  95. Value: "",
  96. }
  97. PasswordFileFlag = cli.StringFlag{
  98. Name: "password",
  99. Usage: "Path to password file for (un)locking an existing account.",
  100. Value: "",
  101. }
  102. // logging and debug settings
  103. LogFileFlag = cli.StringFlag{
  104. Name: "logfile",
  105. Usage: "Send log output to a file",
  106. }
  107. LogLevelFlag = cli.IntFlag{
  108. Name: "loglevel",
  109. Usage: "0-5 (silent, error, warn, info, debug, debug detail)",
  110. Value: int(logger.InfoLevel),
  111. }
  112. LogJSONFlag = cli.StringFlag{
  113. Name: "logjson",
  114. Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
  115. Value: "",
  116. }
  117. VMDebugFlag = cli.BoolFlag{
  118. Name: "vmdebug",
  119. Usage: "Virtual Machine debug output",
  120. }
  121. // RPC settings
  122. RPCEnabledFlag = cli.BoolFlag{
  123. Name: "rpc",
  124. Usage: "Whether RPC server is enabled",
  125. }
  126. RPCListenAddrFlag = cli.StringFlag{
  127. Name: "rpcaddr",
  128. Usage: "Listening address for the JSON-RPC server",
  129. Value: "127.0.0.1",
  130. }
  131. RPCPortFlag = cli.IntFlag{
  132. Name: "rpcport",
  133. Usage: "Port on which the JSON-RPC server should listen",
  134. Value: 8545,
  135. }
  136. RPCCORSDomainFlag = cli.StringFlag{
  137. Name: "rpccorsdomain",
  138. Usage: "Domain on which to send Access-Control-Allow-Origin header",
  139. Value: "",
  140. }
  141. // Network Settings
  142. MaxPeersFlag = cli.IntFlag{
  143. Name: "maxpeers",
  144. Usage: "Maximum number of network peers",
  145. Value: 16,
  146. }
  147. ListenPortFlag = cli.IntFlag{
  148. Name: "port",
  149. Usage: "Network listening port",
  150. Value: 30303,
  151. }
  152. BootnodesFlag = cli.StringFlag{
  153. Name: "bootnodes",
  154. Usage: "Space-separated enode URLs for discovery bootstrap",
  155. Value: "",
  156. }
  157. NodeKeyFileFlag = cli.StringFlag{
  158. Name: "nodekey",
  159. Usage: "P2P node key file",
  160. }
  161. NodeKeyHexFlag = cli.StringFlag{
  162. Name: "nodekeyhex",
  163. Usage: "P2P node key as hex (for testing)",
  164. }
  165. NATFlag = cli.StringFlag{
  166. Name: "nat",
  167. Usage: "Port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
  168. Value: "any",
  169. }
  170. JSpathFlag = cli.StringFlag{
  171. Name: "jspath",
  172. Usage: "JS library path to be used with console and js subcommands",
  173. Value: ".",
  174. }
  175. BacktraceAtFlag = cli.GenericFlag{
  176. Name: "backtrace_at",
  177. Usage: "When set to a file and line number holding a logging statement a stack trace will be written to the Info log",
  178. Value: glog.GetTraceLocation(),
  179. }
  180. LogToStdErrFlag = cli.BoolFlag{
  181. Name: "logtostderr",
  182. Usage: "Logs are written to standard error instead of to files.",
  183. }
  184. LogVModuleFlag = cli.GenericFlag{
  185. Name: "vmodule",
  186. Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a V level.",
  187. Value: glog.GetVModule(),
  188. }
  189. )
  190. func GetNAT(ctx *cli.Context) nat.Interface {
  191. natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
  192. if err != nil {
  193. Fatalf("Option %s: %v", NATFlag.Name, err)
  194. }
  195. return natif
  196. }
  197. func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
  198. hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
  199. var err error
  200. switch {
  201. case file != "" && hex != "":
  202. Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
  203. case file != "":
  204. if key, err = crypto.LoadECDSA(file); err != nil {
  205. Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
  206. }
  207. case hex != "":
  208. if key, err = crypto.HexToECDSA(hex); err != nil {
  209. Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
  210. }
  211. }
  212. return key
  213. }
  214. func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
  215. // Set verbosity on glog
  216. glog.SetV(ctx.GlobalInt(LogLevelFlag.Name))
  217. // Set the log type
  218. glog.SetToStderr(ctx.GlobalBool(LogToStdErrFlag.Name))
  219. // Set the log dir
  220. glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
  221. return &eth.Config{
  222. Name: common.MakeName(clientID, version),
  223. DataDir: ctx.GlobalString(DataDirFlag.Name),
  224. ProtocolVersion: ctx.GlobalInt(ProtocolVersionFlag.Name),
  225. NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
  226. LogFile: ctx.GlobalString(LogFileFlag.Name),
  227. LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
  228. LogJSON: ctx.GlobalString(LogJSONFlag.Name),
  229. Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
  230. MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
  231. AccountManager: GetAccountManager(ctx),
  232. VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
  233. MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
  234. Port: ctx.GlobalString(ListenPortFlag.Name),
  235. NAT: GetNAT(ctx),
  236. NodeKey: GetNodeKey(ctx),
  237. Shh: true,
  238. Dial: true,
  239. BootNodes: ctx.GlobalString(BootnodesFlag.Name),
  240. }
  241. }
  242. func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Database) {
  243. dataDir := ctx.GlobalString(DataDirFlag.Name)
  244. blockDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "blockchain"))
  245. if err != nil {
  246. Fatalf("Could not open database: %v", err)
  247. }
  248. stateDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "state"))
  249. if err != nil {
  250. Fatalf("Could not open database: %v", err)
  251. }
  252. return core.NewChainManager(blockDb, stateDb, new(event.TypeMux)), blockDb, stateDb
  253. }
  254. func GetAccountManager(ctx *cli.Context) *accounts.Manager {
  255. dataDir := ctx.GlobalString(DataDirFlag.Name)
  256. ks := crypto.NewKeyStorePassphrase(path.Join(dataDir, "keys"))
  257. return accounts.NewManager(ks)
  258. }
  259. func StartRPC(eth *eth.Ethereum, ctx *cli.Context) {
  260. config := rpc.RpcConfig{
  261. ListenAddress: ctx.GlobalString(RPCListenAddrFlag.Name),
  262. ListenPort: uint(ctx.GlobalInt(RPCPortFlag.Name)),
  263. CorsDomain: ctx.GlobalString(RPCCORSDomainFlag.Name),
  264. }
  265. xeth := xeth.New(eth, nil)
  266. _ = rpc.Start(xeth, config)
  267. }