flags.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package utils
  2. import (
  3. "crypto/ecdsa"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "path"
  9. "runtime"
  10. "github.com/codegangsta/cli"
  11. "github.com/ethereum/go-ethereum/accounts"
  12. "github.com/ethereum/go-ethereum/core"
  13. "github.com/ethereum/go-ethereum/crypto"
  14. "github.com/ethereum/go-ethereum/eth"
  15. "github.com/ethereum/go-ethereum/ethdb"
  16. "github.com/ethereum/go-ethereum/common"
  17. "github.com/ethereum/go-ethereum/event"
  18. "github.com/ethereum/go-ethereum/logger"
  19. "github.com/ethereum/go-ethereum/p2p/nat"
  20. "github.com/ethereum/go-ethereum/rpc"
  21. "github.com/ethereum/go-ethereum/xeth"
  22. )
  23. func init() {
  24. cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
  25. VERSION:
  26. {{.Version}}
  27. COMMANDS:
  28. {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  29. {{end}}{{if .Flags}}
  30. GLOBAL OPTIONS:
  31. {{range .Flags}}{{.}}
  32. {{end}}{{end}}
  33. `
  34. cli.CommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...]
  35. {{if .Description}}{{.Description}}
  36. {{end}}{{if .Subcommands}}
  37. SUBCOMMANDS:
  38. {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  39. {{end}}{{end}}{{if .Flags}}
  40. OPTIONS:
  41. {{range .Flags}}{{.}}
  42. {{end}}{{end}}
  43. `
  44. }
  45. // NewApp creates an app with sane defaults.
  46. func NewApp(version, usage string) *cli.App {
  47. app := cli.NewApp()
  48. app.Name = path.Base(os.Args[0])
  49. app.Author = ""
  50. app.Authors = nil
  51. app.Email = ""
  52. app.Version = version
  53. app.Usage = usage
  54. return app
  55. }
  56. // These are all the command line flags we support.
  57. // If you add to this list, please remember to include the
  58. // flag in the appropriate command definition.
  59. //
  60. // The flags are defined here so their names and help texts
  61. // are the same for all commands.
  62. var (
  63. // General settings
  64. /*
  65. VMTypeFlag = cli.IntFlag{
  66. Name: "vm",
  67. Usage: "Virtual Machine type: 0 is standard VM, 1 is debug VM",
  68. }
  69. */
  70. UnlockedAccountFlag = cli.StringFlag{
  71. Name: "unlock",
  72. Usage: "Unlock a given account untill this programs exits (address:password)",
  73. }
  74. VMDebugFlag = cli.BoolFlag{
  75. Name: "vmdebug",
  76. Usage: "Virtual Machine debug output",
  77. }
  78. DataDirFlag = cli.StringFlag{
  79. Name: "datadir",
  80. Usage: "Data directory to be used",
  81. Value: common.DefaultDataDir(),
  82. }
  83. MinerThreadsFlag = cli.IntFlag{
  84. Name: "minerthreads",
  85. Usage: "Number of miner threads",
  86. Value: runtime.NumCPU(),
  87. }
  88. MiningEnabledFlag = cli.BoolFlag{
  89. Name: "mine",
  90. Usage: "Enable mining",
  91. }
  92. UnencryptedKeysFlag = cli.BoolFlag{
  93. Name: "unencrypted-keys",
  94. Usage: "disable private key disk encryption (for testing)",
  95. }
  96. LogFileFlag = cli.StringFlag{
  97. Name: "logfile",
  98. Usage: "Send log output to a file",
  99. }
  100. LogLevelFlag = cli.IntFlag{
  101. Name: "loglevel",
  102. Usage: "0-5 (silent, error, warn, info, debug, debug detail)",
  103. Value: int(logger.InfoLevel),
  104. }
  105. LogFormatFlag = cli.StringFlag{
  106. Name: "logformat",
  107. Usage: `"std" or "raw"`,
  108. Value: "std",
  109. }
  110. // RPC settings
  111. RPCEnabledFlag = cli.BoolFlag{
  112. Name: "rpc",
  113. Usage: "Whether RPC server is enabled",
  114. }
  115. RPCListenAddrFlag = cli.StringFlag{
  116. Name: "rpcaddr",
  117. Usage: "Listening address for the JSON-RPC server",
  118. Value: "127.0.0.1",
  119. }
  120. RPCPortFlag = cli.IntFlag{
  121. Name: "rpcport",
  122. Usage: "Port on which the JSON-RPC server should listen",
  123. Value: 8545,
  124. }
  125. // Network Settings
  126. MaxPeersFlag = cli.IntFlag{
  127. Name: "maxpeers",
  128. Usage: "Maximum number of network peers",
  129. Value: 16,
  130. }
  131. ListenPortFlag = cli.IntFlag{
  132. Name: "port",
  133. Usage: "Network listening port",
  134. Value: 30303,
  135. }
  136. BootnodesFlag = cli.StringFlag{
  137. Name: "bootnodes",
  138. Usage: "Space-separated enode URLs for discovery bootstrap",
  139. Value: "",
  140. }
  141. NodeKeyFileFlag = cli.StringFlag{
  142. Name: "nodekey",
  143. Usage: "P2P node key file",
  144. }
  145. NodeKeyHexFlag = cli.StringFlag{
  146. Name: "nodekeyhex",
  147. Usage: "P2P node key as hex (for testing)",
  148. }
  149. NATFlag = cli.StringFlag{
  150. Name: "nat",
  151. Usage: "Port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
  152. Value: "any",
  153. }
  154. )
  155. func GetNAT(ctx *cli.Context) nat.Interface {
  156. natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
  157. if err != nil {
  158. Fatalf("Option %s: %v", NATFlag.Name, err)
  159. }
  160. return natif
  161. }
  162. func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
  163. hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
  164. var err error
  165. switch {
  166. case file != "" && hex != "":
  167. Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
  168. case file != "":
  169. if key, err = crypto.LoadECDSA(file); err != nil {
  170. Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
  171. }
  172. case hex != "":
  173. if key, err = crypto.HexToECDSA(hex); err != nil {
  174. Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
  175. }
  176. }
  177. return key
  178. }
  179. func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
  180. return &eth.Config{
  181. Name: common.MakeName(clientID, version),
  182. DataDir: ctx.GlobalString(DataDirFlag.Name),
  183. LogFile: ctx.GlobalString(LogFileFlag.Name),
  184. LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
  185. LogFormat: ctx.GlobalString(LogFormatFlag.Name),
  186. MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
  187. AccountManager: GetAccountManager(ctx),
  188. VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
  189. MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
  190. Port: ctx.GlobalString(ListenPortFlag.Name),
  191. NAT: GetNAT(ctx),
  192. NodeKey: GetNodeKey(ctx),
  193. Shh: true,
  194. Dial: true,
  195. BootNodes: ctx.GlobalString(BootnodesFlag.Name),
  196. }
  197. }
  198. func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Database) {
  199. dataDir := ctx.GlobalString(DataDirFlag.Name)
  200. blockDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "blockchain"))
  201. if err != nil {
  202. Fatalf("Could not open database: %v", err)
  203. }
  204. stateDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "state"))
  205. if err != nil {
  206. Fatalf("Could not open database: %v", err)
  207. }
  208. return core.NewChainManager(blockDb, stateDb, new(event.TypeMux)), blockDb, stateDb
  209. }
  210. func GetAccountManager(ctx *cli.Context) *accounts.Manager {
  211. dataDir := ctx.GlobalString(DataDirFlag.Name)
  212. var ks crypto.KeyStore2
  213. if ctx.GlobalBool(UnencryptedKeysFlag.Name) {
  214. ks = crypto.NewKeyStorePlain(path.Join(dataDir, "plainkeys"))
  215. } else {
  216. ks = crypto.NewKeyStorePassphrase(path.Join(dataDir, "keys"))
  217. }
  218. return accounts.NewManager(ks)
  219. }
  220. func StartRPC(eth *eth.Ethereum, ctx *cli.Context) {
  221. addr := ctx.GlobalString(RPCListenAddrFlag.Name)
  222. port := ctx.GlobalInt(RPCPortFlag.Name)
  223. dataDir := ctx.GlobalString(DataDirFlag.Name)
  224. l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port))
  225. if err != nil {
  226. Fatalf("Can't listen on %s:%d: %v", addr, port, err)
  227. }
  228. go http.Serve(l, rpc.JSONRPC(xeth.New(eth, nil), dataDir))
  229. }